当前位置: 首页 > 工具软件 > UniMRCP > 使用案例 >

freeswitch使用unimrcp实时语音转写lua脚本调用

长孙燕七
2023-12-01

freeswitch对接unimrcp不作多说,本文记录下lua脚本将语音识别文本存入数据库,防止自己忘了,代码还待优化,仅作测试用

session:answer();

local caller_number = argv[1];     --主叫号码
local called_number = argv[2];     --被叫号码


-- freeswitch是前面配置的名字
local dbh = freeswitch.Dbh("freeswitch","root","xxxx");

-- 打印日志
freeswitch.consoleLog("NOTICE","start connect DB...\r\n");

--如果连接不正确,则退出脚本
assert(dbh:connected());



function onInput(s,type,obj)
        freeswitch.consoleLog("INFO","callback--------type: '" .. type .. "'\n")
        if(type=="dtmf") then
                freeswitch.consoleLog("INFO","dtmf dot need!!!");
                return "break"
        end
        if(type=="event") then
                local channid=obj:getHeader("Channel-Identifier")
                if(channid ~=nil) then
                  freeswitch.consoleLog("INFO","-----????------->Channel-Identifier:" .. channid ..  "\n");
                end
                local eventh=obj:getHeader("Speech-Type")
                if(eventh=="begin-speaking") then
                        freeswitch.consoleLog("INFO","begin-speaking--------obj:" .. obj:serialize() .. "\n");
                        return ""
                end
				if(eventh=="detected-speech") then
                        s:execute("detect_speech","pause")
                        freeswitch.consoleLog("INFO","detected-speech--------obj:" .. obj:serialize() ..  "\n");
                        if(obj:getBody()) then
                                local speech_output=obj:getBody()
      
                                if(speech_output ~= nul) then
                                        freeswitch.consoleLog("INFO","detected-speech-------results:" .. speech_output.."\n");
                                        freeswitch.consoleLog("INFO","<-------------------------->\n");
                                        local callid=s:get_uuid()
                                        if(string.find(speech_output,"Completion-Cause") == nil) then
                                                local sql=string.format(
                                                "INSERT INTO speech_record(call_id,speech_text,role_flag) VALUES('%s',%s,'%s');"
                                                ,callid,"'"..speech_output.."'",caller_number);
                                                assert(dbh:connected())
                                                dbh:query(sql)
                                                if( 0 == dbh:affected_rows() ) then
                                                        freeswitch.consoleLog("ERROR", "CDRB SQL FAILED:" .. sql )
                                                else
                                                        freeswitch.consoleLog("DEBUG", "CDR new +1,callid:" .. callid)
                                                end
                                        end
                                        s:execute("detect_speech","resume")
                                else
                                        s:speak("对不起,无法识别")
                                end
                        end
                end
        else
                return "break"
        end

end

local dest = "{ignore_early_media=false,bridge_early_media=true,origination_caller_id_number=caller,origination_caller_id_name=8888,call_timeout=120}user/"..called_number.."";
session2 = freeswitch.Session(dest);

session:setVariable("fire_asr_events", "true");

session:setInputCallback('onInput','')

local callId=session:getVariable("uuid");
-- hello为语法文件名称
session:execute("detect_speech", "unimrcp {sid="..callId..",caller_number="..caller_number..",called_number="..called_number.."}hello default")

--桥接
freeswitch.bridge(session, session2);

使用odbc方式连接mysql数据库,推荐使用pg数据库,odbc方式坑多,建表如speech_record,存储什么数据看自己需求,我这就存了callId,主叫号和转写文本

 类似资料: