我正在使用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()方法。


TwiML <Record> docs(重点是我):


“ transcribeCallback”属性…允许您指定一个URL,转录完成后Twilio将向该URL发出异步POST请求。此…请求将包含…“ TranscriptionSid”。


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

从您的record.setTranscribeCallback("http://ip/handle-recording");行,您似乎大多数位都已正确连接。如果对POST/handle-recording请求被路由到实际处理转录的类(您的第二个片段),我不知道为什么转录本为null。




[T]这里没有我可以找到的有关如何使用它的明确示例。


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

关于java - twilio:难以获得转录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31476238/

10-11 04:30