-
Notifications
You must be signed in to change notification settings - Fork 0
/
slack.js
98 lines (84 loc) · 2.4 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"use strict";
var request = require('request');
var deferred = require('deferred');
var fs = require('fs');
var files = {};
files.ip185 = '111';
files.ip193 = '222';
files.ip194 = 'C:\\founder\\command_file\\command';
function Slack(hook_url) {
this.hook_url = hook_url;
}
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.hook_url;
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 = {
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 (obj.token == 'token') {
var file = files['ip' + obj.text];
fs.appendFile(file, 'restart tomcat', function (err) {
if (err)
console.log("fail " + err);
else
console.log(obj.text + "写入文件ok");
});
} else {
this.send({text:'Slash Commands的token非法,请检查请求来源.',channel:'#hooksinfo'})
}
if (!cb) {
return {text: ''};
} else {
return cb.call(null, obj);
}
};
module.exports = Slack;