-
Notifications
You must be signed in to change notification settings - Fork 1
/
slack.js
59 lines (50 loc) · 1.34 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
53
54
55
56
57
58
59
/*!
* Docker Image Factory - Notification module
*
*/
'use strict';
var winston = require('winston'),
Slack = require('node-slack'),
nunjucks = require('nunjucks');
module.exports = SlackNotifier;
function SlackNotifier(cfg) {
this.cfg = cfg || {};
this.slack = new Slack(cfg.url);
}
/**
* Returns true if notification is enabled
*/
SlackNotifier.prototype.isEnabled = function () {
return (this.cfg.url) ? true : false;
};
SlackNotifier.prototype.notify = function (isSuccessful, ctx) {
var cfg = this.cfg;
if (this.isEnabled() && !isSuccessful) {
var channel = cfg.room;
this.sendMessage('slack.json', ctx, channel);
}
};
/**
* Creates the context object to send the
* template engine and then renders the template
*/
SlackNotifier.prototype.sendMessage = function(templateName, ctx, channel) {
var _this = this;
winston.debug('Posting slack message to ' + channel + ' channel');
// Context object with any addition notification details
var obj = {
ctx: ctx,
notification: {
channel: channel,
date: Date.now() / 1000
}
};
// Render the template and send the message
nunjucks.render(templateName, obj, function(err, resp) {
if(!err) {
_this.slack.send(JSON.parse(resp));
} else {
winston.error('Failed to render template. Reason: ' + err.message);
}
});
};