Search...
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.
Authorization
Request
This endpoint does not take any parameters
Response Object
Fulfilled - When the message is sent.
Returns an empty object.
Status/Error Codes
Related Content:
Was this helpful?
Send a chat message from the backend
Copy Code
1import wixChatBackend from 'wix-chat-backend';23 export function sendChatMessage(messageText, channelId, metadata, sendAsVisitor) {4 wixChatBackend.sendMessage({5 "messageText": messageText,6 "channelId": channelId,7 "metadata": {metadata},8 "sendAsVisitor": sendAsVisitor9 })10 .then( () => {11 console.log("Chat message sent");12 })13 .catch( (error) => {14 console.log(error);15 });16 }
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.
Copy Code
1/*************2 * page code *3 *************/4import { sendChatMessage } from 'backend/chat';5import wixWindow from 'wix-window';67// ...89export async function sendMessageButton_click(event) {10 const channel = await $w("#myChatbox").getChannel({ type: "Business" });11 const channelId = channel.id;1213 const messageText = $w('#textInput').value;14 const sendAsVisitor = "true";1516 sendChatMessage(messageText, channelId, sendAsVisitor);17}1819/****************************20 * Backend code - chat.jsw *21 ****************************/22import wixChatBackend from 'wix-chat-backend';2324export function sendChatMessage(messageText, channelId, sendAsVisitor) {25 wixChatBackend.sendMessage({26 "messageText": messageText,27 "channelId": channelId,28 "sendAsVisitor": sendAsVisitor29 })30}3132/**********************************33 * Backend event code - events.js *34 **********************************/35export function wixChat_onMessage(event) {36 const chatMessage = event.payload.text;37 const chatParticipant = event.participantId;38}3940/* Example SendMessageEvent object:41 *42 * {43 * "channelId": "23b345b6-c78d-9012-e3f4-567g89h0i01k",44 * "type": "TEXT",45 * "summary": "Hey, I've got a question about your products",46 * "participantId": "12a345b6-e78f-8011-f3f5-567g89h0i12j",47 * "direction": "VisitorToBusiness",48 * "createdAt": "2019-10-27T06:02:12.008Z",49 * "payload": {50 * "text": "Hey, I've got a question about your products"51 * },52 * "metadata": {}53 * }54 */55