forked from xoxco/node-slack
-
Notifications
You must be signed in to change notification settings - Fork 1
/
slack.js
72 lines (58 loc) · 1.87 KB
/
slack.js
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
"use strict";
var request = require('request');
var deferred = require('deferred');
function Slack(webhookURL, http_proxy_options) {
this.webhookURL = webhookURL;
this.http_proxy_options = http_proxy_options;
}
Slack.prototype.send = function(message, cb) {
if (!message.text) {
if (cb) cb.call(null,{message:'No text specified'},null);
return;
}
if (!message.channel) { message.channel = '#general'; }
var command = this.webhookURL;
var body = {
channel: message.channel,
text: message.text,
username: message.username
};
if (message.icon_url) { body.icon_url = message.icon_url; }
if (message.icon_emoji) { body.icon_emoji = message.icon_emoji; }
if (message.attachments) { body.attachments = message.attachments; }
if (message.unfurl_links) { body.unfurl_links = message.unfurl_links; }
if (message.link_names) { body.link_names = message.link_names; }
var option = {
proxy: (this.http_proxy_options && this.http_proxy_options.proxy) || process.env.https_proxy || process.env.http_proxy,
url: command,
body: JSON.stringify(body)
};
if(!cb) var d = deferred();
var req = request.post(option, function(err, res, body) {
if (!err && body!='ok') {
err = {message: body};
body = null;
}
if (d) return err ? d.reject(err) : d.resolve({res: res, body: body});
if (cb) return cb.call(null, err, body);
return null;
});
return d ? d.promise : req;
};
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;
if (!cb) {
return {text:''};
} else {
return cb.call(null,obj);
}
};
module.exports = Slack;