freeswitch如何播放语音验证码(esl方式)

1.首先注册网关,gateway,具体不再说

2.在conf\dialplan\default.xml,添加


 <!--  test code start -->
    <extension name="test_say1">
      <condition field="destination_number" expression="^7777$"> 
       <action application="answer"></action>  
       <action application="playback" data="ivr/ivr-contact_system_administrator.wav"></action>
        <action application="sleep" data="100"></action>
 <action application="log" data=""></action>
     
        <action application="say" data="en name_spelled intered 3456789"></action>
<action application="say" data="en name_spelled intered 3456789"></action>
 
 <action application="say" data="en name_spelled intered 3456789"></action>
        <action application="playback" data="voicemail/vm-goodbye.wav"></action>
       <!--  <action application="hangup"></action>-->
  <action application="transfer" data="">
      </condition>
    </extension> 



3.java esl登场



public class EventEslInboundTest {
 private static final Logger log      = LoggerFactory.getLogger(EventEslInboundTest.class);
private static String       host     = "127.0.0.1";
private static int          port     = 8021;
private static String       password = "ClueCon";
 
 
public static void inBand(){

 
        final Client client = new Client();
        try
        {
            client.connect( host, port, password, 10 );
            
          //  client.connect( "10.0.0.85", port, password, 10 );
        }
        catch ( InboundConnectionFailure e )
        {
            log.error( "Connect failed", e );
            return;
        }        
 
        //注册事件处理程序
        client.addEventListener( new IEslEventListener()
        {
            public void eventReceived( EslEvent event )
            {
            //System.out.println("Event received [{}]" + event.getEventHeaders());
                //记录接听次数和时间
                if(event.getEventName().equals("CHANNEL_ANSWER")){
                  //your code here
                }
                if(event.getEventName().equals("CHANNEL_BRIDGE")){
                  //your code here
                }
 
                if(event.getEventName().equals("CHANNEL_DESTROY")){
                      //
                }
 
                if(event.getEventName().equals("CHANNEL_HANGUP_COMPLETE")){
                    //挂断
                 
                }
 
            }
            public void backgroundJobResultReceived( EslEvent event )
            {
                String uuid= event.getEventHeaders().get("Job-UUID");
            log.info("Background job result received+:"+event.getEventName()+"/"+event.getEventHeaders());// +"/"+JoinString(event.getEventHeaders())+"/"+JoinString(event.getEventBodyLines()));
           
            }
 
        } );
 
        //定义事件日志输出格式,但是java esl 目前只支持plain格式    ,http://wiki.freeswitch.org/wiki/Event_Socket
        //2012-12-25 19:20:30  [ main:426 ] - [ ERROR ]  java.lang.IllegalStateException: Only 'plain' event format is supported at present
        client.setEventSubscriptions("plain", "all");
       /* String uuid= client.sendAsyncApiCommand("originate", "user/1002 &park()");
       System.out.println("============++=="+uuid); 
        Execute e = new Execute(client, uuid);
       try {
//exe.transfer("1002");
      e.say("en", "1234", "name_spelled", "intered");
    } catch (ExecuteException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    } */


//        client.setEventSubscriptions( "xml", "CHANNEL_DESTROY" );
//        client.setEventSubscriptions( "json", "CHANNEL_CREATE");
 
        //不区分大小写,对事件进行过滤,只关注需要的事件
      /*  client.addEventFilter("Event-Name", "CHANNEL_CREATE");
        client.addEventFilter("Event-Name", "CHANNEL_HANGUP");
        client.addEventFilter("Event-Name", "CHANNEL_HANGUP_COMPLETE");
        client.addEventFilter("Event-Name", "CHANNEL_ANSWER");
        client.addEventFilter("Event-Name", "CHANNEL_DESTROY");
        client.addEventFilter("Event-Name", "CHANNEL_BRIDGE");
        client.addEventFilter("Event-Name", "CHANNEL_ORIGINATE");
        client.addEventFilter("Event-Name", "CHANNEL_OUTGOING");*/
 
// 可以取消前面的订阅定义,重新定义新的事件订阅
//        client.cancelEventSubscriptions();
//        client.setEventSubscriptions( "plain", "all" );
//        client.addEventFilter( "Event-Name", "heartbeat" );
//        client.addEventFilter( "Event-Name", "channel_create" );
//        client.addEventFilter( "Event-Name", "background_job" );
 
       EslMessage response = client.sendSyncApiCommand("originate", "{ignore_early_media=true}sofia/gateway/mygatew/xxx  7777 xml default 'zz'");
    //  String AsyncApiCommand =  client.sendAsyncApiCommand("originate", "sofia/gateway/mygatew/015674873480 12396 xml default 'zz'");
    //  System.out.println("AsyncApiCommand="+AsyncApiCommand); 
      System.out.println("aaaaaaaaaaaaaaaaa");
       // EslMessage response = client.sendSyncApiCommand("status", "");
         Pattern Regex = Pattern.compile("OK\\s([\\w\\-]+)",
                Pattern.CANON_EQ); 
 
 
        System.out.println("body_string"+response.toString());
 
        for ( String bodyLine : response.getBodyLines() )
        {
            System.out.println(bodyLine);
        Matcher RegexMatcher = Regex.matcher(bodyLine);
            if(RegexMatcher.find()){
                String uuuid=RegexMatcher.group(1);
                if(!uuuid.isEmpty()){
                    //your codes here
                System.out.println("++++++++++++"+uuuid);
                 
                }
 
            }
        }
        
        
       // client.close();
}


public static void main(String[] args) {
inBand();
}
}

       EslMessage response = client.sendSyncApiCommand("originate", "{ignore_early_media=true}sofia/gateway/mygatew/xxx  7777 xml default 'zz'");


xxx指的是你的手机号码,注意ignore_early_media=true}必须添加,否则语音不完整,也可以在conf\dialplan\default.xml,      <action application="answer"></action> answer改为  wait_for_answer,二者选其一

4.测试完毕


  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值