-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSlack.js
52 lines (48 loc) · 1.23 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
const request = require('request');
const $ = require('jquery-deferred');
const _ = require('lodash');
const emojis = require('./utils');
function Slack(channel) {
this.channel = channel;
}
Slack.prototype.postMessage = function(query, soResults) {
const def = $.Deferred();
const _that = this;
const base_options = {
body: {},
json: true,
url: process.env.SLACK_URL
};
const arrResults = _.map(soResults.items, function(resu) {
return {
value: resu.link,
title: resu.title,
short: true
};
});
const options = _.extend(base_options, {
body: {
channel: _that.channel,
icon_emoji: _.sample(emojis),
attachments: [
{
fallback:
'Required text summary of the attachment that is shown by clients that understand attachments but choose not to show them.',
color: '#e74c3c',
fields: arrResults
}
]
}
});
request.post(options, function(error, response, body) {
if (error) {
def.reject({ status: 500, data: { error: error.message } });
} else if (response.statusCode == 200) {
def.resolve(body);
} else {
def.reject(body);
}
});
return def.promise();
};
module.exports = Slack;