简体   繁体   中英

Twilio MMS with Node.js - Sending multiple media files

The Twilio docs indicate that each MMS can have up to 10 media file attachments but the Node.js example only shows 1 attachment:

var client = require('twilio')(accountSid, authToken);
 client.messages.create({ 
    to: "+16518675309", 
    from: "+14158141829", 
    body: "Hey Jenny! Good luck on the bar exam!", 
    mediaUrl: "http://farm2.static.flickr.com/1075/1404618563_3ed9a44a3a.jpg"  
 }, function(err, message) { 
    console.log(message.sid); 
 });

I would have expected the mediaUrl property to be an Array of strings not a single value. How do you indicate more than one media attachment ?

Twilio developer evangelist here.

You can absolutely supply an array here if you have more than one mediaUrl to send. The code:

var client = require('twilio')(accountSid, authToken);
 client.messages.create({ 
    to: "+16518675309", 
    from: "+14158141829", 
    body: "Hey Jenny! Good luck on the bar exam!", 
    mediaUrl: ["http://farm2.static.flickr.com/1075/1404618563_3ed9a44a3a.jpg", "http://another-image.com/image.jpg"]
 }, function(err, message) { 
    console.log(message.sid); 
 });

Will work just as well as using a string if you only have one image.

Under the hood, the Node.js helper library uses the querystring module to turn parameters into a form encoded POST body. So, when you use an array of mediaUrls, like in my example, it will get turned into:

'to=%2B16518675309&from=%2B14158141829&body=Hey%20Jenny!%20Good%20luck%20on%20the%20bar%20exam!& mediaUrl =http%3A%2F%2Ffarm2.static.flickr.com%2F1075%2F1404618563_3ed9a44a3a.jpg& mediaUrl =http%3A%2F%2Fanother-image.com%2Fimage.jpg'

Whilst that's a bit hard to read, I've highlighted the two instances of mediaUrl in the parameters. Twilio interprets the two values as a list of URLs and delivers your two images in the MMS.

Let me know if this helps!

edit

If you want to do this with TwiML, see the example below or in the documentation :

const MessagingResponse = require('twilio').twiml.MessagingResponse;

const response = new MessagingResponse();
const message = response.message();
message.body('Hello Jenny');
message.media('https://demo.twilio.com/owl.png');
message.media('https://demo.twilio.com/bunny.png');

console.log(response.toString());

To add more media, just keep using the media method on the response, you can add up to 10 media items this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM