|
| 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