-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
131 lines (102 loc) · 2.89 KB
/
index.js
File metadata and controls
131 lines (102 loc) · 2.89 KB
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
122
123
124
125
126
127
128
129
130
131
var config = require('./config');
var request = require('request');
var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var web = express();
web.use(morgan());
web.use(bodyParser());
web.listen(config.port);
wlog("Listening for http on " + config.port);
var Slack = require('node-slack');
var slackbot = new Slack(config.slack.domain, config.slack.token);
var Hipchat = require('node-hipchat');
var hipbot = new Hipchat(config.hipchat.token);
web.get('/', function(req, res){
res.end("I AM WIZARDBOT");
});
// messages from slack
web.post('/messages', function(req,res) {
sendHipChatMessage(req.body);
res.end("");
});
function wlog () {
var args = [].slice.apply(arguments);
args.unshift("WIZARDBOT:");
console.log.apply(console, args);
}
var sendHipChatMessage = function(msg) {
if (msg.user_id === 'USLACKBOT') {
wlog("skipping message from myself " + msg.user_name + ": " + msg.text);
return;
}
wlog("relaying to hipchat " + msg.user_name + ": " + msg.text);
hipbot.postMessage({
room: config.hipchat.room,
from: msg.user_name + " (bot)",
message: msg.text,
message_format: 'text',
notify: 1
}, function(data,err){
if (err) {
wlog("Error sending message from " + msg.user_name, err);
}
});
};
var getHipChatMessages = function (cb) {
var ignorebot = function(data, err){
if (err) { return cb(err, data); }
// filter out messages from the api, eg the bot
data = data.messages.filter(function(msg){
return msg.from.user_id !== 'api';
});
data = data.map(function(msg) {
msg.user_name = msg.from.name;
msg.text = msg.message;
return msg;
});
cb(err, data);
};
hipbot.getHistory({
room: config.hipchat.room
}, ignorebot);
};
var sendSlackMessage = function (msg) {
slackbot.send({
text: msg.text,
channel: config.slack.room,
username: msg.user_name + ' (bot)'
});
};
var hipchatMessages = [];
var pollHipChat = function(dont_send){
wlog("checking for hipchat");
getHipChatMessages(function(err, data){
if (err) {
wlog("Error polling hipchat backing off 30 seconds");
setTimeout(pollHipChat, 30000);
return;
}
var newMessages = data.filter(function(msg){
for (var i =0; i < hipchatMessages.length; i++){
var oldmsg = hipchatMessages[i];
if (msg.username === oldmsg.username && msg.text === oldmsg.text) {
return false;
}
}
return true;
});
hipchatMessages = hipchatMessages.concat(newMessages);
wlog("found " + newMessages.length + " new messags");
if (!dont_send) {
newMessages.forEach(sendSlackMessage);
}
setTimeout(pollHipChat, 5000);
});
};
pollHipChat(true);
setInterval(function(){
request.head('http://wizardbot.herokuapp.com/messages', function(){
wlog('waking myself up');
});
},60000);