Twilio: Problems getting a transcription

I am creating a service using twilio where the user calls and leaves a message. The service then reads the transcription of this message to do some unnecessary processing.

The problem I am running into is that I cannot successfully extract this transcription from anywhere. I'm pretty sure about this 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, which starts the entry to be called, and then after the user completes, passes it to another class for processing. (My IP address has been deleted)

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 handles the transcription. For right now, I just repeat whatever the user said 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 also tried to get the transcription using sid, but whenever I try this approach I get an error because the TranscriptionSid I am 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 configured correctly because I can play the recording successfully. If anyone can provide any help, I would really appreciate it. Thank!

+3


source to share


1 answer


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

From the TwiML <Record>

docs
(emphasis mine):

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

This is different from the parameter action

that ends when the recording is complete , because the transcription is being processed by another process in the Twilio framework.



From your line record.setTranscribeCallback("http://ip/handle-recording");

, it looks like most of the bits are wired correctly. If POST

requested /handle-recording

, redirected to your class that actually handles the transcription (your second snippet), I don't know why the transcript will be empty.


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

The official Java Tutorial for Automated Polling uses the <Record>

verb method setTranscribeCallback()

( source ).

0


source







All Articles