-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatBot.js
385 lines (343 loc) · 12.5 KB
/
chatBot.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
const login = require("facebook-chat-api");
var jay = require('./createAccount.js');
var config = require('./config.js')
var MongoClient = require('mongodb').MongoClient
var flag1 = false;
var flag1a = false;
var flag2 = false;
var flag3 = false;
var currentflag = 0;
var currentP1ID = 0;
var betString = null;
var currentP2ID = 0;
var money = null;
var canJudge = 0;
var finJudge = 0;
var binary =-1;
var Cstep = false;
var f1 = false;
var f2 = false;
var finalConfirmation = false;
var currentThread = 0;
var whichWon = -1;
var iii = -1;
var currentstep=0;
var bool=false;
var disagree = false;
var url = 'mongodb://capen:bettingapp@ds133418.mlab.com:33418/bettingapp';
// Use connect method to connect to the server
MongoClient.connect(url, function(err, db) {
//console.log(jay.withdraw);
//this is a callback, the input to the callback is the output of the function
var x = function (err, api) {
if(err) {
return console.error(err);
}
//EXAMPLE USAGE OF GETNAME AND GETID
/*getName(api, 100000728466267, function(err, name) {
console.log(name);
});
getID(api, "Aaron Kau", 1492866947455267, function(err, id) {
console.log(id);
});*/
//api.setOptions({listenEvents: true})
api.listen(function(err, message) {
var tokens = message.body.split(' ');
console.log(tokens +"\n"+tokens[0])
console.log(currentstep)
if (tokens[0].valueOf()==="help".valueOf()) {
api.sendMessage("Type \"bet [name]\" to start a bet \r\nType info for group chat info\r\nType betinfo for current bet info\r\nType exit to cancel bet", message.threadID);
}
if (tokens[0].valueOf()==="info".valueOf()){
api.getThreadInfo(message.threadID, function(err, info){
api.sendMessage(info[0], message.threadID);
});
}
if (tokens[0].valueOf()==="betinfo".valueOf()){
getName(api, currentP1ID, function(err, res) {
getName(api, currentP2ID, function(err, res2) {
jay.getBalance(res,function(err,bal1) {
jay.getBalance(res2, function(err,bal2) {
api.sendMessage("Player 1: "+res+"Player 2: "+res2+" who have these amounts, already ("+bal1+", "+ bal2 +") for this amount: "+money, message.threadID);
})
})
})
})
}
//-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-
if (tokens[0]==="exit"||tokens[0]==="close"||tokens[0]==="clear"||tokens[0]==="stop") { //cancel bet
flag1 = false;
flag1a = false;
flag2 = false;
flag3 = false;
currentflag = 0;
currentP1ID = 0;
betString = null;
currentP2ID = 0;
money = null;
canJudge = 0;
finJudge = 0;
binary =-1;
Cstep = false;
f1 = false;
f2 = false;
finalConfirmation = false;
currentThread = 0;
whichWon = -1;
iii = -1;
currentstep=0;
bool=false;
disagree = false;
api.sendMessage("Reset!", message.threadID);
}
//BETS START HERE!!!
if ((finalConfirmation===true)&&(tokens[0]==="win:")) { // to determine winner
getName(api, message.senderID, function(err, obj) {
if (obj == finJudge) {
var fullname = "";
for (var i=1;i<tokens.length;i++) {
if (i>1) fullname += " "
fullname += tokens[i];
}
getID(api, fullname, message.threadID , function(err, id) {
if (err) {
console.log(err);
}
console.log(fullname);
var temp = id
if (temp===currentP1ID) {
api.sendMessage(canJudge + " has decided that " + fullname + " is the winner of the bet! Congratulations!", message.threadID);
jay.deposit(currentP1ID, 2*money, function(err, response) {
});
whichWon=1;
} else if (temp===currentP2ID) {
api.sendMessage(canJudge + " has decided that " + fullname + " is the winner of the bet! Congratulations!", message.threadID);
whichWon=2;
jay.deposit(currentP2ID, 2*money, function(err, response){
});
} else {
console.log("error!")
}
getName(api, currentP1ID, function(err, p1) {
getName(api, currentP2ID, function(err, p2){
var winnerName;
if(whichWon==1){ winnerName=p1;}
else if(whichWon==2){winnerName=p2;}
var date = new Date();
db.collection('bets').insert({
date: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate(),
betee: p2,
better:p1,
amount: money,
reason: betString,
winner: winnerName
});
})
})
});
}
})
}
if ((Cstep===true)&&(message.senderID===currentP1ID||message.senderID===currentP2ID)) { //to confirm
if ((message.body==="yes")&&(message.senderID===currentP1ID)) {
f1="yes";
}
if ((message.body==="yes")&&(message.senderID===currentP2ID)) {
f2="yes";
}
if ((message.body==="no")&&(message.senderID===currentP1ID)) {
f1="no";
}
if ((message.body==="no")&&(message.senderID===currentP2ID)) {
f2="no";
}
if (f1==="yes"&&f2==="yes") {
api.sendMessage("Everyone has agreed on the judge!\r\nBet has started!"+"\r\nJudge " + canJudge +" enter \"win: player name\" to confirm the winner.", message.threadID);
console.log("MONEY : " + money + " MONEY AS INT " + parseInt(money));
getName(api, currentP2ID, function(err, name) {
var id2 = parseInt(currentP2ID)
console.log(id2);
jay.createAccount(id2, name, function(err, body){
// console.log("1 " + body);
//jay.withdraw(id2, 3, function(err, body) {
// console.log("2 " + JSON.stringify(body));
//});
jay.withdraw(id2, parseInt(money), function(err, body) {
console.log("2 " + JSON.stringify(body))
})
//console.log("created 2 " + err + " body: " + JSON.stringify(body));
});
});
console.log("hi");
getName(api, currentP1ID, function(err, name) {
var id1 = parseInt(currentP1ID)
console.log(id1);
jay.createAccount(id1, name, function(err, body){
console.log("3 " + body);
//jay.withdraw(id1, 3, function(err, body) {
// console.log("4 " + JSON.stringify(body));
//});
jay.withdraw(id1, parseInt(money), function(err, body) {
console.log("4 " + JSON.stringify(body))
})
//console.log("created 1 " + err + " body: " + JSON.stringify(body));
});
});
Cstep=false;
f1=false;
f2=false;
finalConfirmation=true;
} else {
getName(api, currentP1ID, function(err, res) {
getName(api, currentP2ID, function(err, res2) {
if ((f1=="no")&&(f2=="no")) {
api.sendMessage("No one has agreed on " + canJudge, message.threadID);
} else if ((f1=="yes")&&(f2=="no")) {
api.sendMessage(res2 + " has rejected! \r\nPlease nominate another judge.", message.threadID);
currentstep=3;
bool=true;
f1=false;
f2=false;
} else if ((f1=="no")&&(f2=="yes")) {
api.sendMessage(res + " has rejected! \r\nPlease nominate another judge.", message.threadID);
currentstep=3;
bool=true;
f1=false;
f2=false;
} //else if ((trap1===)&&(trap2===)) {
// api.sendMessage("Error: "+res+" has entered " + f1 + " and " + res2 + " has entered " + f2+".", message.threadID);
//}
})
})
}
}
bool=false;
if (/*flag3===true*/currentstep==4&&(((message.senderID===currentP1ID)&&(iii===1))||((message.senderID===currentP2ID)&&(iii===0)))) { //to confirm with other person
if (canJudge===message.body) {
flag3 =false;
currentstep++;
finJudge=canJudge;
api.sendMessage("The judge is " + finJudge+".\r\nBoth players please type \"yes\" to confirm final bet!!!", message.threadID);
Cstep = true;
} else if (message.body==="no") {
api.sendMessage("Player One, please nominate another judge.", message.threadID);
currentstep--;
bool=true;
}
} else if (currentstep == 4) {
//api.sendMessage(iii + " " + canJudge, message.threadID);
}
if ((!bool)&&(currentstep==3/*flag2===true*/&&(message.senderID===currentP1ID||message.senderID===currentP2ID))) { //to assign the judge, requires flag1 if sequence to happen already
canJudge=message.body
currentstep++;
flag3 =true;
api.sendMessage("The candidate judge is " + canJudge + ".\r\nRecipient, enter \"no\" if you disagree. Or enter the name of the nominated judge if you agree.", message.threadID);
flag2=false;
if (message.senderID===currentP1ID) {
iii=0;
} else {
iii=1;
}
}
if (currentstep==2/*flag1===true*/&&message.senderID==currentP1ID) { //to prompt for the amount of money
getName(api, currentP1ID, function(err, res) {
getName(api, currentP2ID, function(err, res2) {
jay.getBalance(res,function(err,bal1) {
jay.getBalance(res2, function(err,bal2) {
jay.createAccount(currentP1ID, res, function(err, body){
jay.createAccount(currentP2ID, res2, function(err, body){
if (isNaN(message.body)) {
api.sendMessage("Error, not a number! Try again! (quit to start over)", message.threadID);
} else if (bal1-parseInt(message.body)<0) {
api.sendMessage(res + " does not have sufficient funds. Use a lower number.", message.threadID);
} else if (bal2-parseInt(message.body)<0) {
api.sendMessage(res2 +" does not have sufficient funds. Use a lower number.", message.threadID);
} else {
money = message.body;
flag1=false;
flag2=true;
currentstep++;
api.sendMessage(res+" or "+ res2 +", please name a judge.", message.threadID);
}
})
})
})
})
})
})
}
if (tokens[0].valueOf()==="outcome".valueOf()&&/*(flag1a===true)*/currentstep==1&&(message.senderID==currentP1ID)) { //add condition player 1 must be the one to enter
api.sendMessage("Start a bet for how much? (number)", message.threadID);
flag1 = true;
flag1a = false;
currentstep+=1;
betString = message.body.substring(message.body.indexOf(" "));
}
if (tokens[0].valueOf()==="bet".valueOf()) {
flag1 = true;
currentstep+=1;
//getName(api, message.senderID, function(err, obj) {
// currentP1ID = obj;
//})
currentP1ID = message.senderID;
var fullname = "";
for (var i=1;i<tokens.length;i++) {
if (i>1) fullname+= " ";
fullname += tokens[i];
}
console.log(fullname);
getID(api, fullname, message.threadID, function(err, obj) {
if (err) {
api.sendMessage("We were unable to find this user in this group chat. Please enter the user's full name.", message.threadID);
console.log(err);
currentstep -= 1;
} else {
api.sendMessage("Please enter the winning condition or description of your bet. \r\nEnter \"outcome (outcome)\"", message.threadID);
console.log(obj);
currentP2ID = obj;
}
});
//currentP2ID = tokens[1];
}
//console.log(tokens[0] + " outcome")
//console.log(currentstep + " " + 1)
//console.log(message.senderID + " " + currentP1ID);
//console.log(currentstep);
//api.sendMessage("---",message.threadID);
//api.sendMessage(message.body+" "+message.senderID, message.threadID);
});
}
// Create simple echo bot
login({email: config.fb_user, password: config.fb_pass}, x); // fucntion takes in obj email/pass and a callback;
});
function getID(api, name, threadID, callback) {
var found = false;
api.getThreadInfo(threadID, function(err, info) {
if (err) {
callback(err);
return;
} else {
api.getUserInfo(info.participantIDs, function(err, obj) {
info.participantIDs.forEach(function(id) {
if (obj[id].name == name) {
callback(null, id);
found = true;
}
});
if (!found)
callback("Unable to find ID");
});
}
return;
});
}
function getName(api, userID, callback) {
api.getUserInfo(userID, function(err, obj) {
if (err) {
callback(err);
return;
} else {
callback(null, obj[userID].name);
}
});
}