forked from akshaynathr/Remember-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
568 lines (397 loc) · 19.9 KB
/
app.py
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
'''Main file'''
import os
import sys
import json
import requests
from flask import Flask, request
from wiki import *
from models import dbSetUp,r
from jokes import jokes
from facts import facts
from quotes import quotes
from random import randint
from news import *
import datetime
from search import *
from manager import *
import arrow
dbSetUp()
_chatbot_agent=None
app = Flask(__name__)
token="remember_bot"
@app.route('/', methods=['GET'])
def verify():
# when the endpoint is registered as a webhook, it must echo back
# the 'hub.challenge' value it receives in the query arguments
if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
if not request.args.get("hub.verify_token") == token:
return "Verification token mismatch", 403
return request.args["hub.challenge"], 200
return "Hello world", 200
@app.route('/', methods=['POST'])
def webhook():
# endpoint for processing incoming messaging events
flag=0
data = request.get_json()
log(data) # you may not want to log every incoming message in production, but it's good for testing
if data["object"] == "page":
for entry in data["entry"]:
for messaging_event in entry["messaging"]:
if messaging_event.get('read'):
print("Message read")
continue
if messaging_event.get('delivery'):
print("Message delivered")
continue
if messaging_event.get("message"): # someone sent us a message
sender_id = messaging_event["sender"]["id"] # the facebook ID of the person sending you the message
recipient_id = messaging_event["recipient"]["id"] # the recipient's ID, which should be your page's facebook ID
if messaging_event["message"].get("text"):
message_text = messaging_event["message"]["text"]
message_text =message_text.strip("?")
message_text =message_text.strip(".")
message_text =message_text.strip("~")
#message_text.strip("\")
message_text =message_text.strip("!")
message_text =message_text.strip("$")
message_text =message_text.strip("%")
message_text =message_text.strip("*")
message_text =message_text.strip("-")
message_text =message_text.strip("'")
# else:
# . # the message's text
# . message_text="DONE"
msg_lst=message_text.split(" ")
'''SAVE FUNCTIONS '''
if msg_lst[0].lower()=="save":
flag=1
l=len(msg_lst[0])
if len(message_text)==l or len(msg_lst)==2:
send_message(sender_id,"Please specify something to save. Type 'Save ExamId 12345' ")
else:
y=message_text.lower()
t=y.split()
value=t[-1]
key=t[1:len(t)-1]
k=""
for i in key:
if i!='as' or i!='on' or i!='is':
k=k+" "+i
k=k[1:]
k=k.strip()
res=save_post(sender_id,k,value)
if res:
print("Successfully saved")
send_message(sender_id,"saved successfully")
else:
print("Error in db save")
send_message(sender_id,"Error in saving. Please try again")
'''GET FUNCTIONS'''
if msg_lst[0].lower()=="get" or msg_lst[0].lower()=="recall":
flag=1
key=msg_lst[1:]
print (key)
k=""
for i in key:
k=k+" "+i
k=k[1:]
k=k.strip(" ")
print (k)
res=get_post(sender_id,k)
if len(res)==0:
send_message(sender_id,"Oops no memories found")
else:
send_message(sender_id,res[0]["value"])
''' WIKI FUNCTIONS '''
if msg_lst[0].lower()=="wiki":
flag=1
l=len(msg_lst[0])
if len(message_text)==l:
send_message(sender_id,"You specified nothing to search. Type 'Wiki search_term' ")
else:
search=message_text[l:]
result=search_wiki(search)
if len(result)==0:
send_message(sender_id,"Oops. Cannot find anything in wiki")
else:
article=get_results_wiki(result[0])
heading = article.heading
image=article.image
url=article.url
fall_back_url="https://en.wikipedia.org/"
data=generic_template(url=url,img_url=image,title=heading,sub_title=None,fall_back_url=fall_back_url,btn_url=url,btn_title="Go to wiki",recipient=sender_id)
#send_message(sender_id,"test")
#send_message(sender_id,"Fetching data from wikipedia")
send_message(sender_id,None,data)
return "Ok",200
''' SAY/TELL FUNCTIONs '''
if msg_lst[0].lower()=="say" or msg_lst[0].lower()=="tell":
flag=1
if len(msg_lst)<=1:
send_message(sender_id,"What should i say?")
else:
if msg_lst[-1].lower()=="joke":
send_message(sender_id,get_jokes())
elif msg_lst[-1].lower()=="fact":
send_message(sender_id,get_fact())
elif msg_lst[-1].lower()=="quote":
send_message(sender_id,get_quote())
else:
send_message(sender_id,message_text[5:])
''' news function '''
if message_text.lower()=="what's new in techcrunch?" or message_text.lower()=="whats new in techcrunch?" or message_text.lower()=="show me techcrunch" or message_text.lower()=="news in techcrunch" or message_text.lower()=="what's new in techcrunch" or (msg_lst[0].lower()!="search" and message_text.lower().find("techcrunch")!=-1):
flag=1
news=get_news()
data=news_generic_template(news,sender_id)
print (data)
send_message(sender_id,None,data)
''' REMIND FUNCTION '''
if msg_lst[0].lower()=='remind':
flag=1
if len(msg_lst)<2:
send_message(sender_id,"Please tell me something to remind you. Eg: Remind me to watch sherlock tomorrow")
else:
if message_text.find("hours") !=-1:
connection=r.connect("localhost",28015)
delta=int(msg_lst[-2])
time=arrow.utcnow().to('Asia/Calcutta')
time=time.replace(hours=+delta)
_t=time
time=time.for_json()
topic=msg_lst[3]+msg_lst[4]
r.db('remember_bot').table('reminder').insert({"sender_id":sender_id, "time":time,"active":1,"topic":topic}).run(connection)
t="Reminder is set on "
t=t+str(_t.format('YYYY-MM-DD HH:mm:ss'))
connection.close()
send_message(sender_id,t)
if message_text.find("minutes")!=-1 or message_text.find("minute")!=-1:
connection=r.connect("localhost",28015)
delta=(msg_lst[-2])
if delta=='one':
delta=1
elif delta=='two':
delta=2
elif delta=='three':
delta=3
elif delta=='four':
delta=4
elif delta=='five':
delta=5
elif delta=='six':
delta=6
elif delta=='seven':
delta=7
elif delta=='eight':
delta=8
elif delta=='nine':
delta=9
else:
delta=int(msg_lst[-2])
time=arrow.utcnow().to('Asia/Calcutta')
time=time.replace(minutes=+delta)
_t=time
time=time.for_json()
topic=msg_lst[3]+msg_lst[4]
r.db('remember_bot').table('reminder').insert({"sender_id":sender_id, "time":time,"active":1,"topic":topic}).run(connection)
t="Reminder is set on "
t=t+str(_t.format('YYYY-MM-DD HH:mm:ss'))
connection.close()
send_message(sender_id,t)
if message_text.find("tomorrow")!=-1:
if msg_lst[-1]=="tomorrow":
connection=r.connect("localhost",28015)
time=arrow.utcnow().to('Asia/Calcutta')
time=time.replace(days=+1)
_t=time
time=time.for_json()
topic=msg_lst[3]+" "+msg_lst[4]
r.db('remember_bot').table('reminder').insert({"sender_id":sender_id, "time":time,"active":1,"topic":topic}).run(connection)
t="Reminder is set on "
t=t+str(_t.format('YYYY-MM-DD HH:mm:ss'))
connection.close()
send_message(sender_id,t)
''' name '''
message_text.strip('?')
if message_text=="What's your name":
flag=1
send_message(sender_id,"I am remember bot")
''' search '''
if msg_lst[0].lower()=="search":
flag=1
data=create_template(recipient=sender_id,search_title=message_text[7:])
if data=="Error":
send_message(sender_id,"Cannot find any result")
else:
send_message(sender_id,"Showing top three results from google")
send_message(sender_id,None,data)
return "ok",200
''' help '''
if msg_lst[0].lower()=="help" or msg_lst[0].lower()=="what can you do?" or msg_lst[0].lower()=="what are your superpowers?" or msg_lst[0].lower()=="superpowers?":
flag=1
powers='''Hi , I am remember bot. I have the following superpowers.\n\nI can show you news from techcrunch. Just say "What's new in techcrunch?"\n\nI can search wiki . Just say "Wiki <search term>"\n\nI can tell you a joke, a quote, or a fact. Just ask me "Tell a joke".\n\nI can google search for you. Tell "Search <search term>"\n\nI can store your important posts. Say "Save ExamId 12345"\n\nI can remind you things too. Ex: Remind me to call anand after 10 minutes.\n\n'''
send_message(sender_id,powers)
''' chat functions '''
if flag==0:
flag=1
if len(msg_lst)>0:
global _chatbot_agent
if _chatbot_agent==None:
_chatbot_agent=initialize_bot()
reply=ask_bot(message_text,_chatbot_agent)
send_message(sender_id,reply)
else:
send_message(sender_id,"I don't understand your question. Sorry..")
#return "ok", 200
if messaging_event.get("delivery"): # delivery confirmation
pass
if messaging_event.get("optin"): # optin confirmation
pass
if messaging_event.get("postback"): # user clicked/tapped "postback" button in earlier message
pass
print("Ok")
return "ok",200
def send_message(recipient_id, message_text,data=None):
log("sending message to {recipient}: {text}".format(recipient=recipient_id, text=message_text))
params = {
"access_token": "EAAPJyQPXUXMBAHcfCZChSIaJzaNahe04jsGkBSdlhTz637ZA3rUmBcUXJ6cQKbOVmLUAfSLT4hA5zY5ZCfi2Lv6uk7lTxKM5OT29nHMhrNJSOq0lzZCSeftHAPnt7CPdiTCZAsVi4Vdnc4li83Ml0MbloINZCJmFZCItKD9e7IYZCwZDZD"
}
headers = {
"Content-Type": "application/json"
}
#data=generic_template()
data2 = json.dumps({
"recipient": {
"id": recipient_id
},
"message": {
"text": message_text
}
})
if data==None:
data=data2
send_writing(recipient_id)
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
if r.status_code != 200:
log(r.status_code)
log(r.text)
else:
print("Code:")
log(r.status_code)
def log(message): # simple wrapper for logging to stdout on heroku
print( str(message))
sys.stdout.flush()
def generic_template(url,img_url,title,sub_title,fall_back_url,btn_url,btn_title,recipient):
# data2=json.dumps({
# "recipient": {
# "id": recipient_id
# },
# "message":{
# "attachment":{
# "type":"template",
# "payload":{
# "template_type":"generic",
# "elements":[
# {
# "title":"Welcome to Peter\'s Hats",
# "image_url":"http://www.villagehatshop.com/photos/product/standard/4511390S61417/mens-hats/mj-panama-straw-outback-hat.jpg",#"https://petersfancybrownhats.com/company_image.png",
# "subtitle":"We\'ve got the right hat for everyone.",
# "default_action": {
# "type": "web_url",
# "url": "https://techcrunch.comview?item=103",
# "messenger_extensions": True,
# "webview_height_ratio": "tall",
# "fallback_url": "https://techcrunch.com"
# },
# "buttons":[
# {
# "type":"web_url",
# "url":"https://petersfancybrownhats.com",
# "title":"View Website"
# },{
# "type":"postback",
# "title":"Start Chatting",
# "payload":"DEVELOPER_DEFINED_PAYLOAD"
# }
# ]
# }
# ]
# }
# }
# }
# })
data2=json.dumps({
"recipient": {
"id": recipient
},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
{
"title":title,
"image_url":img_url,#"https://petersfancybrownhats.com/company_image.png",
"subtitle":sub_title,
"default_action": {
"type": "web_url",
"url": url,
"messenger_extensions": True,
"webview_height_ratio": "tall",
"fallback_url": fall_back_url
},
"buttons":[
{
"type":"web_url",
"url":btn_url,
"title":"Read more"
}
]
}
]
}
}
}
})
print (data2)
return data2
''' this function sends the typing bubble '''
def send_writing(recipient):
data=json.dumps({
"recipient":{
"id":recipient
},
"sender_action":"typing_on"
})
params = {
"access_token": "EAAPJyQPXUXMBAHcfCZChSIaJzaNahe04jsGkBSdlhTz637ZA3rUmBcUXJ6cQKbOVmLUAfSLT4hA5zY5ZCfi2Lv6uk7lTxKM5OT29nHMhrNJSOq0lzZCSeftHAPnt7CPdiTCZAsVi4Vdnc4li83Ml0MbloINZCJmFZCItKD9e7IYZCwZDZD"
}
headers = {
"Content-Type": "application/json"
}
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
if r.status_code != 200:
log(r.status_code)
log(r.text)
''' DATABASE QUERIES '''
def save_post(sender_id,key,value):
connection=r.connect("localhost",28015)
res=r.db('remember_bot').table('post').insert({'sender_id':sender_id, 'key':key,'value':value} ).run(connection)
connection.close()
return res
def get_post(sender_id,key):
connection=r.connect("localhost",28015)
res=list(r.db('remember_bot').table('post').filter((r.row['sender_id']==sender_id) & (r.row['key']==key) ).run(connection))
return (res)
''' JOKE , FACT ,QUOTE FUNCTIONS '''
def get_jokes():
t=randint(0,len(jokes))
return jokes[t]
def get_fact():
t=randint(0,len(facts))
return facts[t]
def get_quote():
t=randint(0,len(quotes))
return quotes[t]
'''-------------------------------------------------'''
''' REMINDER SUPPORTING FUNCTIONS '''