-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
99 lines (83 loc) · 2.94 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
import os, sys
from flask import Flask, request
from utils import wit_response,get_wiki_content
import json
import requests
app = Flask(__name__)
PAGE_ACCESS_TOKEN = ""
@app.route('/', methods=['GET'])
def verify():
# Webhook verification
if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
if not request.args.get("hub.verify_token") == "hello":
return "Verification token mismatch", 403
return request.args["hub.challenge"], 200
return "Hello world", 200
@app.route('/', methods=['POST'])
def webhook():
data = request.get_json()
#log(body)
try:
if data['object'] == 'page':
for entry in data['entry']:
for messaging_event in entry['messaging']:
# IDs
sender_id = messaging_event['sender']['id']
recipient_id = messaging_event['recipient']['id']
if messaging_event.get('message'):
# Extracting text message
if 'text' in messaging_event['message']:
messaging_text = messaging_event['message']['text']
else:
messaging_text = 'no text'
#print("messaging_text:"+messaging_text)
greetlist=['hi','hii','hiii','hello','helloo','hey']
if(messaging_text.lower() in greetlist):
greetresp='Hi, I am a Wikibot. I can provide you information from Wikipedia. Please ask your question'
response_content = {
"recipient": {
"id": sender_id
},
"message": {
"text": greetresp
}
}
else :
#categories = wit_response("hello")
categories = ""
#print("categories is:"+categories)
if categories.startswith('Please'):
response_content = {
"recipient": {
"id": sender_id
},
"message": {
"text": categories
}
}
else:
elements,urlinfo= get_wiki_content(messaging_text)
elements=''.join(str(e) for e in elements)
#print(elements)
ele=elements[300:400]
lst=ele.find('.')
lst=ele[:lst+1]
response_content = {
"recipient": {
"id": sender_id
},
"message": {
"text": elements[:300]+lst +'\n'+'Read More: '+ urlinfo
}
}
headers = {"Content-Type": "application/json"}
url = "https://graph.facebook.com/v2.6/me/messages?access_token=%s" % PAGE_ACCESS_TOKEN
r = requests.post(url, data=json.dumps(response_content), headers=headers)
except Exception as ex:
print ("main class exp "+str(ex))
return "ok", 200
def log(message):
print(message)
sys.stdout.flush()
if __name__ == "__main__":
app.run(debug=True)