0

I'm trying to use the whatsapp API to send and receive messages. I managed to understand most of the features of this API, but I still can't identify how to list the received messages.

I'm using the glitch to register the webhook and the address is registered in the API panel. In the log, I identify the sending and receiving of the message. I would like to know which endpoint I use to receive the message.

For submission I use this endpoint in my NodeJs project:

      axios({
        method: "POST",
        url:
          "https://graph.facebook.com/" +
          phone_number_id +
          "/messages?access_token=" +
          token_wts,
        data: {
          messaging_product: "whatsapp",
          to: from,
          text: { body: "Ack: " + msg_body },
        },
        headers: { "Content-Type": "application/json" },
      });

And to list the messages of the clients, how do I do it?

In the glitch, this is my app.js:

// Accepts POST requests at /webhook endpoint
app.post("/webhook", (req, res) => {
  // Parse the request body from the POST
  let body = req.body;

  // Check the Incoming webhook message
  console.log(JSON.stringify(req.body, null, 2));

  // info on WhatsApp text message payload: https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples#text-messages
  if (req.body.object) {
    if (
      req.body.entry &&
      req.body.entry[0].changes &&
      req.body.entry[0].changes[0] &&
      req.body.entry[0].changes[0].value.messages &&
      req.body.entry[0].changes[0].value.messages[0]
    ) {
      let phone_number_id =
        req.body.entry[0].changes[0].value.metadata.phone_number_id;
      let from = req.body.entry[0].changes[0].value.messages[0].from; // extract the phone number from the webhook payload
      let msg_body = req.body.entry[0].changes[0].value.messages[0].text.body; // extract the message text from the webhook payload
      axios({
        method: "POST", // Required, HTTP method, a string, e.g. POST, GET
        url:
          "https://graph.facebook.com/v12.0/" +
          phone_number_id +
          "/messages?access_token=" +
          token,
        data: {
          messaging_product: "whatsapp",
          to: from,
          text: { body: "Ack: " + msg_body },
        },
        headers: { "Content-Type": "application/json" },
      });
    }
    res.sendStatus(200);
  } else {
    // Return a '404 Not Found' if event is not from a WhatsApp API
    res.sendStatus(404);
  }
});

// Accepts GET requests at the /webhook endpoint. You need this URL to setup webhook initially.
// info on verification request payload: https://developers.facebook.com/docs/graph-api/webhooks/getting-started#verification-requests
app.get("/webhook", (req, res) => {
  /**
   * UPDATE YOUR VERIFY TOKEN
   *This will be the Verify Token value when you set up webhook
   **/
  const verify_token = process.env.VERIFY_TOKEN;

  // Parse params from the webhook verification request
  let mode = req.query["hub.mode"];
  let token = req.query["hub.verify_token"];
  let challenge = req.query["hub.challenge"];

  // Check if a token and mode were sent
  if (mode && token) {
    // Check the mode and token sent are correct
    if (mode === "subscribe" && token === "xxxx") {
      // Respond with 200 OK and challenge token from the request
      console.log("WEBHOOK_VERIFIED");
      res.status(200).send(challenge);
    } else {
      // Responds with '403 Forbidden' if verify tokens do not match
      res.sendStatus(403);
    }
  }
});

I appreciate anyone who can help me analyze!

E. Biagi
  • 452
  • 3
  • 12
  • Whatsapp is taking care of displaying the messages, so i'm not sure that you can get the message history. Why do you need them ? – Bruno Hanss Jun 22 '23 at 14:38
  • You can receive every message on the webhook and maybe you could save them in some sort of database if you need to reuse them later. – Bruno Hanss Jun 22 '23 at 14:40
  • I'm creating a chat integrated with whatsapp in a web system. The idea is for employees to send and receive messages through the platform. That's why the need to bring the messages in the same way as in whatsapp web. @BrunoHanss – E. Biagi Jun 22 '23 at 15:20
  • Explained the things in [answer1](https://stackoverflow.com/a/73009444/8987128) and [answer2](https://stackoverflow.com/a/72997818/8987128). – turivishal Jun 22 '23 at 15:24
  • I checked that this example exists: https://developers.facebook.com/docs/whatsapp/cloud-api/webhooks/payload-examples – E. Biagi Jun 22 '23 at 15:54
  • but I still don't understand. What would be the communication logic between the client and the company in the case of receiving messages. – E. Biagi Jun 22 '23 at 15:54

0 Answers0