tawn33y / whatsapp-cloud-api

A Node.js library for creating bots and sending/receiving messages using the Whatsapp Cloud API.
https://www.npmjs.com/package/whatsapp-cloud-api
GNU General Public License v3.0
187 stars 51 forks source link

ERR_UNHANDLED_REJECTION when trying to receive the Message from the bot #46

Closed Mizar-Contasti closed 1 year ago

Mizar-Contasti commented 1 year ago

Hi Everyone, currently Im trying to receive "Received your text message!" that the bot sends when receives a message.

Right now I'm receiving start message when I start the server. But when trying to send the message, I got this output.

image

Right now My code is this ->

` const { createBot } = require('whatsapp-cloud-api');

(async () => { try { // replace the values below const from = ""; const token = ""; const to = ""; // const to = "524426581099"; const webhookVerifyToken = "";

// Create a bot that can send messages
const bot = createBot(from, token);

// Send text message
const result = await bot.sendText(to, "Hello world");

// Start express server to listen for incoming messages
// NOTE: See below under `Documentation/Tutorial` to learn how
// you can verify the webhook URL and make the server publicly available
await bot.startExpressServer({
  webhookVerifyToken,
});

// Listen to ALL incoming messages
// NOTE: remember to always run: await bot.startExpressServer() first
bot.on("message", async (msg) => {
  console.log(msg);

  if (msg.type === "text") {
    await bot.sendText(msg.from, "Received your text message!");
  } else if (msg.type === "image") {
    await bot.sendText(msg.from, "Received your image!");
  }
});

} catch (err) { console.log(err); } })(); `

tawn33y commented 1 year ago

To view the full error, try wrapping the sendText in a try catch, i.e:

bot.on("message", async (msg) => {
  console.log(msg);

  try {
    if (msg.type === "text") {
      await bot.sendText(msg.from, "Received your text message!");
    } else if (msg.type === "image") {
      await bot.sendText(msg.from, "Received your image!");
    }
  } catch (err) {
    console.log(err);
  }
});
tawn33y commented 1 year ago

@Mizar-Contasti, can I mark this as completed?