cic-internal-integration/apps/cic-ussd/cic_ussd/runnable/client.py

107 lines
3.1 KiB
Python
Raw Normal View History

2021-03-06 22:25:57 +01:00
#!/usr/bin/python3
# Author: Louis Holbrook <dev@holbrook.no> (https://holbrook.no)
# Description: interactive console for Sempo USSD session
# SPDX-License-Identifier: GPL-3.0-or-later
# standard imports
import os
import sys
import uuid
import json
import argparse
import logging
import urllib
from xdg.BaseDirectory import xdg_config_home
2021-05-11 12:58:00 +02:00
from urllib import parse, request
2021-03-06 22:25:57 +01:00
# third-party imports
from confini import Config
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
default_config_dir = os.environ.get('CONFINI_DIR', '/usr/local/etc/cic')
2021-03-06 22:25:57 +01:00
argparser = argparse.ArgumentParser(description='CLI tool to interface a Sempo USSD session')
argparser.add_argument('-c', type=str, default=default_config_dir, help='config root to use')
2021-03-06 22:25:57 +01:00
#argparser.add_argument('-d', type=str, default='local', help='deployment name to interface (config root subdirectory)')
argparser.add_argument('--host', type=str, default='localhost')
argparser.add_argument('--port', type=int, default=9000)
argparser.add_argument('--nossl', help='do not use ssl (careful)', action='store_true')
argparser.add_argument('phone', help='phone number for USSD session')
argparser.add_argument('-v', help='be verbose', action='store_true')
argparser.add_argument('-vv', help='be more verbose', action='store_true')
args = argparser.parse_args(sys.argv[1:])
if args.v == True:
logging.getLogger().setLevel(logging.INFO)
elif args.vv == True:
logging.getLogger().setLevel(logging.DEBUG)
#config_dir = os.path.join(args.c, args.d)
config_dir = os.path.join(args.c)
os.makedirs(config_dir, 0o777, True)
config = Config(config_dir)
config.process()
logg.debug('config loaded from {}'.format(config_dir))
host = config.get('CLIENT_HOST')
port = config.get('CLIENT_PORT')
ssl = config.get('CLIENT_SSL')
if host == None:
host = args.host
if port == None:
port = args.port
if ssl == None:
ssl = not args.nossl
elif ssl == 0:
ssl = False
else:
ssl = True
def main():
2021-03-06 22:25:57 +01:00
# TODO: improve url building
url = 'http'
if ssl:
url += 's'
url += '://{}:{}'.format(host, port)
url += '/?username={}&password={}'.format(config.get('USSD_USER'), config.get('USSD_PASS'))
logg.info('service url {}'.format(url))
logg.info('phone {}'.format(args.phone))
session = uuid.uuid4().hex
data = {
'sessionId': session,
2021-11-02 15:00:36 +01:00
'serviceCode': config.get('USSD_SERVICE_CODE'),
2021-03-06 22:25:57 +01:00
'phoneNumber': args.phone,
2021-09-17 13:15:43 +02:00
'text': "",
2021-03-06 22:25:57 +01:00
}
state = "_BEGIN"
while state != "END":
if state != "_BEGIN":
user_input = input('next> ')
data['text'] = user_input
req = urllib.request.Request(url)
2021-05-11 12:58:00 +02:00
urlencoded_data = parse.urlencode(data)
data_bytes = urlencoded_data.encode('utf-8')
req.add_header('Content-Type', 'application/x-www-form-urlencoded')
2021-03-06 22:25:57 +01:00
req.data = data_bytes
response = urllib.request.urlopen(req)
response_data = response.read().decode('utf-8')
state = response_data[:3]
out = response_data[4:]
print(out)
if __name__ == "__main__":
main()