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:
Sends a chat message from the backend.
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.
function sendMessage(messageInfo: MessageInfo): Promise<void>;
An object representing the message to be sent.
import wixChatBackend from "wix-chat-backend";
export function sendChatMessage(
messageText,
channelId,
metadata,
sendAsVisitor,
) {
wixChatBackend
.sendMessage({
messageText: messageText,
channelId: channelId,
metadata: { metadata },
sendAsVisitor: sendAsVisitor,
})
.then(() => {
console.log("Chat message sent");
})
.catch((error) => {
console.log(error);
});
}