跳转到主要内容

经过实战检验的FreeSWITCH事件套接字协议客户端实现,使用Gevent。

项目描述

GreenSWITCH: FreeSWITCH事件套接字协议

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

经过实战检验的FreeSWITCH事件套接字协议客户端实现,使用Gevent。

这是使用Gevent Greenlets实现的FreeSWITCH事件套接字协议。它已在生产中,每天处理数百个调用。

全面支持Python3!

入站套接字模式

>>> 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

出站套接字模式

出站已实现同步和异步支持。主要思路是创建一个应用程序,该应用程序将传入一个OutboundSession作为参数。这个OutboundSession代表由ESL连接处理的调用。已实现基本功能

  • 回放

  • play_and_get_digits

  • 挂断

  • 停放

  • uuid_kill

  • 应答

  • sleep

使用当前API,很容易混淆同步和异步操作,例如:play_and_get_digits方法将以块模式返回按下的DTMF数字,这意味着一旦你在Python代码中调用该方法,执行流程就会阻塞并等待应用程序结束,只有在应用程序结束后才会返回到下一行。但是,在获取数字后,如果您需要消耗外部系统,例如将其发布到外部API,您可以在调用API的过程中让呼叫者听到MOH,您可以调用播放方法时设置block=False,即 playback('my_moh.wav', block=False),在您的API调用结束后,我们需要告诉FreeSWITCH停止播放文件并交还呼叫控制,为此我们可以使用uuid_kill方法。

外发套接字模式示例

'''
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()

祝您使用愉快!

欢迎提供反馈。

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

greenswitch-0.0.16.tar.gz (9.9 kB 查看哈希值)

上传时间 源代码

支持者