0

I'm currently working on a project that need to send whatsapp's messages, for this work i'm using my favorite node.js framework Express.js.

Honestly i have never done such kind of work it's my first time dealing with whatsapp cloud api , i did read the entire documentation about how to have everything done with express but when i try to apply what i learned it turns into a nightmare your help will be very important.

Here is the example configuration of my code

    axios({
        method: 'POST',
        url: 'https://graph.facebook.com/v14.0/Phone Number ID/messages',
        data: {
            message_product: 'whatsapp',
            to: 'client number',
            type: "template",
            template: {
                name: "hello_world",
                language: {
                    code: "en_US"
                }
            },
        },
        headers: {
            "Authorization": "Bearer  + token",
            "Content-Type": "application/json"
        }
    }).then(result => {
        console.log(result)
    }).catch(err => {
        console.log(err.message)
    })

And here is the error that i'm getting

read ECONNRESET
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

2 Answers2

0

I am working in same idea of project and it worked for me

var options = {
method: 'POST',
  url: 'https://graph.facebook.com/v15.0/PHONE_NUMBER_ID/messages',
  headers: {
    Authorization: 'Bearer TOKEN',
    'Content-Type': 'application/json'
  },
  data: {
    messaging_product: 'whatsapp',
    to: '{number}',
    type: 'template',
    template: {name: 'hello_world', language: {code: 'en_US'}}
  }
};

axios.request(options).then(function (response) {
  console.log(response.data);
}).catch(function (error) {
  console.error(error);
});
aLx
  • 16
  • 1
0

Here is an example of a function I created to send requests to the /messages endpoint using NodeJs and Express:

export async function sendWhatsAppResponse(parameters = {}, responseText = ""){
  //create payload to be posted
  const whatsappMessageURL = `https://graph.facebook.com/v16.0/${parameters.phoneNumberId}/messages`;
  const responsePayload = {
    messaging_product: 'whatsapp',
    recipientType: 'individual',
    type: 'text',
    to: parameters.userPhoneNumber,
    text: {
      preview_url: false,
      body: responseText
    }
  }
  const header = {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${envVariables.WAToken}`
  }

  //send response
  try {
    axios.post(whatsappMessageURL,  responsePayload, {
      headers: header
    })  
  } catch(e) {
    console.log(e);
  }
}

You need to ensure that your access token has the correct permissions and that the phone number ID you are inserting into your URL contains the correct value found on your whatsapp business platform > whatsapp > Getting started. The ECONNRSET error is a connection issue with the API you are trying to reach, see this StackOverflow post.

user16217248
  • 3,119
  • 19
  • 19
  • 37
FredylanGM
  • 11
  • 2