CorvidReference

wix-chat-backend

The wix-chat-backend module contains functionality for working with the Wix Chat application from backend code.

Using the backend Chat API, site owners and contributors (referred to as the site's "business") can exchange chat messages with site members, contacts, and visitors (referred to as "visitors").

The backend Chat API is not applicable to social chat. For social chat between site members, use the client-side Chat API.

Note:

You cannot use a channel programmatically until it is created. A channel is created the first time the business or a visitor sends a message via the chatbox widget to a specific recipient.

To use the Chat API, import wixChat from the wix-chat-backend module:

   import wixChat from 'wix-chat-backend';

Table of Contents

FUNCTIONS

?
Perform actions on an object.
sendMessage( )Sends a chat message from the backend.

OBJECTS

?
Objects used when setting, getting, or calling the properties and methods listed above.
MessageInfoAn object representing a chat message to be sent.

sendMessage( )

Sends a chat message from the backend.

Description

The sendMessage() function sends a text message to a specific channel from the backend. The message can be from the business to a visitor, or from a visitor to the business.

See Also

onMessage()

Syntax

function sendMessage(messageInfo: MessageInfo): Promise<void>
PARAMETERS
?
Values that you pass to a function.
messageInfo
An object representing the message to be sent.
RETURN VALUE
?
Value that a function evaluates to when it is finished running.
Promise<void>
Fulfilled - When the message is sent.

Examples

Send a chat message from the backend

import wixChat from 'wix-chat-backend';

 export function sendChatMessage(messageText, channelId, metadata, sendAsVisitor) {
   wixChat.sendMessage({
     "messageText": messageText,
     "channelId": channelId,
     "metadata": {metadata},
     "sendAsVisitor": sendAsVisitor
   })
   .then( () => {
     console.log("Chat message sent");
   })
   .catch( (error) => {
     console.log(error);
   });
 }

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": {}
  *  }
  */

MessageInfo

An object representing a chat message to be sent.

See Also

sendMessage()

Syntax

type MessageInfo = {
  messageText: string
  channelId: string
  metadata: Object
  sendAsVisitor: boolean
}
MEMBERS
?
The properties of an object.
messageText
string
The content of the message.
channelId
string
The ID of the channel to send the message to. Currently only IDs of business channels are supported.
metadata(Optional)
Object
An object representing additional contextual message information included in a chat message. The site visitor does not see the metadata.
sendAsVisitor(Optional)
boolean
Indicates whether the message is sent from the visitor to the business. If sendAsVisitor is true, the message is sent from the site visitor to the business. If sendAsVisitor is undefined or false, the message is sent from the business to the site visitor. The default is undefined.

Examples

Send a chat message from the backend

import wixChat from 'wix-chat-backend';

 export function sendChatMessage(messageText, channelId, metadata, sendAsVisitor) {
   wixChat.sendMessage({
     "messageText": messageText,
     "channelId": channelId,
     "metadata": {metadata},
     "sendAsVisitor": sendAsVisitor
   })
   .then( () => {
     console.log("Chat message sent");
   })
   .catch( (error) => {
     console.log(error);
   });
 }