我正在使用Twilio的NodeJS模块& API发送带有图像的MMS消息(来自远程URL),我希望在发送消息后立即删除在Twilio服务器上创建的媒体实例。
我的消息发送正确,在回调中,我试图列出当前消息的媒体实例,然后循环遍历这些实例并删除。问题是,当前消息从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('');
}
});发布于 2015-02-04 17:42:07
因此,在我试图获取媒体列表的时候,尝试获取媒体列表并不有效,因为媒体实例还不存在。
我在statusCallback上运行了一个单独的小应用程序(我通过上面代码STATUS_CALLBACK_URL中的一个常量提供了一个URL ),直到现在,它只是检查一下,看看我尝试给用户的一条消息是否没有被Twilio正确地处理,并通过短信提醒用户注意问题。因此,我在同一应用程序中添加了一个检查,查看消息是否真的“发送”给用户,然后检查并删除与该消息相关的媒体实例,它运行良好。这是我的密码:
// 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");
});
}
});
}https://stackoverflow.com/questions/28184753
复制相似问题