|
| 1 | +#my code |
| 2 | +from flask import Flask, request |
| 3 | +import threading |
| 4 | +import paho.mqtt.client as mqtt |
| 5 | +import requests |
| 6 | +import json |
| 7 | +from mqtt_client import start_mqtt_subscriber |
| 8 | +import random |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +app = Flask(__name__) |
| 13 | + |
| 14 | +import os |
| 15 | + |
| 16 | +response = "" |
| 17 | +#identity = data_queue.get() |
| 18 | +mqtt_data = {} |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + |
| 23 | +def calculating(amount): |
| 24 | + try: |
| 25 | + price = float(amount) * 1 |
| 26 | + except Exception as e: |
| 27 | + raise Exception("Amount cannot be converted to valid number. Check and try again") |
| 28 | + return price |
| 29 | + |
| 30 | +#///creating the methods of communiction |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | +@app.route('/', methods=['POST', 'GET']) |
| 35 | +def ussd_callback(): |
| 36 | + |
| 37 | + print |
| 38 | + global response |
| 39 | + global mqtt_data |
| 40 | + session_id = request.values.get("sessionId", None)#/////getting the session id |
| 41 | + service_code = request.values.get("serviceCode", None)#//////////getting the service code |
| 42 | + phone_number = request.values.get("phoneNumber", None)#getting the phone number that requested |
| 43 | + text =request.values.get("text", "default")#getting the request |
| 44 | + session_state = text.split('*') |
| 45 | + |
| 46 | + current_level = len(session_state) |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + if current_level == 1: |
| 54 | + response = "CON Hello and welcome team Zuri Bin.We work with you to deliver your domestic waste prompty; when you need it.\n" |
| 55 | + response += "1. Report someone 's trash\n" |
| 56 | + response += "2. Report my own trash" |
| 57 | + |
| 58 | + elif current_level == 2 and session_state[1] == '1': |
| 59 | + response = "CON Hello and thank you for reporting trash. Kindly choose location\n" |
| 60 | + response += "1:Nairobi\n" |
| 61 | + response += "2:We are currently not in other regions" |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + elif current_level ==2 and session_state[1]== '2': |
| 66 | + response = "END Thanks we will try our best to get into your region." |
| 67 | + |
| 68 | + elif current_level ==3 and session_state[2]== '1': |
| 69 | + response = "CON Kindly specify your region\n" |
| 70 | + response += "1:CBD\n" |
| 71 | + response += "2:Pipeline\n" |
| 72 | + response += "3:Kileleshwa\n" |
| 73 | + response += "4:Langata" |
| 74 | + |
| 75 | + elif current_level == 4 and session_state[3] == '1': |
| 76 | + response = "CON How big is your trash\n" |
| 77 | + response += "1:Big\n" |
| 78 | + response += "2:Medium\n" |
| 79 | + response += "3:Small" |
| 80 | + |
| 81 | + elif current_level == 5 and session_state[4] == '1': |
| 82 | + response = 'END Thank you. Your concern is noted.' |
| 83 | + |
| 84 | + elif current_level == 5 and session_state[4] == '2': |
| 85 | + response = 'END Thank you. Your concern is noted.' |
| 86 | + |
| 87 | + elif current_level == 5 and session_state[4] == '3': |
| 88 | + response = 'END Thank you. Your concern is noted.' |
| 89 | + |
| 90 | + elif current_level == 5 and session_state[3] == '2': |
| 91 | + response = 'END Apparently we have not reached this region\n.' |
| 92 | + elif current_level == 5 and session_state[3] == '3': |
| 93 | + response = 'END Apparently we have not reached this region\n.' |
| 94 | + elif current_level == 5 and session_state[3] == '4': |
| 95 | + response = 'END Apparently we have not reached this region\n.' |
| 96 | + |
| 97 | + return response |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +#Receive response from africas talking |
| 102 | +@app.route('/call', methods=['POST']) |
| 103 | +def call_back_client(): |
| 104 | + return '<Response> <Dial phoneNumbers="" maxDuration="5"/></Response>' |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + mqtt_thread = threading.Thread(target=start_mqtt_subscriber) |
| 109 | + |
| 110 | + mqtt_thread.daemon = True |
| 111 | + # Daemonize the thread so it exits when the main thread exits |
| 112 | + mqtt_thread.start() |
| 113 | + app.run(host="0.0.0.0", port=os.environ.get('PORT')) |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
0 commit comments