Skip to main content

Battle proven FreeSWITCH Event Socket Protocol client implementation with Gevent.

Project description

GreenSWITCH: FreeSWITCH Event Socket Protocol

https://github.com/EvoluxBR/greenswitch/actions/workflows/tests.yml/badge.svg https://img.shields.io/pypi/v/greenswitch.svg https://img.shields.io/pypi/dm/greenswitch.svg

Battle proven FreeSWITCH Event Socket Protocol client implementation with Gevent.

This is an implementation of FreeSWITCH Event Socket Protocol using Gevent Greenlets. It is already in production and processing hundreds of calls per day.

Full Python3 support!

Inbound Socket Mode

>>> import greenswitch
>>> fs = greenswitch.InboundESL(host='127.0.0.1', port=8021, password='ClueCon')
>>> fs.connect()
>>> r = fs.send('api list_users')
>>> print r.data

Outbound Socket Mode

Outbound is implemented with sync and async support. The main idea is to create an Application that will be called passing an OutboundSession as argument. This OutboundSession represents a call that is handled by the ESL connection. Basic functions are implemented already:

  • playback

  • play_and_get_digits

  • hangup

  • park

  • uuid_kill

  • answer

  • sleep

With current api, it’s easy to mix sync and async actions, for example: play_and_get_digits method will return the pressed DTMF digits in a block mode, that means as soon as you call that method in your Python code the execution flow will block and wait for the application to end only returning to the next line after ending the application. But after getting digits, if you need to consume an external system, like posting this to an external API you can leave the caller hearing MOH while the API call is being done, you can call the playback method with block=False, playback(‘my_moh.wav’, block=False), after your API end we need to tell FreeSWITCH to stop playing the file and give us back the call control, for that we can use uuid_kill method.

Example of Outbound Socket Mode:

'''
Add a extension on your dialplan to bound the outbound socket on FS channel
as example below

<extension name="out socket">
    <condition>
        <action application="socket" data="<outbound socket server host>:<outbound socket server port> async full"/>
    </condition>
</extension>

Or see the complete doc on https://freeswitch.org/confluence/display/FREESWITCH/mod_event_socket
'''
import gevent
import greenswitch

import logging
logging.basicConfig(level=logging.DEBUG)


class MyApplication(object):
    def __init__(self, session):
        self.session = session

    def run(self):
        """
        Main function that is called when a call comes in.
        """
        try:
            self.handle_call()
        except:
            logging.exception('Exception raised when handling call')
            self.session.stop()

    def handle_call(self):
        # We want to receive events related to this call
        # They are also needed to know when an application is done running
        # for example playback
        self.session.myevents()
        print("myevents")
        # Send me all the events related to this call even if the call is already
        # hangup
        self.session.linger()
        print("linger")
        self.session.answer()
        print("answer")
        gevent.sleep(1)
        print("sleep")
        # Now block until the end of the file. pass block=False to
        # return immediately.
        self.session.playback('ivr/ivr-welcome')
        print("welcome")
        # blocks until the caller presses a digit, see response_timeout and take
        # the audio length in consideration when choosing this number
        digit = self.session.play_and_get_digits('1', '1', '3', '5000', '#',
                                                 'conference/conf-pin.wav',
                                                 'invalid.wav',
                                                 'test', '\d', '1000', "''",
                                                 block=True, response_timeout=5)
        print("User typed: %s" % digit)
        # Start music on hold in background without blocking code execution
        # block=False makes the playback function return immediately.
        self.session.playback('local_stream://default', block=False)
        print("moh")
        # Now we can do a long task, for example, processing a payment,
        # consuming an APIs or even some database query to find our customer :)
        gevent.sleep(5)
        print("sleep 5")
        # We finished processing, stop the music on hold and do whatever you want
        # Note uuid_break is a general API and requires full permission
        self.session.uuid_break()
        print("break")
        # Bye caller
        self.session.hangup()
        print("hangup")
        # Close the socket so freeswitch can leave us alone
        self.session.stop()

    server = greenswitch.OutboundESLServer(bind_address='0.0.0.0',
                                   bind_port=5000,
                                   application=MyApplication,
                                   max_connections=5)
    server.listen()

Enjoy!

Feedbacks always welcome.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

greenswitch-0.0.13.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

greenswitch-0.0.13-py2-none-any.whl (10.3 kB view details)

Uploaded Python 2

File details

Details for the file greenswitch-0.0.13.tar.gz.

File metadata

  • Download URL: greenswitch-0.0.13.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.8.2 requests/2.27.1 setuptools/44.1.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/2.7.16

File hashes

Hashes for greenswitch-0.0.13.tar.gz
Algorithm Hash digest
SHA256 c962c0bcff22aaadc73a7110c2a6f75faf187f92bb3d53f004f604ca3f2b219a
MD5 3b9bad95c63020ce22afa581f80750a8
BLAKE2b-256 71619a5bc88d2a8aaece8bc5f2faf49ecec562c1e0a4280a8d7cd2842aa589b9

See more details on using hashes here.

File details

Details for the file greenswitch-0.0.13-py2-none-any.whl.

File metadata

  • Download URL: greenswitch-0.0.13-py2-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 2
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.8.2 requests/2.27.1 setuptools/44.1.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/2.7.16

File hashes

Hashes for greenswitch-0.0.13-py2-none-any.whl
Algorithm Hash digest
SHA256 c49bde2b04c5ea2626b6a1368d29137377c5534cd615dfd4837167e9cecfd975
MD5 f3451c31ea42b20dcc0d1313a396faa5
BLAKE2b-256 139ebc471f2ae6ef6980b99c0d8bfdd011b00a4eca47c7a04c7e0a1e79de2cb1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page