FreeSWITCH技巧:如何向通话的另一方号码发送dtmf?

来源:互联网 发布:女生发型 知乎 编辑:程序博客网 时间:2024/05/02 00:30

注:这里的文章都是本人的日常总结,请尊重下个人的劳动成果,转载的童鞋请注明出处,谢谢。如您转载的文章发生格式错乱等问题而影响阅读,可与本人联系,无偿提供本文的markdown源代码。联系邮箱:jizhask@gmail.com.

需求描述

在实际的应用中,经常有这样的需求,比如一个号码拨打外线,需要送dtmf出去(如拨打10086,根据提示按1按2等),在这种情况下,如果处理呢?

需求分析

其实该问题可以分解为下面两个子问题: 
1、如何根据一方号码,获取与之通话的另一方号码? 比如66903 拨打 66904,那如何根据66903来获取到66904呢? 
解决方法: 
通过执行FreeSWITC的API命令,可以获取另一条信道的信息,如下:

 show channels like 66903@ as xml

在该命令的返回值中抽取sent_callee_num,即为另一方的号码。 
下面是具体的实现方法:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. --根据一方号码获取另一条腿的UUID  
  2. function getOtherLegUUID(api,num)  
  3.     local uuid;  
  4.     local res=api:executeString("show channels like "..num.."@ as xml")  
  5.   
  6.     ----判断获取的channel信息是否为空  
  7.     --fslog("debug","show channels res",res);  
  8.     --如果channel信息不为空  
  9.     if res and string.len(res) >0 then  
  10.         local _,_, sendCalleeNum = string.find(res,"<sent_callee_num>(.-)<%/sent_callee_num>");  
  11.   
  12.         if sendCalleeNum then  
  13.             uuid = getUUIDByNum(api,sendCalleeNum);  
  14.         end  
  15.     end  
  16.   
  17.     return uuid;  
  18. end  

2、如何向另一方号码发送dtmf? 
解决方法: 
该问题比较容易处理,只需要调用uuid_send_dtmf方法即可。前提条件是获取到那个信道的UUID。

具体代码

下面是完整的脚本代码,仅供参考:

[plain] view plaincopy在CODE上查看代码片派生到我的代码片
  1. --/****************************************************/  
  2. --脚本名称:send_dtmf_toleg.lua  
  3. --脚本参数:  
  4. --          argv[1]  当前通话的号码  
  5. --          argv[2]  待发送的dtmf信息  
  6. --脚本功能:  
  7. --          根据当前通话的号码,查出另一条腿的uuid,然后向该uuid发送dtmf  
  8. --作者:     默言 2013-11-24  
  9. --/****************************************************/  
  10.   
  11. --输出freeeswitch日志  
  12. function fslog(loglevel,logtitle, logbody)  
  13.     loglevel = loglevel or "debug";  
  14.     logtitle = logtitle or "";  
  15.     logbody = logbody or "";  
  16.     freeswitch.consoleLog(loglevel, "\n" .. argv[0] .. " : " .. logtitle .. "{" .. logbody .. "}\n");  
  17. end  
  18.   
  19. --获取号码对应的uuid  
  20. --参数:num, 待查询号码  
  21. --返回:号码对应的通道的uuid  
  22. function getUUIDByNum(api, num)  
  23.     local uuid;  
  24.   
  25.     local res=api:executeString("show channels like "..num.."@ as xml")  
  26.     if res then  
  27.         --使用正则表达式从字符串中截取uuid  
  28.         --'-'代表最短匹配  
  29.         _,_,uuid = string.find(res,"<uuid>(.-)<%/uuid>")  
  30.     end  
  31.   
  32.     fslog("debug","getUUID:" .. num, uuid);  
  33.     return uuid;  
  34. end  
  35.   
  36. --根据一方号码获取另一条腿的UUID  
  37. function getOtherLegUUID(api,num)  
  38.     local uuid;  
  39.     local res=api:executeString("show channels like "..num.."@ as xml")  
  40.   
  41.     ----判断获取的channel信息是否为空  
  42.     --fslog("debug","show channels res",res);  
  43.     --如果channel信息不为空  
  44.     if res and string.len(res) >0 then  
  45.         local _,_, sendCalleeNum = string.find(res,"<sent_callee_num>(.-)<%/sent_callee_num>");  
  46.   
  47.         if sendCalleeNum then  
  48.             uuid = getUUIDByNum(api,sendCalleeNum);  
  49.         end  
  50.     end  
  51.   
  52.     return uuid;  
  53. end  
  54.   
  55.   
  56. do  
  57.     local legNum=argv[1];  
  58.     --待发送的dtmf信息  
  59.     local dtmfs = argv[2];  
  60.   
  61.     fslog("debug","start to send dtmf, legnum",legNum);  
  62.     fslog("debug","need send dtmfs", dtmfs);  
  63.   
  64.     if legNum and tonumber(legNum) then  
  65.         api=freeswitch.API();  
  66.         uuid=getOtherLegUUID(api, legNum);  
  67.         if uuid then  
  68.             local cmd = "uuid_send_dtmf " .. uuid .. " " .. dtmfs;  
  69.             fslog("debug","send_dtmf cmd", cmd);  
  70.             api:executeString(cmd);  
  71.         else  
  72.             fslog("warning","cannot get another leg uuid","");  
  73.         end  
  74.     else  
  75.         fslog("warning","invalid parameters","");  
  76.     end  
  77. end
0 0