Webhook get attachment with nodejs

You can download the attachment sent to your device using webhook.

This function will only working on device with all feature package.

Special note : any autoreply feature won't work if you are using webhook.

const fs = require("fs");
const express = require("express");
const app = express();
app.use(express.json());
app.use(express.static("public"));

app.post("/webhook", async function (req, res) {
  console.log(req.body);
  const response = await fetch(req.body.url);
  const result = await response.arrayBuffer();
  fs.writeFile(`./public/file.${req.body.extension}`, Buffer.from(result), (err) => {
    if (err) console.log(err);
  });
  res.end();
});

app.listen(3000, function (err) {
  if (err) console.log(err);
  console.log("Server listening on PORT", 3000);
});

If your attachment have message with it, you can find it in req.body.url.

The attachment will be downloaded and saved in the public folder.

To save somewhere else, determine the path where the attachment should be saved.

Note: The attachment will follow file limitation rules. if you are receiving an attachment outside file limitation rules, you will not receive it on your webhook.

Related knowledge

See more
Made with in Indonesia