frugalvox

A tiny VoIP IVR framework by hackers, for hackers
git clone git://git.luxferre.top/frugalvox.git
Log | Files | Refs | README | LICENSE

echobeep.py (2318B)


      1 # An example action script for FrugalVox
      2 # Implements three actions: echo test, caller address readback and parameterized beep
      3 
      4 import os, sys # example of using commonly available Python modules
      5 from pyVoIP.VoIP import CallState # example of using an installation-specific module
      6 from fvx import tts_to_buf, detect_dtmf, audio_buf_len, emptybuf, get_caller_addr, flush_input_audio, playbuf # example of using the FrugalVox kernel module (fvx)
      7 
      8 def run_action(action_id, params, call_obj, user_id, config, clips, calls):
      9     if action_id == '32': # echo test: just enter 32#
     10         playbuf(tts_to_buf('Entering the echo test, press pound to return', config['tts']), call_obj)
     11         flush_input_audio(call_obj)
     12         cache_digit = None # in-band digit cache
     13         while call_obj.state == CallState.ANSWERED: # main event loop
     14             audiobuf = call_obj.read_audio(audio_buf_len, True) # blocking audio buffer read
     15             call_obj.write_audio(audiobuf) # echo the audio
     16             digit = call_obj.get_dtmf() # get a single out-of-band DTMF digit
     17             if digit == '' and audiobuf != emptybuf: # no out-of-band digit, try in-band detection
     18                 ib_digit = detect_dtmf(audiobuf)
     19                 if ib_digit != cache_digit:
     20                     if ib_digit == None: # digit transmission ended
     21                         digit = cache_digit # save the digit
     22                         cache_digit = None  # reset the cache
     23                     else: # digit transmission started
     24                         cache_digit = ib_digit
     25             if digit == '#':
     26                 playbuf(tts_to_buf('Echo test ended', config['tts']), call_obj)
     27                 return
     28             elif digit in clips['dtmf']:
     29                 playbuf(clips['dtmf'][digit], call_obj)
     30     elif action_id == '24': # Caller ID readback: enter 24#
     31         playbuf(tts_to_buf('Your caller ID is %s' % get_caller_addr(call_obj), config['tts']), call_obj) # demonstration of the on-the-fly TTS
     32     else: # beep command: 22*3# tells to beep 3 times
     33         times = 1 # how many times we should beep
     34         if len(params) > 0:
     35             times = int(params[0])
     36         if times > 10: # limit beeps to 10
     37             times = 10
     38         # send the beeps
     39         playbuf((clips['beep']+(emptybuf*10)) * times, call_obj)
     40         return