Merge remote-tracking branch 'origin/master' into lash/browser-emulator-ussd
This commit is contained in:
		
						commit
						fd263e7648
					
				
							
								
								
									
										103
									
								
								apps/cic-ussd/emulator/cli/cli.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										103
									
								
								apps/cic-ussd/emulator/cli/cli.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,103 @@
 | 
			
		||||
#!/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
 | 
			
		||||
from urllib import request
 | 
			
		||||
 | 
			
		||||
# third-party imports
 | 
			
		||||
from confini import Config
 | 
			
		||||
 | 
			
		||||
logging.basicConfig(level=logging.WARNING)
 | 
			
		||||
logg = logging.getLogger()
 | 
			
		||||
 | 
			
		||||
config_dir = os.path.join(xdg_config_home, 'cli-ussd')
 | 
			
		||||
 | 
			
		||||
argparser = argparse.ArgumentParser(description='CLI tool to interface a Sempo USSD session')
 | 
			
		||||
argparser.add_argument('-c', type=str, default=config_dir, help='config root to use')
 | 
			
		||||
#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
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
if __name__ == "__main__":
 | 
			
		||||
 | 
			
		||||
    # 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,
 | 
			
		||||
            'serviceCode': config.get('APP_SERVICE_CODE'),
 | 
			
		||||
            'phoneNumber': args.phone,
 | 
			
		||||
            'text': config.get('APP_SERVICE_CODE'),
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    state = "_BEGIN"
 | 
			
		||||
    while state != "END":
 | 
			
		||||
 | 
			
		||||
        if state != "_BEGIN":
 | 
			
		||||
            user_input = input('next> ')
 | 
			
		||||
            data['text'] = user_input
 | 
			
		||||
 | 
			
		||||
        req = urllib.request.Request(url)
 | 
			
		||||
        data_str = json.dumps(data)
 | 
			
		||||
        data_bytes = data_str.encode('utf-8')
 | 
			
		||||
        req.add_header('Content-Type', 'application/json')
 | 
			
		||||
        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)
 | 
			
		||||
							
								
								
									
										2
									
								
								apps/cic-ussd/emulator/cli/config/app.ini
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								apps/cic-ussd/emulator/cli/config/app.ini
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,2 @@
 | 
			
		||||
[app]
 | 
			
		||||
service_code = *483*46#
 | 
			
		||||
							
								
								
									
										4
									
								
								apps/cic-ussd/emulator/cli/config/client.ini
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								apps/cic-ussd/emulator/cli/config/client.ini
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
			
		||||
[client]
 | 
			
		||||
host = 
 | 
			
		||||
port = 
 | 
			
		||||
ssl =
 | 
			
		||||
							
								
								
									
										3
									
								
								apps/cic-ussd/emulator/cli/config/ussd.ini
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								apps/cic-ussd/emulator/cli/config/ussd.ini
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,3 @@
 | 
			
		||||
[ussd]
 | 
			
		||||
user =
 | 
			
		||||
pass =
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user