-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
273 lines (233 loc) · 10.4 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
# doing necessary imports
from flask import Flask, render_template, request, jsonify, make_response
from flask_cors import CORS, cross_origin
import requests
import pymongo
import json
import os
from saveConversation import Conversations
from DataRequests import MakeApiRequests
from sendEmail import EMailClient
from pymongo import MongoClient
app = Flask(__name__) # initialising the flask app with the name 'app'
# geting and sending response to dialogflow
'''
@app.route('/')
def hello_world():
return "Hello WOrld"'''
'''
@app.route('/webhook', methods=['POST'])
@cross_origin()
def webhook():
req = request.get_json(silent=True, force=True)
print(req)
return {
'fulfillmentText':'Hello From the Other Side.'
}
'''
#'''
@app.route('/webhook', methods=['POST'])
@cross_origin()
def webhook():
req = request.get_json(silent=True, force=True)
res = processRequest(req)
res = json.dumps(res, indent=4)
print(res)
r = make_response(res)
r.headers['Content-Type'] = 'application/json'
return r
# processing the request from dialogflow
def processRequest(req):
# dbConn = pymongo.MongoClient("mongodb://localhost:27017/") # opening a connection to Mongo
#log = Conversations.Log()
sessionID = req.get('responseId')
result = req.get("queryResult")
intent = result.get("intent").get('displayName')
query_text = result.get("queryText")
parameters = result.get("parameters")
cust_name = parameters.get("cust_name")
cust_contact = parameters.get("cust_contact")
cust_email = parameters.get("cust_email")
#db = configureDataBase()
if intent == 'covid_searchcountry':
cust_country = parameters.get("geo-country")
if(cust_country=="United States"):
cust_country = "USA"
fulfillmentText, deaths_data, testsdone_data = makeAPIRequest(cust_country)
webhookresponse = "***Covid Report*** \n\n" + " New cases :" + str(fulfillmentText.get('new')) + \
"\n" + " Active cases : " + str(
fulfillmentText.get('active')) + "\n" + " Critical cases : " + str(fulfillmentText.get('critical')) + \
"\n" + " Recovered cases : " + str(
fulfillmentText.get('recovered')) + "\n" + " Total cases : " + str(fulfillmentText.get('total')) + \
"\n" + " Total Deaths : " + str(deaths_data.get('total')) + "\n" + " New Deaths : " + str(
deaths_data.get('new')) + \
"\n" + " Total Test Done : " + str(deaths_data.get('total')) + "\n\n*******END********* \n "
print(webhookresponse)
#log.saveConversations(sessionID, cust_country, webhookresponse, intent, db)
#log.saveCases( "country", fulfillmentText, db)
return {
"fulfillmentMessages": [
{
"text": {
"text": [
webhookresponse
]
}
},
{
"text": {
"text": [
"Do you want me to send the detailed report to your e-mail address? Type.. \n 1. Sure \n 2. Not now "
# "We have sent the detailed report of {} Covid-19 to your given mail address.Do you have any other Query?".format(cust_country)
]
}
}
]
}
elif intent == "Welcome" or intent == "continue_conversation" or intent == "not_send_email" or intent == "endConversation" or intent == "Fallback" or intent == "covid_faq" or intent == "select_country_option":
fulfillmentText = result.get("fulfillmentText")
#log.saveConversations(sessionID, query_text, fulfillmentText, intent, db)
elif intent == "send_report_to_email":
fulfillmentText = result.get("fulfillmentText")
#log.saveConversations(sessionID, "Sure send email", fulfillmentText, intent, db)
#val = log.getcasesForEmail("country", "", db)
#print("===>",val)
prepareEmail([cust_name, cust_contact, cust_email,val])
elif intent == "totalnumber_cases":
fulfillmentText = makeAPIRequest("world")
webhookresponse = "***World wide Report*** \n\n" + " Confirmed cases :" + str(
fulfillmentText.get("total_cases")) + \
"\n" + " Deaths cases : " + str(
fulfillmentText.get("deaths")) + "\n" + " Recovered cases : " + str(fulfillmentText.get('recovered')) + \
"\n" + " Active cases : " + str(
fulfillmentText.get("active_cases")) + "\n" + " Fatality Rate : " + str(
fulfillmentText.get("death_ratio")) + \
"\n" + " Recovery Rate : " + str(
fulfillmentText.get("recovery_ratio")) + "\n\n*******END********* \n "
print(webhookresponse)
#log.saveConversations(sessionID, "Cases worldwide", webhookresponse, intent, db)
#log.saveCases("world", fulfillmentText, db)
return {
"fulfillmentMessages": [
{
"text": {
"text": [
webhookresponse
]
}
},
{
"text": {
"text": [
"Do you want me to send the detailed report to your e-mail address? Type.. \n 1. Sure \n 2. Not now "
# "We have sent the detailed report of {} Covid-19 to your given mail address.Do you have any other Query?".format(cust_country)
]
}
}
]
}
elif intent == "covid_searchstate":
fulfillmentText = makeAPIRequest("state")
print(len(fulfillmentText))
webhookresponse1 = ''
webhookresponse2 = ''
webhookresponse3 = ''
for i in range(0,11):
webhookresponse = fulfillmentText[i]
# print(webhookresponse['state'])
# js = json.loads(webhookresponse.text)
# print(str(js.state))
webhookresponse1 += "*********\n" + " State :" + str(webhookresponse['state']) + \
"\n" + " Confirmed cases : " + str(
webhookresponse['confirmed']) + "\n" + " Death cases : " + str(webhookresponse['deaths']) + \
"\n" + " Active cases : " + str(
webhookresponse['active']) + "\n" + " Recovered cases : " + str(
webhookresponse['recovered']) + "\n*********"
for i in range(11, 21):
webhookresponse = fulfillmentText[i]
# print(webhookresponse['state'])
# js = json.loads(webhookresponse.text)
# print(str(js.state))
webhookresponse2 += "*********\n" + " State :" + str(webhookresponse['state']) + \
"\n" + " Confirmed cases : " + str(
webhookresponse['confirmed']) + "\n" + " Death cases : " + str(webhookresponse['deaths']) + \
"\n" + " Active cases : " + str(
webhookresponse['active']) + "\n" + " Recovered cases : " + str(
webhookresponse['recovered']) + "\n*********"
for i in range(21, 38):
webhookresponse = fulfillmentText[i]
print(webhookresponse['state'])
js = json.loads(webhookresponse.text)
# print(str(js.state))
webhookresponse3 += "*********\n" + " State :" + str(webhookresponse['state']) + \
"\n" + " Confirmed cases : " + str(
webhookresponse['confirmed']) + "\n" + " Death cases : " + str(webhookresponse['deaths']) + \
"\n" + " Active cases : " + str(
webhookresponse['active']) + "\n" + " Recovered cases : " + str(
webhookresponse['recovered']) + "\n*********"
print("***World wide Report*** \n\n" + webhookresponse1 + "\n\n*******END********* \n")
print("***World wide Report*** \n\n" + webhookresponse2 + "\n\n*******END********* \n")
print("***World wide Report*** \n\n" + webhookresponse3 + "\n\n*******END********* \n")
log.saveConversations(sessionID, "Indian State Cases", webhookresponse1, intent, db)
return {
"fulfillmentMessages": [
{
"text": {
"text": [
webhookresponse1
]
}
},
{
"text": {
"text": [
webhookresponse2
]
}
},
{
"text": {
"text": [
webhookresponse3
]
}
},
{
"text": {
"text": [
"Do you want me to send the detailed report to your e-mail address? Type.. \n 1. Sure \n 2. Not now "
# "We have sent the detailed report of {} Covid-19 to your given mail address.Do you have any other Query?".format(cust_country)
]
}
}
]
}
else:
return {
"fulfillmentText": "something went wrong,Lets start from the begning, Say Hi",
}
#def configureDataBase():
# client = MongoClient("mongodb+srv://covid:covid>@cluster0.dbozc.mongodb.net/myFirstDatabase?retryWrites=true&w=majority")
# return client.get_database('covid19DB')
def makeAPIRequest(query):
api = MakeApiRequests.Api()
if query == "world":
return api.makeApiWorldwide()
if query == "state":
return api.makeApiRequestForIndianStates()
else:
return api.makeApiRequestForCounrty(query)
def prepareEmail(contact_list):
mailclient = EMailClient.GMailClient()
mailclient.sendEmail(contact_list)
#'''
'''
if __name__ == '__main__':
port = int(os.getenv("PORT"))
print("Starting app on port %d" % port)
app.run(debug=False, port=port, host='0.0.0.0')
'''
if __name__ == "__main__":
app.run(port=8000, debug=True)
#'''
# running the app on the local machine on port 8000