-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelloWorld.js
329 lines (289 loc) · 13.8 KB
/
HelloWorld.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
var express = require("express");
var logfmt = require("logfmt");
var unirest = require('unirest');
var app = express();
app.use(logfmt.requestLogger());
app.get('/', function(req, res) {
res.send('BARNEY STINSON BOT');
});
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
console.log("Listening on " + port);
});
var GroupMe = require('./lib/groupme');
var API = GroupMe.Stateless;
var BOT_LISTENS_FOR = "Barney";
/************************************************************************
* Read the access token from the command line.
***********************************************************************/
var ACCESS_TOKEN = process.argv[2];
/************************************************************************
* Getting the bot configured and set up:
***********************************************************************/
var USER_ID = process.argv[3];
var BOT_NAME = 'Barney Stinson';
// var BOT_NAME = 'TEST BOT';
/************************************************************************
* Bro Code
***********************************************************************/
var bro_code = [
"Bros before hoes",
"A Bro will not talk about something lame in front of a woman",
"Bros don't date their bro ex girlfriends",
"Bros buy porn for all the bros",
"A bro never sends a greeting card to another bro",
"Bros do not share dessert",
"Poorly-planned-mediocre-social-media-practical-jokes before honesty",
"A Bro shall always alert another Bro of any girl fight",
"Bros cannot make eye contact during a devil's threeway",
"A Bro will, whenever possible, provide a bro with protection",
"A bro saves a bro from his ex",
"A bro saves a bro from the friend zone",
"A Bro shall at all times say \"yes\"",
"The mom of a Bro is always off-limits. But the step-mom of a Bro is fair game if she initiates it and/or is wearing at least one article of leopards print clothing",
"A bro pretends to like cigars",
"A bro never dates a bro's ex-girlfriend (unless granted permission)",
"A bro that calls \"dibs\" first, has dibs",
"No leaving a Bro hanging",
"A Bro always likes the new profile picture of another bro",
"A true Bro will never be \"Necklace Guy\".",
"A bro shall not have a weird moment with another bro's fiance",
];
/************************************************************************
* Play Book
***********************************************************************/
var play_book = [
"Bros before hoes",
"A Bro will not talk about something lame in front of a woman",
"Bros don't date their bro ex girlfriends",
"Bros buy porn for all the bros",
"A bro never sends a greeting card to another bro",
"Bros do not share dessert",
"Poorly-planned-mediocre-social-media-practical-jokes before honesty",
"A Bro shall always alert another Bro of any girl fight",
"Bros cannot make eye contact during a devil's threeway",
"A Bro will, whenever possible, provide a bro with protection",
"A bro saves a bro from his ex",
"A bro saves a bro from the friend zone",
"A Bro shall at all times say \"yes\"",
"The mom of a Bro is always off-limits. But the step-mom of a Bro is fair game if she initiates it and/or is wearing at least one article of leopards print clothing",
"A bro pretends to like cigars",
"A bro never dates a bro's ex-girlfriend (unless granted permission)",
"A bro that calls \"dibs\" first, has dibs",
"No leaving a Bro hanging",
"A Bro always likes the new profile picture of another bro",
"A true Bro will never be \"Necklace Guy\".",
"A bro shall not have a weird moment with another bro's fiance",
];
/************************************************************************
* Set up the message-based IncomingStream and the HTTP push
***********************************************************************/
var bot_id = null;
var retryCount = 3;
// Constructs the IncomingStream, identified by the access token and
var incoming = new GroupMe.IncomingStream(ACCESS_TOKEN, USER_ID, null);
/*
// This logs the status of the IncomingStream
incoming.on('status', function() {
var args = Array.prototype.slice.call(arguments);
var str = args.shift();
console.log("[IncomingStream 'status']", str, args);
});
*/
// This waits for the IncomingStream to complete its handshake and start listening.
// We then get the bot id of a specific bot.
incoming.on('connected', function() {
console.log("[IncomingStream 'connected']");
API.Bots.index(ACCESS_TOKEN, function(err,ret) {
if (!err) {
var botdeets;
for (var i = 0; i < ret.length; i++) {
if (ret[i].name == BOT_NAME) {
bot_id = ret[i].bot_id;
}
}
console.log("[API.Bots.index return] Firing up bot!", bot_id);
}
});
});
// This waits for messages coming in from the IncomingStream
// If the message contains @BOT, we parrot the message back.
incoming.on('message', function(msg) {
console.log("[IncomingStream 'message'] Message Received");
if (msg["data"]
&& msg["data"]["subject"]
&& msg["data"]["subject"]["text"]
/*&& msg["data"]["subject"]["text"].indexOf(BOT_LISTENS_FOR) >= 0*/) {
if (bot_id && msg["data"]["subject"]["name"] != BOT_NAME) {
var txt = msg["data"]["subject"]["text"];
/************************************************************************
* Get current location of user
***********************************************************************/
var ipAddr = req.headers["x-forwarded-for"];
if (ipAddr){
var list = ipAddr.split(",");
ipAddr = list[list.length-1];
} else {
ipAddr = req.connection.remoteAddress;
}
var url = 'http://freegeoip.net/json/' + ipAddr;
var loc = null;
var Request = unirest.get(url)
.end(function (response) {
loc = response.body;
console.dir(response);
});
/************************************************************************
* Weather Responses
***********************************************************************/
if(txt.search("weather") != -1 || txt.search("Weather") != -1) {
// Require the module
var Forecast = require('forecast');
// Initialize
var forecast = new Forecast({
service: 'forecast.io',
key: 'f267218743d71c6d486401ad298558fa',
units: 'f', // Only the first letter is parsed
cache: true, // Cache API requests?
ttl: { // How long to cache requests. Uses syntax from moment.js: http://momentjs.com/docs/#/durations/creating/
minutes: 27,
seconds: 45
}
});
var message = "Nope. Nada. Zilch.";
// var url = 'http://freegeoip.net/json/' + ipAddr;
// var loc = null;
// var Request = unirest.get(url)
// .end(function (response) {
// loc = response.body;
// });
// Retrieve weather information from coordinates (Ann Arbor, MI)
forecast.get([42.2828, -83.7347], function(err, weather) {
if(err) console.dir(err);
else {
var temp = weather.currently.temperature;
console.dir("Current temp: " + temp);
if(temp > 60) {
console.log("It's " + temp.toString() + " degrees outside! Time to bring out the sundresses!");
message = "It's " + tempd.toString() + " degrees outside! Time to bring out the sundresses!";
}
else if(temp > 40) {
console.log("It's cool outside... Just like me.");
message = "It's cool outside... Just like me.";
}
else if(temp > 20) {
console.log("Brrrr it's cold! But baby don't worry... Daddy's home!");
message = "Brrrr it's cold! But baby don't worry... Daddy's home!";
}
else if(temp > 0) {
console.log("Suit up! It's freezing!");
message = "Suit up! It's freezing!";
}
else {
console.log("It's " + temp.toString() + " degrees right now. So cold that it's going to be Legen...wait for it...DARY!");
message = "It's " + temp.toString() + " degrees right now. So cold that it's going to be Legen...wait for it...DARY!";
}
API.Bots.post(
ACCESS_TOKEN, // Identify the access token
bot_id, // Identify the bot that is sending the message
message, // Construct the message
{}, // No pictures related to this post
function(err,res) {
if (err) {
console.log("[API.Bots.post] Weather Response Error!");
} else {
console.log("[API.Bots.post] Weather Response Sent!");
}
});
}
});
}
/************************************************************************
* Bro code
***********************************************************************/
else if(txt.search("bro code") != -1 || txt.search("Bro code") != -1) {
var message = bro_code[Math.floor(Math.random() * bro_code.length)];
API.Bots.post(
ACCESS_TOKEN, // Identify the access token
bot_id, // Identify the bot that is sending the message
message, // Construct the message
{}, // No pictures related to this post
function(err,res) {
if (err) {
console.log("[API.Bots.post] Reply Message Error!");
} else {
console.log("[API.Bots.post] Reply Message Sent!");
}
});
}
// /************************************************************************
// * Play Book
// ***********************************************************************/
// else if(txt.search("HI") != -1 || txt.search("Hi") != -1 || txt.search("Hey") != -1 || txt.search("hi") != -1 || txt.search("hey") != -1 || txt.search("hello") != -1 || txt.search("HEY") != -1) {
// var message = play_book[Math.floor(Math.random() * play_book.length)];
// API.Bots.post(
// ACCESS_TOKEN, // Identify the access token
// bot_id, // Identify the bot that is sending the message
// message, // Construct the message
// {}, // No pictures related to this post
// function(err,res) {
// if (err) {
// console.log("[API.Bots.post] Reply Message Error!");
// } else {
// console.log("[API.Bots.post] Reply Message Sent!");
// }
// });
// }
/************************************************************************
* Default BRODA responses
***********************************************************************/
else if(msg["data"]["subject"]["name"] != BOT_NAME && txt.indexOf("Broda") != -1) {
var url = "http://brospeak.com/?api=yeah&input=" + txt;
txt = txt.replace("Broda ", "")
var Request = unirest.get(url)
.end(function (response) {
console.dir("Text: " + txt + "\nBroSpeak: " + response.body);
txt = response.body;
/*var Request = unirest.get("https://yoda.p.mashape.com/yoda?sentence=" + txt)
.headers({
"X-Mashape-Authorization": "iR2g3eyxXH6tK1tZELkkVJikSMeafCWC"
})
.end(function (response) {
console.log(response.body);*/
API.Bots.post(
ACCESS_TOKEN, // Identify the access token
bot_id, // Identify the bot that is sending the message
response.body, // Construct the message
{}, // No pictures related to this post
function(err,res) {
if (err) {
console.log("[API.Bots.post] Reply Message Error!");
} else {
console.log("[API.Bots.post] Reply Message Sent!");
}
});
//});
});
}
}
}
});
// This listens for the bot to disconnect
incoming.on('disconnected', function() {
console.log("[IncomingStream 'disconnect']");
if (retryCount > 3) {
retryCount = retryCount - 1;
incoming.connect();
}
})
// This listens for an error to occur on the Websockets IncomingStream.
incoming.on('error', function() {
var args = Array.prototype.slice.call(arguments);
console.log("[IncomingStream 'error']", args);
if (retryCount > 3) {
retryCount = retryCount - 1;
incoming.connect();
}
})
// This starts the connection process for the IncomingStream
incoming.connect();