Posts tagged with node.js

I'm having issues trying to download media files from the WhatsApp Business API. Following along with their documentation (https://developers.facebook.com/docs/whatsapp/cloud-api/reference/media#download-media) they provide a cURL command that is successful in downloading a media file when used - however, the same request (I think) when done using NodeJS' fetch returns text/html responses with vague error wording and a 200 status code.

# Successful cURL: curl "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=281274211304477&ext=1705672414&hash=ATtH6AOGFu0RqEpENicHUg8HCUkVfwGzfrHVCdiE7J8AUA" --header "Authorization: Bearer ..." 
// Successful cURL from child_process.exec: exec(   `curl --location "${mediaURL}" --header "Authorization: Bearer ..."` ); 
// Unsuccessful fetch: fetch(   "https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=281274211304477&ext=1705672414&hash=ATtH6AOGFu0RqEpENicHUg8HCUkVfwGzfrHVCdiE7J8AUA",   { headers: { Authorization: "Bearer ..." } } ); 

Suggestion from WhatsApp Cloud API Receiving Images from Users Error is also unsuccessful, unfortunately:

// Also unsuccessful fetch: fetch("https://lookaside.fbsbx.com/whatsapp_business/attachments/?mid=281274211304477&ext=1705672414&hash=ATtH6AOGFu0RqEpENicHUg8HCUkVfwGzfrHVCdiE7J8AUA", {   headers: {     Authorization: "Bearer ...",     "User-Agent": "curl/7.64.1",   }, }) 

Note: The lookaside.fbsbx.com URLs are all retrieved successfully from the WhatsApp Business APIs.

Per the documentation, a 404 error should occur if the link has expired, however, this isn't the case - and the access token hasn't expired either. Looking through the Community Forums, specifically https://developers.facebook.com/community/threads/1367000994138329/?join_id=f25971b6b4f9cc4, many conversations suggest that the User-Agent header should be spoofed - yet this doesn't seem to work either, although tweaking the User-Agent header within Postman yields varying results.

Successful in Postman:

Unsuccessful when User-Agent is adjusted in Postman:

Unsuccessful when User-Agent is removed in Postman:

Any suggestions would be greatly appreciated. I was unable to use the Meta for Developers' "Report a bug" forms as support appears to be unavailable for WhatsApp Business APIs.

Also seen posts: 1, 2 on StackOverflow; 1, 2, 3, 4, 5, 6 on Meta Support Forums.

The goal is to take data we have and create google ads' Customer List with it automatically, using NodeJS, specifically this:

https://developers.google.com/google-ads/api/docs/remarketing/audience-segments/customer-match/get-started#create-customer

I looked into documentation for google-ads-api, googleapi and a couple of other libraries but found nothing helpful. I'm also pretty new with Google Ads API, so I might be missing something obvious.

There seems to be no good examples or documentation on this (except the PHP, C# etc ones in the above link), so if it's not possible in NodeJS I would love to have a concrete confirmation and/or explanation as to why.

I am a developer. working on a project where I need to integrate my site with WhatsApp API to send messages. Moreover, I want my various clients can send messages to their respective audiences with their own numbers. How can I achieve that?

I have checked only BSPs can create a WhatsApp Business platform on WhatsApp cloud API. BSPs can provide such a solution. However, there is no mention of how to integrate or embed the form with the website so clients can fill and register themselves.

I am creating a whatsapp business api app that replies to whatsapp messages with a messages I have received from my API. However, it is triggerring multiple times after the initial messages has been sent.

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);        axios({             method: "POST",             url: END_POINT_URL             headers: {               "Content-Type": "application/json",               Authorization: TOKEN,             data: {               to: from,                type: "text",               text: {                 preview_url: false,                 body: responseData.answer,                },               messaging_product: "whatsapp",               recipient_type: "individual"             },           })           .then(() => {             // Confirm the message has been sent             res.status(200).end();           })           .catch((error) => {             console.error("Error sending WhatsApp message: ", error);             res.status(500).end();           });     })     .catch(error => {       console.error(error);       res.status(500).json({         message: "An error occurred while chatting.",       });     });     }       }  }); 

I have tried debugging but to no avail, sometimes the messages are sent at super random periods too

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.