forked from twobucks/slack-message
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (43 loc) · 1.21 KB
/
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
47
"use strict";
const request = require("request");
const log = require("npmlog");
const util = require("./app/util");
const urlGenerator = require("./app/urlGenerator");
const args = process.argv.slice(2);
function slackMessage(){
if(args.length < 3) {
throw new Error("All properties are required 'slack-message <token> <channel> <message>'");
} else if (args.length > 3) {
throw new Error("No additional properties allowed 'slack-message <token> <channel> <message>'");
}
const message = {
token: args[0],
channel: args[1],
text: args[2]
}
sendMessage(message);
}
function sendMessage(message) {
const sendMessageUrl = urlGenerator.generateSlackUrl(message);
if(typeof sendMessageUrl !== "string") {
util.showErrors(sendMessageUrl);
throw new Error("Could not generate a valid url :(");
}
const spinner = util.startSpinner("Sending message..", 8);
request.get(sendMessageUrl, (err, res, body) => {
if(err) {
throw new Error(err);
}
const bodyJSON = JSON.parse(body);
spinner.stop();
process.stdout.write('\n');
if (!bodyJSON.ok) {
throw new Error(body);
}
log.info("Message sent!");
});
}
module.exports = {
slackMessage,
sendMessage
};