sendMessage( )
Sends a message to the business or participant.
Description
Specify the message type in message.content
by including the appropriate object: basic
, template
, minimal
, or form
.
For more information on message types
and how each type renders in Inbox and the Chat widget,
see Message Types.
This function requires elevated permissions to run. This function is not universal and runs only on the backend.
Syntax
function sendMessage(conversationId: string, message: Message, options: SendMessageOptions): Promise<SendMessageResponse>
sendMessage Parameters
NAME
TYPE
DESCRIPTION
ID of the conversation to add the message to.
Message to send.
Additional options for sending a message.
Returns
Return Type:
NAME
TYPE
DESCRIPTION
Sent message.
Was this helpful?
1import { messages } from 'wix-inbox.v2';23/* Sample conversationId value: 'e56ed8f7-4bbf-3d99-b2f9-962e555264c3'4 *5 * Sample messsage value:6 * {7 * content: {8 * previewText: "Welcome",9 * basic: {10 * items: [11 * {12 * text: "Welcome to Amy's Shoes!"13 * }14 * ]15 * }16 * },17 * visibility: 'BUSINESS_AND_PARTICIPANT',18 * direction: 'BUSINESS_TO_PARTICIPANT',19 * sourceChannel: 'CHAT',20 * targetChannels: ['CHAT']21 * }22 */2324export async function mySendMessageFunction(conversationId, message) {25 try {26 const basicMessage = await messages.sendMessage(conversationId, message);27 const messageId = basicMessage.message._id;28 const sender = basicMessage.message.sender;29 const visibility = basicMessage.message.visibility;3031 return basicMessage;32 } catch (error) {33 console.error(error);34 //Handle the error35 }3637}3839/* Promise resolves to:40 * {41 * "_createdDate": "2023-11-27T16:51:58.040Z"42 * "_id": "1701103918040190",43 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a",44 * "badges": [],45 * "content": {46 * "previewText": "Welcome",47 * "contentType": "BASIC"48 * "basic": {49 * "items": [50 * {51 * "text": "Welcome to Amy's Shoes!"52 * }53 * ]54 * },55 * },56 * "direction": "BUSINESS_TO_PARTICIPANT",57 * "sender": {58 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"59 * },60 * "sequence": "1701103918040190",61 * "sourceChannel": "CHAT",62 * "targetChannels": [63 * "CHAT"64 * ],65 * "visibility": "BUSINESS_AND_PARTICIPANT",66 * }67 */
1import { Permissions, webMethod } from 'wix-web-module';2import { messages } from 'wix-inbox.v2';3import { elevate } from 'wix-auth';45/* Sample conversationId value: 'e56ed8f7-4bbf-3d99-b2f9-962e555264c3'6 *7 * Sample messsage value:8 * {9 * content: {10 * previewText: "Welcome",11 * basic: {12 * items: [13 * {14 * text: "Welcome to Amy's Shoes!"15 * }16 * ]17 * }18 * },19 * visibility: 'BUSINESS_AND_PARTICIPANT',20 * direction: 'BUSINESS_TO_PARTICIPANT',21 * sourceChannel: 'CHAT',22 * targetChannels: ['CHAT']23 * }24 */2526export const mySendMessageFunction = webMethod(Permissions.Anyone, async (conversationId, message) => {27 const elevatedSendMessage = elevate(messages.sendMessage);2829 try {30 const basicMessage = await elevatedSendMessage(conversationId, message);31 const messageId = basicMessage.message._id;32 const sender = basicMessage.message.sender;33 const visibility = basicMessage.message.visibility;3435 return basicMessage;36 } catch (error) {37 console.error(error);38 //Handle the error39 }4041});4243/* Promise resolves to:44 * {45 * "_createdDate": "2023-11-27T16:51:58.040Z"46 * "_id": "1701103918040190",47 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a",48 * "badges": [],49 * "content": {50 * "previewText": "Welcome",51 * "contentType": "BASIC"52 * "basic": {53 * "items": [54 * {55 * "text": "Welcome to Amy's Shoes!"56 * }57 * ]58 * },59 * },60 * "direction": "BUSINESS_TO_PARTICIPANT",61 * "sender": {62 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"63 * },64 * "sequence": "1701103918040190",65 * "sourceChannel": "CHAT",66 * "targetChannels": [67 * "CHAT"68 * ],69 * "visibility": "BUSINESS_AND_PARTICIPANT",70 * }71 */72
1import { Permissions, webMethod } from 'wix-web-module';2import { messages } from 'wix-inbox.v2';3import { elevate } from 'wix-auth';45/* Sample conversationId value: '74aa9ee1-8e36-3028-b441-57dacfbd841e'6 *7 * Sample message value:8 * {9 * direction: 'BUSINESS_TO_PARTICIPANT',10 * visibility: 'BUSINESS_AND_PARTICIPANT',11 * content: {12 * previewText: "JoJo, you're getting a spa day!",13 * template: {14 * title: "JoJo, you're getting a spa day!",15 * imageUrl: 'https://static.wixstatic.com/media/f93e3d633e79921f14330f1911fc1139.jpg/v1/fill/w_1200,h_798,al_c,q_85,usm_0.66_1.00_0.01/f93e3d633e79921f14330f1911fc1139.webp',16 * textLines: [17 * "Thanks for booking with us!",18 * "You're booked for a massage, mud bath, and facial on May 9, 2021."19 * ],20 * buttons: [{21 * label: 'View My Invoice',22 * launchUrl: 'https://static.wixstatic.com/media/f93e3d633e79921f14330f1911fc1139.jpg/v1/fill/w_1200,h_798,al_c,q_85,usm_0.66_1.00_0.01/f93e3d633e79921f14330f1911fc1139.webp'23 * },24 * {25 * label: 'Change My Appointment',26 * postbackProperties: {27 * interactionId: 'spa_day_54321__change_appointment'28 * }29 * }30 * ]31 * }32 * },33 * sourceChannel: "CHAT",34 * targetChannels: ['CHAT']35 * }36 */3738export const mySendMessageFunction = webMethod(Permissions.Anyone, async (conversationId, message) => {39 const elevatedSendMessage = elevate(messages.sendMessage);4041 try {42 const templateMessage = await elevatedSendMessage(conversationId, message);4344 return templateMessage;45 } catch (error) {46 console.log(error);47 //Handle the error48 }4950});5152/* Promise resolves to:53 * {54 * "_createdDate": "2023-11-29T10:40:52.822Z",55 * "_id": "1701254452822623",56 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a",57 * "badges": [],58 * "message": {59 * "content": {60 * "previewText": "JoJo, you're getting a spa day!",61 * "template": {62 * "buttons":63 * [64 * {65 * "label": "View My Invoice",66 * "launchUrl": "https://static.wixstatic.com/media/f93e3d633e79921f14330f1911fc1139.jpg/v1/fill/w_1200,h_798,al_c,q_85,usm_0.6 6_1.00_0.01/f93e3d633e79921f14330f1911fc1139.webp"67 * },68 * {69 * "label": "Change My Appointment",70 * "postbackProperties":71 * {72 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a",73 * "interactionId": "spa_day_54321__change_appointment"74 * }75 * }76 * ],77 * "imageUrl": "https://static.wixstatic.com/media/f93e3d633e79921f14330f1911fc1139.jpg/v1/fill/w_1200,h_798,al_c,q_85,usm_0.66_1.0 0_0.01/f93e3d633e79921f14330f1911fc1139.webp"78 * "orientation": "PORTRAIT",79 * "textLines":80 * [81 * "Thanks for booking with us!",82 * "You're booked for a massage, mud bath, and facial on May 9, 2021."83 * ],84 * "title": "JoJo, you're getting a spa day!"85 * },86 * },87 * "direction": "BUSINESS_TO_PARTICIPANT",88 * "sender": {89 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"90 * },91 * "sequence": "1701254452822623",92 * "sourceChannel": "CHAT",93 * "targetChannels": [],94 * "visibility": "BUSINESS_AND_PARTICIPANT",95 * }96 * }97 */98
This code detects a when a site visitor likes a blog post by registering the onPostLiked
event listener in the events.js
file. This event triggers an event object containing the anonymousVisitorId
, which is passed as an argument to getOrCreateConversationIdFromVisitor
. The conversationId is returned and passed as a parameter to sendMinimal
, which sends a minimal message to the site owner's inbox dashboard.
1/****************************2 * Backend code - events.js *3 ****************************/45import { sendMinimalMessage } from 'backend/inbox.web';67export function wixBlog_onPostLiked(event) {8 console.log(`New post liked with ID ${event.data.postId}`);9 const anonymousVisitorId = event.data.anonymousVisitorId;10 sendMinimalMessage(anonymousVisitorId);11}1213/*******************************14 * Backend code - inbox.web.js *15 ******************************/1617import { Permissions, webMethod } from 'wix-web-module';18import { conversations, messages } from 'wix-inbox.v2';19import { elevate } from 'wix-auth';2021export const sendMinimalMessage = webMethod(Permissions.Anyone, async (anonymousVisitorId) => {22 const message = {23 direction: 'PARTICIPANT_TO_BUSINESS',24 visibility: 'BUSINESS',25 content: {26 previewText: 'New Like',27 minimal: {28 text: 'New Like',29 iconUrl: 'https://static.wixstatic.com/media/727514_1a58537f1b6a44a7b09956cdbc5ac774~mv2.png/v1/fill/w_297,h_324,al_c,lg_1,q_85/727514_1a58537f1b6a44a7b09956cdbc5ac774~mv2.webp'30 }31 }32 }3334 try {35 const conversationId = await getOrCreateConversationIdFromVisitor(anonymousVisitorId);3637 const elevatedSendMessage = elevate(messages.sendMessage);38 const minimalMessage = await elevatedSendMessage(conversationId, message);39 console.log(`The message object returned is ${minimalMessage}`);4041 return minimalMessage;42 } catch (error) {43 console.error(error);44 //Handle the error45 }46});4748export const getOrCreateConversationIdFromVisitor = webMethod(Permissions.Anyone, async (anonymousVisitorId) => {49 const participantId = {50 anonymousVisitorId: anonymousVisitorId51 };5253 try {54 const elevatedGetOrCreateConversation = elevate(conversations.getOrCreateConversation);55 const conversationObject = await elevatedGetOrCreateConversation(participantId);5657 return conversationObject.conversation._id;58 } catch (error) {59 console.error(error);60 //Handle the error61 }62});
The page code passes input values submitted by a logged-in member to the backend sendFormData
function. This function retrieves the logged-in member ID, gets an existing or creates a new converation object and uses the conversation ID to send a form message to the site owner's inbox dashboard.
1/*******************************2 * Backend code - inbox.web.js *3 ******************************/45import { Permissions, webMethod } from 'wix-web-module';6import { conversations, messages } from 'wix-inbox.v2';7import { currentMember } from 'wix-members-backend'8import { elevate } from 'wix-auth';910export const sendFormData = webMethod(Permissions.Anyone, async (formData) => {11 const message = {12 direction: 'PARTICIPANT_TO_BUSINESS',13 visibility: 'BUSINESS',14 content: {15 previewText: 'New Contact',16 form: {17 title: 'New Contact',18 description: 'New Contact Form',19 fields: [20 {21 name: 'First Name',22 value: formData.firstName23 },24 {25 name: 'Last Name',26 value: formData.lastName27 },28 {29 name: 'email',30 value: formData.email31 },32 {33 name: 'Wants Newsletter',34 value: formData.subscribeNewsletter.toString()35 }36 ],37 }38 }39 }4041 try {42 const conversationId = await getOrCreateConversationIdFromMember();4344 const elevatedSendMessage = elevate(messages.sendMessage);45 const formMessage = await elevatedSendMessage(conversationId, message);4647 return formMessage;48 } catch (error) {49 console.error(error);50 //Handle the error51 }52});5354export const getOrCreateConversationIdFromMember = webMethod(Permissions.Anyone, async () => {55 const member = await currentMember.getMember();56 const participantId = {57 memberId: member._id58 };5960 try {61 const elevatedGetOrCreateConversation = elevate(conversations.getOrCreateConversation);62 const conversationObject = await elevatedGetOrCreateConversation(participantId);6364 return conversationObject.converation._id;65 } catch (error) {66 console.error(error);67 //Handle the error68 }69});70/*************71 * Page code *72 *************/7374import { sendFormData } from 'backend/inbox.web';7576$w.onReady(function () {77 $w('#submitButton').onClick(async () => {78 const firstName = $w('#firstName').value;79 const lastName = $w('#lastName').value;80 const email = $w('#email').value;81 const subscribeNewsletter = $w('#subscribeNewsletter').checked;82 const dataToInsert = {83 firstName,84 lastName,85 email,86 subscribeNewsletter87 };8889 const formData = await sendFormData(dataToInsert);90 console.log(`The form data is ${formData}`);91 });92});