Search.../

sendMessage( )

Developer Preview

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.

Admin Method

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
conversationId
string

ID of the conversation to add the message to.

message
Message

Message to send.

options
Optional
SendMessageOptions

Additional options for sending a message.

Returns

Return Type:

Promise<
SendMessageResponse
>
NAME
TYPE
DESCRIPTION
message
Message

Sent message.

Was this helpful?

Send a basic message

Copy Code
1import { messages } from 'wix-inbox.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample conversationId value: 'e56ed8f7-4bbf-3d99-b2f9-962e555264c3'
5 *
6 * Sample messsage value:
7 * {
8 * content: {
9 * previewText: "Welcome",
10 * basic: {
11 * items: [
12 * {
13 * text: "Welcome to Amy's Shoes!"
14 * }
15 * ]
16 * }
17 * },
18 * visibility: 'BUSINESS_AND_PARTICIPANT',
19 * direction: 'BUSINESS_TO_PARTICIPANT',
20 * sourceChannel: 'CHAT',
21 * targetChannels: ['CHAT']
22 * }
23 */
24
25export async function mySendMessageFunction(conversationId, message) {
26 const elevatedSendMessage = elevate(messages.sendMessage);
27
28 try {
29 const basicMessage = await elevatedSendMessage(conversationId, message);
30 const messageId = basicMessage.message._id;
31 const sender = basicMessage.message.sender;
32 const visibility = basicMessage.message.visibility;
33
34 return basicMessage;
35 } catch (error) {
36 console.error(error);
37 //Handle the error
38 }
39
40}
41
42/* Promise resolves to:
43 * {
44 * "_createdDate": "2023-11-27T16:51:58.040Z"
45 * "_id": "1701103918040190",
46 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a",
47 * "badges": [],
48 * "content": {
49 * "previewText": "Welcome",
50 * "contentType": "BASIC"
51 * "basic": {
52 * "items": [
53 * {
54 * "text": "Welcome to Amy's Shoes!"
55 * }
56 * ]
57 * },
58 * },
59 * "direction": "BUSINESS_TO_PARTICIPANT",
60 * "sender": {
61 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"
62 * },
63 * "sequence": "1701103918040190",
64 * "sourceChannel": "CHAT",
65 * "targetChannels": [
66 * "CHAT"
67 * ],
68 * "visibility": "BUSINESS_AND_PARTICIPANT",
69 * }
70 */
Send a template message

Copy Code
1import { messages } from 'wix-inbox.v2';
2import { elevate } from 'wix-auth';
3
4/* Sample conversationId value: '74aa9ee1-8e36-3028-b441-57dacfbd841e'
5 *
6 * Sample message value:
7 * {
8 * direction: 'BUSINESS_TO_PARTICIPANT',
9 * visibility: 'BUSINESS_AND_PARTICIPANT',
10 * content: {
11 * previewText: "JoJo, you're getting a spa day!",
12 * template: {
13 * title: "JoJo, you're getting a spa day!",
14 * 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',
15 * textLines: [
16 * "Thanks for booking with us!",
17 * "You're booked for a massage, mud bath, and facial on May 9, 2021."
18 * ],
19 * buttons: [{
20 * label: 'View My Invoice',
21 * 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'
22 * },
23 * {
24 * label: 'Change My Appointment',
25 * postbackProperties: {
26 * interactionId: 'spa_day_54321__change_appointment'
27 * }
28 * }
29 * ]
30 * }
31 * },
32 * sourceChannel: "CHAT",
33 * targetChannels: ['CHAT']
34 * }
35 */
36
37export async function mySendMessageFunction(conversationId, message) {
38 const elevatedSendMessage = elevate(messages.sendMessage);
39
40 try {
41 const templateMessage = await elevatedSendMessage(conversationId, message);
42
43 return templateMessage;
44 } catch (error) {
45 console.log(error);
46 //Handle the error
47 }
48
49}
50
51/* Promise resolves to:
52 * {
53 * "_createdDate": "2023-11-29T10:40:52.822Z",
54 * "_id": "1701254452822623",
55 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a",
56 * "badges": [],
57 * "message": {
58 * "content": {
59 * "previewText": "JoJo, you're getting a spa day!",
60 * "template": {
61 * "buttons":
62 * [
63 * {
64 * "label": "View My Invoice",
65 * "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"
66 * },
67 * {
68 * "label": "Change My Appointment",
69 * "postbackProperties":
70 * {
71 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a",
72 * "interactionId": "spa_day_54321__change_appointment"
73 * }
74 * }
75 * ],
76 * "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"
77 * "orientation": "PORTRAIT",
78 * "textLines":
79 * [
80 * "Thanks for booking with us!",
81 * "You're booked for a massage, mud bath, and facial on May 9, 2021."
82 * ],
83 * "title": "JoJo, you're getting a spa day!"
84 * },
85 * },
86 * "direction": "BUSINESS_TO_PARTICIPANT",
87 * "sender": {
88 * "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"
89 * },
90 * "sequence": "1701254452822623",
91 * "sourceChannel": "CHAT",
92 * "targetChannels": [],
93 * "visibility": "BUSINESS_AND_PARTICIPANT",
94 * }
95 * }
96 */
97
Send a minimal message to a site visitor

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.

Copy Code
1/****************************
2 * Backend code - events.js *
3 ****************************/
4
5import { sendMinimalMessage } from 'backend/inbox';
6
7export 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}
12
13/****************************
14 * Backend code - inbox.jsw *
15 ****************************/
16
17import { conversations, messages } from 'wix-inbox.v2';
18import { elevate } from 'wix-auth';
19
20export async function sendMinimalMessage(anonymousVisitorId) {
21 const message = {
22 direction: 'PARTICIPANT_TO_BUSINESS',
23 visibility: 'BUSINESS',
24 content: {
25 previewText: 'New Like',
26 minimal: {
27 text: 'New Like',
28 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'
29 }
30 }
31 }
32
33 try {
34 const conversationId = await getOrCreateConversationIdFromVisitor(anonymousVisitorId);
35
36 const elevatedSendMessage = elevate(messages.sendMessage);
37 const minimalMessage = await elevatedSendMessage(conversationId, message);
38 console.log(`The message object returned is ${minimalMessage}`);
39
40 return minimalMessage;
41 } catch (error) {
42 console.error(error);
43 //Handle the error
44 }
45}
46
47export async function getOrCreateConversationIdFromVisitor(anonymousVisitorId) {
48 const participantId = {
49 anonymousVisitorId: anonymousVisitorId
50 };
51
52 try {
53 const elevatedGetOrCreateConversation = elevate(conversations.getOrCreateConversation);
54 const conversationObject = await elevatedGetOrCreateConversation(participantId);
55
56 return conversationObject.conversation._id;
57 } catch (error) {
58 console.error(error);
59 //Handle the error
60 }
61}
Get or create a new conversation with a member

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.

Copy Code
1/****************************
2 * Backend code - inbox.jsw *
3 ****************************/
4
5import { conversations, messages } from 'wix-inbox.v2';
6import { currentMember } from 'wix-members-backend'
7import { elevate } from 'wix-auth';
8
9export async function sendFormData(formData) {
10 const message = {
11 direction: 'PARTICIPANT_TO_BUSINESS',
12 visibility: 'BUSINESS',
13 content: {
14 previewText: 'New Contact',
15 form: {
16 title: 'New Contact',
17 description: 'New Contact Form',
18 fields: [
19 {
20 name: 'First Name',
21 value: formData.firstName
22 },
23 {
24 name: 'Last Name',
25 value: formData.lastName
26 },
27 {
28 name: 'email',
29 value: formData.email
30 },
31 {
32 name: 'Wants Newsletter',
33 value: formData.subscribeNewsletter.toString()
34 }
35 ],
36 }
37 }
38 }
39
40 try {
41 const conversationId = await getOrCreateConversationIdFromMember();
42
43 const elevatedSendMessage = elevate(messages.sendMessage);
44 const formMessage = await elevatedSendMessage(conversationId, message);
45
46 return formMessage;
47 } catch (error) {
48 console.error(error);
49 //Handle the error
50 }
51}
52
53export async function getOrCreateConversationIdFromMember() {
54 const member = await currentMember.getMember();
55 const participantId = {
56 memberId: member._id
57 };
58
59 try {
60 const elevatedGetOrCreateConversation = elevate(conversations.getOrCreateConversation);
61 const conversationObject = await elevatedGetOrCreateConversation(participantId);
62
63 return conversationObject.converation._id;
64 } catch (error) {
65 console.error(error);
66 //Handle the error
67 }
68}
69/*************
70 * Page code *
71 *************/
72
73import { sendFormData } from 'backend/inbox';
74
75$w.onReady(function () {
76 $w('#submitButton').onClick(async () => {
77 const firstName = $w('#firstName').value;
78 const lastName = $w('#lastName').value;
79 const email = $w('#email').value;
80 const subscribeNewsletter = $w('#subscribeNewsletter').checked;
81 const dataToInsert = {
82 firstName,
83 lastName,
84 email,
85 subscribeNewsletter
86 };
87
88 const formData = await sendFormData(dataToInsert);
89 console.log(`The form data is ${formData}`);
90 });
91});