freeswitch+lua实现IVR(互动式语音应答)

发布时间 2024-01-13 15:37:35作者: 菩提树下的杨过

IVR(Interactive Voice Response)交互式语言应答,是呼叫中心的1个经典应用场景,FreeSwitch官方有一个利用lua实现的简单示例,大致原理是利用lua脚本+TTS实现,记录一下:(环境:FreeSwitch 1.10.11 + Windows 10)

步骤1:安装TTS

FreeSwitch自带了1个TTS引擎(发音效果比较生硬,仅支持英文,不过用来学习足够了),找到安装目录下的 freeswitch/conf/modules.conf.xml

    <!-- ASR /TTS -->
    <load module="mod_flite"/>
    <!-- <load module="mod_pocketsphinx"/> -->
    <!-- <load module="mod_cepstral"/> -->
    <!-- <load module="mod_tts_commandline"/> -->
    <!-- <load module="mod_rss"/> -->

找到ASR /TTS这一节,把mode_flite注释去掉,然后重启FreeSwitch 生效(如果没生效,检查是否有mod_flite.dll这个文件)

 

步骤2:配置路由

\FreeSWITCH\conf\dialplan\default\welcome.xml,在default目录 下,创建welcome.xml文件,内容如下:

<include>
  <extension name="welcome_ivr">
    <condition field="destination_number" expression="^2910$">
      <action application="lua" data="welcome.lua"/> 
    </condition>
  </extension>
</include>

这段的意思是 如果被叫号码是2910,将由welcome.lua脚本来执行后续逻辑。

 

步骤3:编写交互逻辑lua脚本

\FreeSWITCH\scripts\welcome.lua (创建该文件),内容如下:

-- 先应答,防止电话断掉
session:answer();
while (session:ready() == true) do
    -- 防止自动挂断
    session:setAutoHangup(false);
    -- 设置TTS引擎参数
    session:set_tts_params("flite", "kal");
    -- 播放欢迎语音
    session:speak("Hello. Welcome to the VoIp World!");
    -- 睡100ms
    session:sleep(100);
    -- 播放提示语音
    session:speak("please select an Action.");
    session:sleep(100);
    -- 按1转到1001分机
    session:speak("to call 1001, press 1");
    session:sleep(100);
    -- 按2挂断
    session:speak("to hangup , press 2");
    session:sleep(2000);
    -- 等待按键(5秒超时)
    digits = session:getDigits(1, "", 5000);
    if (digits == "1")  then
        -- 按1,转到1001分机
        session:execute("bridge","user/1001");
    end
    if (digits == "2")  then
        -- 按2,播放bye,bye语音,然后挂断
        session:speak("bye bye");
        session:hangup();
    end
end

 

参考文档:

https://developer.signalwire.com/freeswitch/FreeSWITCH-Explained/Client-and-Developer-Interfaces/Lua-API-Reference/Lua-examples/Lua-Welcome-IVR-example_3965157/#about