Converting Raw Webhook Data in Zapier

I’m having trouble with Zapier’s webhook feature. My Slack bot sends files to Zapier, but it only catches raw data. I want to turn this raw data back into a JPEG file so I can upload it to Google Drive.

Here’s what my bot code looks like:

bot.on('file_shared', (msg) => {
  const fileUrl = msg.file.private_url;
  
  const options = {
    method: 'GET',
    url: fileUrl,
    headers: {
      Authorization: `Bearer ${bot.config.token}`,
    }
  };

  request(options, (error, response, content) => {
    console.log('File download status:', response.statusCode);
  }).pipe(request.post(zapierWebhook));
});

I tried using this Node.js code in Zapier to recreate the JPEG:

const https = require('https');
const fs = require('fs');

https.get(inputData, (response) => {
  response.pipe(fs.createWriteStream('image.jpg'));
});
result = {status: 'success'};

But I’m not sure how to make Zapier use the recreated file. Any ideas on how to solve this?

have u tried using zapier’s built-in file handling instead? u could use the ‘Store Binary File’ action to save the raw data as a file, then use that in the next step to upload to gdrive. might be easier than custom code. just a thought!

I encountered a similar issue when working with webhook data in Zapier. Instead of trying to recreate the file within Zapier, consider modifying your bot code to send the file as a multipart/form-data request. This way, Zapier can directly recognize it as a file.

You can adjust your request options like this:

const formData = {
  file: {
    value: request(options),
    options: {
      filename: 'image.jpg',
      contentType: 'image/jpeg'
    }
  }
};

request.post({url: zapierWebhook, formData: formData});

This approach should allow Zapier to receive the file intact, making it easier to process and upload to Google Drive without additional conversion steps.

I’ve dealt with similar webhook challenges in Zapier before. One approach that worked well for me was using Base64 encoding. Instead of sending the raw file data, you could convert it to a Base64 string in your bot code before sending it to Zapier. Then in Zapier, you can decode the Base64 string back into a file.

Here’s how you might modify your bot code:

const base64 = response.body.toString('base64');
request.post(zapierWebhook, { json: { fileData: base64, fileName: 'image.jpg' } });

In Zapier, you can use a Code step to decode the Base64 and create a file:

const buffer = Buffer.from(inputData.fileData, 'base64');
output = {file: buffer, fileName: inputData.fileName};

This method has worked reliably for me in handling file transfers via webhooks. It maintains the file integrity and gives you more control over the process.