Webhook get submission with nodejs

You can get the submission data using webhook with javascript.

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

async function sendFonnte(data) {
  const url = "https://api.fonnte.com/send";

  const customHeaders = {
    "Content-Type": "application/json",
    Authorization: TOKEN ,
  };

  const response = await fetch(url, {
    method: "POST",
    headers: customHeaders,
    body: JSON.stringify(data),
  });
  console.log(await response.json());
}

app.post("/submission", function (req, res) {
  console.log(req.body);
  const data = {
      target: "082227097005",
      message: "webhook submission working great!",
    sendFonnte(data);
  } 
  res.end();
});

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

Available parameter

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.

Webhook reply message with nodejs

Using webhook will enable your device to respond on incoming message with dynamic response.

We will use express for easier usage.

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

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

async function sendFonnte(data) {
  const url = "https://api.fonnte.com/send";

  const customHeaders = {
    "Content-Type": "application/json",
    Authorization: TOKEN ,
  };

  const response = await fetch(url, {
    method: "POST",
    headers: customHeaders,
    body: JSON.stringify(data),
  });
  console.log(await response.json());
}

app.post("/webhook", function (req, res) {
  console.log(req.body);
  if (req.body.message == "test") {
    const data = {
      target: req.body.sender,
      message: "working great!",
    };
    sendFonnte(data);
  } else {
    const data = {
      target: req.body.sender,
      message: "this is default reply from fonnte",
    };
    sendFonnte(data);
  }
  res.end();
});

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

Available parameter

Replying using attachment is only available on devices with a package of super, advanced or ultra.