huawei-lte-api
huawei-lte-api copied to clipboard
ussd request example
Would appreciate it if someone is able to give an example of running a ussd request to know balance amount. Thanks!
+1
https://github.com/Salamek/huawei-lte-api/blob/af6c9e05ca3c0d7c40133994fb8af735e1525ce0/huawei_lte_api/api/Ussd.py#L13
Not sure if it works or not...
got it working with my test script (sharing here if it helps anyone), thanks
#!/usr/bin/env python3
import pprint
from typing import Any, Callable
from time import sleep
from huawei_lte_api.Client import Client
from huawei_lte_api.Connection import Connection
def dump(method: Callable[[], Any]) -> None:
print("==== %s" % method.__qualname__)
try:
pprint.pprint(method())
except Exception as e:
print(str(e))
print("")
def getussd(client: Client, timeout: int = 60) -> None:
for _ in range(timeout):
try:
ussd_response = client.ussd.get()
# print(ussd_response['content'])
return ussd_response['content']
except:
ussd_status = client.ussd.status()
# print(ussd_status)
# return
sleep(1)
print("USSD response not received within the specified timeout.")
with Connection("http://192.168.8.1", "admin", "pasasword") as connection:
client = Client(connection)
# Send USSD code
ussd_response = client.ussd.send("*136#")
# Check the USSD status to see if sent successfully
ussd_status = client.ussd.status()
if 'result' in ussd_status and ussd_status['result'] == '1':
# Poll for USSD response
result = getussd(client)
print(result)
if "You have entered an invalid selection." in result:
print("We're in the wrong menu, try go back..")
ussd_response = client.ussd.send("99")
result = getussd(client)
print(result)
elif "10. Detailed balance" in result:
print("Request get detailed balance..")
ussd_response = client.ussd.send("10")
result = getussd(client)
print(result)
if "Balance Type:" in result and "2. Data" in result:
print("Request get detailed balance..")
ussd_response = client.ussd.send("2")
result = getussd(client)
print(result)
else:
print("USSD request failed to send.")
Thanks for the code! I will try it out and report back any problems. Thanks.
@edyirdaw did it work for you? I have B535-333 and I'd like to use USSD to get info about account balance and data transfer (gigabytes I have available).
@Misiu I was a bit busy and couldnt try it out extensively. I am currently stuck at whether the device is currently connected/operating in the correct mode to accept the python commands it is being sent.
I have been working on similar thing today maybe you want to check this https://gist.github.com/mmubarak0/2ab9bf76143955025683b9398d70c4f3
@Salamek Can I work on creating a pull request that includes an example for sending a USSD code?
@mmubarak0 sure, put it in examples folder ;-)
this is completed as of #218