Advertisement
Guest User

commands.py

a guest
May 3rd, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 20.22 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. # Initial code for this file derived from eventsocket project - (https://github.com/fiorix/eventsocket),
  3. # which is distributed under the Mozilla Public License Version 1.1
  4.  
  5. """
  6. FreeSWITCH Commands class
  7.  
  8. Please refer to http://wiki.freeswitch.org/wiki/Mod_event_socket#Command_documentation
  9. """
  10.  
  11.  
  12. class Commands(object):
  13.     def api(self, args):
  14.         "Please refer to http://wiki.freeswitch.org/wiki/Event_Socket#api"
  15.         return self._protocol_send("api", args)
  16.  
  17.     def bgapi(self, args):
  18.         "Please refer to http://wiki.freeswitch.org/wiki/Event_Socket#bgapi"
  19.         return self._protocol_send("bgapi", args)
  20.  
  21.     def exit(self):
  22.         "Please refer to http://wiki.freeswitch.org/wiki/Event_Socket#exit"
  23.         return self._protocol_send("exit")
  24.  
  25.     def resume(self):
  26.         """Socket resume for Outbound connection only.
  27.  
  28.        If enabled, the dialplan will resume execution with the next action
  29.  
  30.        after the call to the socket application.
  31.  
  32.        If there is a bridge active when the disconnect happens, it is killed.
  33.        """
  34.         return self._protocol_send("resume")
  35.  
  36.     def eventplain(self, args):
  37.         "Please refer to http://wiki.freeswitch.org/wiki/Event_Socket#event"
  38.         return self._protocol_send('event plain', args)
  39.  
  40.     def eventjson(self, args):
  41.         "Please refer to http://wiki.freeswitch.org/wiki/Event_Socket#event"
  42.         return self._protocol_send('event json', args)
  43.  
  44.     def event(self, args):
  45.         "Please refer to http://wiki.freeswitch.org/wiki/Event_Socket#event"
  46.         return self._protocol_send("event", args)
  47.  
  48.     def execute(self, command, args='', uuid='', lock=True):
  49.         return self._protocol_sendmsg(command, args, uuid, lock)
  50.  
  51.     def get_var(self, var, uuid=""):
  52.         """
  53.        Please refer to http://wiki.freeswitch.org/wiki/Mod_commands#uuid_getvar
  54.  
  55.        For Inbound connection, uuid argument is mandatory.
  56.        """
  57.         if not uuid:
  58.             try:
  59.                 uuid = self.get_channel_unique_id()
  60.             except AttributeError:
  61.                 uuid = None
  62.         if not uuid:
  63.             return None
  64.         api_response = self.api("uuid_getvar %s %s" % (uuid, var))
  65.         result = api_response.get_body().strip()
  66.         if result == '_undef_' or result[:4] == '-ERR':
  67.             result = None
  68.         return result
  69.  
  70.     def set_var(self, var, value, uuid=""):
  71.         """
  72.        Please refer to http://wiki.freeswitch.org/wiki/Mod_commands#uuid_setvar
  73.  
  74.        For Inbound connection, uuid argument is mandatory.
  75.        """
  76.         if not value:
  77.             value = ''
  78.         if not uuid:
  79.             try:
  80.                 uuid = self.get_channel_unique_id()
  81.             except AttributeError:
  82.                 uuid = None
  83.         if not uuid:
  84.             return None
  85.         api_response = self.api("uuid_setvar %s %s %s" % (uuid, var, str(value)))
  86.         result = api_response.get_body()
  87.         if not result == '_undef_' or result[:4] == '-ERR':
  88.             result = ''
  89.         result = result.strip()
  90.         return result
  91.  
  92.     def filter(self, args):
  93.         """Please refer to http://wiki.freeswitch.org/wiki/Event_Socket#filter
  94.  
  95.        The user might pass any number of values to filter an event for. But, from the point
  96.        filter() is used, just the filtered events will come to the app - this is where this
  97.        function differs from event().
  98.  
  99.        >>> filter('Event-Name MYEVENT')
  100.        >>> filter('Unique-ID 4f37c5eb-1937-45c6-b808-6fba2ffadb63')
  101.        """
  102.         return self._protocol_send('filter', args)
  103.  
  104.     def filter_delete(self, args):
  105.         """Please refer to http://wiki.freeswitch.org/wiki/Event_Socket#filter_delete
  106.  
  107.        >>> filter_delete('Event-Name MYEVENT')
  108.        """
  109.         return self._protocol_send('filter delete', args)
  110.  
  111.     def divert_events(self, flag):
  112.         """Please refer to http://wiki.freeswitch.org/wiki/Event_Socket#divert_events
  113.  
  114.        >>> divert_events("off")
  115.        >>> divert_events("on")
  116.        """
  117.         return self._protocol_send('divert_events', flag)
  118.  
  119.     def sendevent(self, args):
  120.         """Please refer to http://wiki.freeswitch.org/wiki/Event_Socket#sendevent
  121.  
  122.        >>> sendevent("CUSTOM\nEvent-Name: CUSTOM\nEvent-Subclass: myevent::test\n")
  123.  
  124.        This example will send event :
  125.          Event-Subclass: myevent%3A%3Atest
  126.          Command: sendevent%20CUSTOM
  127.          Event-Name: CUSTOM
  128.        """
  129.         return self._protocol_send('sendevent', args)
  130.  
  131.     def auth(self, args):
  132.         """Please refer to http://wiki.freeswitch.org/wiki/Event_Socket#auth
  133.  
  134.        This method is only used for Inbound connections.
  135.        """
  136.         return self._protocol_send("auth", args)
  137.  
  138.     def myevents(self, uuid=""):
  139.         """For Inbound connection, please refer to http://wiki.freeswitch.org/wiki/Event_Socket#Special_Case_-_.27myevents.27
  140.  
  141.        For Outbound connection, please refer to http://wiki.freeswitch.org/wiki/Event_Socket_Outbound#Events
  142.  
  143.        >>> myevents()
  144.  
  145.        For Inbound connection, uuid argument is mandatory.
  146.        """
  147.         if self._is_eventjson:
  148.             return self._protocol_send("myevents json", uuid)
  149.         else:
  150.             return self._protocol_send("myevents", uuid)
  151.  
  152.     def linger(self):
  153.         """Tell Freeswitch to wait for the last channel event before ending the connection
  154.  
  155.        Can only be used with Outbound connection.
  156.  
  157.        >>> linger()
  158.  
  159.        """
  160.         return self._protocol_send("linger")
  161.  
  162.     def verbose_events(self, uuid="", lock=True):
  163.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_verbose_events
  164.  
  165.        >>> verbose_events()
  166.  
  167.        For Inbound connection, uuid argument is mandatory.
  168.        """
  169.         return self._protocol_sendmsg("verbose_events", "", uuid, lock)
  170.  
  171.     def answer(self, uuid="", lock=True):
  172.         """
  173.        Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_answer
  174.  
  175.        >>> answer()
  176.  
  177.        For Inbound connection, uuid argument is mandatory.
  178.        """
  179.         return self._protocol_sendmsg("answer", "", uuid, lock)
  180.  
  181.     def bridge(self, args, uuid="", lock=True):
  182.         """
  183.        Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_bridge
  184.  
  185.        >>> bridge("{instant_ringback=false,bridge_early_media=true,ignore_early_media=false}sofia/gateway/myGW/177808")
  186.  
  187.        For Inbound connection, uuid argument is mandatory.
  188.        """
  189.         return self._protocol_sendmsg("bridge", args, uuid, lock)
  190.  
  191.     def hangup(self, cause="", uuid="", lock=True):
  192.         """Hangup call.
  193.  
  194.        Hangup `cause` list : http://wiki.freeswitch.org/wiki/Hangup_Causes (Enumeration column)
  195.  
  196.        >>> hangup()
  197.  
  198.        For Inbound connection, uuid argument is mandatory.
  199.        """
  200.         return self._protocol_sendmsg("hangup", cause, uuid, lock)
  201.  
  202.     def ring_ready(self, uuid="", lock=True):
  203.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_ring_ready
  204.  
  205.        >>> ring_ready()
  206.  
  207.        For Inbound connection, uuid argument is mandatory.
  208.        """
  209.         return self._protocol_sendmsg("ring_ready", "", uuid)
  210.  
  211.     def record_session(self, filename, uuid="", lock=True):
  212.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_record_session
  213.  
  214.        >>> record_session("/tmp/dump.gsm")
  215.  
  216.        For Inbound connection, uuid argument is mandatory.
  217.        """
  218.         return self._protocol_sendmsg("record_session", filename, uuid, lock)
  219.  
  220.     def bind_meta_app(self, args, uuid="", lock=True):
  221.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_bind_meta_app
  222.  
  223.        >>> bind_meta_app("2 ab s record_session::/tmp/dump.gsm")
  224.  
  225.        For Inbound connection, uuid argument is mandatory.
  226.        """
  227.         return self._protocol_sendmsg("bind_meta_app", args, uuid, lock)
  228.  
  229.     def bind_digit_action(self, args, uuid="", lock=True):
  230.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_bind_digit_action
  231.  
  232.        >>> bind_digit_action("test1,456,exec:playback,ivr/ivr-welcome_to_freeswitch.wav")
  233.  
  234.        For Inbound connection, uuid argument is mandatory.
  235.        """
  236.         return self._protocol_sendmsg("bind_digit_action", args, uuid, lock)
  237.  
  238.     def digit_action_set_realm(self, args, uuid="", lock=True):
  239.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_digit_action_set_realm
  240.  
  241.        >>> digit_action_set_realm("test1")
  242.  
  243.        For Inbound connection, uuid argument is mandatory.
  244.        """
  245.         return self._protocol_sendmsg("digit_action_set_realm", args, uuid, lock)
  246.  
  247.     def clear_digit_action(self, args, uuid="", lock=True):
  248.         """
  249.        >>> clear_digit_action("test1")
  250.  
  251.        For Inbound connection, uuid argument is mandatory.
  252.        """
  253.         return self._protocol_sendmsg("clear_digit_action", args, uuid, lock)
  254.  
  255.     def wait_for_silence(self, args, uuid="", lock=True):
  256.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_wait_for_silence
  257.  
  258.        >>> wait_for_silence("200 15 10 5000")
  259.  
  260.        For Inbound connection, uuid argument is mandatory.
  261.        """
  262.         return self._protocol_sendmsg("wait_for_silence", args, uuid, lock)
  263.  
  264.     def sleep(self, milliseconds, uuid="", lock=True):
  265.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_sleep
  266.  
  267.        >>> sleep(5000)
  268.        >>> sleep("5000")
  269.  
  270.        For Inbound connection, uuid argument is mandatory.
  271.        """
  272.         return self._protocol_sendmsg("sleep", milliseconds, uuid, lock)
  273.  
  274.     def vmd(self, args, uuid="", lock=True):
  275.         """Please refer to http://wiki.freeswitch.org/wiki/Mod_vmd
  276.  
  277.        >>> vmd("start")
  278.        >>> vmd("stop")
  279.  
  280.        For Inbound connection, uuid argument is mandatory.
  281.        """
  282.         return self._protocol_sendmsg("vmd", args, uuid, lock)
  283.  
  284.     def set(self, args, uuid="", lock=True):
  285.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_set
  286.  
  287.        >>> set("ringback=${us-ring}")
  288.  
  289.        For Inbound connection, uuid argument is mandatory.
  290.        """
  291.         return self._protocol_sendmsg("set", args, uuid, lock)
  292.  
  293.     def set_global(self, args, uuid="", lock=True):
  294.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_set_global
  295.  
  296.        >>> set_global("global_var=value")
  297.  
  298.        For Inbound connection, uuid argument is mandatory.
  299.        """
  300.         return self._protocol_sendmsg("set_global", args, uuid, lock)
  301.  
  302.     def unset(self, args, uuid="", lock=True):
  303.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_unset
  304.  
  305.        >>> unset("ringback")
  306.  
  307.        For Inbound connection, uuid argument is mandatory.
  308.        """
  309.         return self._protocol_sendmsg("unset", args, uuid, lock)
  310.  
  311.     def start_dtmf(self, uuid="", lock=True):
  312.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_start_dtmf
  313.  
  314.        >>> start_dtmf()
  315.  
  316.        For Inbound connection, uuid argument is mandatory.
  317.        """
  318.         return self._protocol_sendmsg("start_dtmf", "", uuid, lock)
  319.  
  320.     def stop_dtmf(self, uuid="", lock=True):
  321.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_stop_dtmf
  322.  
  323.        >>> stop_dtmf()
  324.  
  325.        For Inbound connection, uuid argument is mandatory.
  326.        """
  327.         return self._protocol_sendmsg("stop_dtmf", "", uuid, lock)
  328.  
  329.     def start_dtmf_generate(self, uuid="", lock=True):
  330.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_start_dtmf_generate
  331.  
  332.        >>> start_dtmf_generate()
  333.  
  334.        For Inbound connection, uuid argument is mandatory.
  335.        """
  336.         return self._protocol_sendmsg("start_dtmf_generate", "true", uuid, lock)
  337.  
  338.     def stop_dtmf_generate(self, uuid="", lock=True):
  339.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_stop_dtmf_generate
  340.  
  341.        >>> stop_dtmf_generate()
  342.  
  343.        For Inbound connection, uuid argument is mandatory.
  344.        """
  345.         return self._protocol_sendmsg("stop_dtmf_generate", "", uuid, lock)
  346.  
  347.     def queue_dtmf(self, args, uuid="", lock=True):
  348.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_queue_dtmf
  349.  
  350.        Enqueue each received dtmf, that'll be sent once the call is bridged.
  351.  
  352.        >>> queue_dtmf("0123456789")
  353.  
  354.        For Inbound connection, uuid argument is mandatory.
  355.        """
  356.         return self._protocol_sendmsg("queue_dtmf", args, uuid, lock)
  357.  
  358.     def flush_dtmf(self, uuid="", lock=True):
  359.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_flush_dtmf
  360.  
  361.        >>> flush_dtmf()
  362.  
  363.        For Inbound connection, uuid argument is mandatory.
  364.        """
  365.         return self._protocol_sendmsg("flush_dtmf", "", uuid, lock)
  366.  
  367.     def play_fsv(self, filename, uuid="", lock=True):
  368.         """Please refer to http://wiki.freeswitch.org/wiki/Mod_fsv
  369.  
  370.        >>> play_fsv("/tmp/video.fsv")
  371.  
  372.        For Inbound connection, uuid argument is mandatory.
  373.        """
  374.         return self._protocol_sendmsg("play_fsv", filename, uuid, lock)
  375.  
  376.     def record_fsv(self, filename, uuid="", lock=True):
  377.         """Please refer to http://wiki.freeswitch.org/wiki/Mod_fsv
  378.  
  379.        >>> record_fsv("/tmp/video.fsv")
  380.  
  381.        For Inbound connection, uuid argument is mandatory.
  382.        """
  383.         return self._protocol_sendmsg("record_fsv", filename, uuid, lock)
  384.  
  385.     def playback(self, filename, terminators=None, uuid="", lock=True, loops=1):
  386.         """Please refer to http://wiki.freeswitch.org/wiki/Mod_playback
  387.  
  388.        The optional argument `terminators` may contain a string with
  389.        the characters that will terminate the playback.
  390.  
  391.        >>> playback("/tmp/dump.gsm", terminators="#8")
  392.  
  393.        In this case, the audio playback is automatically terminated
  394.        by pressing either '#' or '8'.
  395.  
  396.        For Inbound connection, uuid argument is mandatory.
  397.        """
  398.         if not terminators:
  399.             terminators = 'none'
  400.         self.set("playback_terminators=%s" % terminators, uuid)
  401.         return self._protocol_sendmsg("playback", filename, uuid, lock, loops)
  402.  
  403.     def transfer(self, args, uuid="", lock=True):
  404.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_transfer
  405.  
  406.        >>> transfer("3222 XML default")
  407.  
  408.        For Inbound connection, uuid argument is mandatory.
  409.        """
  410.         return self._protocol_sendmsg("transfer", args, uuid, lock)
  411.  
  412.     def att_xfer(self, url, uuid="", lock=True):
  413.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_att_xfer
  414.  
  415.        >>> att_xfer("user/1001")
  416.  
  417.        For Inbound connection, uuid argument is mandatory.
  418.        """
  419.         return self._protocol_sendmsg("att_xfer", url, uuid, lock)
  420.  
  421.     def endless_playback(self, filename, uuid="", lock=True):
  422.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_endless_playback
  423.  
  424.        >>> endless_playback("/tmp/dump.gsm")
  425.  
  426.        For Inbound connection, uuid argument is mandatory.
  427.        """
  428.         return self._protocol_sendmsg("endless_playback", filename, uuid, lock)
  429.  
  430.     def record(self, filename, time_limit_secs="", silence_thresh="", \
  431.                 silence_hits="", terminators=None, uuid="", lock=True, loops=1):
  432.         """
  433.        Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_record
  434.  
  435.        """
  436.         if terminators:
  437.             self.set("playback_terminators=%s" % terminators)
  438.         args = "%s %s %s %s" %(filename, time_limit_secs, silence_thresh, silence_hits)
  439.         self._protocol_sendmsg("record", args=args, uuid=uuid, lock=True)
  440.  
  441.     def play_and_get_digits(self, min_digits=1, max_digits=1, max_tries=1, timeout=5000, \
  442.                             terminators='', sound_files=[], invalid_file = "", var_name='pagd_input', \
  443.                             valid_digits='0123456789*#', digit_timeout=None, play_beep=False):
  444.         """
  445.        Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_play_and_get_digits
  446.        """
  447.         if not sound_files:
  448.             if play_beep:
  449.                 play_str = 'tone_stream://%(300,200,700)'
  450.             else:
  451.                 play_str = 'silence_stream://10'
  452.         else:
  453.             self.set("playback_delimiter=!")
  454.             play_str = "file_string://silence_stream://1"
  455.             for sound_file in sound_files:
  456.                 play_str = "%s!%s" % (play_str, sound_file)
  457.             if play_beep:
  458.                 beep = 'tone_stream://%(300,200,700)'
  459.                 play_str = "%s!%s" % (play_str, beep)
  460.  
  461.         if not invalid_file:
  462.             invalid_file='silence_stream://150'
  463.         if digit_timeout is None:
  464.             digit_timeout = timeout
  465.         reg = []
  466.         for d in valid_digits:
  467.             if d == '*':
  468.                 d = '\*'
  469.             reg.append(d)
  470.         regexp = '|'.join(reg)
  471.         regexp = '^(%s)+' % regexp
  472.  
  473.         play_str = play_str.replace("'", "\\'")
  474.  
  475.         args = "%d %d %d %d '%s' '%s' %s %s %s %d" % (min_digits, max_digits, max_tries, \
  476.                                                     timeout, terminators, play_str,
  477.                                                     invalid_file, var_name, regexp,
  478.                                                     digit_timeout)
  479.         self.execute('play_and_get_digits', args)
  480.  
  481.     def preanswer(self):
  482.         """
  483.        Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_pre_answer
  484.  
  485.        Can only be used for outbound connection
  486.        """
  487.         self.execute("pre_answer")
  488.  
  489.     def conference(self, args, uuid="", lock=True):
  490.         """Please refer to http://wiki.freeswitch.org/wiki/Mod_conference
  491.  
  492.        >>> conference(args)
  493.  
  494.        For Inbound connection, uuid argument is mandatory.
  495.        """
  496.         return self._protocol_sendmsg("conference", args, uuid, lock)
  497.  
  498.     def speak(self, text, uuid="", lock=True, loops=1):
  499.         """Please refer to http://wiki.freeswitch.org/wiki/TTS
  500.  
  501.        >>> "set" data="tts_engine=flite"
  502.        >>> "set" data="tts_voice=kal"
  503.        >>> speak(text)
  504.  
  505.        For Inbound connection, uuid argument is mandatory.
  506.        """
  507.         return self._protocol_sendmsg("speak", text, uuid, lock, loops)
  508.  
  509.     def hupall(self, args):
  510.         "Please refer to http://wiki.freeswitch.org/wiki/Mod_commands#hupall"
  511.         return self._protocol_sendmsg("hupall", args, uuid='', lock=True)
  512.  
  513.     def say(self, args, uuid="", lock=True, loops=1):
  514.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_say
  515.  
  516.        >>> say(en number pronounced 12345)
  517.  
  518.        For Inbound connection, uuid argument is mandatory.
  519.        """
  520.         return self._protocol_sendmsg("say", args, uuid, lock)
  521.  
  522.     def sched_hangup(self, args, uuid="", lock=True):
  523.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_sched_hangup
  524.  
  525.        >>> sched_hangup("+60 ALLOTTED_TIMEOUT")
  526.  
  527.        For Inbound connection, uuid argument is mandatory.
  528.        """
  529.         return self._protocol_sendmsg("sched_hangup", args, uuid, lock)
  530.  
  531.     def sched_transfer(self, args, uuid="", lock=True):
  532.         """Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_sched_transfer
  533.  
  534.        >>> sched_transfer("+60 9999 XML default")
  535.  
  536.        For Inbound connection, uuid argument is mandatory.
  537.        """
  538.         return self._protocol_sendmsg("sched_transfer", args, uuid, lock)
  539.  
  540.     def redirect(self, args, uuid="", lock=True):
  541.         """
  542.        Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_redirect
  543.  
  544.        >>> redirect("sip:foo@bar.com")
  545.  
  546.        For Inbound connection, uuid argument is mandatory.
  547.        """
  548.         return self._protocol_sendmsg("redirect", args, uuid, lock)
  549.  
  550.     def deflect(self, args, uuid="", lock=True):
  551.         """
  552.        Please refer to http://wiki.freeswitch.org/wiki/Misc._Dialplan_Tools_deflect
  553.  
  554.        >>> deflect("sip:foo@bar.com")
  555.  
  556.        For Inbound connection, uuid argument is mandatory.
  557.        """
  558.         return self._protocol_sendmsg("deflect", args, uuid, lock)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement