3

I'm creating a service using twilio where the user calls in and leaves a message. Then the service reads the transcription of that message to do some irrelevant processing.

The problem I'm having is that I can't successfully retrieve that transcription from anywhere. I'm almost certain it's because I don't understand how to use the setTranscribeCallback() method, but there are no clear examples of how to use it that I could find.

This is my class that starts the recording for the call, and then after the user is finished, passes it off to the other class for processing. (My server's ip address removed)

public class TwilioVoice extends HttpServlet {

private static final long serialVersionUID = 1L;

public void service(HttpServletRequest request, HttpServletResponse response)
        throws IOException {

    TwiMLResponse twiml = new TwiMLResponse();

    Record record = new Record();


    record.setMaxLength(20);
    record.setTranscribe(true);
    record.setTranscribeCallback("http://ip/handle-recording");
    record.setAction("http://ip/handle-recording");


    try {
        twiml.append(new Say("Please state your address"));
        twiml.append(record);
    } catch (TwiMLException e) {
        e.printStackTrace();
    }

    response.setContentType("application/xml");
    response.getWriter().print(twiml.toXML());
}

This is the class that actually processes the transcription. For right now, I just have whatever the user said repeat back to them.

public void service(HttpServletRequest request, HttpServletResponse response)
        throws IOException {


    String text = request.getParameter("TranscriptionText");
    TwiMLResponse twiml = new TwiMLResponse();


    try {
        twiml.append(new Say(text));
        twiml.append(new Say("Goodbye"));

    } catch (TwiMLException e) {
        e.printStackTrace();
    }

    response.setContentType("application/xml");
    response.getWriter().print(twiml.toXML());
}

I've also tried to get the transcription using its sid, but whenever I try this approach, I get an error because the TranscriptionSid i'm using is null.

public void service(HttpServletRequest request, HttpServletResponse response)
        throws IOException {

    String transcriptionSid = request.getParameter("TranscriptionSid");
    TwiMLResponse twiml = new TwiMLResponse();
    Transcription transcript = null;
    String text;

    try {
        transcript = getTranscript(transcriptionSid );
    } catch (TwilioRestException e1) {
        e1.printStackTrace();
    }

    if (transcript != null) {
        text = transcript.getTranscriptionText();
    } else {
        text = "NO GO!";
    }

    try {
        twiml.append(new Say(text));
        twiml.append(new Say("Goodbye"));

    } catch (TwiMLException e) {
        e.printStackTrace();
    }

    response.setContentType("application/xml");
    response.getWriter().print(twiml.toXML());
}

private Transcription getTranscript(String sid) throws TwilioRestException {
    TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

    return client.getAccount().getTranscription(sid);

}

I know my web.xml file is set up right, because I can successfully play back the recording. If any one can provide any help, I'd really appreciate it. Thanks!

rsmith6
  • 81
  • 5

1 Answers1

0

I don't understand how to use the setTranscribeCallback() method.

From the TwiML <Record> docs (emphasis mine):

The 'transcribeCallback' attribute…allows you to specify a URL to which Twilio will make an asynchronous POST request when the transcription is complete. This…request will contain…'TranscriptionSid'.

This is different from the action parameter which finishes when the recording is complete because the transcription is handled by a different process within Twilio's infrastructure.

From your record.setTranscribeCallback("http://ip/handle-recording"); line, it looks like you have most of the bits wired up correctly. If POST requests to /handle-recording are routed to your class that actually processes the transcription (your second snippet), I don't know why the transcript would be null.


[T]here are no clear examples of how to use it that I could find.

The official Java tutorial for automated surveys uses the <Record> verb's setTranscribeCallback() method (source).