1
votes

I am playing with Twilio Voice API and have a callback URL to my application whenever an answering machine/human is detected. The mp3 I am trying to play works fine through Twiml Runtime Bin. However, when application responds with the Twiml, twilio always recites the URL instead of playing the music.

public void makeOutBoundCall()
{
    Twilio.init(TWILIO_ACCOUNT_SID, TWILIO_ACCOUNT_TOKEN);

    CallCreator callCreator = new CallCreator(TWILIO_ACCOUNT_SID, new PhoneNumber(TO_NUMBER),
            new PhoneNumber(TWILIO_NUMBER),
            URI.create(APP_URL + TwilioVoiceApplication.TWILIO_AMD_CALLBACL_URL))
                    .setMachineDetection("DetectMessageEnd")
                    .setMachineDetectionTimeout(20000);
    callCreator.create();
} 


@RequestMapping(method = RequestMethod.POST)
public String amdCallback(@RequestParam Map<String, String> queryMap) throws Exception
{
    // Default behavior hangup the call
    Hangup hangup = new Hangup();
    VoiceResponse voiceResponse = new VoiceResponse.Builder().hangup(hangup)
            .build();
    if (queryMap.containsKey("AnsweredBy"))
    {
        if (queryMap.get("AnsweredBy")
                .equals("human"))
        {
            voiceResponse = new VoiceResponse.Builder()
                    .say(new Say.Builder("Hello Monkey").build())
                    .play(new Play.Builder("http://demo.twilio.com/hellomonkey/monkey.mp3")
                            .build())
                    .build();
        }
    }
    return voiceResponse.toString();
}
1
The problem was with the response header, adding "content-type: text/xml", fixed the problemChinmay
Glad you got it sorted! I actually recommend you add this as an answer below and accept it. It makes it easier for others to see the solution if they have the same problem.philnash

1 Answers

0
votes
@RequestMapping(method = RequestMethod.POST, produces = "application/xml")
public ResponseEntity<String> amdCallback(@RequestParam Map<String, String> queryMap)
            throws Exception
    {
    // Default behavior hangup the call
    Hangup hangup = new Hangup();
    VoiceResponse voiceResponse = new VoiceResponse.Builder().hangup(hangup)
            .build();
    if (queryMap.containsKey("AnsweredBy"))
    {
        if (queryMap.get("AnsweredBy")
                .equals("human"))
        {
            voiceResponse = new VoiceResponse.Builder()
                    .say(new Say.Builder("Hello Monkey").build())
                    .play(new Play.Builder("http://demo.twilio.com/hellomonkey/monkey.mp3")
                            .build())
                    .build();
        }
    }
    HttpHeaders headers = new HttpHeaders();
    //Add the content-type:text/html in response header
    headers.add(HttpHeaders.CONTENT_TYPE, "text/xml");

    ResponseEntity<String> response = ResponseEntity.ok()
            .headers(headers)
            .body(voiceResponse.toXml());
    return response;
}