1、概述
freeswitch
支持多种语言的业务开发,包括C/C++,java,python,js,lua,Golang等等。
freeswitch
在使用python
做业务开发时,有俩种接入方式,一种是ESL接口,另一种是mod_python
模块。
python
的ESL接口是通过socket
套接字与freeswitch
本文来源gao.dai.ma.com搞@代*码#网进行命令交互,包括发送命令、命令响应和事件回调等,类似于在外部增加一个第三方模块控制fs行为。ESL接口部分会在后续的章节中详细介绍。
今天我们要介绍的是fs内部
的mod_python
语言支持模块,该模块允许我们使用python
脚本开发fs呼叫控制流程。
2、环境
centos:CentOS release 7.0 (Final)或以上版本
freeswitch:v1.8.7
GCC:4.8.5
3、安装mod_python模块
freeswitch
源码安装时,默认不安装mod_python
模块,需要我们进入目录编译安装。
cd /root/freeswitch-1.8.7/src/mod/languages/mod_python make install cd /usr/local/freeswitch/mod ll -tr -rwxr-xr-x. 1 root root 753208 9月 14 10:41 mod_python.so -rwxr-xr-x. 1 root root 1360 9月 14 10:41 mod_python.la
4、python脚本
增加testapi.py
脚本
vi /usr/local/freeswitch/scripts/testapi.py import freeswitch def fsapi(session,stream,env,args): stream.write("hello") freeswitch.consoleLog("info","test")
增加testapp.py
脚本
vi /usr/local/freeswitch/scripts/testapp.py import freeswitch def handler(session, args): session.answer() freeswitch.console_log("info","testCall\n") session.streamFile("local_stream://moh") freeswitch.msleep(3000) session.hangup()
5、配置启动
修改freeswitch
模块加载配置文件
cd /usr/local/freeswitch/conf/autoload_configs vi modules.conf.xml <!-- Languages --> <load module="mod_python"/>
修改dialplan
拨号计划
cd /usr/local/freeswitch/conf/dialplan vi public.xml … <include> <context name="public"> <extension name="test"> <condition> <action application="python" data="testapp"/> </condition> </extension> …
启动freeswitch
cd /usr/local/freeswitch/bin ./freeswitch -nonat 2021-09-14 10:57:06.392800 [NOTICE] mod_python.c:551 Python Framework Loading... 2021-09-14 10:57:06.405965 [CONSOLE] switch_loadable_module.c:1540 Successfully Loaded [mod_python] 2021-09-14 10:57:06.405982 [NOTICE] switch_loadable_module.c:292 Adding Application 'python' 2021-09-14 10:57:06.406012 [NOTICE] switch_loadable_module.c:315 Adding Chat Application 'python' 2021-09-14 10:57:06.406030 [NOTICE] switch_loadable_module.c:338 Adding API Function 'pyrun' 2021-09-14 10:57:06.406046 [NOTICE] switch_loadable_module.c:338 Adding API Function 'python'