Search code examples
pythonasteriskagi

Python script for showing an asterisk while waiting 10 seconds


I'm trying to place a call 'on hold' in asterisk using a python AGI script, the function will check if the person is available, when he is asterisk will dial the person if he is not the script should wait 10 seconds before checking availabilty again and dial when the person is available. However I ran into a small problem, using the time.sleep(10) function hangs up the call when a person is not available, I expect this is because the thread the script runs on will sleep and asterisk thinks the script is done running and hangs up the call. Removing the time.sleep() gives me what I want without the time interval.

agi = AGI()
agi.verbose("python agi started")
place_call_on_hold("SIP/6001")

def place_call_on_hold(s):
  agi.verbose("entered place_call_on_hold function")
  while True:
    status = agi.get_variable('DEVICE_STATE(%(redirect)s)'%{'redirect':s})
    agi.verbose(status)
    if status == 'NOT_INUSE':
      agi.verbose("Info: place_call_on_hold: calling the number")
      agi.appexec("Dial",s)
    else:
      agi.verbose("Info: place_call_on_hold: sleeping for 10 sec")
      time.sleep(10)

Is there a way to wait 10 seconds without using the sleep() function or how can I make sure the call won't end before the time.sleep wakes back up?


Solution

  • I fixed my problem by just calling the Asterisk Wait(10) function instead of time.sleep(10).