当前位置: 代码迷 >> java >> twilio:无法获得转录
  详细解决方案

twilio:无法获得转录

热度:22   发布时间:2023-07-31 11:08:18.0

我正在使用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文件设置正确,因为我可以成功播放录音。 如果任何人可以提供任何帮助,我真的很感激。 谢谢!

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

(强调我的)

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

与录制完成完成的action参数不同,因为转录是由Twilio基础设施内的不同进程处理的。

来自你的record.setTranscribeCallback("http://ip/handle-recording"); 看来你的大部分位都是正确的。 如果对/handle-recording POST请求被路由到实际处理转录的类 (您的第二个片段) ,我不知道为什么转录本将为null。


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

自动调查的使用<Record>动词的setTranscribeCallback()方法( )