-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack_service.js
41 lines (34 loc) · 1.39 KB
/
slack_service.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
var request = require("request");
// SlackService constructor.
var SlackService = function(slackWebhookUrl) {
this.webhookUrl = slackWebhookUrl;
};
SlackService.prototype.buildMessage = function(feedEntry){
var message = {attachments: [{
pretext: "New activity. <" + feedEntry.link + "|Check it>",
fallback: "New activity",
fields: [{title: "Title", value: feedEntry.title, short: false},
{title: "Categories", value: feedEntry.categories.join(", "), short: false},
{title: "Updated date", value: new Date(feedEntry.date).toString(), short: true},
{title: "Publish date", value: new Date(feedEntry.pubdate).toString(), short: true}
]
}]
};
return message;
}
SlackService.prototype.fire = function(data, successCallback, errorCallback) {
var self = this;
request.post({
headers: {'content-type' : 'application/json'},
url: self.webhookUrl,
body: JSON.stringify(data)
}, function(error, response, body){
if(error){
errorCallback(error);
}else{
successCallback(body);
}
});
};
// Export the SlackService constructor from this module.
module.exports = SlackService;