-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
262 lines (182 loc) · 6.03 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
import os,sys
import datetime as dt
from flask import Flask,request
from utils import wit_response
import json
import requests
from pymessenger import Bot
from fbmq import Page,Attachment,Template,QuickReply
from flask_sqlalchemy import SQLAlchemy
from numpy import random
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=True
#app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:kashyap@localhost/fb_bot'
app.config['SQLALCHEMY_DATABASE_URI'] = "postgres://pommhsyqyqzeru:2711b3952a28db3a024f241c9fb06fb793fc0967052f60cf735c77f410ac2bb6@ec2-50-19-218-160.compute-1.amazonaws.com:5432/daocq4mn1fibj3"
db = SQLAlchemy(app)
PAGE_ACCESS_TOKEN = 'EAAbYwLFXiZAABAKawijZBEv0i38L0PcP2c3TiRN4nZC2Fmw1M6kbXsSeg6QVF4RqZBVO3uuwnn33HQZCglLyLujVwspDPzyN6wqiZAj69xPJdnyufbxmQmxdfKWTpCSvpCfcMs1BF0vkdDZB1ZBBDZAuOKdw4txLZCgSF2HFUNMfOy8gp5ZCN9eAep0'
bot = Bot(PAGE_ACCESS_TOKEN)
page = Page(PAGE_ACCESS_TOKEN)
########## TABLES #########
class drink(db.Model):
sn = db.Column(db.Integer, primary_key = True)
id = db.Column(db.String(80))
item = db.Column(db.String(80))
def __init__(self,sn,item,id):
self.sn = sn
self.item = item
self.id = id
class veg(db.Model):
sn = db.Column(db.Integer, primary_key = True)
id = db.Column(db.String(80))
item = db.Column(db.String(80))
def __init__(self,sn,item,id):
self.sn = sn
self.item = item
self.id =id
class non_veg(db.Model):
sn = db.Column(db.Integer, primary_key = True)
id = db.Column(db.String(80))
item = db.Column(db.String(80))
def __init__(self,sn,item,id):
self.sn = sn
self.item = item
self.id = id
class videos(db.Model):
url = db.Column(db.String(80),primary_key = True)
desc = db.Column(db.String(80),unique =True)
def __init__(self,url,desc):
self.url = url
self.desc = desc
#############CREATE DATABASE#######
#db.create_all()
#import python_db2
@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") == "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(data)
if data['object'] == 'page':
for entry in data['entry']:
for messaging_event in entry['messaging']:
sender_id = str(messaging_event['sender']['id'])
recipient_id = str(messaging_event['recipient']['id'])
if messaging_event.get('message'):
if 'text' in messaging_event['message']:
messaging_text = messaging_event['message']['text']
else:
messaging_text = 'no text:'
#print(sender_id)
response = "none"
entity,value = wit_response(messaging_text)
#print(entity)
if entity == "greetings":
quick_reply(sender_id)
elif entity == "diet_entity":
page.send(sender_id,"Veg or Non-Veg?")
elif entity == "video_entity":
send_videos(sender_id)
elif entity =="subscribe_entity":
page .send(sender_id,"You will get daily fitness quotes!!")
elif entity == "diet_type":
if value == 'veg':
veg_diet(sender_id)
else:
non_veg_diet(sender_id)
else:
page.send(sender_id,"Please enter a valid choice!!")
quick_reply(sender_id)
return "ok",200
return "ok",200
###########SEND VIDEOS##########
def send_videos(sender_id):
w=videos.query.all()
xyz=random.choice(w)
#print(xyz.url)
url=str(xyz.url)
desc=str(xyz.desc)
page.send(sender_id,Attachment.Video(url))
page.send(sender_id,"desc")
quick_reply(sender_id)
###########SEND NON-VEG-DIET ACC. To TIME###########
def non_veg_diet(sender_id):
list=[]
hour =dt.datetime.now().hour
if(hour>5 and hour <12):
w=non_veg.query.filter_by(id='Breakfast').all()
z = w[randint(0,len(w)-1)].item
list.append(z)
juice=drink.query.filter_by(id='Drink').all()
z = juice[random.randint(0,len(juice)-1)].item
list.append(z)
elif(hour>12 and hour <16):
w=non_veg.query.filter_by(id='Non_Veg_Lunch_Mandatory').all()
for i in w:
list.append(i.item)
x=non_veg.query.filter_by(id='Non_Veg_Lunch_Optional').all()
y=random.choice(x,2)
for i in y:
list.append(i.item)
elif(hour>16 and hour <18):
page.send(sender_id,"Snacks not available !!!")
quick_reply(sender_id)
return
elif(hour>18 and hour<24):
w=non_veg.query.filter_by(id="Non_Veg_Dinner_Mandatory").all()
for i in w:
list.append(i.item)
x = non_veg.query.filter_by(id='Non_Veg_Dinner_Optional').all()
y=random.choice(x,2)
for i in y:
list.append(i.item)
for i in list:
page.send(sender_id,i)
quick_reply(sender_id)
###########SEND VEG-DIET ACC. To TIME###########
def veg_diet(sender_id):
list=[]
hour =dt.datetime.now().hour
if(hour>5 and hour <12):
w=veg.query.filter_by(id='Breakfast').all()
z = w[randint(0,len(w)-1)].item
list.append(z)
juice=drink.query.filter_by(id='Drink').all()
z = juice[random.randint(0,len(juice)-1)].item
list.append(z)
elif(hour>12 and hour <16):
w=veg.query.filter_by(id='Veg_Lunch_Mandatory').all()
for i in w:
list.append(i.item)
x=veg.query.filter_by(id='Veg_Lunch_Optional').all()
y=random.choice(x,2)
for i in y:
list.append(i.item)
elif(hour>16 and hour <18):
page.send(sender_id,"Snacks not available !!!")
quick_reply(sender_id)
return
elif(hour>18 and hour<24):
w=veg.query.filter_by(id="Veg_Dinner_Mandatory").all()
for i in w:
list.append(i.item)
x = veg.query.filter_by(id='Veg_Dinner_Optional').all()
y=random.choice(x,2)
for i in y:
list.append(i.item)
for i in list:
s=str(i)
page.send(sender_id,s)
quick_reply(sender_id)
def quick_reply(sender_id):
quick_replies = [{'title': 'Get Your Diet', 'payload' : 'pick_action'},
{'title': 'Fitness-Videos', 'payload' : 'pick_action'},
{'title': 'Subscribe ', 'payload' : 'pick_action'}
]
page.send(sender_id,"Welcome to Diet-Bot !!!",quick_replies=quick_replies,metadata ="DEVELOPER_DEFINED_METADATA")
if __name__ == "__main__":
app.run(debug = True,port=50)