Search code examples
freeswitch

How to send and Receive DTMF in Freeswitch ESL client


I am working with Freeswitch ESL client, I worked on originating call and establishing connection between two applications and making them communicate with each other. I have tried playing sound at one end and recording at the other, It is working fine now my requirement is to send dtmf at one end receiving at other end, I have tried following

private void sendDtmf(Channel channel) {
    SendMsg senDtmf = new SendMsg();
    senDtmf.addCallCommand("execute");
    senDtmf.addExecuteAppName("send_dtmf");
    senDtmf.addExecuteAppArg("2174");
    EslMessage response = sendSyncMultiLineCommand( channel,senDtmf.getMsgLines() );
    if (response.getHeaderValue(Name.REPLY_TEXT).startsWith("+OK")) {
        System.out.println(this.getClass().getName() + " >> DTMF Send");
        System.out.println("Resp: " + response.toString());
        log.info(this.getClass().getName() + " >> DTMF Send");
    } else {
        log.error(this.getClass().getName() + " >> DTMF failed :"
                + response.getHeaderValue(Name.REPLY_TEXT));
        System.out.println(this.getClass().getName() + " >> DTMF failed :"
                + response.getHeaderValue(Name.REPLY_TEXT));
    }
}

private void getdtmf( Channel channel, VoxtaMsg voxmsg) 
{ 
    SendMsg getDtmf= new SendMsg(); 
    getDtmf.addCallCommand( "execute" );
    getDtmf.addExecuteAppName( "play_and_get_digits" );
    getDtmf.addExecuteAppArg("4 4 3 7000 # /tmp/sounds/test.wav /tmp/sounds/test1.wav dtmf \\d+");
    EslMessage response = sendSyncMultiLineCommand( channel,getDtmf.getMsgLines() ); 
    if ( response.getHeaderValue( Name.REPLY_TEXT).startsWith( "+OK" ) )
    {
        System.out.println(this.getClass().getName()+" >> DTMF Received");
        log.info( this.getClass().getName()+" >> DTMF Received" ); 
    } 
    else
    {
        log.error( this.getClass().getName() + " >> DTMF failed: [{}}" +
        response.getHeaderValue( Name.REPLY_TEXT ));
        System.out.println(this.getClass().getName() + " >> DTMF failed: [{}}" +
                response.getHeaderValue( Name.REPLY_TEXT ) ); 
        log.debug("----------------------done-------------------------");
    }
 }

but could not get any result, Do I need to configure any thing in dial plans, or my total approach is wrong?


Solution

  • You need to attach an event listener and wait for the "CHANNEL_EXECUTE_COMPLETE" command. Two things are needed. 1) Subscribe to the events on the connection 2) Add an event listener and get back the value of the response variable which you are setting to be dtmf.

    Assume that the client object is the connection.

    client.connect(...);
    client.setEventSubscriptions("plain", "CHANNEL_EXECUTE_COMPLETE");
    client.addEventListener(new IEslEventListener() {
            @Override
            public void eventReceived(EslEvent event) {
                Map<String, String> vars = event.getEventHeaders();
    
                if (event.getEventName().equals("CHANNEL_EXECUTE_COMPLETE") && vars.get("Application").equals("play_and_get_digits")) {
                     PlayAndGetDigitsCallback(vars.get("dtmf"));
                }
            }
    
            @Override
            public void backgroundJobResultReceived(EslEvent event) {
    
            }
    });
    
    private void PlayAndGetDigitsCallback(String digits) {
        System.out.println("Digits received: " + digits);
    }