MMS with file from local machine

215 views
Skip to first unread message

shane....@gmail.com

unread,
Mar 31, 2023, 12:08:11 AM3/31/23
to Twilio Functions
I know this is not a twilio function question but years ago when I was doing alot of twilio functions there were alot of smart people that had answers to many of my questions.  I am trying to use twilio sdk in node.js to send mms messages with jpgs from my local machine.  I could not find much about it so I asked chatgpt and they provided sample code of base64 encoding the jpg and including it in the mediaurl of the client.message.create.  But it does not work.  I even asked them for a reference and they provided: https://www.twilio.com/docs/sms/send-messages#send-an-mms-message-with-base64-encoded-media.  But this url does not work. I can send all day long with images available by web url but I need to send local images that are behind a TIC FW (so nrogk etc will not work).  If someone knows of a better google group to post this to would appreciate suggestion. 

thanks

shane....@gmail.com

unread,
Mar 31, 2023, 12:10:06 AM3/31/23
to Twilio Functions
BTW here is the code suggested by chatgpt that I cannot get to work:


const accountSid = 'your_account_sid';
const authToken = 'your_auth_token';
const client = require('twilio')(accountSid, authToken);

const fs = require('fs');

// Read the image file into a buffer
const imageBuffer = fs.readFileSync('/path/to/image.jpg');

// Encode the image buffer as base64
const base64Image = Buffer.from(imageBuffer).toString('base64');

// Create a data URI from the base64-encoded image
const dataUri = `data:image/jpeg;base64,${base64Image}`;

// Send an MMS message with the data URI as the media
client.messages
  .create({
    body: 'Check out this image!',
    to: 'recipient_phone_number',
    from: 'your_twilio_phone_number',
    mediaUrl: dataUri,
  })
  .then(message => console.log(message.sid))
  .catch(err => console.error(err));

vre...@gmail.com

unread,
Mar 31, 2023, 7:54:57 AM3/31/23
to Twilio Functions
You can't use a local URL or plain image data. Twilio needs a publicly exposed URL in order to make the request to it and get the image and attach it to the message.

You could use something like Assets (if you want to use a twilio service) to upload the files and that will give you an accessible URL
Reply all
Reply to author
Forward
0 new messages