twilio:无法获得转录

时间:2015-07-17 12:49:23

标签: java twilio twiml

我正在使用twilio创建一个服务,用户在其中调用并留下消息。然后服务读取该消息的转录以进行一些不相关的处理。

我遇到的问题是我无法从任何地方成功检索到该转录。我几乎肯定这是因为我不明白如何使用setTranscribeCallback()方法,但是没有明确的如何使用它的例子我可以找到。

这是我的班级,它开始录音,然后在用户完成后,将其传递给另一个班级进行处理。 (我的服务器的IP地址已删除)

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());
}

这是实际处理转录的类。就目前而言,我只是想让用户说再重复一遍。

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());
}

我也尝试使用它的sid获取转录,但每当我尝试这种方法时,我都会收到错误,因为我使用的TranscriptionSid是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);

}

我知道我的web.xml文件设置正确,因为我可以成功播放录制内容。如果任何人可以提供任何帮助,我真的很感激。谢谢!

1 个答案:

答案 0 :(得分:0)

  

我不明白如何使用setTranscribeCallback()方法。

来自TwiML <Record> docs (强调我的)

  

'transcribeCallback'属性...允许您指定Twilio在转录完成时向其发出异步POST请求的URL 。此...请求将包含...'TranscriptionSid'。

这与action参数不同,该参数在录制完成时完成 ,因为转录由Twilio基础架构内的其他进程处理。

从您的record.setTranscribeCallback("http://ip/handle-recording");行看起来,您的大部分内容都已正确连线。如果对POST的{​​{1}}请求被路由到实际处理转录的 (您的第二个代码段),我不知道为什么成绩单将为空。

  

[T]这里没有我能找到的如何使用它的明确例子。

自动调查的official Java tutorial使用/handle-recording动词的<Record>方法source