-
Notifications
You must be signed in to change notification settings - Fork 122
/
webApp.py
executable file
·164 lines (118 loc) · 4.54 KB
/
webApp.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
import os
from flask import Flask, request, render_template
from flask_restful import Resource, Api
import africastalking
app = Flask(__name__)
api = Api(app)
username = os.getenv("user_name", "sandbox")
api_key = os.getenv("api_key", "fake")
africastalking.initialize(username, api_key)
sms = africastalking.SMS
airtime = africastalking.Airtime
payment = africastalking.Payment
@app.route("/")
def index():
return render_template("index.html")
class send_sms(Resource):
def get(self):
return {"hello": "world"}
def post(self):
number = str(request.form["number"])
return sms.send("Test message", [number])
api.add_resource(send_sms, "/sms")
class send_airtime(Resource):
def get(self):
return {"hello": "world"}
def post(self):
number = str(request.form["number"])
full_amount = str(request.form["amount"])
currency_code = full_amount[0:3]
amount = full_amount[4:]
return airtime.send(number, amount, currency_code)
api.add_resource(send_airtime, "/airtime")
class mobile_checkout(Resource):
def get(self):
return {"hello": "world"}
def post(self):
number = str(request.form["number"])
product_name = str(request.form["product_name"])
full_amount = str(request.form["amount"])
currency_code = full_amount[0:3]
amount = full_amount[4:]
return payment.mobile_checkout(product_name, number, currency_code, amount)
api.add_resource(mobile_checkout, "/mobile_checkout")
class mobile_b2c(Resource):
def get(self):
return {"hello": "world"}
def post(self):
number = str(request.form["number"])
product_name = str(request.form["product_name"])
full_amount = str(request.form["amount"])
currency_code = full_amount[0:3]
amount = full_amount[4:]
name = str(request.form["name"])
recipients = [
{
"name": name,
"phoneNumber": number,
"currencyCode": currency_code,
"amount": amount,
"metadata": {},
}
]
return payment.mobile_b2c(product_name, recipients)
api.add_resource(mobile_b2c, "/mobile_b2c")
@app.route("/ussd", methods=["GET", "POST"])
def ussd():
# session_id = request.values.get("sessionId", None)
# service_code = request.values.get("serviceCode", None)
phone_number = request.values.get("phoneNumber", None)
text = request.values.get("text", "default")
if text == "":
response = "CON What would you want to check \n"
response += "1. My Account \n"
response += "2. My phone number"
elif text == "1":
response = "CON Choose account information you want to view \n"
response += "1. Account number \n"
response += "2. Account balance"
elif text == "2":
response = "END Your phone number is " + phone_number
elif text == "1*1":
accountNumber = "ACC1001"
response = "END Your account number is " + accountNumber
elif text == "1*2":
balance = "KES 10,000"
response = "END Your balance is " + balance
else:
response = "END Invalid choice"
return response
@app.route("/voice", methods=["GET", "POST"])
def voice():
# session_id = request.values.get("sessionId", None)
# is_active = request.values.get("isActive", None)
# phone_number = request.values.get("callerNumber", None)
response = '<Response> <GetDigits timeout="30" finishOnKey="#">'
response += '<Say voice="man" playBeep="false">Please enter your account '
response += "number followed by the hash sign</Say> </GetDigits> </Response>"
dtmfDigits = request.values.get("dtmfDigits", None)
if dtmfDigits == "1234":
response = '<Response> <GetDigits timeout="30" finishOnKey="#">'
response += ' <Say voice="man" playBeep="false"> Press 1 followed by a hash '
response += "sign to get your account balance or 0 followed by a hash sign to"
response += " quit</Say> </GetDigits></Response>"
elif dtmfDigits == "1":
response = "<Response>"
response += (
'<Say voice="man" playBeep="false" >Your balance is 1234 Shillings</Say>'
)
response += "<Reject/> </Response>"
elif dtmfDigits == "0":
response = "<Response>"
response += (
'<Say voice="man" playBeep="false" >Its been a pleasure, good bye </Say>'
)
response += "<Reject/> </Response>"
return response
if __name__ == "__main__":
app.run(debug=True)