经过实战检验的FreeSWITCH事件套接字协议客户端实现,使用Gevent。
项目描述
GreenSWITCH: FreeSWITCH事件套接字协议
经过实战检验的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的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 5506f8000ae442880ebad3bad8c4edfb948d515dc576bca01816b6be52e84df6 |
|
MD5 | c040feb231c700beb68ddad3025efbd1 |
|
BLAKE2b-256 | b661af406a0a1d9d25821c907cc870862a82d3be7c3d6acacef7986cfd19baa7 |