forked from IBM/spring-boot-microservices-on-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendSlack.js
106 lines (95 loc) · 3.13 KB
/
sendSlack.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
99
100
101
102
103
104
105
106
// Copyright 2018 IBM Corp. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var request = require('request');
/**
* Action to post to slack
* @param {string} url - Slack webhook url
* @param {string} channel - Slack channel to post the message to
* @param {string} username - name to post the message as
* @param {string} text - message to post
* @param {string} icon_emoji - (optional) emoji to use as the icon for the message
* @param {boolean} as_user - (optional) when the token belongs to a bot, whether to post as the bot itself
* @param {object} attachments - (optional) message attachments (see Slack documentation for format)
* @return {object} Promise
*/
function main(params) {
let errorMsg = checkParams(params);
if (errorMsg) {
return { error: errorMsg };
}
var body = {
channel: params.channel,
username: params.username
};
if (params.icon_emoji) {
// guard against sending icon_emoji: undefined
body.icon_emoji = params.icon_emoji;
}
if (params.as_user === true) {
body.as_user = true;
}
if (params.text) {
body.text = params.text;
}
if (params.attachments) {
body.attachments = JSON.stringify(params.attachments);
}
if (params.token) {
//
// this allows us to support /api/chat.postMessage
// e.g. users can pass params.url = https://slack.com/api/chat.postMessage
// and params.token = <their auth token>
//
body.token = params.token;
} else {
//
// the webhook api expects a nested payload
//
// notice that we need to stringify; this is due to limitations
// of the formData npm: it does not handle nested objects
//
body = {
payload: JSON.stringify(body)
};
}
var promise = new Promise(function (resolve, reject) {
request.post({
url: params.url,
formData: body
}, function (err, res, body) {
if (err) {
console.log('error: ', err, body);
reject(err);
} else {
console.log('success: ', params.text, 'successfully sent');
resolve(res);
}
});
});
return promise;
}
/**
* Checks if all required params are set.
*/
function checkParams(params) {
if (params.text === undefined && params.attachments === undefined) {
return ('No text provided');
}
else if (params.url === undefined) {
return 'No Webhook URL provided';
}
else {
return undefined;
}
}