Reputation: 59
I am trying to create a whatsapp bot with whatsapp-web.js library.
When I try to send messages, client.on("message",function(){}) is not being called.
Note: that the client.on("ready",function(){}) works fine and I get a Wall-e alive reply in my whatsapp android application.
const qrcode = require('qrcode-terminal');
const { Client, MessageMedia } = require('whatsapp-web.js');
const client = new Client();
client.on('qr', qr => {
qrcode.generate(qr, { small: true });
});
client.on('ready', () => {
console.log('Client is ready!');
client.getChats().then(chats => {
const myGroup = chats.find(id => id.name === "Wall-E")
client.sendMessage(myGroup.id._serialized, "Walle alive!")
});
});
client.on('message',async message => {
console.log('messag request')
console.log(message.body)
if (message.body === 'wall-e !ping') {
console.log('ping request')
client.sendMessage(message.from, 'pong');
} else if (message.body === 'wall-e ~meme') {
console.log('meme request')
const meme = await axios('https://meme-api.herokuapp.com/gimme').then(res => res.data)
client.sendMessage(message.from, await MessageMedia.formUrl(meme.url))
} else if (message.body === 'wall-e ~joke') {
console.log('joke request')
const joke = await axios('https://v2.jokeapi.dev/joke/Any?safe-mode').then(res => res.data)
if(joke.joke){
await client.sendMessage(message.from, joke.joke)
}else if(joke.setup){
const jokeMessage= await client.sendMessage(message.from, joke.setup)
if(joke.delivery){
setTimeout(function(){ jokeMessage.reply(joke.delivery)},5000)
}
}
});
client.initialize();
Upvotes: 1
Views: 2167
Reputation: 41
I faced this issue before this could be because of the older version of WhatsApp-api.js, try uninstalling the whatsapp-api.js package and install it again. it will start working.
Upvotes: 0