huawei-lte-api icon indicating copy to clipboard operation
huawei-lte-api copied to clipboard

ussd request example

Open edyirdaw opened this issue 1 year ago • 9 comments

Would appreciate it if someone is able to give an example of running a ussd request to know balance amount. Thanks!

edyirdaw avatar Dec 18 '23 07:12 edyirdaw

+1

fragtion avatar Jan 30 '24 08:01 fragtion

https://github.com/Salamek/huawei-lte-api/blob/af6c9e05ca3c0d7c40133994fb8af735e1525ce0/huawei_lte_api/api/Ussd.py#L13

Not sure if it works or not...

Salamek avatar Jan 30 '24 09:01 Salamek

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.")

fragtion avatar Jan 30 '24 12:01 fragtion

Thanks for the code! I will try it out and report back any problems. Thanks.

edyirdaw avatar Feb 05 '24 15:02 edyirdaw

@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 avatar Mar 11 '24 21:03 Misiu

@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.

edyirdaw avatar Mar 23 '24 15:03 edyirdaw

I have been working on similar thing today maybe you want to check this https://gist.github.com/mmubarak0/2ab9bf76143955025683b9398d70c4f3

mmubarak0 avatar Jun 26 '24 15:06 mmubarak0

@Salamek Can I work on creating a pull request that includes an example for sending a USSD code?

mmubarak0 avatar Jul 03 '24 16:07 mmubarak0

@mmubarak0 sure, put it in examples folder ;-)

Salamek avatar Jul 03 '24 19:07 Salamek

this is completed as of #218

Salamek avatar Aug 22 '24 00:08 Salamek