Guest User

Freeswitch IVR Script

a guest
Sep 13th, 2011
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. function split(pString, pPattern)
  2. local Table = {} -- NOTE: use {n = 0} in Lua-5.0
  3. local fpat = "(.-)" .. pPattern
  4. local last_end = 1
  5. local s, e, cap = pString:find(fpat, 1)
  6. while s do
  7. if s ~= 1 or cap ~= "" then
  8. table.insert(Table,cap)
  9. end
  10. last_end = e+1
  11. s, e, cap = pString:find(fpat, last_end)
  12. end
  13. if last_end <= #pString then
  14. cap = pString:sub(last_end)
  15. table.insert(Table, cap)
  16. end
  17. return Table
  18. end
  19.  
  20. --function addslashes(s)
  21. -- return (string.gsub(s, "([^'])'", "%1\'"))
  22. --end
  23.  
  24. -- Backslash-escape special characters:
  25. function addslashes(s)
  26. -- Double quote, single quote, and backslash (per the PHP
  27. -- manual):
  28. s = string.gsub(s, "(['\"\\])", "\\%1")
  29. -- The null character gets turned into a pair of printing
  30. -- characters by PHP addslashes. Let's do the same:
  31. return (string.gsub(s, "%z", "\\0"))
  32. end -- addslashes
  33.  
  34. function onInput(s, type, obj)
  35. freeswitch.consoleLog("info", "Callback with type " .. type .. "\n");
  36. if (type == "event") then
  37. local event = obj:getHeader("Speech-Type");
  38. if (event == "begin-speaking") then
  39. freeswitch.consoleLog("info", "\n" .. obj:serialize() .. "\n");
  40. -- Return break on begin-speaking events to stop playback of the fire or tts.
  41. return "break";
  42. end
  43. if (event == "detected-speech") then
  44. freeswitch.consoleLog("info", "\n" .. obj:serialize() .. "\n");
  45. if (obj:getBody()) then
  46. -- Pause speech detection (this is on auto but pausing it just in case)
  47. session:execute("detect_speech", "pause");
  48. -- Get XML string for possible failed call processing
  49. result = obj:getBody()
  50.  
  51. -- When a body is returned starting with "Completion" the call has failed
  52. no1, no2 = string.find(result, "Completion")
  53.  
  54. if (no2 ~= nil) then
  55. session:streamFile("/usr/local/freeswitch/sounds/en/us/callie/directory/8000/call-failed.wav");
  56. else
  57. -- Add escape slashes to returned XML
  58. slashResult = addslashes(result)
  59. xmlResult = split(slashResult,"<")
  60. response = split(xmlResult[4],">")
  61. freeswitch.consoleLog("warning", "\n Response:\n\n" .. response[2] .. "\n\n");
  62. if (response[2] == "yes") then
  63. session:streamFile("/usr/local/freeswitch/sounds/you-said-yes.wav");
  64. else if (response[2] == "no") then
  65. session:streamFile("/usr/local/freeswitch/sounds/you-said-no.wav");
  66. end
  67. end
  68. end
  69. end
  70. end
  71. end
  72. end
  73.  
  74.  
  75.  
  76. -- Answer the call.
  77. session:answer();
  78.  
  79. -- Register the input callback
  80. session:setInputCallback("onInput");
  81.  
  82. -- Sleep a little bit to give media time to be fully up.
  83. session:sleep(500);
  84.  
  85. --Play sound file
  86. session:streamFile("/usr/local/freeswitch/sounds/say-yes-or-no.wav")
  87.  
  88. -- Start the detect_speech app. This attaches the bug to fire events
  89. -- session:execute("set", "fire_asr_events=1")
  90. session:execute("detect_speech", "unimrcp nuance/boolean nuance5-mrcp2");
  91.  
  92. -- This sleep is what blocks till the detected-speech event. This has to give you enough time to speak plus get the results.
  93. session:sleep(3000);
  94. session:sleep(3000);
  95.  
  96. --Stop speech detect
  97. session:execute("detect_speech", "stop")
Add Comment
Please, Sign In to add comment