FreeSwitch+lua實現IVR(互動式語音應答)

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章