-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·46 lines (38 loc) · 973 Bytes
/
index.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
module.exports = Slack;
const request = require('request-promise-native');
function Slack (channel, token, bot_name, live = false) {
this.channel = channel;
this.token = token;
this.bot_name = bot_name;
this.live = live;
}
// Public Methods
Slack.prototype = {
send_message: async function (text) {
return post_request(this, text)
.catch((err) => {
console.error(err);
throw err;
});
}
};
// Private Methods
async function post_request (slack, text) {
const options = {
url: 'https://slack.com/api/chat.postMessage',
headers: {
'content-type': 'application/x-www-form-urlencoded',
'Authorization': `Bearer ${slack.token}`
},
form: {
channel: slack.channel,
text: text,
username: `${slack.live ? 'prod' : 'dev'}-${slack.bot_name}`
}
};
if (!slack.live) {
console.log(`Slack message not sent (on DEV): ${text}`);
return;
}
return request.post(options);
}