-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (54 loc) · 2.16 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
from twilio.twiml.messaging_response import MessagingResponse
from flask import Flask, request
import random
import requests
import json
app = Flask(__name__)
@app.route("/")
def about():
abt_msg = ('This is a Whatsapp Bot that extracts the leads from the covid.army api and provide covid resources on whatsapp easily.\n'
'This initiative is taken by Moiz Rajkotwala\n'
'Online Portfolio -- http://moizrajkotwala.netlify.app'
'GitHub -- https://github.com/TechBoyy6')
return abt_msg
@app.route("/msg", methods=['POST'])
def sms_reply():
msg = request.form.get('Body')
para = msg.lower().split(" ")
s = requests.Session()
if len(para) >= 2:
try:
url="https://api.covid.army/api/tweets/{}/{}".format(para[0], "".join(para[1:]))
r = s.get(url)
output = json.loads(r.text)
rnd_index = random.randint(0, len(output))
res_type = output[rnd_index]["resource_type"]
res_num = output[rnd_index]["phone"]
fi_num = "".join(res_num)
res_txt = output[rnd_index]["text"]
user_out = res_type+"\n"+fi_num+"\n"+res_txt
main_msg = MessagingResponse()
main_msg.message(user_out)
return str(main_msg)
except:
err_msg = MessagingResponse()
err_msg.message("Nothing Found :(\nTo imporve the search\nInput correct spelling\nTry different location or resource\n\nOr else type 'Help'")
return str(err_msg)
elif msg.lower() == "1":
link = "https://api.covid.army/api/resources"
r = s.get(link)
res_output = json.loads(r.text)
gvn = '\n'.join(res_output)
res_msg = MessagingResponse()
res_msg.message(gvn)
return str(res_msg)
else:
intro = """Hello, Myself 'Covid Resource Bot'\n
1. To get the list of resources you can search for.\n
Msg 'Your_Location<space>Resource_you_want' to get lead message.
example - mumbai oxygen"""
intro_msg = MessagingResponse()
intro_msg.message(intro)
return str(intro_msg)
if __name__ == "__main__":
app.run()