【问题标题】:Twilio & NodeJS: can't find and delete Media instances/resourcesTwilio 和 Node JS:找不到和删除媒体实例/资源
【发布时间】:2015-01-28 04:41:34
【问题描述】:

我正在使用 Twilio 的 NodeJS 模块和 API 发送带有附加图像的 MMS 消息(来自远程 URL),并且我想在发送消息后立即删除在 Twilio 服务器上创建的媒体实例。

我的消息正确发送,在回调中,我试图 1) 列出当前消息的媒体实例,然后 2) 遍历这些实例并删除。问题是从 API 返回的当前消息的 mediaList 数组始终为空。

这是我的代码:

twilio_client.messages.create({
    body: "Thanks for taking a photo. Here it is!",
    to: req.query.From,
    from: TWILIO_SHORTCODE,
    mediaUrl: photo_URL,
    statusCallback: STATUS_CALLBACK_URL
    }, function(error, message) {
        if (!error) {

            twilio_client.messages(message.sid).media.list(function(err, data) {
                console.log(data);
                // The correct object comes back as 'data' here per the API
                // but the mediaList array is empty
            }

            console.log('Message sent via Twilio.');
            res.status(200).send('');
        } else {
            console.log('Could not send message via Twilio: ');
            console.log(error);
            res.status(500).send('');
        }
});

【问题讨论】:

    标签: arrays node.js rest callback twilio


    【解决方案1】:

    因此,事实证明,在我尝试获取媒体列表时尝试获取媒体列表是行不通的,因为媒体实例还不存在。

    我在 statusCallback 上运行了一个单独的小应用程序(我通过上面代码中的常量 STATUS_CALLBACK_URL 提供了一个 URL),直到现在,我只是检查了我尝试向用户发送 MMS 的消息是否没有由 Twilio 正确处理,并通过 SMS 提醒用户出现问题。因此,我在同一个应用程序中添加了一个检查,以查看消息是否实际“发送”给用户,然后检查并删除与该消息关联的媒体实例,它工作正常。这是我的代码:

    // issue message to user if there's a problem with Twilio getting the photo
    if (req.body.SmsStatus === 'undelivered' || req.body.SmsStatus === 'failed') {
        twilio_client.messages.create({
        body: "We're sorry, but we couldn't process your photo. Please try again.",
        to: req.body.To,
        from: TWILIO_SHORTCODE
        }, function(error, message) {
            if (!error) {
                console.log('Processing error message sent via Twilio.');
                res.send(200,'');
            } else {
                console.log('Could not send processing error message via Twilio: ' + error);
                res.send(500);
            }
        });
    }
    
    // delete media instance from Twilio servers
    if (req.body.SmsStatus === 'sent') {
        twilio_client.messages(req.body.MessageSid).media.list(function(err, data) {
            if (data.media_list.length > 0) {
                data.media_list.forEach(function(mediaElement) {
                  twilio_client.media(mediaElement.sid).delete;
                  console.log("Twilio media instance deleted"); 
                });
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-31
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      相关资源
      最近更新 更多