【问题标题】:twilio: Having trouble getting a Transcriptiontwilio:无法获得转录
【发布时间】:2015-10-07 04:47:02
【问题描述】:

我正在使用 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 为空。

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

【问题讨论】:

    标签: java twilio twilio-twiml


    【解决方案1】:

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

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

    “transcribeCallback”属性...允许您指定一个 URL,Twilio 在转录完成时将向其发出异步 POST 请求。这个…请求将包含…'TranscriptionSid'。

    这与action 参数不同,后者在录制完成后结束,因为转录由 Twilio 基础架构中的不同进程处理。

    从您的record.setTranscribeCallback("http://ip/handle-recording"); 行看来,您的大部分位都已正确连接。如果POST/handle-recording 的请求被路由到您的实际处理转录的类 (您的第二个sn-p),我不知道为什么转录会为空。


    [T]我找不到关于如何使用它的明确示例。

    用于自动调查的official Java tutorial 使用<Record> 动词的setTranscribeCallback() 方法(source)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-21
      相关资源
      最近更新 更多