-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
259 lines (206 loc) · 7.69 KB
/
main.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
import template
import template1
import os
import sys
import json
from bs4 import BeautifulSoup
import random
import requests
from flask import Flask, request
app = Flask(__name__)
PAGE_TOKEN = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
VERIFY_TOKEN = "hello"
welcome = ["hi", "hey", "hello"]
@app.route('/', methods=['GET'])
def verify():
if request.args.get("hub.mode") == "subscribe" and request.args.get("hub.challenge"):
if not request.args.get("hub.verify_token") == VERIFY_TOKEN:
return "Verification token mismatch", 403
return request.args["hub.challenge"], 200
return "Working", 200
@app.route('/', methods=['POST'])
def webook():
# endpoint for processing incoming messaging events
data = request.get_json()
print data
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("message"): # someone sent us a message
print '?'*20
print messaging_event
print '?'*20
sender_id = messaging_event["sender"]["id"] # the facebook ID of the person sending you the message
recipient_id = messaging_event["recipient"]["id"]
print messaging_event
message_text = messaging_event["message"]["text"]
# the message's text
if message_text in welcome:
send_typing(sender_id)
send_message(sender_id,"Welcome to Travel Guide! Explore Rajasthan with just one tap.")
send_quick_reply(sender_id)
elif message_text == 'Hotels':
send_typing(sender_id)
send_message(sender_id,"Which city do you want?")
hotels=template.hotel('jaipur')
if len(hotels)>5:
send_message(sender_id,hotels[0])
send_message(sender_id,hotels[1])
send_message(sender_id,hotels[2])
send_message(sender_id,hotels[3])
send_message(sender_id,hotels[4])
send_quick_reply(sender_id)
else:
send_typing(sender_id,hotels[0])
if message_text == "Tourist Attraction":
send_typing(sender_id)
places=template1.place('jaipur')
if len(places)>5:
send_message(sender_id,places[0])
send_message(sender_id,places[1])
send_message(sender_id,places[2])
send_message(sender_id,places[3])
send_message(sender_id,places[4])
send_quick_reply(sender_id)
else:
send_typing(sender_id,attractions[0])
if messaging_event.get("delivery"):
pass
if messaging_event.get("optin"): # optin confirmation
pass
if messaging_event.get("postback"): # user clicked/tapped "postback" button in earlier message
msg = messaging_event["postback"]['payload']
send_id = messaging_event["sender"]['id']
if msg == "Travel":
send_msg = "Travel Guide is your personal guide assistant which gives you all information about travel and tourism."
send_message(send_id, send_msg)
elif msg == "start_button":
send_msg =(send_id,"Hello " + str(name.replace("_"," ")) + " \n" + "Welcome to Travel Guide, your travel assistant to give the information you want." )
elif msg == "yash":
send_msg = "This project was done by Shrey Patel and Yash Shah at the rajasthan hackathon 4.0"
send_message(send_id, send_msg)
return "ok", 200
def send_message(recipient_id, message_text):
log("sending message to {recipient}: {text}".format(recipient=recipient_id, text=message_text))
params = {
"access_token": PAGE_TOKEN
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"message": {
"text": message_text
}
})
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)
def send_typing(recipient_id):
params = {
"access_token": PAGE_TOKEN
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"sender_action":"typing_on"
})
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
def send_quick_reply(recipient_id):
params = {
"access_token": PAGE_TOKEN
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"recipient": {
"id": recipient_id
},
"message": {
"text":'Services we provide.',
"quick_replies":[
{
"content_type":"text",
"title":"Tourist Attraction",
"payload":"next"
},
{
"content_type":"text",
"title":"Hotels",
"payload":"next"
},
{
"content_type":"text",
"title":"Nearby Hospitals",
"payload":"next"
},
{
"content_type":"text",
"title":"Emergency Numbers",
"payload":"next"
},
]
}
})
r = requests.post("https://graph.facebook.com/v2.6/me/messages", params=params, headers=headers, data=data)
#menu
def per_menu():
params = {
"access_token": PAGE_TOKEN
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"setting_type" : "call_to_actions",
"thread_state" : "existing_thread",
"call_to_actions":[
{
"type":"postback",
"title":"Travel Guide",
"payload":"Travel"
},
{
"type":"postback",
"title":"Created by Blue Wizzards",
"payload":"yash"
}
]
})
r = requests.post("https://graph.facebook.com/v2.6/me/thread_settings", params=params, headers=headers, data=data)
#menu
def start_button():
params = {
"access_token": PAGE_TOKEN
}
headers = {
"Content-Type": "application/json"
}
data = json.dumps({
"setting_type" : "call_to_actions",
"thread_state" : "new_thread",
"call_to_actions":[
{
"payload":"start_button"
}
]
})
r = requests.post("https://graph.facebook.com/v2.6/me/thread_settings", params=params, headers=headers, data=data)
per_menu()
start_button()
def log(message): # simple wrapper for logging to stdout on heroku
print "-"*10
# print str(message)
sys.stdout.flush()
if __name__ == '__main__':
app.run(debug=True,port=80)