【发布时间】: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