pyswitch package¶
Twisted Protocols for communication with FreeSWITCH
PySWITCH allows you to communicate with FreeSWITCH using inbound and outbound EventSocket connections.
The protocols are designed to be included in applications that want to allow for multi-protocol communication using the Twisted protocol. Their integration with FreeSWITCH does not require any modification to the FreeSWITCH source code (though an eventsocket account is obviously required for the Inbound connections, and you have to actually call the Outbound server from the dialplan).
pyswitch.fsprotocol.FSProtocol class¶
pyswitch.inbound.InboundProtocol
and pyswitch.outbound.OutboundProtocol
inherit from FSProtocol all the methods defined in here are available on both those classes.
All the following methods return Deferred object
which will fire when the result is available from FreeSWITCH
-
class
pyswitch.fsprotocol.
FSProtocol
¶ FreeSWITCH EventSocket protocol implementation.
All the FreeSWITCH api and dptool commands are defined in this class
-
answer
(uuid='', lock=True)¶ Answer cahnnel
uuid – (str) uuid of target channel lock – (bool) lock the channel until execution is finished
-
apiAvmd
(uuid, start=True, background=False)¶ Execute avmd on provided channel. uuid (str) – uuid of the target channel start (bool) – If True avmd will start if false avmd will be stopped
-
apiConferenceDial
(name, url, background=False)¶ Dial the given url from conference
name – (str) name of the conference url – (str) FreeSWITCH compatible call URL
-
apiConferenceKick
(name, member, background=False)¶ Kick the given member from conference
name – (str) name of the conference member – (str) member id or all or last
-
apiConferenceList
(name=None, delim=None, background=False)¶ List the conference name - (str) name of the conference. if not given all the conferences will be listed delim - (str) delimiter to use for separating values
-
apiConferenceListCount
(name, background=True)¶ Return number of members in the conference
name – (str) name of the conference
-
apiConferenceMute
(name, member, background=False)¶ Mute given member in a conference
name – (str) name of the conference member – (str) member id or all or last
-
apiConferencePlay
(name, filename, member=None, background=False)¶ Playback given file in conference
name – (str) name of the conference filename – (str) name of the audio file to be played in conference member – (str) member id in conference
-
apiConferenceStop
(name, fid=None, member=None, background=False)¶ Stop an ongoing/queued playback in conference
name – (str) name of the conference fid – (str) file ID to stop takes all|async|current|last member – (str) member id in conference
-
apiConferenceUnMute
(name, member, background=False)¶ UnMute given member in a conference
name – (str) name of the conference member – (str) member id or all or last
-
apiConferenceVolume
(name, member, value=0, direction='out', background=False)¶ Set volume of conference
name – (str) name of the conference memeber – (str) member id or all or last value – (int) 0 - 4 direction – (str) in or out
-
apiGlobalGetVar
(variable='', background=False)¶ Get the value of a global variable
variable – name of the variable
returns the value of the provided global variable if argument variable is not present then all global variables are returned.
-
apiGlobalSetVar
(variable, value, background=False)¶ Set the value of a global variable
variable – name of the variable whose value needs to be set value – value of the variable to be set
-
apiHupAll
(cause='NORMAL_CLEARING', variable='', value='', background=False)¶ Hangup all the existing channels
cause – cause for hanging up variable – channel having the provided variable will be checked for hangup value – value of the variable. Hangup only if this matches
-
apiLoad
(module_name, background=False)¶ Load external module
module_name – (str) name of the module
-
apiOriginate
(url, application='', appargs='', extension='', dialplan='', context='', cidname='', cidnum='', timeout='', channelvars={}, background=False)¶ Originate a new channel and connect it back to the specified extension or application
url – (str) call url . Should be a valid FreeSWITCH supported URL extension – (str) Extension number that the originated call has to be connected back to. make sure
that you provide dialplan and context also when this arg is provided.application – (str) Application name to connect to, either extension or application has to be provided appargs – (str) application arguments dialplan – (str) FreeSWITCH dialplan context – (str) Context to look for the extension cidname – (str) Outbound caller ID name cidnum – (str) Outbound caller ID number channelvars – (dict) key value pairs of channel variables to be set on originated channel.
-
apiReload
(module_name, background=False)¶ Reload and external module
module_name – (str) name of the module
-
apiReloadXML
(background=False)¶ Reload XML configuration
-
apiStatus
(background=False)¶ Fetch freeswitch status
-
apiUUIDBreak
(uuid, all=True, background=False)¶ Break out of media being sent to a channel. For example, if an audio file is being played to a channel, issuing uuid_break will discontinue the media and the call will move on in the dialplan, script, or whatever is controlling the call.
uuid - (str) uuid of the target channel all - (bool) to break all queued up audio files or only the current one
-
apiUUIDBridge
(uuid1, uuid2, background=False)¶ Bridge two active channel uuids
uuid1 – (str) Channel 1 uuid uuid2 – (str) Second channel uuid
-
apiUUIDBroadcast
(uuid, path, leg='aleg', background=False)¶ Play a <path> file to a specific <uuid> call.
uuid – (str) uuid of the target channel path – (str) path of the file to be played leg – (str) on which leg to play file possible options - aleg,bleg,both defaults to aleg
-
apiUUIDChat
(uuid, msg, background=False)¶ Send a chat message to target channel
uuid – (str) uuid of the target channel msg – (str) chat message to be sent
-
apiUUIDDeflect
(uuid, uri, background=False)¶ Deflect an answered SIP call off of FreeSWITCH by sending the REFER method.
uuid_deflect waits for the final response from the far end to be reported. It returns the sip fragment from that response as the text in the FreeSWITCH response to uuid_deflect. If the far end reports the REFER was successful, then FreeSWITCH will issue a bye on the channel.
uuid – (str) uuid of the target channel uri – (str) destination sip URI
-
apiUUIDDisplace
(uuid, switch, path, limit, mux=True, background=False)¶ Displace the audio for the target <uuid> with the specified audio <path>.
uuid – (str) uuid of the target channel switch – (str) possible options are start,stop path – (str) path of the file to be played limit – (int/str) number of seconds before terminating the displacement mux – (bool) cause the original audio to be mixed, i.e. you can still converse with the other party while the file is playing
-
apiUUIDExists
(uuid, background=False)¶ Check if a given uuid exists
uuid – (str) uuid of the target channel
-
apiUUIDFlushDTMF
(uuid, background=False)¶ Flush queued DTMF digits
uuid – (str) uuid of the target channel
-
apiUUIDHold
(uuid, off=False, background=False)¶ Place a call on hold
uuid – (str) uuid of the target channel off – (bool) turn on or turn off hold
-
apiUUIDKill
(uuid, cause='', background=False)¶ Kill a given channel
uuid – (str) uuid of the target channel cause – (str) hangup reason
-
apiUUIDMedia
(uuid, off=False, background=False)¶ Reinvite a channel bridging media
uuid – (str) uuid of the target channel off – (bool)
-
apiUUIDPark
(uuid, background=False)¶ Park a given channel
uuid – (str) uuid of the target channel
-
apiUUIDRecord
(uuid, start, path, limit=None, background=False)¶ Record channel to given path
uuid–(str) uuid of the target channel start–(bool) start or stop recording path –(str) path of file to where channel should be recorded
-
apiUUIDSendDTMF
(uuid, dtmf, background=False)¶ Send dtmf to given channel
uuid – (str) uuid of the target channel dtmf – (str) DTMF data to be sent
-
apiUnload
(module_name, background=False)¶ Unload external module
module_name – (str) name of the module to unload
-
apiVersion
(background=False)¶ Fetch FreeSWITCH version
-
avmd
(start=True, uuid='', lock=True)¶ Start or stop avmd on current channel
-
bridge
(endpoints=[], uuid='', lock=True)¶ Bridge and endpoint to given channel
endpoints – (list) list of endpoint FreeSWITCH URIs
-
conference
(confname, uuid='', lock=True)¶ Connect the channel to give conference
confname – (str) conference name
-
deregisterEvent
(ecb)¶ Deregister a callback for the given event
ecb – (EventCallback) instance of EventCallback object
-
disconnectNotice
()¶ Handle disconnect notice
-
disconnectNoticeReceived
(msg)¶ Override this to receive disconnect notice from FreeSWITCH
-
disconnectedFromFreeSWITCH
()¶ Over-ride this to get notified of FreeSWITCH disconnection
-
endlessPlayback
(path, uuid='', lock=True)¶ Play a file endlessly
-
flowBreak
(clear_queue=False, uuid='', lock=True)¶ Break the current action that is being performed on call
-
hangup
(uuid='', lock=True)¶ Hangup current channel
-
inspectMessage
()¶ Inspect message and dispatch based on self.state or Content-Type of message
-
myevents
(uuid='')¶ Tie up the connection to particular channel events
-
needToSubscribe
(event)¶ Decide if we need to subscribe to an event or not by comparing the event provided against already subscribeEvents
event – (str) event name
returns bool
-
onBackgroundJob
(message, df)¶ we may not need this method anymore
-
onBackgroundJobFinalEvent
(event)¶ we may not need this method anymore
-
playAndGetDigits
(min, max, tries=3, timeout=4000, terminators='#', filename='', invalidfile='', varname='', regexp='\\d', uuid='', lock=True)¶ Play the given sound file and get back caller’s DTMF min – (int) minimum digits length max – (int) maximum digits length tires – (int) number of times to play the audio file default is 3 timeout – (int) time to wait after fileblack in milliseconds . default is 4000 filename – (str) name of the audio file to be played varname – (str) DTMF digit value will be set as value to the variable of this name regexp – (str) regurlar expression to match the DTMF uuid – (str) uuid of the target channel
Make sure CHANNEL_EXECUTE_COMPLETE event is subcribed otherwise finalDF will never get invoked
-
playback
(path, terminators=None, uuid='', lock=True)¶ Playback given file name on channel
path – (str) path of the file to be played ‘
-
recordSession
(filename, uuid='', lock=True)¶ Record entire session using record_session dialplan tool
-
registerEvent
(event, subscribe, function, *args, **kwargs)¶ Register a callback for the event event – (str) Event name as sent by FreeSWITCH , Custom events should give subclass also
eg : CUSTOM conference::maintenancesubsribe – (bool) if True subscribe to this event function – callback function accepts a event dictionary as first argument args – argumnet to be passed to callback function kwargs – keyword arguments to be passed to callback function
returns instance of EventCallback , keep a reference of this around if you want to deregister it later
-
schedHangup
(secs, uuid='', lock=True)¶ Schedule hangup
seconds – (int/str) seconds to wait before hangup
-
sendMsg
(msg)¶ Send message to FreeSWITCH
msg – (event) Event object
-
set
(variable, value, uuid='', lock=True)¶ Set a channel variable
variable – (str) name of the channel variable value – (str) value of the channel variable
-
stopRecordSession
(path, uuid='', lock=True)¶ Stop recording session
-
subscribeEvents
(events)¶ Subscribe to FreeSWITCH events.
events -(str) ‘all’ subscribe to all events or event names separated by space this method can subscribe to multiple events but if the event is of CUSTOM type then only one CUSTOM event with subclass should be given
-
voice_start
(uuid='', lock=True)¶ Start AMD on current channel
-
voice_stop
(uuid='', lock=True)¶ Stop AMD on current channel
-
pyswitch.fsprotocol.Event class¶
Event
class implements a Python dictionary
like object. This object is used as data type for information sent or received from FreeSWITCH
Events and API or dialplan commands both can be represented using this object.
-
class
pyswitch.fsprotocol.
Event
¶ Event object class has the following methods
-
decode
()¶ Rebuild the event headers with with URL format decoded values
-
as_string
()¶ Returns string representation of Event object. You are not required to use this directly. This method is useful while sending Event object message on wire.
-
pyswitch.inbound module¶
Inherit your factory from InboundFactory while connecting to FreeSWITCH as InboundSocket ESL.
Pass on the password set in FreeSWITCH ESL as the argument to this class. The protocol
attribute set in this class must be inherited from FSProtocol
or
InboundProtocol
-
class
pyswitch.inbound.
InboundFactory
(password)¶ Bases:
twisted.internet.protocol.ClientFactory
A factory for InboundSocketProtocol
-
protocol
¶ alias of
InboundProtocol
-
-
class
pyswitch.inbound.
InboundProtocol
¶ Bases:
pyswitch.fsprotocol.FSProtocol
Inbound connection to FreeSWITCH. Using inbound socket connections you can check status, make outbound calls, etc.
-
authFailed
(error)¶ Override this for when authentication failed
-
authSuccess
(msg)¶ Override this for when authentication is sueccessful
-
pyswitch.outbound module¶
-
class
pyswitch.outbound.
OutboundProtocol
¶ Bases:
pyswitch.fsprotocol.FSProtocol
Outbound connection from FreeSWITCH.
Inherit from your outbound socket protocol from this class.
-
connectComplete
(callinfo)¶ This method is invoked when FreeSWITCH connects to outbound socket program callinfo is an Event object filled with all the channel variables call details passed in by FreeSWITCH. Override this method in your class.
-