huawei-modem-python-api-client
huawei-modem-python-api-client copied to clipboard
How do you use ussd
I'm just getting a response OK, I'm not able to display the ussd response
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??
not yet
Call ussd.get frequeltly to get the response. Ok mean the request has been sent to the operator.
@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.
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())