def stasis_start_cb(channel_obj, ev):
“”“Handler for StasisStart”“”
channel = channel_obj.get(‘channel’)
channel_name = channel.json.get(‘name’)
args = ev.get(‘args’)
if not args:
print "Error: {} didn't provide any arguments!".format(channel_name)
return
if args and args[0] != 'inbound':
# Only handle inbound channels here
return
if len(args) != 2:
print "Error: {} didn't tell us who to dial".format(channel_name)
channel.hangup()
return
user_id = channel.json.get('caller').get('number')
destination = args[1]
# Check user credit
credit_balance = check_user_credit(user_id)
if credit_balance is None or credit_balance < 0.5:
print "Insufficient credit for user {}".format(user_id)
channel.hangup()
return
print "{} entered our application".format(channel_name)
channel.ring()
try:
print "Dialing {}".format(destination)
outgoing = client.channels.originate(endpoint=destination, app='billing-app', appArgs='dialed',timeout= '30')
except requests.HTTPError:
print "Whoops, pretty sure %s wasn't valid" % destination
channel.hangup()
return
def outgoing_start_cb(channel_obj, ev):
"""StasisStart handler for our dialed channel"""
print "{} answered; bridging with {}".format(outgoing.json.get('name'), channel.json.get('name'))
channel.answer()
bridge = client.bridges.create(type='mixing')
bridge.addChannel(channel=[channel.id, outgoing.id])
#Clean up the bridge when done
channel.on_event('StasisEnd', lambda *args: safe_bridge_destroy(bridge))
outgoing.on_event('StasisEnd', lambda *args: safe_bridge_destroy(bridge))
# start_time = time.time()
# print "start time {}".format(start_time)
#print "channel state {} and {}".format(outgoing.json.get('state'), channel.json.get('state'))
def state_change(channel_obj,ev):
if channel.json.get('state') == 'Up' and outgoing.json.get('state') == 'Up':
print "channel state {} and {}".format(outgoing.json.get('state'), channel.json.get('state'))
start_time = time.time()
elapsed_time = time.time() - start_time
deducted_amount = elapsed_time * 0.50
deduct_credit(user_id, deducted_amount)
print "Deducted ${} from user {}".format(deducted_amount, user_id)
# Check for insufficient balance
credit_balance = check_user_credit(user_id)
if credit_balance is not None and credit_balance < 0.50:
print "Insufficient balance for user {}".format(user_id)
insufficient_balance(channel)
time.sleep(1)
outgoing.on_event('ChannelStateChange', state_change)
outgoing.on_event('StasisStart', outgoing_start_cb)
channel.on_event('StasisEnd', lambda *args: safe_hangup(outgoing))
outgoing.on_event('StasisEnd', lambda *args: safe_hangup(channel))
client.on_channel_event(‘StasisStart’, stasis_start_cb)
I am trying to get the state of the channels after the call has been bridged, hiwever the values im getting are down and ringing. Any help on how to get value when the state changes to “Up”