CorvidReference

Events

Events that are fired by actions relating to chat messages.

Wix Chat events are fired on your site's backend when certain events related to the Wix Chat application occur. You can write event handlers that react to these events. Event handler functions receive data that corresponds to the event that fired. Use event handlers to create custom responses to chat message events.

To add a chat event handler, add an events.js file to the Backend section of your site if one does not already exist. All event handler functions for your site are defined in this file.

Event handler functions are defined using the following pattern:

 export function <wixAppName>_<eventName>(event) { }

For example, an event handler that handles backend chat messages looks like this:

 export function wixChat_onMessage(event) { }

Note that backend events don't work when previewing your site.

Table of Contents

FUNCTIONS

?
Perform actions on an object.
onMessage( )An event that fires when a chat message is sent to or from the business.

OBJECTS

?
Objects used when setting, getting, or calling the properties and methods listed above.
MessagePayloadThe content of a chat message.
SendMessageEventAn object representing a chat message that was sent.

onMessage( )

An event that fires when a chat message is sent to or from the business.

Description

The onMessage() event handler runs when a backend chat message is sent to or from the business. The received SendMessageEvent object contains information about the message that was sent.

Note

Backend events don't work when previewing your site.

Syntax

function onMessage(event: SendMessageEvent): void
PARAMETERS
?
Values that you pass to a function.
event
The message data.

Examples

An event when a chat message is sent

import wixChat from 'wix-chat-backend';

 export function wixChat_onMessage(event) {
   const message = event.payload.text;
   const participant = event.participantId;
 }

 /*  Example SendMessageEvent object:
  *
  *  {
  *    "channelId": "23b345b6-c78d-9012-e3f4-567g89h0i01k",
  *    "type": "TEXT",
  *    "summary": "Hey, I've got a question about your products",
  *    "participantId": "12a345b6-e78f-8011-f3f5-567g89h0i12j",
  *    "direction": "VisitorToBusiness",
  *    "createdAt": "2019-10-27T06:02:12.008Z",
  *    "payload": {
  *      "text": "Hey, I've got a question about your products"
  *    },
  *    "metadata": {}
  *  }
  */

Get message text from an input element and send it as a chat message

In this example, we use an input element to get the text of a chat message from a site visitor. We get the business channel ID and send the chat message to the business.

/********************
 * page code *
 ********************/
 import {sendChatMessage} from 'backend/chat';
 import wixWindow from 'wix-window';

 // ...

 export async function sendMessageButton_click(event) {
   const channel = await getChannel({type: "Business"});
   const channelId = channel.id;

   const messageText = $w('#textInput').value;
   const sendAsVisitor = "true";

   sendChatMessage(messageText, channelId, sendAsVisitor);
}

/****************************
 * backend code - chat.jsw *
 ****************************/
 import wixChat from 'wix-chat-backend';

 export function sendChatMessage(messageText, channelId, sendAsVisitor) {
   wixChat.sendMessage({
     "messageText": messageText,
     "channelId": channelId,
     "sendAsVisitor": sendAsVisitor
   })
 }

/**********************************
 * backend event code - events.js *
 **********************************/
 import wixChat from 'wix-chat-backend';

 export function wixChat_onMessage(event) {
   const chatMessage = event.payload.text;
   const chatParticipant = event.participantId;
 }

 /*  Example SendMessageEvent object:
  *
  *  {
  *    "channelId": "23b345b6-c78d-9012-e3f4-567g89h0i01k",
  *    "type": "TEXT",
  *    "summary": "Hey, I've got a question about your products",
  *    "participantId": "12a345b6-e78f-8011-f3f5-567g89h0i12j",
  *    "direction": "VisitorToBusiness",
  *    "createdAt": "2019-10-27T06:02:12.008Z",
  *    "payload": {
  *      "text": "Hey, I've got a question about your products"
  *    },
  *    "metadata": {}
  *  }
  */

MessagePayload

The content of a chat message.

Description

Currently only content of type text is included in the MessagePayload object.

See Also

SendMessageEvent, onMessage()

Syntax

type MessagePayload = {
  text: string
}
MEMBERS
?
The properties of an object.
text
string
Text of the chat message.

Examples

An event when a backend chat message is sent

import wixChat from 'wix-chat-backend';

 export function wixChat_onMessage(event) {
   const message = event.payload.text;
   const participant = event.participantId;
 }

 /*  Example SendMessageEvent object:
  *
  *  {
  *    "channelId": "23b345b6-c78d-9012-e3f4-567g89h0i01k",
  *    "type": "TEXT",
  *    "summary": "Hey, I've got a question about your products",
  *    "participantId": "12a345b6-e78f-8011-f3f5-567g89h0i12j",
  *    "direction": "VisitorToBusiness",
  *    "createdAt": "2019-10-27T06:02:12.008Z",
  *    "payload": {
  *      "text": "Hey, I've got a question about your products"
  *    },
  *    "metadata": {}
  *  }
  */

SendMessageEvent

An object representing a chat message that was sent.

See Also

onMessage()

Syntax

type SendMessageEvent = {
  channelId: string
  direction: string
  type: string
  summary: string
  participantId: string
  createdAt: Date
  payload: MessagePayload
  metadata: Object
}
MEMBERS
?
The properties of an object.
channelId
string
ID of the channel on which the message was sent.
direction
string

Direction of the message. One of the following:

  • "VisitorToBusiness": From a site visitor to the business.
  • "BusinessToVisitor": From the business to a site visitor.
type
string
Type of message. Currently only TEXT is supported.
summary
string
First 250 characters of the chat message. Currently only text is included in the summary.
participantId
string
The sender's member ID. For a message sent from the site's business, the site owner's member ID.
createdAt
Date
Date and time the message was sent.
payload
Content of the message.
metadata(Optional)
Object
An object representing additional contextual message information included in a chat message. The site visitor does not see the metadata.

Examples

An event when a backend chat message is sent

import wixChat from 'wix-chat-backend';

 export function wixChat_onMessage(event) {
   const message = event.payload.text;
   const participant = event.participantId;
 }

 /*  Example SendMessageEvent object:
  *
  *  {
  *    "channelId": "23b345b6-c78d-9012-e3f4-567g89h0i01k",
  *    "type": "TEXT",
  *    "summary": "Hey, I've got a question about your products",
  *    "participantId": "12a345b6-e78f-8011-f3f5-567g89h0i12j",
  *    "direction": "VisitorToBusiness",
  *    "createdAt": "2019-10-27T06:02:12.008Z",
  *    "payload": {
  *      "text": "Hey, I've got a question about your products"
  *    },
  *    "metadata": {}
  *  }
  */