Access transcriptionText from twilio

524 views Asked by At

I want to access the transcription text that has been generated by transcribe in my Twilio account under transcriptions as I want to compare user recorded response as text

twiml.say('Hi there! Please speak your response after the beep,-Get ready!')
        .record({
            transcribe:true,
            timeout:5,
            maxLength:30,
            transcribeCallback:'/recording',
            action:'/recording'

        });
app.post('/recording', (request,response) => {
if(transcriptionText=='yes'){

    twiml.say('thank you for positive response');
}
  response.type('text/xml');
  response.send(twiml.toString());

});
1

There are 1 answers

23
philnash On

Twilio developer evangelist here.

When using transcription with <Record>, once the recording is complete the call will continue on to make a request to the action attribute synchronously. Whatever you return from the action attribute URL will control the call.

The actual transcription, however, takes a bit more time and when you get a webhook to the transcribeCallback URL it is done asynchronously, outside the context of the call. So, returning TwiML will not affect the call at all.

You will get the transcription text by inspecting the body of the request. There are plenty of parameters sent to the transcribeCallback, but the one you are looking for is the TranscriptionText. In your Node.js app, which looks like Express to me, you can get hold of it by calling request.body.TranscriptionText.

If you do want to affect the call when you receive the transcribe callback you will need to use the REST API to modify the call and redirect it to some new TwiML.

Let me know if that helps at all.

[edit]

From the comments I can see you are trying to drive a part of the call from a spoken response. The transcribeCallback URL isn't called immediately as the transcription needs to be done, so you need an action URL that you can send your caller to while you wait.

So, adjust your recording route to have different endpoints for action and transcribeCallback:

app.post("/voice", (request, response) => {
  var twiml = new twilio.TwimlResponse();
  twiml.say('Hi there! Please speak your response after the beep,-Get ready!')
    .record({
      transcribe:true,
      timeout:5,
      maxLength:30,
      transcribeCallback:'/transcribe',
      action:'/recording'
  });
  response.type('text/xml');
  response.send(twiml.toString());
})

Then your recording endpoint will need to keep the user waiting while Twilio transcribes the text.

app.post('/recording', (request,response) => {
  var twiml = new twilio.TwimlResponse();
  // A message for the user
  twiml.say('Please wait while we transcribe your answer.');
  twiml.pause();
  // Then redirect around again while we wait
  twiml.redirect('/recording');
  response.type('text/xml');
  response.send(twiml.toString());
});

Finally, when you get the transcribe callback you can figure out the course from the transcribed text somehow and then redirect the live call into a new endpoint that carries on the call with the new information.

app.post('/transcribe', (request, response) => {
  var text = request.body.TranscriptionText;
  var callSid = require.body.CallSid;

  // Do something with the text
  var courseId = getCourseFromText(text);

  var accountSid = '{{ account_sid }}'; // Your Account SID from www.twilio.com/console
  var authToken = '{{ auth_token }}';   // Your Auth Token from www.twilio.com/console
  var client = new twilio.RestClient(accountSid, authToken);

  // Redirect the call
  client.calls(callSid).update({
    url: '/course?courseId=' + courseId,
    method: 'POST'
  }, (err, res) => {
    response.sendStatus(200);
  })
});