GreenSWITCH

repository·master·Indexed 41 days ago

https://github.com/evoluxbr/greenswitch

A Python implementation of the FreeSWITCH Event Socket Protocol (ESL) using Gevent. It supports Inbound socket mode for sending API commands via InboundESL and Outbound socket mode for call control via OutboundESLServer and OutboundSession.

Tokens
1.2K
Snippets
3
Records
5
Agent score
50%

What's inside GreenSWITCH

  1. Use Inbound Socket Mode

    master

    Inbound mode allows your Python application to connect to a FreeSWITCH instance and send commands via the Event Socket Protocol (ESL). Use greenswitch.InboundESL to establish a connection and the .send() method to execute API commands.

    import greenswitch
    
    # Connect to the FreeSWITCH instance
    fs = greenswitch.InboundESL(host='127.0.0.1', port=8021, password='ClueCon')
    fs.connect()
    
    # Send an API command
    r = fs.send('api list_users')
    print(r.data)
  2. Use Outbound Socket Mode

    master

    Outbound mode allows FreeSWITCH to connect to your application to hand off call control. To implement this, you must:

    1. Create a class (an Application) that accepts an OutboundSession in its constructor.
    2. Implement a run() method in your class to handle the call logic.
    3. Use greenswitch.OutboundESLServer to listen for incoming connections from FreeSWITCH.

    In your FreeSWITCH dialplan, you must trigger the connection using the socket application:

    <extension name="out socket">
        <condition>
            <action application="socket" data="<your_server_host>:<your_server_port> async full"/>
        </condition>
    </extension>
    import gevent
    import greenswitch
    
    class MyApplication(object):
        def __init__(self, session):
            self.session = session
    
        def run(self):
            try:
                self.handle_call()
            except:
                self.session.stop()
    
        def handle_call(self):
            # Call logic goes here
            self.session.answer()
            self.session.hangup()
            self.session.stop()
    
    server = greenswitch.OutboundESLServer(
        bind_address='0.0.0.0', 
        bind_port=5000, 
        application=MyApplication,
        max_connections=5
    )
    server.listen()
  3. Manage Call Events with OutboundSession

    master

    When handling an outbound call, use the following methods on the OutboundSession object to control event visibility and lifecycle:

    • session.myevents(): Instructs FreeSWITCH to send all events related to the current call. This is necessary to monitor application progress (e.g., knowing when a playback finishes).
    • session.linger(): Ensures that events related to the call are received even after the call has already hung up.
  4. Control Audio and DTMF with OutboundSession

    master

    The OutboundSession provides methods to interact with the media stream:

    • session.playback(file, block=True): Plays an audio file.
      • If block=True (default), the execution flow waits until the file finishes playing.
      • If block=False, the method returns immediately, allowing you to perform other tasks (like API calls) in the background while the audio plays.
    • session.play_and_get_digits(...): Plays audio and waits for the caller to press DTMF digits. This method is blocking.
      • Parameters include various patterns, timeouts, and audio files for success/failure.
      • response_timeout: Use this to account for the length of the audio being played.
    • session.uuid_break(): Stops the current media playback (e.g., stopping Music on Hold) to return control to your application logic.
    # Blocking playback
    session.playback('ivr/ivr-welcome', block=True)
    
    # Non-blocking playback (e.g., for Music on Hold)
    session.playback('local_stream://default', block=False)
    
    # Get digits (blocking)
    digit = session.play_and_get_digits('1', '1', '3', '5000', '#', 'conf.wav', 'inv.wav', 'test', '\d', '1000', "''", block=True, response_timeout=5)
    
    # Stop background playback
    session.uuid_break()
  5. Basic OutboundSession Call Control Methods

    master

    The following methods are available on the OutboundSession to manage the call state:

    • answer(): Answers the call.
    • hangup(): Hangs up the call.
    • park(): Parks the call.
    • uuid_kill(): Kills the call/session.
    • sleep(): Sleeps the session.
    • stop(): Closes the socket connection so FreeSWITCH can release the channel.