3

我有一个功能,可以让你录制,如果你不满意,可以删除你的录音。当您按 * 时,您可以删除您最后的录音。

我的录音代码是:

exports.handler = function(context, event, callback) {
    const twiml = new Twilio.twiml.VoiceResponse();
    twiml.say('Welcome! Please record your announcement after the beep!');
    twiml.say('After your recording, hang up if you are satisfied or press star to delete the newest recording.');
    twiml.record({
      finishOnKey: '*',
      recordingStatusCallback: '/delete',
      action: '/delete'
    });

 callback(null, twiml);
};

删除最后一段录音的代码(/delete):

exports.handler = function(context, event, callback) {

  const twiml = new Twilio.twiml.VoiceResponse();
  const id = event.RecordingSid;
  console.log(id);
  console.log(typeof id);
  const accountSid = 'AC1775ae53d952710bb1b4e47ea19c009c';
  const authToken = {my_token};
  const client = require('twilio')(accountSid, authToken);

client.recordings(id)
      .remove(function(err, data) {
            if (err) {
                twiml.say("there is an error");
                console.log(err.status);
                throw err.message;
            } else {
                console.log("deleted successfully.");
                twiml.say("deleted successfully");
            }
      })
     .then(recording => console.log(recording.sid))
     .done();
twiml.say('Your announcement is deleted');

  callback(null, twiml);
};

现在,我得到了正确的 id 并且没有错误,但是当我检查录音日志时,录音仍然存在。这是为什么?我需要给它一些时间让它删除吗?
来自的日志console.log(recording.sid)也没有显示,我不确定为什么它没有执行应该删除记录的整个代码块。
提前致谢!

4

2 回答 2

1

Twilio 开发人员布道者在这里。

当您将 和 设置为recordingStatusCallback时,Twilio 将尝试对 进行两次调用;录制完成时一次,用户响应时一次。/deleteaction<Gather>/delete<Gather>

旨在与recordingStatusCallback调用本身异步进行工作,因此我将专注于action

录制完成后(用户按 *),Twilio 将向该<Record> action属性发出请求。在该请求中,作为请求正文的一部分,您将收到RecordingURL(即使录制状态尚未完成)。如果需要删除记录,您应该将其传递RecordingURL<Gather>(或将其存储在会话中)的结果,并使用它来执行请求或提取记录 SID 以与 Twilio 节点模块一起使用。DELETE

这些帮助有用?

于 2018-05-29T09:32:26.410 回答
1

我想到了!这是 /delete 的正确代码:
我更改的主要内容是将回调移动到 remove(function(...)) 内,因此该函数仅在 HTTP 请求完成后返回。

exports.handler = function(context, event, callback) {


  const twiml = new Twilio.twiml.VoiceResponse();
  const id = event.RecordingSid;
  var client = context.getTwilioClient();
  console.log(client);
  const URL = event.RecordingUrl;

    client.recordings(id)
    .remove(function(err, data) {
                if (err) {
                    twiml.say("there is an error");
                    console.log(err.status);
                    throw err.message;
                } else {
                    console.log("deleted successfully.");
                    twiml.say("deleted successfully");
                    callback(null, twiml);
                }
    })
    .then(recording => console.log(recording.sid))
    .done();
    twiml.say('Your announcement is deleted');


};
于 2018-05-31T18:48:48.093 回答