Posts tagged with webhooks

I am trying to return a response generated from an API call via whatsapp through the use of the Whatsapp business cloud API and a webhook. I can log the message and it is correct however when I return it by using the webhook it does not then get sent in Whatsapp. Here is my code:

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",     url: API_URL,     data: {       id: phone_number_id,       question: msg_body,       email: "N/A",       conversation: [],       save: true,       resolved: "N/A",       ads: 0,     },     headers: {       "Content-Type": "application/json",       "x-api-key": process.env.API_KEY,     },   })     .then(apiResponse => {       if (apiResponse.status !== 200) {         throw new Error(`Request failed with status ${apiResponse.status}`);       }       return apiResponse.data;     })     .then(responseData => {       console.log(responseData);       res.status(200).json({         message: responseData.answer,       });     })     .catch(error => {       console.error(error);       res.status(500).json({         message: "An error occurred while chatting.",       });     });     }       }  }); 

As you can see I am console logging the responseData and it is showing me a good response. But as mentioned when I return it it does not then get sent to the whatsapp phone number that it received the initial post request from.

I am trying to achieve this whole process by using Meta For Developers and their docs for the Whatsapp business Cloud API but can't figure it out.

I am building an application using the WhatsApp Business Cloud API. Essentially I want to know if it's possible to build an application that collects all the messages from the day and downloads it. Specifically the media attached to it using the API. As far as I know you can use webhooks to get incoming messages. But in order to do this the application has to be run forever, and my cousin has a problem with this as it could cause problems to run an application forever vs just run it once a day. I'm able to send messages using the API and a python wrapper but that's much more simple than what I'm trying to do. Aditionally, there is an option of using selenium but that's not really an automated solution to what we're trying to do because everytime selenium executes the browser we'd need to log in using the QR code. If anyone has any idea if this is possible (or not possible) I'd be very appreciative!

from heyoo import WhatsApp import logging import requests from dotenv import load_dotenv from flask import Flask, request, make_response app = Flask(__name__) token = 'EAAVk5rqOCsABAAcPPrZC6GnlZAJuykdFIQd4DhkuRVNeGntfFOU5jaK4jG2yCZBS6i7kFQGk3kRvDP0fExBXRsFyqWUqfVVJsSxeQdcA9XHWpRuUsnnuwqLcZAQpwTiuoZCXv4ixCcHYlPEe6NGHupCalHvWw9NQRoZAVnegU5ZCBvX6eO9E8vyum1lQ2SSt7OuUpdpIkmkyBK8tiEL8rpGwM8RrqZA3A10ZD' messenger = WhatsApp(token,  phone_number_id='100398242927044') messenger.send_message('Hey its JJ ', '1xxxxxxxx') logging.basicConfig(     level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s" ) print("hello") @app.route("/", methods=["GET", "POST"]) def hook(): 

//some code that isn't working yet

I'm new to all of this stuff so I don't really know how to set up a webhook either but my cousin who's business I'm building this app for doesn't ideally want to use a webhook.

I have deployed my webhook and connected my WABA. Once I send an image to this business account. It did not return the media id from the response. Actually, the JSON returned to me like this:

{     "entry": [         {             "changes": [                 {                     "field": "messages",                     "value": {                         "contacts": [                             {                                 "profile": {                                     "name": "XXXXXXX"                                 }                             }                         ],                         "messages": [                             {                                 "from": "XXXXXXXXXX",                                 "id": "wamid.aisjdoiajsodiajsodasd\u003d",                                 "timestamp": "1657527108",                                 "type": "image"                             }                         ],                         "metadata": {}                     }                 }             ],             "id": "124071984791824"         }     ],     "object": "whatsapp_business_account" } 

Or should I try the Whatsapp On-premises API? https://developers.facebook.com/docs/whatsapp/on-premises/reference/media/media-id

I'm trying to get WhatsApp's Cloud API working. I managed to set up Meta Business Account and configure a WhatsApp app. Then I configured a webhook and subscribed to messages event (see the following screenshot).

I then managed to send a message via the API using the following request:

curl -i -X POST `   https://graph.facebook.com/v13.0/103690452403982/messages `   -H 'Authorization: Bearer MY_TOKEN' `   -H 'Content-Type: application/json' `   -d '{ \"messaging_product\": \"whatsapp\", \"to\": \"MY_NUMBER\", \"type\": \"template\", \"template\": { \"name\": \"hello_world\", \"language\": { \"code\": \"en_US\" } } }' 

I received the message and it came through the webhook as well. If I reply to that message, it comes through the webhook too.

The problem

However, when I send a message to the associated number from a different WhatsApp number (not via the API) it is received but the webhook is not called.

I suspect some incorrect configuration on my side. When I text the number from a different phone, the chat has a notice about E2E encryption - something which is not present in a chat window of the API-sent message. I assume that E2E-encrypted messages cannot be passed to the webhook because only the recipients should be able to decrypt the message.

Any ideas what I might be missing?

Thank you in advance