pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
15.49k stars 3.69k forks source link

Problem " application stops" #441

Closed aleluis777 closed 3 years ago

aleluis777 commented 3 years ago

Bug description

application stops after sending a message from the api. as shown

Reproduction steps

Install npm package

  1. npm install whatsapp-web.js
  2. node test.js 3.The start example works with msg.reply but when I try to send the message like this below, the ANDROID APP stops (problem in the app)

image image

...

WhatsApp

Library

ngekoding commented 3 years ago

I was had the same problem before implementing isRegisteredUser validation. But after use the validation everything working fine.

Another think I do is updating Whatsapp on Phone, maybe this related to the problem.

aleluis777 commented 3 years ago

I was had the same problem before implementing isRegistered validation. But after use the validation everything working fine.

Another think I do is updating Whatsapp on Phone, maybe this related to the problem.

what do you mean by isregister validation? Could you give me more information? I am using the example and it works correctly. the record returns true

ngekoding commented 3 years ago

There is a function to check if the number is already registered with whatsapp or not, so before sending the message we can check the number (chatId).

Here my code look like:

// Send message
app.post('/send-message', [
  body('number').notEmpty(),
  body('message').notEmpty(),
], async (req, res) => {
  const errors = validationResult(req).formatWith(({
    msg
  }) => {
    return msg;
  });

  if (!errors.isEmpty()) {
    return res.status(422).json({
      status: false,
      message: errors.mapped()
    });
  }

  const number = phoneNumberFormatter(req.body.number);
  const message = req.body.message;

  // Related solution
  const isRegisteredNumber = await client.isRegisteredUser(number);

  if (!isRegisteredNumber) {
    return res.status(422).json({
      status: false,
      message: 'The number is not registered'
    });
  }

  client.sendMessage(number, message).then(response => {
    res.status(200).json({
      status: true,
      response: response
    });
  }).catch(err => {
    res.status(500).json({
      status: false,
      response: err
    });
  });
});

Just for more information, at the code above I make number formatter function to help reformat the number to xxx@c.us format.

// Formatted to Indonesian (62)
const phoneNumberFormatter = function(number) {
  // 1. Menghilangkan karakter selain angka
  let formatted = number.replace(/\D/g, '');

  // 2. Menghilangkan angka 0 di depan (prefix)
  //    Kemudian diganti dengan 62
  if (formatted.startsWith('0')) {
    formatted = '62' + formatted.substr(1);
  }

  if (!formatted.endsWith('@c.us')) {
    formatted += '@c.us';
  }

  return formatted;
}

module.exports = {
  phoneNumberFormatter
}

If you interested, here I make an implementation example with this library https://github.com/ngekoding/whatsapp-api-tutorial

parthibd commented 3 years ago

@aleluis777 This happened to you because you tried to send to an invalid number and it corrupted the WA application. It happened to me once but after implementing checking if the number is actually a whatsapp number, it didn't happen again.

If your issue is solved, please close this.

aleluis777 commented 3 years ago

@ aleluis777 Esto le sucedió porque intentó enviar a un número inválido y corrompió la aplicación WA. Me pasó una vez, pero después de implementar la verificación de si el número es en realidad un número de WhatsApp, no volvió a suceder.

Si se resuelve su problema, ciérrelo.

exact! my problem is that to all the numbers I had added the character 0 at the beginning

CarlosPinedaT commented 3 years ago

Español : Yo tambien tuve el mismo problema, estaba enviando el numero con un + (mas) adelante del id del teléfono. Lo eliminé y todo funcionó!


English I also had the same problem, I was sending the number with a + (plus) in front of the phone id. I removed it and everything worked!