Whatsapp-web.js: Send Message dont give a return

Created on 10 Sep 2020  ·  14Comments  ·  Source: pedroslopez/whatsapp-web.js

When i use client.SenMessage i cant know if its works or not because i never get a response.

router.post('/api/v1/send',(req,response) => {

var from  = req.body.from;
var mensagem = req.body.mensagem;
console.log(client.sendMessage(from,mensagem));

});

What i doing wrong ???

stale

All 14 comments

More details plz!
Show us your code.

PS.: Do not forget to update the package to v1.8.2

Thisis my code "client.sendMessage(from,mensagem)" i want send a message to a from number its working, the problemn on return of this "client.sendMessage(from,mensagem)" i need when i call this know if the message was send or not understand ?

You can use the event message_createevent:message_create

client.on('message_create', async (msg) => {
    console.log('NEW MESSAGE', msg);
})

This will be fired when you send or receive a message.
This could be used together of message_ack event to get the status of message sent

client.on('message_ack', async (msg, ack) => {
    console.log('MESSAGE SENT', msg, ack);
})

This package will try send the message even to a WA's invalid number. So, you'll need to verify before of send a message. For this i recommend you check the method isRegisteredUser()

To remember
sendMessage()method return a Promise containing the message object

guicuton gave you some great tips 👍

Something to keep in mind is sendMessage() returns a Promise that resolves when the message is queued up to be sent. In your example, since you are logging the return value directly, you would be logging a promise. If you want to log the created message object, you should first await the promise if using async-await syntax or .then() otherwise. (console.log(await client.sendMessage(from,mensagem));)

If you want to confirm that the message was sent, this is done through the message_ack event, like the previous post specifies.

Thank you so much !!!! This tips solve mu problemns!!!

Hi everyone, i can use the code like this

client.on('message_create', async (msg) => {
// save msg in database
})
client.on('message_ack', async (msg, ack) => {
//when i receive this i check my data base and change de message status ?
})

Yes Lucas!
with message_create you'll receive object with message values (sent and received) and with message_ack you'll receive new ack status from message sent

ACK STATUS

  • ACK_ERROR: -1
  • ACK_PENDING: 0
  • ACK_SERVER: 1
  • ACK_DEVICE: 2
  • ACK_READ: 3
  • ACK_PLAYED: 4

Yes it i understand, what i dont is how can is use this two functions together ???

Whats i dont understand is when or how it is called

client.on('message_ack', async (msg, ack) => {
//when i receive this i check my data base and change de message status ?
})

Please somebody save meee (smallville theme kkkk), i dont understand how can i use this two "events functions" togheter ...

Each event has it's own results...
For each of them you should take your "actions"

client.on('message_create', async (msg) => {
//here goes all your code to save messages received and sent
})
client.on('message_ack', async (msg, ack) => {
//here goes all your code to save/update the message sent by paired client
})

Each event has it's own results...
For each of them you should take your "actions"

client.on('message_create', async (msg) => {
//here goes all your code to save messages received and sent
})
client.on('message_ack', async (msg, ack) => {
//here goes all your code to save/update the message sent by paired client
})

Thanks a lot but its return a promisse to my php server

client.on('message_create', async (msg) => {
//here goes all your code to save messages received and sent
})
clie

how can i solve this?

@LucasDR you're going to a "offtopic" theme instead a real issue on the lib.
Join the discord server and look for this kind of help there since that your problem is "how to do" instead a "real bug/problem" on the lib

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fdciabdul picture fdciabdul  ·  6Comments

dedenhendrap picture dedenhendrap  ·  8Comments

marcopandolfo picture marcopandolfo  ·  5Comments

MillesC picture MillesC  ·  6Comments

ngekoding picture ngekoding  ·  5Comments