Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e41b2c3

Browse files
Merge pull request avinashkranjan#672 from dsg1320/feature/covidStats
Feature/covid stats
2 parents 00bde60 + 97f52b5 commit e41b2c3

File tree

2 files changed

+223
-0
lines changed

2 files changed

+223
-0
lines changed

‎Covid India Stats App/README.md‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# COVID19 STATS APP FOR INDIAN STATES
2+
3+
This project uses the following API for getting the stats
4+
[COVID19 STATS](https://api.covid19india.org/data.json)
5+
6+
## Requirments
7+
- Flask
8+
- ngrok: for getting public URL for your local API else deploy in servers like Heroku
9+
- Facebook page
10+
- Facebook developer account: for connecting backend with chatbot app
11+
12+
## How to use this code ?
13+
- Create app in developer portal and connect with facebook page
14+
- Obtain app id
15+
- Generate token for the page you have linked with the app
16+
- Install flask and other requirements and run the app
17+
- Use ngrok like software for creating public URL
18+
- Add public URL as callback URL in developer portal
19+
- Edit subscription of your added page and tick the following
20+
- messages
21+
- messaging_postbacks
22+
- messaging_referrals
23+
- The verification token for this script is "abc".This will be used for verifying the webhook
24+
- Ensure that you have added the generation token in the step 1 in the code
25+
26+
```ACCESS_TOKEN = "random token"```
27+
28+
Now send a message from messenger from your facebook page and get corresponding stats
29+
30+
![messenger-1](https://user-images.githubusercontent.com/73653978/111796544-4a97fa00-88ee-11eb-9e8a-e22031a6d665.PNG)
31+
32+
![messenger-2](https://user-images.githubusercontent.com/73653978/111796445-318f4900-88ee-11eb-9db0-07bc8c765bae.PNG)
33+
34+
![messenger-3](https://user-images.githubusercontent.com/73653978/111796633-5e436080-88ee-11eb-9f2e-f8e257bf43fb.PNG)
35+
36+
## Author
37+
[Devika S G](https://github.com/dsg1320)

‎Covid India Stats App/app.py‎

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import json
2+
from flask import Flask, request
3+
import requests
4+
# Token that has to be generated from webhook page portal
5+
ACCESS_TOKEN = "random token"
6+
# Token that has to be added for verification with developer portal
7+
VERIFICATION_TOKEN = "abc"
8+
# Identifier payloads for initial button
9+
C19INDIA = "C19INDIA"
10+
app = Flask(__name__)
11+
12+
13+
# This get endpoint is for verification with messenger app
14+
@app.route('/webhook', methods=['GET'])
15+
def webhook():
16+
verify_token = request.args.get("hub.verify_token")
17+
if verify_token == VERIFICATION_TOKEN:
18+
return request.args.get("hub.challenge")
19+
return 'Unable to authorise.'
20+
21+
22+
@app.route("/webhook", methods=['POST'])
23+
def webhook_handle():
24+
data = request.get_json()
25+
26+
if data["object"] == "page": # To verify that the request is being originated from a page
27+
28+
for entry in data["entry"]:
29+
for event in entry["messaging"]:
30+
31+
if event.get("message"): # somebody typed a message
32+
process_message(event)
33+
elif event.get("postback"): # user clicked/tapped "postback" button in earlier message
34+
process_postback(event)
35+
return 'ok'
36+
37+
38+
def process_message(event):
39+
sender_id = event["sender"]["id"] # the facebook ID of the person sending you the message
40+
41+
# could receive text or attachment but not both
42+
if "text" in event["message"]:
43+
send_initial_menu(sender_id)
44+
45+
46+
def send_initial_menu(sender_id):
47+
message_data = json.dumps({
48+
"recipient": {
49+
"id": sender_id
50+
},
51+
"message": {
52+
"attachment": {
53+
"type": "template",
54+
"payload": {
55+
"template_type": "generic",
56+
"elements": [{
57+
"title": "Covid India Stats",
58+
"subtitle": "Get the covid19 stats of Indian states",
59+
"buttons": [{
60+
"type": "web_url",
61+
"url": "https://www.worldometers.info/coronavirus/country/india/",
62+
"title": "Open Worldometer India"
63+
}, {
64+
"type": "postback",
65+
"title": "Get Stats By Indian States",
66+
"payload": C19INDIA,
67+
}],
68+
}]
69+
}
70+
}
71+
}
72+
})
73+
74+
call_send_api(message_data)
75+
76+
77+
def send_state_list(sender_id):
78+
message_data = json.dumps({
79+
"recipient": {
80+
"id": sender_id
81+
},
82+
"message": {
83+
"attachment": {
84+
"type": "template",
85+
"payload": {
86+
"template_type": "generic",
87+
"elements": [{
88+
"title": "Select State",
89+
"buttons": create_state_list(1)
90+
}, {
91+
"title": "Select State",
92+
"buttons": create_state_list(2)
93+
}, {
94+
"title": "Select State",
95+
"buttons": create_state_list(3)
96+
}, {
97+
"title": "Select State",
98+
"buttons": create_state_list(4)
99+
}, {
100+
"title": "Select State",
101+
"buttons": create_state_list(5)
102+
}, {
103+
"title": "Select State",
104+
"buttons": create_state_list(6)
105+
}, {
106+
"title": "Select State",
107+
"buttons": create_state_list(7)
108+
}, {
109+
"title": "Select State",
110+
"buttons": create_state_list(8)
111+
}, {
112+
"title": "Select State",
113+
"buttons": create_state_list(9)
114+
}, {
115+
"title": "Select State",
116+
"buttons": create_state_list(10)
117+
}]
118+
}
119+
}
120+
}
121+
})
122+
123+
call_send_api(message_data)
124+
125+
126+
def create_state_list(index):
127+
state_list = ["Maharashtra", "Kerala", "Karnataka", "Andhra Pradesh", "Tamil Nadu", "Delhi", "Uttar Pradesh",
128+
"West Bengal", "Odisha", "Rajasthan", "Chhattisgarh", "Telangana", "Haryana", "Gujarat", "Bihar",
129+
"Madhya Pradesh", "Assam", "Punjab", "Jharkhand", "Uttarakhand", "Himachal Pradesh", "Goa", "Tripura",
130+
"Manipur", "Arunachal Pradesh", "Meghalaya", "Nagaland", "Sikkim", "Mizoram"]
131+
payload_list = []
132+
start_index = 0 + 3 * (index - 1)
133+
end_index = 29 if (start_index + 3) > 29 else (start_index + 3)
134+
for i in range(start_index, end_index):
135+
postback = {}
136+
postback["type"] = "postback"
137+
postback["title"] = state_list[i]
138+
postback["payload"] = state_list[i]
139+
payload_list.append(postback)
140+
return payload_list
141+
142+
143+
def get_stats_send(sender_id, state):
144+
response = json.loads(requests.get("https://api.covid19india.org/data.json").text)
145+
list_state = response['statewise']
146+
for i in list_state:
147+
if i['state'] == state:
148+
x = i
149+
break
150+
message_data = json.dumps({
151+
"recipient": {
152+
"id": sender_id
153+
},
154+
"message": {
155+
"text": "ACTIVE CASES: {}\nCONFIRMED CASES: {}\nDEATHS: {}\nRECOVERED: {}".format(x['active'],
156+
x['confirmed'],
157+
x['deaths'],
158+
x['recovered'])
159+
}
160+
})
161+
call_send_api(message_data)
162+
163+
164+
def process_postback(event):
165+
sender_id = event["sender"]["id"]
166+
payload = event["postback"]["payload"]
167+
168+
if payload == C19INDIA:
169+
send_state_list(sender_id)
170+
else:
171+
get_stats_send(sender_id, payload)
172+
173+
174+
def call_send_api(message_data):
175+
params = {
176+
"access_token": ACCESS_TOKEN
177+
}
178+
headers = {
179+
"Content-Type": "application/json"
180+
}
181+
182+
r = requests.post("https://graph.facebook.com/v5.0/me/messages", params=params, headers=headers, data=message_data)
183+
184+
185+
if __name__ == "__main__":
186+
app.run()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /