-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbotanalytics.js
83 lines (69 loc) · 1.53 KB
/
botanalytics.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
'use strict';
var rp = require('request-promise');
var uuid = require('node-uuid');
var _ = require('lodash');
var VERSION = '1.0.0';
function BotAnalyticsSlack(apiKey) {
var that = this;
that.apiKey = apiKey;
function logMessage(bot, message) {
if (message.type != 'message') {
// ignore these types.
return;
}
// map bot as user if not specified
if (!message.user) {
message.user = bot.identity.id;
}
// map team if not specified
if (!message.team) {
message.team = bot.team_info.id;
}
// map ts if not specified
if (!message.ts) {
message.ts = new Date().getTime() / 1000;
}
// track
rp({
uri: 'https://botanalytics.co/api/v1/messages/slack/',
method: 'POST',
headers: {
'Authorization': 'Token ' + that.apiKey
},
json: {
'message': message
}
});
}
that.connect = function(res) {
if (!res) {
console.log('FAILED TO CONNECT TO BOT ANALYTICS BECAUSE RTM.START FAILED');
}
rp({
uri: 'https://botanalytics.co/api/v1/bots/slack/initialize/',
method: 'POST',
headers: {
'Authorization': 'Token ' + that.apiKey
},
json: res
});
};
// botkit middleware endpoints
that.send = function(bot, message, next) {
logMessage(bot, message);
next();
};
// botkit middleware endpoints
that.receive = function(bot, message, next) {
logMessage(bot, message);
next();
};
}
module.exports = function(apiKey) {
if (!apiKey) {
throw new Error('YOU MUST SUPPLY AN API KEY TO BOT ANALYTICS!');
}
return {
slack: new BotAnalyticsSlack(apiKey)
};
};