-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphone
executable file
·99 lines (74 loc) · 2.79 KB
/
phone
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
#!/usr/bin/env python3
from requests.exceptions import HTTPError
import argparse
import requests
import uuid
import sys
import os
def ussd(url, body):
try:
return requests.post(url, json=body, headers={'Accept': 'application/json'})
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 validate(response):
content = response.json();
if response.status_code != 200:
message = content['message']
print(f'\nError ({response.status_code}): {message}')
sys.exit(0);
if content['flow'] == 'break':
data = content['data']
print(f'\n{data}')
sys.exit(0);
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="*721#", metavar='')
parser.add_argument('--service', help="USSD shortcode", type=str, default="*721#", 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", 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 = clean(args.service)
dail = service if args.dail == '' else clean(args.dail)
# answer = clean(dail.replace(service, ''))
body = {
'session_id': session_id,
'network_code': args.network,
'phone_number': args.phone,
'input': dail,
'service_code': service,
'new_session': 'yes',
# 'answer': answer
}
response = ussd(args.api, body)
validate(response)
dail = input(response.json()['data'] + "\n")
# ..................................
while True:
# dail += "*" + answer
body['input'] = dail
# body['answer'] = answer
body['new_session'] = 'no'
response = ussd(args.api, body)
validate(response)
dail = input("\n" + response.json()['data'] + "\n")
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Exiting...')
try:
sys.exit(0)
except SystemExit:
os._exit(0)