用gevent实现经过战斗验证的freeswitch事件套接字协议客户端。

greenswitch的Python项目详细描述


greenswitch:freeswitch事件套接字协议

https://travis-ci.org/EvoluxBR/greenswitch.svg?branch=masterhttps://img.shields.io/pypi/v/greenswitch.svghttps://img.shields.io/pypi/dm/greenswitch.svg

用gevent实现经过战斗验证的freeswitch事件套接字协议客户端。

这是一个使用gevent的自由开关事件套接字协议的实现 绿色的。它已经投入生产,每天处理数百个电话。

完全支持python3!

入站套接字模式

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

当前只实现入站套接字,对出站套接字的支持应该 很快就好。

出站套接字模式

出站是通过同步和异步支持实现的。主要思想是创造 将被调用的应用程序,将outboundsession作为参数传递。 此OutboundSession表示由ESL连接处理的调用。 基本功能已经实现:

  • playback
  • play_and_get_digits
  • hangup
  • park
  • uuid_kill
  • answer
  • 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
'''importgeventimportgreenswitchimportlogginglogging.basicConfig(level=logging.DEBUG)classMyApplication(object):def__init__(self,session):self.session=sessiondefrun(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()defhandle_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 playbackself.session.myevents()print("myevents")# Send me all the events related to this call even if the call is already# hangupself.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 numberdigit=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 permissionself.session.uuid_break()print("break")# Bye callerself.session.hangup()print("hangup")# Close the socket so freeswitch can leave us aloneself.session.stop()server=greenswitch.OutboundESLServer(bind_address='0.0.0.0',bind_port=5000,application=MyApplication,max_connections=5)server.listen()

享受吧!

欢迎反馈。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
使用概要文件后找不到java bean不确定原因   多线程如果信号量锁获取/tryAcquire失败,如何使Java线程执行不同的任务而不是阻塞?   java编译器在同一目录中找不到其他类   在Java中,如何检查表示时间戳的字符串是否为有效日期?   java Commons vfs FindFile虚拟文件   TomcatJava。util。计时器空指针异常   java是在Oracle和Vertica之间移动数据的有效方法   java Adobe Acrobat Reader无法打开pdf文件,因为该文件不是受支持的文件类型,或者该文件已损坏   java使用usb驱动程序libusb、usb4java ecc。。为什么它如此不受支持?   java如何在第二列或特定列中插入jface TreeViewer?   java通过internet发送对象并调用其方法   带超声波传感器的Esp32Cam   java Cassandra分页问题最后一页的分页状态不正确   Java/WildFly/MongoDB/JAAS身份验证始终返回403禁止