Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function split(pString, pPattern)
- local Table = {} -- NOTE: use {n = 0} in Lua-5.0
- local fpat = "(.-)" .. pPattern
- local last_end = 1
- local s, e, cap = pString:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(Table,cap)
- end
- last_end = e+1
- s, e, cap = pString:find(fpat, last_end)
- end
- if last_end <= #pString then
- cap = pString:sub(last_end)
- table.insert(Table, cap)
- end
- return Table
- end
- --function addslashes(s)
- -- return (string.gsub(s, "([^'])'", "%1\'"))
- --end
- -- Backslash-escape special characters:
- function addslashes(s)
- -- Double quote, single quote, and backslash (per the PHP
- -- manual):
- s = string.gsub(s, "(['\"\\])", "\\%1")
- -- The null character gets turned into a pair of printing
- -- characters by PHP addslashes. Let's do the same:
- return (string.gsub(s, "%z", "\\0"))
- end -- addslashes
- function onInput(s, type, obj)
- freeswitch.consoleLog("info", "Callback with type " .. type .. "\n");
- if (type == "event") then
- local event = obj:getHeader("Speech-Type");
- if (event == "begin-speaking") then
- freeswitch.consoleLog("info", "\n" .. obj:serialize() .. "\n");
- -- Return break on begin-speaking events to stop playback of the fire or tts.
- return "break";
- end
- if (event == "detected-speech") then
- freeswitch.consoleLog("info", "\n" .. obj:serialize() .. "\n");
- if (obj:getBody()) then
- -- Pause speech detection (this is on auto but pausing it just in case)
- session:execute("detect_speech", "pause");
- -- Get XML string for possible failed call processing
- result = obj:getBody()
- -- When a body is returned starting with "Completion" the call has failed
- no1, no2 = string.find(result, "Completion")
- if (no2 ~= nil) then
- session:streamFile("/usr/local/freeswitch/sounds/en/us/callie/directory/8000/call-failed.wav");
- else
- -- Add escape slashes to returned XML
- slashResult = addslashes(result)
- xmlResult = split(slashResult,"<")
- response = split(xmlResult[4],">")
- freeswitch.consoleLog("warning", "\n Response:\n\n" .. response[2] .. "\n\n");
- if (response[2] == "yes") then
- session:streamFile("/usr/local/freeswitch/sounds/you-said-yes.wav");
- else if (response[2] == "no") then
- session:streamFile("/usr/local/freeswitch/sounds/you-said-no.wav");
- end
- end
- end
- end
- end
- end
- end
- -- Answer the call.
- session:answer();
- -- Register the input callback
- session:setInputCallback("onInput");
- -- Sleep a little bit to give media time to be fully up.
- session:sleep(500);
- --Play sound file
- session:streamFile("/usr/local/freeswitch/sounds/say-yes-or-no.wav")
- -- Start the detect_speech app. This attaches the bug to fire events
- -- session:execute("set", "fire_asr_events=1")
- session:execute("detect_speech", "unimrcp nuance/boolean nuance5-mrcp2");
- -- This sleep is what blocks till the detected-speech event. This has to give you enough time to speak plus get the results.
- session:sleep(3000);
- session:sleep(3000);
- --Stop speech detect
- session:execute("detect_speech", "stop")
Add Comment
Please, Sign In to add comment