-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
67 lines (58 loc) · 2.23 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Created by Jordan on 11/24/2014.
*/
var util = require('util'),
request = require('request'),
winston = require('winston');
var Slack = exports.Slack = function (params) {
params = params || {};
if(!params.apiToken || !params.channel){
throw new Error("options cannot be null");
}
else{
this.channel = params.channel;
this.apiToken = params.apiToken;
this.metaChannel = params.metaChannel || this.channel;
this.username = params.username || "Winston";
this.level = params.level || "silly";
this.silent = params.silent || false;
this.raw = params.raw || false;
this.handleExceptions = params.handleExceptions || true;
this.iconEmoji = {
error: ":finnadie:",
warn: ":feelsgood:",
info: ":goberserk:",
verbose: ":rage4:",
debug: ":rage2:",
silly: ":suspect:"
} || params.iconEmoji;
this.iconEmojiDefault = params.iconEmojiDefault || ":troll:";
this.handleExceptions = params.handleExceptions || false;
this.name = params.name;
}
};
util.inherits(Slack, winston.Transport);
winston.transports.Slack = Slack;
Slack.prototype.log = function (level, msg, meta, callback) {
var message =
"https://slack.com/api/chat.postMessage" +
"?token=" + encodeURIComponent(this.apiToken) +
"&channel=" + encodeURIComponent(this.channel) +
"&text=" + encodeURIComponent(msg) +
"&username=" + encodeURIComponent("[" + level + "] " + this.username) +
"&icon_emoji=" + encodeURIComponent(this.iconEmoji[level] || this.iconEmojiDefault);
request.get(message);
if (Object.keys(meta).length) {
var fileMessage =
"https://slack.com/api/files.upload" +
"?token=" + encodeURIComponent(this.apiToken) +
"&content=" + encodeURIComponent(JSON.stringify(meta, null, 4)) +
"&filetype=json" +
"&filename=" + encodeURIComponent(level) + '.json' +
"&initial_comment=" + encodeURIComponent(msg) +
"&channels=" + encodeURIComponent(this.metaChannel) +
"&pretty=1";
request.get(fileMessage);
}
callback(null, true);
};