-
Notifications
You must be signed in to change notification settings - Fork 0
/
botManager.js
123 lines (94 loc) · 2.83 KB
/
botManager.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var webhookKey = "hippo";
var _ = require('underscore'),
async = require('async'),
Hipchatter = require('hipchatter');
function getWebhookName(bot) {
return [webhookKey, bot.name].join('.');
};
function isOurWebhook(webhookName) {
return (webhookName.indexOf(webhookKey) === 0);
};
function getWebhookPath(bot, roomId, selfURL) {
var path = "";
if (selfURL) {
path = selfURL;
}
path += "/webhook/hipchat/" + roomId + '/' + bot.name;
return path;
};
function unregisterBots (cb) {
// get list of webhooks
async.concat(global.config.hipchat.rooms, function (roomConfig, concatCallback) {
var hipchat = new Hipchatter(roomConfig.authCode);
hipchat.webhooks(roomConfig.roomId, function (err, hooks) {
// we don't know what room it's on
_.each(hooks.items, function (hook) {
hook.room = roomConfig;
});
concatCallback(err, hooks.items);
});
}, function (err, hooks) {
// run callback - we have the rooms to delete already so who cares if they change
cb();
// Delete all webhooks we previously registered
_.each(hooks, function (hook) {
var hipchat = new Hipchatter(hook.room.authCode);
if (isOurWebhook(hook.name)) {
hipchat.delete_webhook(hook.room.roomId, hook.id, function (err, err_response) {
console.log(err ? 'delete error- ' + err_response : ('Deleted old webhook ' + hook.name));
});
}
});
});
};
function registerBots(app, selfURL, bots) {
// loop through list of bots
var bot;
// set up webhooks for each room
_.each(global.config.hipchat.rooms, function (room) {
var hipchat = new Hipchatter(room.authCode);
_.each(bots, function (bot) {
// register each bot
var notification = _.extend({
token: room.roomToken
}, bot.notification);
var cb = function (response) {
if (_.isString(response)) {
notification.message = response;
} else if (_.isObject(response)) {
notification = _.defaults(response, notification);
}
hipchat.notify(room.roomId, notification, function () {
console.log('sent notification');
});
};
// listen for a webhook
app.post(getWebhookPath(bot, room.roomId), function (req, res) {
bot.execute(req.body, cb);
});
if (_.isFunction(bot.registration)) {
bot.registration(app, selfURL, cb);
}
if (bot.event && selfURL) {
// register hipchat webhook
hipchat.create_webhook(room.roomId, {
url: getWebhookPath(bot, room.roomId, selfURL),
pattern: bot.pattern || undefined,
event: bot.event,
name: getWebhookName(bot)
}, function (err, err_response) {
console.log(err ? err_response : ('Registered webhook ' + getWebhookName(bot)));
});
}
});
});
};
module.exports = {
setUpBots: function (app, bots) {
var selfURL = app.get('selfURL');
unregisterBots(function () {
// register bots
registerBots(app, selfURL, bots);
});
}
};