-
Notifications
You must be signed in to change notification settings - Fork 0
/
africastalking
executable file
·92 lines (69 loc) · 2.58 KB
/
africastalking
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
#!/usr/bin/env python3
from requests.exceptions import HTTPError
import argparse
import requests
import uuid
import sys
import os
def ussd(url, data):
try:
return requests.post(url, data, headers={'Accept': 'text/plain'})
except HTTPError as http_err:
print(http_err)
sys.exit(0);
except Exception as ex:
print(ex)
sys.exit(0);
def clean(ussdCode):
return ussdCode.lstrip('*').rstrip('#')
def stop(responseText):
cmd = responseText[0:4]
return cmd == 'END '
def main():
session_id = str(uuid.uuid4());
parser = argparse.ArgumentParser(add_help=False, formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('phone', action="store", help="MSISDN")
parser.add_argument('--dail', help="User request", type=str, default="*308#", metavar='')
parser.add_argument('--service', help="USSD shortcode", type=str, default="*308#", metavar='')
parser.add_argument('--network', help="Network code", type=str, default="6001", metavar='')
parser.add_argument('--session', help="Session ID *Auto Generated", type=str, default="", metavar='')
parser.add_argument('--api', help="API URI", type=str, default="http://localhost:8000/api/ussd/africastalking", metavar='')
parser.add_argument('-v', '--version', action='version', version='%(prog)s v1.0 by bmatovu', help="Show program's info")
parser.add_argument('-h', '--help', action='help', default=argparse.SUPPRESS, help='Show usage')
args = parser.parse_args()
# ........................................................
service_code = clean(args.service)
dail = clean(args.dail)
answers = clean(dail.replace(service_code, ''))
data = {
'sessionId': session_id,
'networkCode': args.network,
'phoneNumber': args.phone,
'input': dail,
'serviceCode': service_code,
'text': answers
}
response = ussd(args.api, data)
if stop(response.text):
print("\n" + response.text[4:])
sys.exit(0);
answer = input(response.text[4:] + "\n")
# ..................................
while True:
dail += "*" + answer
data['input'] = dail
data['text'] = answer
response = ussd(args.api, data)
if stop(response.text):
print("\n" + response.text[4:])
break;
answer = input("\n" + response.text[4:] + "\n")
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Exiting...')
try:
sys.exit(0)
except SystemExit:
os._exit(0)