Question

Receive and Download Images/Video from Incoming Media Twilio Messages with Node.js

  • 11 October 2023
  • 4 replies
  • 139 views

I'm trying to programmatically receive and download MMS images/videos from Twlio using a webhook in Zapier

I would like to download images and place them in Google Drive

I’m building a chatbot in Voiceflow to be deployed using Twilio

Everything works except downloading the image and video

I’m using ChatGPT to help with the code but I keep getting errors

Here is the code ChatGPT gave me:

// Input Data from previous step
const mediaUrl = inputData.MediaUrl0;  // Assuming MediaUrl0 is the field name for the media URL from Twilio's POST request

// Function to download image from Twilio
async function downloadImage(url) {
  const response = await fetch(url);
  const buffer = await response.arrayBuffer();
  const base64Image = Buffer.from(buffer).toString('base64');
  return base64Image;
}

// Call the function to download image
downloadImage(mediaUrl)
  .then(base64Image => {
    callback(null, {base64Image});
  })
  .catch(error => {
    callback(error);
  });


Any help from someone more experienced would be great!

 


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

4 replies

Userlevel 6
Badge +8

Hi @UXD Academy!

Welcome to the Zapier Community!

I haven’t yet reviewed your code, but I can see right off the bat that you are not actually passing the input data into your Code step.

In the Code Step action, under the section labeled ‘Input Data’, be sure to add MediaURL10 to the left box and to pass the actual URL from your web hook into the right box. This way, your code step “const mediaUrl” will hold the value of the URL.

Thank you for your help Todd,

Am I doing it correctly?

 

 

Userlevel 6
Badge +8

And actually, after reviewing the code, I see a few other problems with it.

You actually shouldn’t need to “download” the image prior to your Google Drive step at all. You just need to be sure you have a publicly accessible URL for it, then you should be able to use an “Upload File in Google Drive” action and use the URL as the “File”.

If you’re convinced the code step is absolutely necessary and since there could be a lot going on here depending on how your web hook is retrieving data and the specific ways that data needs to be manipulated to make it work with Google Drive, this may be a job best handled by speaking to / workin with a Certified Zapier Expert.

Userlevel 6
Badge +8

@UXD Academy You added the link to the webhook, not the data received from the webhook test data. It should look something like this: