• freeswich学习


    写在前面

    因为所在部分主要负责公司客服业务,需要了解freeswich相关内容,所以这里将学习内容记录下。

    1:安装freesswich

    freeswich是一个实现了软交换协议的开源软件,可以对对接运营上的通话线路,实现拨打电话。

    • 安装依赖
    yum install -y https://files.freeswitch.org/repo/yum/centos-release/freeswitch-release-repo-0-1.noarch.rpm epel-release 
    
    yum install -y yum-utils --enablerepo=extras 
    
    yum install -y yum-plugin-ovl centos-release-scl rpmdevtools yum-utils git wget vim devtoolset-7-gcc* devtoolset-7 libtiff-devel cmake3 libatomic unixODBC unixODBC-devel.x86_64 postgresql-libs postgresql-devel libpqxx-devel
    
    yum install -y gcc-c++ autoconf automake libtool ncurses-devel zlib-devel libjpeg-devel openssl-devel e2fsprogs-devel sqlite-devel libcurl-devel pcre-devel speex-devel ldns-devel libedit-devel libxml2-devel libyuv-devel libvpx-devel libvpx2* libdb4* libidn-devel unbound-devel libuuid-devel lua-devel libsndfile-devel yasm-devel
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 下载源码并编译
    cd /data
    git clone -b v1.10.7 https://github.com/signalwire/freeswitch
    cd /data/freeswitch
    git clone https://github.com/freeswitch/spandsp.git
    git clone https://github.com/freeswitch/sofia-sip.git
    
    
    #编译spandsp
    cd /data/freeswitch/spandsp
    ./bootstrap.sh
    ./configure
    make
    make install
    
    #编译sofia-sip
    cd /data/freeswitch/sofia-sip
    ./bootstrap.sh
    ./configure
    make
    make install
    
    #添加库的路径到系统
    export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:${PKG_CONFIG_PATH} 
    ldconfig
    
    # 编译fs
    cd /data/freeswitch
    ./bootstrap.sh
    ./configure --enable-portable-binary --prefix=/usr/local/freeswitch --with-gnu-ld --with-python --with-openssl --enable-core-odbc-support --enable-zrtp
    make
    make install
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    注意:如果报错,找到modules.conf文件,将报错的模块注释掉即可。如#applications/mod_signalwire #applications/mod_av,具体根据当时的情况看吧,谁报错就注释掉谁。

    • 启动
    [root@localhost bin]# pwd
    /usr/local/freeswitch/bin
    [root@localhost bin]# ./freeswitch -nonat
    2023-11-07 17:14:18.156329 0.00% [INFO] switch_event.c:713 Activate Eventing Engine.
    ...
    2023-11-07 17:14:20.946405 0.00% [CONSOLE] switch_core.c:2544 
    .=============================================================.
    |   _____              ______        _____ _____ ____ _   _   |
    |  |  ___| __ ___  ___/ ___\ \      / /_ _|_   _/ ___| | | |  |
    |  | |_ | '__/ _ \/ _ \___ \\ \ /\ / / | |  | || |   | |_| |  |
    |  |  _|| | |  __/  __/___) |\ V  V /  | |  | || |___|  _  |  |
    |  |_|  |_|  \___|\___|____/  \_/\_/  |___| |_| \____|_| |_|  |
    |                                                             |
    .=============================================================.
    |   Anthony Minessale II, Michael Jerris, Brian West, Others  |
    |   FreeSWITCH (http://www.freeswitch.org)                    |
    |   Paypal Donations Appreciated: paypal@freeswitch.org       |
    |   Brought to you by ClueCon http://www.cluecon.com/         |
    .=============================================================.
    
    .=======================================================================================================.
    |       _                            _    ____ _             ____                                       |
    |      / \   _ __  _ __  _   _  __ _| |  / ___| |_   _  ___ / ___|___  _ __                             |
    |     / _ \ | '_ \| '_ \| | | |/ _` | | | |   | | | | |/ _ \ |   / _ \| '_ \                            |
    |    / ___ \| | | | | | | |_| | (_| | | | |___| | |_| |  __/ |__| (_) | | | |                           |
    |   /_/   \_\_| |_|_| |_|\__,_|\__,_|_|  \____|_|\__,_|\___|\____\___/|_| |_|                           |
    |                                                                                                       |
    |    ____ _____ ____    ____             __                                                             |
    |   |  _ \_   _/ ___|  / ___|___  _ __  / _| ___ _ __ ___ _ __   ___ ___                                |
    |   | |_) || || |     | |   / _ \| '_ \| |_ / _ \ '__/ _ \ '_ \ / __/ _ \                               |
    |   |  _ < | || |___  | |__| (_) | | | |  _|  __/ | |  __/ | | | (_|  __/                               |
    |   |_| \_\|_| \____|  \____\___/|_| |_|_|  \___|_|  \___|_| |_|\___\___|                               |
    |                                                                                                       |
    |     ____ _             ____                                                                           |
    |    / ___| |_   _  ___ / ___|___  _ __         ___ ___  _ __ ___                                       |
    |   | |   | | | | |/ _ \ |   / _ \| '_ \       / __/ _ \| '_ ` _ \                                      |
    |   | |___| | |_| |  __/ |__| (_) | | | |  _  | (_| (_) | | | | | |                                     |
    |    \____|_|\__,_|\___|\____\___/|_| |_| (_)  \___\___/|_| |_| |_|                                     |
    |                                                                                                       |
    .=======================================================================================================.
    
    
    2023-11-07 17:14:20.946482 0.00% [INFO] switch_core.c:2553 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    停止命令 ./freeswich -stop

    • 查看端口号

    会启动好几个端口,各有各的作用。

    [root@localhost bin]# netstat -nlp|grep frees
    tcp        0      0 192.168.192.128:7443    0.0.0.0:*               LISTEN      118580/./freeswitch 
    tcp        0      0 192.168.192.128:5080    0.0.0.0:*               LISTEN      118580/./freeswitch 
    tcp        0      0 192.168.192.128:5060    0.0.0.0:*               LISTEN      118580/./freeswitch 
    tcp        0      0 192.168.192.128:5066    0.0.0.0:*               LISTEN      118580/./freeswitch 
    tcp6       0      0 :::8021                 :::*                    LISTEN      118580/./freeswitch 
    tcp6       0      0 ::1:5080                :::*                    LISTEN      118580/./freeswitch 
    tcp6       0      0 ::1:5060                :::*                    LISTEN      118580/./freeswitch 
    udp        0      0 192.168.192.128:5060    0.0.0.0:*                           118580/./freeswitch 
    udp        0      0 192.168.192.128:5080    0.0.0.0:*                           118580/./freeswitch 
    udp6       0      0 ::1:5060                :::*                                118580/./freeswitch 
    udp6       0      0 ::1:5080                :::*                                118580/./freeswitch 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2:安装软电话

    模拟真实的电话,客服使用的就是这玩意,从这里 下载,下载后,按照如下配置注意ip改成你自己的
    在这里插入图片描述
    变为下图就成功了:
    在这里插入图片描述
    此时我们就可以通过freeswich呼叫软电话了通过fs提供的fs_cli工具和originate命令

    [root@localhost bin]# ./fs_cli -H 127.0.0.1 -p ClueCon
    .=======================================================.
    |            _____ ____     ____ _     ___              |
    |           |  ___/ ___|   / ___| |   |_ _|             |
    |           | |_  \___ \  | |   | |    | |              |
    |           |  _|  ___) | | |___| |___ | |              |
    |           |_|   |____/   \____|_____|___|             |
    |                                                       |
    .=======================================================.
    | Anthony Minessale II, Ken Rice,                       |
    | Michael Jerris, Travis Cross                          |
    | FreeSWITCH (http://www.freeswitch.org)                |
    | Paypal Donations Appreciated: paypal@freeswitch.org   |
    | Brought to you by ClueCon http://www.cluecon.com/     |
    .=======================================================.
    
    .=======================================================================================================.
    |       _                            _    ____ _             ____                                       |
    |      / \   _ __  _ __  _   _  __ _| |  / ___| |_   _  ___ / ___|___  _ __                             |
    |     / _ \ | '_ \| '_ \| | | |/ _` | | | |   | | | | |/ _ \ |   / _ \| '_ \                            |
    |    / ___ \| | | | | | | |_| | (_| | | | |___| | |_| |  __/ |__| (_) | | | |                           |
    |   /_/   \_\_| |_|_| |_|\__,_|\__,_|_|  \____|_|\__,_|\___|\____\___/|_| |_|                           |
    |                                                                                                       |
    |    ____ _____ ____    ____             __                                                             |
    |   |  _ \_   _/ ___|  / ___|___  _ __  / _| ___ _ __ ___ _ __   ___ ___                                |
    |   | |_) || || |     | |   / _ \| '_ \| |_ / _ \ '__/ _ \ '_ \ / __/ _ \                               |
    |   |  _ < | || |___  | |__| (_) | | | |  _|  __/ | |  __/ | | | (_|  __/                               |
    |   |_| \_\|_| \____|  \____\___/|_| |_|_|  \___|_|  \___|_| |_|\___\___|                               |
    |                                                                                                       |
    |     ____ _             ____                                                                           |
    |    / ___| |_   _  ___ / ___|___  _ __         ___ ___  _ __ ___                                       |
    |   | |   | | | | |/ _ \ |   / _ \| '_ \       / __/ _ \| '_ ` _ \                                      |
    |   | |___| | |_| |  __/ |__| (_) | | | |  _  | (_| (_) | | | | | |                                     |
    |    \____|_|\__,_|\___|\____\___/|_| |_| (_)  \___\___/|_| |_| |_|                                     |
    |                                                                                                       |
    .=======================================================================================================.
    
    Type /help  to see a list of commands
    
    
    
    +OK log level  [7]
    freeswitch@localhost.localdomain> originate user/1000 &echo
    ...
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    echo是回音测试。你说啥fs直接给你说啥那种。
    效果如下图:
    在这里插入图片描述

    3:freeswich常用命令

    3.1:api

    同步执行freeswich内部的api,如api version:

    api version
    
    Content-Type: api/response
    Content-Length: 113
    
    FreeSWITCH Version 1.10.7-release+git~20211024T163933Z~883d2cb662~64bit (git 883d2cb 2021-10-24 16:39:33Z 64bit)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.2:bgapi

    api的异步版本,不需要等待freeswich执行完毕,就会直接返回。

    3.3:event

    event命令用于订阅freeswitch的各种事件, 分别支持plain文本类型、xml类型和json类型的事件数据模式返回。
    在这里插入图片描述

    nodejs订阅所有事件:

    //register system events, work well for CUSTOM event
    conn.events('json', 'all', function(){
    });
    
    • 1
    • 2
    • 3

    3.4:noevents /nixevent

    取消事件订阅的命令:

    /nixevent plain CHANNEL_ANSWER CHANNEL_HANGUP CHANNEL_HANGUP_COMPLETE
    
    • 1

    4:软电话之间互相通信

    这里需要安装多个软电话,如下是我在本机安装2个软电话:
    在这里插入图片描述
    现在通过1002,呼叫1001,如下:
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述在这里插入图片描述

    5:实现一个简单的机器人

    假定公司为了节省客服人力,让我们开发一个机器人,来代替人工,在开发之前我们要了解以下freeswich两种工作模式,InBound和OutBound,Inbound模式是freeswitch作为server,esl作为client来进行连接,OutBound反之,InBound如下图:
    在这里插入图片描述
    OutBound如下图:
    在这里插入图片描述
    这里我们采用OutBound这种方式,我们来起一个Esl server,核心代码如下:

    var PORT = 9099;
    var HOST = '192.168.10.56';
    var channelRabbitmq = null;
    var redisConn = null;
    
    // 启动irv server,等待fs client连接
    // var eslServer = new esl.Server({port: PORT}, function(){
    var eslServer = new esl.Server({port: PORT, host: HOST}, function(){
        // LogHelper.time_log("INFO","95_incoming_ivr","esl_server","IVR server is started at port = " + PORT);
        console.log('95 ivr server start at port: ' + PORT);
        // for rabbitmq
        if(channelRabbitmq === null) {
            pubConnectRabbitmq('192.168.192.128', 5673 , 'admin', 'admin', 'testhost');
        }
        if(redisConn === null) {
            connectToRedis(conf.redis.member, conf.redis.password, conf.redis.bCluster, function(conn) {
                console.log('connect to redis suc!!!')
                redisConn = conn;
            });
        }
    });
    
    // 等待来电回调
    eslServer.on('connection::ready', function(conn, id) {
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    此时freeswich还不知道我们的esl server的地址,因此我们需要将这个地址配置在freeeswich的配置文件/usr/local/freeswitch/etc/freeswitch/dialplan/default.xml中,如下:

    
          
            。。。
            
            
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述
    如下我本地,启动后,使用1001呼叫1002的效果:
    在这里插入图片描述
    这里左侧程序就是我们的机器人了。

    6:接通后播放本地音频

    接通使用answer指令,播放音频使用playback,如下:

    eslServer.on('connection::ready', function(conn, id) {
        console.log("connection::ready...");
        // 接听 'answer' 指令执行后呼叫方会变为自动接通状态
        conn.execute('answer', 'uuid', 'taskId', function() { console.log('answered automatic!!!') });
        // 播放音乐
        conn.execute('playback', '/usr/local/freeswitch/xilouernv.wav', 'xxxx', function () {});
    });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    写在后面

    参考文章列表

    【FreeSwitch开发实践】centos7下编译安装freeswitch及常见编译问题的解决

    【FreeSwitch开发实践】使用SIP客户端Yate连接FreeSwitch进行VoIP通话

    freeswitch笔记(3)-esl入门

  • 相关阅读:
    面试官:能用JavaScript手写一个bind函数吗
    02.爬虫---HTTP基本原理
    开发中 — — 异常与日志处理
    群晖synology DSM 7.2设置钉钉Webhooks通知
    vue,react虚拟dom
    需要电脑文字配音工具的快看过来
    如何在.NET电子表格应用程序中创建流程图
    RTQ2117C-QA多协议USB Type-C车载快充(RTQ2117C)
    RK3399平台开发系列讲解(I/O篇)Linux最大文件数的限制机制
    Java应用层数据链路追踪(附优雅打印日志姿势)
  • 原文地址:https://blog.csdn.net/wang0907/article/details/134272351