on
widget.on(eventName: string, callback: (eventData: any) => void): void;
The on
method allows registering event listeners for various chat-related events. The method takes two parameters: the name of the event to listen for (eventName
) and a callback function that will be invoked when the event occurs.
Event properties
eventName
: The name of the event to listen for.callback
: A function that executes when the event is triggered, taking a single parametereventData
that contains event-specific information.
Use-cases
Listen for New Messages
widget.on("add-message", eventData => {
console.log("New message ID:", eventData.id);
});
This use-case registers a listener for the add-message
event. The callback logs the ID of each new message sent by the user.
Listen for New Chats
widget.on("add-chat", eventData => {
console.log("New chat created:", eventData.chatId);
});
This use-case registers a listener for the add-chat
event. The callback logs the ID of each newly created chat.
Append to Existing Message
widget.on("append-message", eventData => {
console.log("Appended text to message ID:", eventData.messageId);
});
This use-case registers a listener for the append-message
event. The callback logs the ID of the message to which text was appended.
Delete a Chat
widget.on("delete-chat", eventData => {
console.log("Deleted chat ID:", eventData.chatId);
});
This use-case registers a listener for the delete-chat
event. The callback logs the ID of each deleted chat.
Provide Messages for a Chat
widget.on("provide-messages", eventData => {
console.log("Messages provided for chat ID:", eventData.chatId);
});
This use-case registers a listener for the provide-messages
event. The callback logs the ID of the chat for which new data is provided.
Request Messages for a Chat
widget.on("request-messages", eventData => {
console.log("Request to load messages for chat ID:", eventData.chatId);
});
This use-case registers a listener for the request-messages
event. The callback logs the ID of the chat that requires messages to be loaded.
Select a Chat
widget.on("select-chat", eventData => {
console.log("Selected chat ID:", eventData.chatId);
});
This use-case registers a listener for the select-chat
event. The callback logs the ID of the selected chat.
Typing State Changed
widget.on("type-message", eventData => {
console.log("Typing state changed for message ID:", eventData.messageId);
});
This use-case registers a listener for the type-message
event. The callback logs the ID of the message whose typing state changed.