forked from Joezo/node-slackhook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslack.js
More file actions
80 lines (65 loc) · 2.22 KB
/
slack.js
File metadata and controls
80 lines (65 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var request = require('request');
function Slack(options) {
this.domain = null;
this.token = null;
this.defaultChannel = '#general';
if( options && options instanceof Object ){
this.domain = options.domain;
this.token = options.token;
if( options.defaultChannel ) this.defaultChannel = options.defaultChannel;
}
this.disabled = false;
}
Slack.prototype.send = function(message,cb) {
if (this.disabled) {
console.log(new Date(), 'Slack integration temporarily disabled. Message not sent: ' + message);
return cb(); // do not send error to prevent cycling
}
if( !( message && message instanceof Object && message.text ) ) {
if( cb && cb instanceof Function ) return cb(new Error('No message'));
return 'No message';
}
var url = 'https://' + this.domain + '.slack.com/services/hooks/incoming-webhook?token=' + this.token;
var channel = message.channel || this.defaultChannel;
var options = {
channel: channel,
text: message.text,
username: message.username,
};
if( message.icon_emoji ) options.icon_emoji = message.icon_emoji;
if( message.icon_url ) options.icon_url = message.icon_url;
if( message.attachments ) options.attachments = message.attachments;
var requestParams = {
url: url,
body: JSON.stringify(options)
};
var self = this;
request.post(requestParams, function(err,res,body) {
//Too many requests
if (res.statusCode == 429) {
console.log(new Date(), 'Disabling Slack integration, too many messages. Integration will be enabled in one minute.');
self.disabled = true;
// Wait one minute to enable again
setTimeout(function () {
self.disabled = false;
}, 60 * 1000);
}
if(err || body != 'ok') {
if( cb && cb instanceof Function ) return cb(err || body);
}
if (cb && cb instanceof Function) cb(err,body);
});
};
Slack.prototype.respond = function(query,cb) {
var obj = {};
obj.token = query.token;
obj.team_id = query.team_id;
obj.channel_id = query.channel_id;
obj.channel_name = query.channel_name;
obj.timestamp = new Date(query.timestamp);
obj.user_id = query.user_id;
obj.user_name = query.user_name;
obj.text = query.text;
return obj;
}
module.exports = Slack;