Advertisement
tugapt

fastagi_server.py

Apr 15th, 2019
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. import re
  2. import threading
  3. import time
  4. import pystrix
  5.  
  6. # https://wiki.asterisk.org/wiki/display/AST/AGICommand_stream+file
  7.  
  8. class FastAGIServer(threading.Thread):
  9.     """
  10.     A simple thread that runs a FastAGI server forever.
  11.     """
  12.     _fagi_server = None #The FastAGI server controlled by this thread
  13.  
  14.     def __init__(self):
  15.         threading.Thread.__init__(self)
  16.         self.daemon = True
  17.         self._fagi_server = pystrix.agi.FastAGIServer()
  18.         self._fagi_server.register_script_handler(re.compile('demo2'), self._demo2_handler)
  19.         self._fagi_server.register_script_handler(re.compile('demo'), self._demo_handler)
  20.         self._fagi_server.register_script_handler(None, self._noop_handler)
  21.  
  22.     def _demo2_handler(self, agi, args, kwargs, match, path):
  23.         print("HEY", agi, args, kwargs, match, path)
  24.         agi.execute(pystrix.agi.core.Answer()) #Answer the call
  25.         agi.execute(pystrix.agi.core.SetVariable("ok", "AGI Ricks")) #Answer the call
  26.         response = agi.execute(pystrix.agi.core.StreamFile('agi_demo/i_just_called', escape_digits=('1', '2'))) #Play a file; allow DTMF '1' or '2' to interrupt
  27.         if response: #Playback was interrupted; if you don't care, you don't need to catch this
  28.             (dtmf_character, offset) = response #The key pressed by the user and the playback time
  29.             print(dtmf_character, offset)
  30.  
  31.  
  32.     def _demo_handler(self, agi, args, kwargs, match, path):
  33.  
  34.         """
  35.         `agi` is the AGI instance used to process events related to the channel, `args` is a
  36.         collection of positional arguments provided with the script as a tuple, `kwargs` is a
  37.         dictionary of keyword arguments supplied with the script (values are enumerated in a list),
  38.         `match` is the regex match object (None if the fallback handler), and `path` is the string
  39.         path supplied by Asterisk, in case special processing is needed.
  40.  
  41.         The directives issued in this function can all raise Hangup exceptions, which should be
  42.         caught if doing anything complex, but an uncaught exception will simply cause a warning to
  43.         be raised, making AGI scripts very easy to write.
  44.         """
  45.         agi.execute(pystrix.agi.core.Answer()) #Answer the call
  46.  
  47.         response = agi.execute(pystrix.agi.core.StreamFile('demo-thanks', escape_digits=('1', '2'))) #Play a file; allow DTMF '1' or '2' to interrupt
  48.         if response: #Playback was interrupted; if you don't care, you don't need to catch this
  49.             (dtmf_character, offset) = response #The key pressed by the user and the playback time
  50.             print(dtmf_character, offset)
  51.  
  52.         agi.execute(pystrix.agi.core.Hangup()) #Hang up the call
  53.  
  54.     def _noop_handler(self, agi, args, kwargs, match, path):
  55.         """
  56.         Does nothing, causing control to return to Asterisk's dialplan immediately; provided just
  57.         to demonstrate the fallback handler.
  58.         """
  59.  
  60.     def kill(self):
  61.         self._fagi_server.shutdown()
  62.  
  63.     def run(self):
  64.         self._fagi_server.serve_forever()
  65.  
  66.  
  67.  
  68. if __name__ == '__main__':
  69.     fastagi_core = FastAGIServer()
  70.     fastagi_core.start()
  71.  
  72.  
  73.     while fastagi_core.is_alive():
  74.         #In a larger application, you'd probably do something useful in another non-daemon
  75.         #thread or maybe run a parallel AMI server
  76.         time.sleep(1)
  77.     fastagi_core.kill()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement