huawei-modem-python-api-client icon indicating copy to clipboard operation
huawei-modem-python-api-client copied to clipboard

How do you use ussd

Open donmbelembe opened this issue 5 years ago • 5 comments

I'm just getting a response OK, I'm not able to display the ussd response

donmbelembe avatar Oct 22 '19 08:10 donmbelembe

I'm just getting a response OK, I'm not able to display the ussd response

Hey donmbelembe have you finally seen how to send and receive ussd messages??

MrSigne avatar Oct 24 '19 16:10 MrSigne

not yet

donmbelembe avatar Dec 23 '19 08:12 donmbelembe

Call ussd.get frequeltly to get the response. Ok mean the request has been sent to the operator.

mboahost avatar Feb 11 '20 11:02 mboahost

@mboahost is right. These operations are ASYNC by nature. So, after you send, you need to "wait" for the response which might arrive a couple of seconds after this.

pablo avatar Feb 11 '20 11:02 pablo

As an example

Assembled "on the knees" but it may be useful:

My modem Huawei E5372 For my operator "MTS" Russia. We have to check whether the ussd message was sent. And also check the answers in a loop. Sometimes the answer is not returned at all.

import huaweisms.api.user as usermod
import huaweisms.api.ussd as ussd
import time


def get_balance(ussd_code='#100#', sleep_time=3):
    ctx = usermod.quick_login("admin", "admin", modem_host='192.168.8.1')
    try:
        u = ussd.send(ctx, ussd_code)
        if u.get("response") == 'OK':
            # print(u.get("response"))
            st = ussd.status(ctx)
            if st.get('response').get('result') == '1':
                # print(st.get('response').get('result'))
                for _ in range(10):
                    g = ussd.get(ctx)
                    # print(g)
                    if g.get('type') == 'response':
                        # print(g.get('response').get('content'))
                        return g.get('response').get('content')
                    time.sleep(sleep_time)
                else:
                    print('No response from operator')
        return False
    except Exception:
        print('Exception error')
        return False


print(get_balance())

Wegazz avatar Oct 08 '20 16:10 Wegazz