Сначала используйте модуль twilio-node (npm install twilio
). После того, как у вас есть это место, вы можете просто получить доступ к телу запроса веб-камеры, как если бы вы запросили тело req.body
.
Как показано в twilio's docs, структура так:
{
MessageSid: String, //ID pertaining to this message
AccountSid: String, //ID pertaining to your twilio account
From: String, //phone number of sender
To: String, //recipients phone number, you in this case
Body: String, //Message body
NumMedia: Number, //number of attached media
//these values only appear when media is present(NumMedia.length > 0)
MediaContentType: [String] //type of content in SMS
MediaUrl: [String] //URL to download the media
}
Вы можете сделать что-то вроде этого, используя Twilio модули, caolan/модуль асинхронной и популярный request/request модуль:
var twilio = require('twilio'),
fs = require('fs'),
async = require('async'),
request = require('request');
app.post('/mms', function(req, res) {
var options = { url: 'https://subtle-gradient-188.herokuapp.com/twiml' };
if (!twilio.validateExpressrequire(req, 'YOUR_TWILIO_AUTH_TOKEN', options)) return res.status(401).send("Bad key!");
if(!req.body.hasOwnProperty('MediaUrl')) return res.send("Missing media...");
var media = req.body.MediaUrl;
//download all media
async.map(media, download, function(err, filenames) {
var resp = new twilio.TwimlResponse();
if(err) {
resp.say("Problem occured");
console.log(err);
}
else resp.say('Files recieved: ' + filenames.join(', '));
res.type('text/xml');
res.send(resp.toString());
});
});
//download a single url to a unique filename
function download(url, cb) {
var name = Date.now() + url.split('/').pop();
request
.get(url)
.on('error', cb)
.on('end', function() {
cb(null, name);
})
.pipe(fs.createWriteStream(name));
}
Я заметил, что twilio возвращает объект мультимедиа не как массив, а скорее как именованное свойство, например MediaContentType0 MediaUrl0 – MonkeyBonkey
Некоторые люди делают это, но вы можете обычно обращаться к этому свойству так же, как к массиву, пока ключ - это номер, а не строка. – tsturzl