Message Handlers
The Client
class provides three event handlers to handle different types of incoming events from the WhatsApp Cloud API. These event handlers can be registered using the on
method.
messages
The messages
event handler is called when a new message is received from a user. This includes text messages, media messages, orders, locations, and other types of messages.
Usage:
wh.on('messages', (message) => {
console.log(`Received message from ${message.sender}: ${message.text}`);
});
Learn more about the messages
event handler
callbacks
The callbacks
event handler is called when a user interacts with an interactive message, such as pressing a button or selecting an option from a section list.
Usage:
wh.on('callbacks', (callback) => {
console.log(`Received callback from ${callback.sender}: ${callback.data.id}`);
});
Learn more about the callbacks
event handler
statuses
The statuses
event handler is called when a message status update is received, such as when a message is delivered, seen, or deleted.
Usage:
wh.on('statuses', (status) => {
console.log(`Message ${status.id} is now ${status.status}`);
});
Learn more about the statuses
event handler
ChatOpened
The ChatOpened
event handler is called when a user opens a new chat with the bot. This includes cases where the user deletes the previous chat and opens a new one.
Usage:
wh.on('ChatOpened', async (chat) => {
console.log(chat);
});
Learn more about the RequestWelcome
type and its properties
Template Status
The Template Status
event handler is not yet documented, and it does not have the common methods available in the other event handlers.
Common Methods
All event handlers (messages
, callbacks
, and statuses
) have the following common methods available:
The documentation for each method can be found in the Client documentation, as the methods available on the event handlers are identical to the corresponding methods on the Client
class, with the exception that the to
parameter does not need to be provided, as it is derived from this.sender
.
replyText(text, options)
: Replies with a text message. Learn morereplyImage(image, options)
: Replies with an image message. Learn morereplyVideo(video, options)
: Replies with a video message. Learn morereplyAudio(audio, options)
: Replies with an audio message. Learn morereplyDocument(document, options)
: Replies with a document message. Learn morereplyLocation(location, options)
: Replies with a location message. Learn morereplyContact(name, phone, options)
: Replies with a contact message. Learn morereplySticker(sticker, options)
: Replies with a sticker message. Learn morereact(emoji)
: Reacts to a message with an emoji. Takes one parameter: the desired emoji (string).unreact()
: Removes the reaction (emoji) from the message. Does not require any parameters.