Closed aleluis777 closed 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.
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
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
@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 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
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!
Bug description
application stops after sending a message from the api. as shown
Reproduction steps
Install npm package
...
WhatsApp
Library