-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
240 lines (223 loc) · 7.24 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
from flask import Flask
from flask import request
from wit import Wit
import json
import requests
import os
import time as tim
app = Flask(__name__)
followup = 0
@app.route('/receive',methods=['POST','GET'])
def g():
#return request.args.get('hub.challenge')
a = request.get_json()
#print a
#print ">>>>>>>>>>>>>>>>>>>"
try:
mess = a['entry'][0]['messaging'][0]['message']['text']
print mess
userID= a['entry'][0]['messaging'][0]['sender']['id']
if followup == True:
followup = False
return
#postFbtext(userID,)
#witResp(mess)
#getWit(mess)
except:
print 'FB message input Failed'
try:
print (respond(userID, mess))
except:
print 'Error in response'
return 'hello world'
def respond(userId,message):
#print('Hello World!')
dicteg = getWit(message)
print dicteg
mode = 0;
location = dicteg.get('location' ,"")
timeA = dicteg.get('datetime',"")
seatno = dicteg.get('number_seats' ,"")
fooditems = dicteg.get('fooditems',"")
for key,value in dicteg.iteritems():
if key == "self_enquiry":
postFbtext(userId,'My name is Emily and I was designed by Varun and Roopesh, at SSN.Also, I promise not to destroy humanity :)')
return 'My name is Emily and I was designed by Varun and Roopesh, at SSN.Also, I promise not to destroy humanity :)'
elif key == "greeting":
postFbtext(userId,'Hello there!')
return 'Hello there!'
elif key == "action":
if value == "book":
mode = 1
print 'selected Book'
elif value == "order" or value == "preorder":
mode = 2
elif value == "specials":
mode = 3
else:
mode = 0
if mode == 1:
aa=reservation(location,timeA,seatno,userId)
return aa
if mode == 2:
print 'outside for loop',mode
bb= preorder(location,fooditems,userId)
return bb
if mode == 3:
cc= getSpecials(location,userId)
return cc
else:
postFbtext(userId,"Sorry! I didnt quite understand what you said, but I get better with time like old wine!")
return "Sorry! I didnt quite understand what you said, but I get better with time like old wine!"
print 'Ending function'
def firebaseJSON():
try:
string = "curl https://emily-c3113.firebaseio.com/.json > temp3.txt"
os.system(string)
except:
print 'Error while accessing firebase'
with open("temp3.txt", "r") as ins:
a = json.load(ins)
return a
def reservation(location,timeA,seatno,userID):
'''if location == "":
followup = True'''
print '>>>>>>>>>>>>>>>>Done'
a=firebaseJSON()
#print a
#dic = {"behaviorMode":1,"seatsRequired":seatno,"wait":"1"}
a['behaviorMode']="1"
string = "curl -X PUT -d '" + json.dumps(a) +" 'https://emily-c3113.firebaseio.com/.json'"
#print ">>>>>>" + string
os.system(string)
a['wait']="1"
string = "curl -X PUT -d '" + json.dumps(a) +"' 'https://emily-c3113.firebaseio.com/.json'"
os.system(string)
a['seatsRequired']=str(seatno)
string = "curl -X PUT -d '" + json.dumps(a) +"' 'https://emily-c3113.firebaseio.com/.json'"
os.system(string)
#print "asdas" + string
while True:
tim.sleep(3)
'''os.system("curl https://emily-c3113.firebaseio.com/wait.json > temp3.txt")
with open("temp3.txt", "r") as ins:
b = json.load(ins)
#print type(b),b
if(str(b) == "0"):'''
break
#print 'outside'
os.system("curl https://emily-c3113.firebaseio.com/response.json > temp3.txt")
with open("temp3.txt", "r") as ins:
b = json.load(ins)
print b
if str(b) == "yes":
postFbtext(userID,"Yes. The table for " + seatno + " was available at " + location +" and the booking was confirmed for " + timeA )
return str("Yes. The table for " + seatno + " was available at " + location +" and the booking was confirmed for " + timeA)
else:
postFbtext(userID,"No. Sorry, the table for " + seatno +" was not available at" +location + " on " + timeA )
return str("No. Sorry, the table for " + seatno +" was not available at" +location + " on " + timeA)
def preorder(location,fooditems,userID):
print 'here >>>>>'
try:
a=firebaseJSON()
#print a
#dic = {"behaviorMode":1,"seatsRequired":seatno,"wait":"1"}
a['behaviorMode']="2"
string = "curl -X PUT -d '" + json.dumps(a) +" 'https://emily-c3113.firebaseio.com/.json'"
#print ">>>>>>" + string
os.system(string)
a['order']=str(fooditems)
string = "curl -X PUT -d '" + json.dumps(a) +"' 'https://emily-c3113.firebaseio.com/.json'"
os.system(string)
a['wait']="1"
string = "curl -X PUT -d '" + json.dumps(a) +"' 'https://emily-c3113.firebaseio.com/.json'"
os.system(string)
#print "asdas" + string
while True:
tim.sleep(3.5)
'''os.system("curl https://emily-c3113.firebaseio.com/wait.json > temp3.txt")
with open("temp3.txt", "r") as ins:
b = json.load(ins)
print type(b),b
if(str(b) == "0"):'''
break
#print 'outside'
os.system("curl https://emily-c3113.firebaseio.com/response.json > temp3.txt")
with open("temp3.txt", "r") as ins:
b = json.load(ins)
print b
if str(b) == "yes":
postFbtext(userID,"Yes. The preorder for "+ fooditems+ " was placed in " + location )
return str("Yes. The preorder for "+ fooditems+ " was placed in " + location )
else:
postFbtext(userID,"Sorry, "+location +" failed to accept order of " + fooditems )
return str("Sorry, "+location +" failed to accept order of " + fooditems )
except:
print 'Error in order'
from query import Specials,getQuery
def getSpecials(location,userID):
try:
a=getQuery()
A =Specials(a,location)
string= ""
if len(A) == 0:
string = "Currently " + location + " does not have a chef suggested specials"
else:
string =location + " is known for"
for i in A:
string = string + str(i) + ", "
postFbtext(userID,string)
return string
except:
print 'Error in Specials'
Witaccess_token = "LVLKPQNZ5IEMVZ6OB5QHGSUZGBBGZCNM"
def witResp(message):
try:
client = Wit(access_token=Witaccess_token)
resp = client.message(message)
except:
print 'Error in wit message() API'
def getJson(userID):
a={
"recipient":{
"id": str(userID)
},
'message':{
"text":"hello, world!"
}
}
return a
def fileToJson(txtfile):
try:
with open(txtfile, "r") as ins:
a = json.load(ins)
return a
except:
print 'Error in converting txt file to Json'
def postFbtext(userID,msg):
#print string
try:
string ="curl -X POST -H \"Content-Type: application/json\" -d \'{ \
\"recipient\":{ \
\"id\":\"" + str(userID) + "\" \
}, \
\"message\":{ \
\"text\":\"" + str(msg) +"\" \
} \
}' \"https://graph.facebook.com/v2.6/me/messages?access_token=EAAD5596IWWYBABpdG4yEChjuvAqU2HQ0xZBYDo48BXrXPBTvcU6yUeXplDdPA63YVGPl7eIBr2AH2sfDwaouq6mkrpNn1VVueP0pHin9s81S2wQtJvveZA3G6fr7NXBKtKHUlLFX2PMzlT5Y8K14q39bHRCh6pjGReZA8IKlQZDZD\""
os.system(string)
except:
print 'Error in Posting to FB'
def getWit(mess):
mess=mess.replace(" ","%20")
cmd = "curl -H 'Authorization: Bearer LVLKPQNZ5IEMVZ6OB5QHGSUZGBBGZCNM' 'https://api.wit.ai/message?v=20160813&q=" + mess + "'"
os.system(cmd + " > temp.txt")
a=fileToJson("temp.txt")
#print a
response = {}
try:
for key,value in a['entities'].iteritems():
response[key] = value[0]['value']
return response
except:
print 'Error in removing unwanted fields'