-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmondobot.js
354 lines (308 loc) · 11.7 KB
/
mondobot.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
require('dotenv').config();
/* Uses the slack button feature to offer a real time bot to multiple teams */
var Botkit = require('botkit'),
redisConfig = {"url": process.env.REDIS_URL},
redisStorage = require('botkit/lib/storage/redis_storage')(redisConfig);
if(process.env.local_redis){
// is this a hack? maybe
redisStorage = require('botkit/lib/storage/redis_storage')();
}
var mondo = require('mondo-bank');
var Witbot = require("witbot")
if (!process.env.slack_clientId || !process.env.slack_clientSecret || !process.env.botkit_port) {
console.log('Error: Specify slack_clientId slack_clientSecret and botkit_port in environment');
process.exit(1);
}
if (!process.env.mondo_client_secret) {
console.log('Error: Specify Mondo client secret in environment');
process.exit(1);
}
if (!process.env.wit_token) {
console.log('Error: Specify wit_token in environment');
process.exit(1);
}
var mondoToken = process.env.mondo_token;
var witbot = Witbot(process.env.wit_token);
var helpers = require('./lib/helpers.js');
var controller = Botkit.slackbot({
storage: redisStorage
}).configureSlackApp(
{
clientId: process.env.slack_clientId,
clientSecret: process.env.slack_clientSecret,
scopes: ['bot'],
}
);
controller.setupWebserver((process.env.PORT || process.env.botkit_port),function(err,webserver) {
webserver.get("/", function(req, res){
res.send('<a href="https://slack.com/oauth/authorize?scope=bot&client_id=16839103392.19247485637"><img alt="Add to Slack" height="40" width="139" src="https://platform.slack-edge.com/img/add_to_slack.png" srcset="https://platform.slack-edge.com/img/add_to_slack.png 1x, https://platform.slack-edge.com/img/add_to_slack@2x.png 2x"></a>')
});
webserver.get('/mondo-oauth/:user', function(req,res){
var userId = req.params.user;
//get code from querystring
var auth_code = req.query.code;
console.log("Auth code", auth_code);
//Final Oauth Step
var grant_type = "authorization_code";
var client_id = "oauthclient_0000932nzeY7632WKef0oj";
var client_secret = process.env.mondo_client_secret;
controller.storage.users.get(userId, function (err, user){
console.log("user", user);
var redirectURI = user.mondo_redirectURI;
console.log("Redirect URI", redirectURI);
var request = require("request");
url = "https://api.getmondo.co.uk/oauth2/token"
request({
"url": url,
"form": {
"grant_type": grant_type,
"client_id": client_id,
"client_secret": client_secret,
"redirect_uri": redirectURI,
"code": auth_code,
},
method: "POST"
}, function (error, response, body) {
data = JSON.parse(body);
user.mondoToken = data.access_token;
controller.storage.users.save(user, function(err, id) {
console.log(user);
});
})
});
res.send("Success! Go back to Slack and start talking to your bank.");
});
controller.createWebhookEndpoints(controller.webserver);
controller.createOauthEndpoints(controller.webserver,function(err,req,res) {
if (err) {
res.status(500).send('ERROR: ' + err);
} else {
res.send('Success!');
}
});
});
// just a simple way to make sure we don't
// connect to the RTM twice for the same team
var _bots = {};
function trackBot(bot) {
_bots[bot.config.token] = bot;
}
controller.on('create_bot',function(bot,config) {
if (_bots[bot.config.token]) {
// already online! do nothing.
} else {
bot.startRTM(function(err) {
if (!err) {
trackBot(bot);
}
bot.startPrivateConversation({user: config.createdBy},function(err,convo) {
if (err) {
console.log(err);
} else {
convo.say('I am a bot that has just joined your team');
convo.say('You must now /invite me to a channel so that I can be of use!');
}
});
});
}
});
// Handle events related to the websocket connection to Slack
controller.on('rtm_open',function(bot) {
console.log('** The RTM api just connected!');
});
controller.on('rtm_close',function(bot) {
console.log('** The RTM api just closed');
// you may want to attempt to re-open
});
controller.hears('hello','direct_message',function(bot,message) {
bot.reply(message,'Hello!');
controller.storage.users.get(message.user, function(err, user){
if(user.mondoToken&&user){
// mondo.accounts(user.)
}
else{
var oauthClientId = "oauthclient_0000932nzeY7632WKef0oj";
var redirectURI = "http://mondoslackbot.herokuapp.com/mondo-oauth/" + message.user + "/";
var final_url = "https://auth.getmondo.co.uk/?client_id=" + oauthClientId + "&redirect_uri=" + redirectURI + "&response_type=code"
//store redirectURI
user.mondo_redirectURI = redirectURI;
controller.storage.users.save(user, function(err, id) {
console.log(user);
});
bot.reply(message, "Hi there click here to access mondo: " + final_url);
}
});
});
controller.hears('^stop','direct_message',function(bot,message) {
bot.reply(message,'Goodbye');
bot.rtm.close();
});
controller.hears(['call me (.*)'],'direct_message,direct_mention,mention',function(bot, message) {
var matches = message.text.match(/call me (.*)/i);
var name = matches[1];
controller.storage.users.get(message.user,function(err, user) {
if (!user) {
user = {
id: message.user,
};
}
user.name = name;
controller.storage.users.save(user,function(err, id) {
bot.reply(message,'Got it. I will call you ' + user.name + ' from now on.');
});
});
});
controller.hears(['what is my name','who am i'],'direct_message,direct_mention,mention',function(bot, message) {
controller.storage.users.get(message.user,function(err, user) {
if (user && user.name) {
bot.reply(message,'Your name is ' + user.name);
} else {
bot.reply(message,'I don\'t know yet!');
}
});
});
controller.hears('.*', 'direct_message, direct_mention', function (bot, message) {
//Add "working on it" reaction
bot.api.reactions.add({timestamp: message.ts, channel: message.channel, name: 'thinking_face'},function(err,res) {
if (err) {
bot.botkit.log("Failed to add emoji reaction :(",err);
}
});
var wit = witbot.process(message.text, bot, message);
wit.hears("transaction", 0.5, function(bot, message, outcome){
bot.botkit.log("transaction");
console.log(bot);
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: 'thinking_face',
},function(err,res) {
if (err) {
bot.botkit.log("soz no emoji lulz",err);
}
});
var holder = "I'm working man... :man_in_business_suit_levitating:";
bot.reply(message, holder, function(){
controller.storage.users.get(message.user, function(err, user){
mondoToken = user.mondoToken;
mondo.accounts(mondoToken, function(err, value){
if(value.accounts.length == 1){
var account_id = value.accounts[0].id;
var text = "Your account belongs to: " + value.accounts[0].description + ", id: " + account_id;
bot.reply(message, text, function(){
mondo.transactions(account_id, mondoToken, function(err, value){
console.log(err);
var text = value.transactions.map(function(transaction, index){
return index + 1 + ". transaction: " + transaction.description + ", " + transaction.description + ", " + helpers.formatGBP(transaction.amount);
}).join("\n");
bot.reply(message, text);
bot.api.reactions.remove({
timestamp: message.ts,
channel: message.channel,
name: 'thinking_face',
},function(err,res) {
if (err) {
bot.botkit.log("soz i failed",err);
}
});
bot.reply(message,{
text: ":smoking: simple.",
icon_url: "http://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2015/04/1428562937barney.gif",
username: "Barney",
image_url: "http://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2015/04/1428562937barney.gif",
});
bot.api.reactions.remove({timestamp: message.ts, channel: message.channel, name: 'thinking_face'},function(err,res) {
if (err) {
bot.botkit.log("Failed to remove emoji reaction :(",err);
}
});
});
});
}
else {
bot.api.reactions.add({
timestamp: message.ts,
channel: message.channel,
name: 'slightly_frowning_face',
},function(err,res) {
if (err) {
bot.botkit.log("soz no emoji lulz",err);
}
});
bot.reply(message, "you have no transactions LOL go spend dat moniez $$$");
}
});
});
});
});
wit.hears("account", 0.5, function(bot, message, outcome){
controller.storage.users.get(message.user, function(err, user){
mondoToken = user.mondoToken;
if(mondoToken){
bot.reply(message, "Hit me up, Barney ");
mondo.accounts(mondoToken, function(err, value){
var text = value.accounts.map(function(account, index){
return "CHECK IT :man_in_business_suit_levitating: \n" + index + 1 + ". Account: " + account.description;
}).join("\n");
bot.reply(message, {
text: text,
icon_url: "http://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2015/04/1428562937barney.gif",
username: "The Stinsonator"});
});
}
else{
bot.reply(message, {
text: "This would display the balance if I had a mondo token :frowning:",
username: "failwhale",
icon_url: "http://media.giphy.com/media/PIfV4QbNzslYQ/giphy.gif"
});
}
bot.api.reactions.remove({timestamp: message.ts, channel: message.channel, name: 'thinking_face'},function(err,res) {
if (err) {
bot.botkit.log("Failed to remove emoji reaction :(",err);
}
});
});
});
wit.hears("balance", 0.5, function(bot, message, outcome){
bot.reply(message, ":horse_racing: , let me just check the Play Book...", function(){
controller.storage.users.get(message.user, function(err, user){
mondoToken = user.mondoToken;
mondo.accounts(mondoToken, function(err, value){
if(value.accounts.length == 1){
var account_id = value.accounts[0].id;
var text = "Your account belongs to: " + value.accounts[0].description;
bot.reply(message, text, function(){
mondo.balance(account_id, mondoToken, function(err, value){
var text = helpers.formatGBP(value.balance);
bot.reply(message, {
text: "You have " + text,
icon_url: "http://dab1nmslvvntp.cloudfront.net/wp-content/uploads/2015/04/1428562937barney.gif",
username: "The Stinsonator"});
});
});
}
});
});
});
});
wit.otherwise(require('./lib/replies/giph.js'));
})
controller.storage.teams.all(function(err,teams) {
if (err) {
throw new Error(err);
}
// connect all teams with bots up to slack!
for (var t in teams) {
console.log("Team ", t);
if (teams[t].bot) {
var bot = controller.spawn(teams[t]).startRTM(function(err) {
if (err) {
console.log('Error connecting bot to Slack:',err);
} else {
trackBot(bot);
}
});
}
}
});