0

I have a webhook set up on the WhatsApp API Cloud with my own phone number. I worked on an application in NodeJS Express and TypeScript to be able to listen to WhatsApp events on my server.

The project is a chatbot. When I receive a message, I trigger certain actions to respond depending on the type of message I receive. Once this is validated, the response to the received message is processed, and then the response is sent through a POST to the WhatsApp API (graph.facebook.com/v16.0/${ID_CELL}/messages).

According to the official WhatsApp documentation, it should receive a status 200 for each message it sends.

If we send a webhook request to your endpoint and your server responds with an HTTP status code that is not 200, or if we can't deliver the webhook for another reason, we will continue trying less frequently until the request is successful, for up to 7 days.

Reference

I tried this, I even return a status 200 from my server's side when an error occurs, as Andres Lopez mentions in this response where he shares a similar experience.Reference

The same is mentioned by Shajeel Afzal. Reference: Reference

But despite trying that, I'm still receiving repeated messages, and I don't know what else to do to avoid this. Please, if someone helps me, sometimes it behaves in a way that it sends a repeated response after some time. I don't know what else to try. I've been researching how to close the webhook or have more control to avoid this, but I can't find anything.

export const receiveMessage = async(req: Request, res: Response, next: NextFunction) => {

      let input = req.body.input[0];
      let changes = input.changes[0];
      let value = changes.value;
      let messageObject = value.messages;

      try{

          const messages: Message = messageObject[0];
          const from = message_object[0]?.from;

          //Type of message
          let typeMessage = messages.type;

          // First we record the log
          await registerEvents(entry, 200, "Receiving Webhook Whatsapp Api Cloud", null);

          // we validate that the message is not undefined
          if (messageObject === undefined) {
              console.log('no message found');
              res.sendStatus(200);
          }

          // we validate the type of messaging

          if (messageType === "text") {
              sendStatusMessage("Writing...", from);
              const data = await textProcess(messages, from);
              SendMessageWhatsApp(data);
              res.sendStatus(200);
          }

          if (messageType === "audio") {
              console.log('is audio');
              const data = await audioProcess(messages, from);
              SendMessageWhatsApp(data);
              res.sendStatus(200);
          }

          if (messageType === "button") {
              const data = await buttonProcess(messages, from);
              SendMessageWhatsApp(data);
              res.sendStatus(200);
          }

          if (messageType === "interactive") {
              const data = await interactiveProcess(messages, from);
              SendMessageWhatsApp(data);
              res.sendStatus(200);
          }



      } catch (error) {

          await registerEvents(input, 500, "Webhook Whatsapp Api Cloud Error", error)
          // Avoid lopp webhook
          console.log('An internal error occurred on the server but 200 should always be sent');
          res.sendStatus(200)
      }

}

I make requests to the API using HTTPS from Node.

function SendMessageWhatsApp(message: any) {
      constant data = JSON.stringify(message);

      constant options = {
          host: "graph.facebook.com",
          path: `/v16.0/${ID_CELL}/messages`,
          method: "POST",
          body: data,
          headers: {
              "Content-type": "application/json",
              Authorization: `Bearer ${TOKEN_CLOUD_API_WSP}`
          }
      };
      const req = https.request(options, res => {
          res.on("data", d => {
              process.stdout.write(d);
          });
      });

      req.on("error", error => {
          console.error(error);
          supabase.from('records').insert([
              {
                  message: error,
                  status: 500,
                  task: "Error sending message to Whatsapp",
                  error: null
              }
          ]);
      });

      req.write(data);
      req.end();
}

Thank you very much in advance for your help. I'll be very grateful.

Izlia
  • 235
  • 1
  • 2
  • 18

0 Answers0