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 (dashboard page code)

Copy Code
1import { messages } from 'wix-inbox.v2';
2
3/* 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 */
23
24export 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;
30
31 return basicMessage;
32 } catch (error) {
33 console.error(error);
34 //Handle the error
35 }
36
37}
38
39/* 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 */
Send a basic message (export from backend code)

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { messages } from 'wix-inbox.v2';
3import { elevate } from 'wix-auth';
4
5/* 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 */
25
26export const mySendMessageFunction = webMethod(Permissions.Anyone, async (conversationId, message) => {
27 const elevatedSendMessage = elevate(messages.sendMessage);
28
29 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;
34
35 return basicMessage;
36 } catch (error) {
37 console.error(error);
38 //Handle the error
39 }
40
41});
42
43/* 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
Send a template message

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { messages } from 'wix-inbox.v2';
3import { elevate } from 'wix-auth';
4
5/* 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 */
37
38export const mySendMessageFunction = webMethod(Permissions.Anyone, async (conversationId, message) => {
39 const elevatedSendMessage = elevate(messages.sendMessage);
40
41 try {
42 const templateMessage = await elevatedSendMessage(conversationId, message);
43
44 return templateMessage;
45 } catch (error) {
46 console.log(error);
47 //Handle the error
48 }
49
50});
51
52/* 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
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.web.js *
3 ****************************/
4
5import { Permissions, webMethod } from 'wix-web-module';
6import { sendMinimalMessage } from 'backend/inbox.web';
7
8export const wixBlog_onPostLiked = webMethod(Permissions.Anyone, (event) => {
9 console.log(`New post liked with ID ${event.data.postId}`);
10 const anonymousVisitorId = event.data.anonymousVisitorId;
11 sendMinimalMessage(anonymousVisitorId);
12});
13
14/****************************
15 * Backend code - inbox.web.js *
16 ****************************/
17
18import { Permissions, webMethod } from 'wix-web-module';
19import { conversations, messages } from 'wix-inbox.v2';
20import { elevate } from 'wix-auth';
21
22export const sendMinimalMessage = webMethod(Permissions.Anyone, async (anonymousVisitorId) => {
23 const message = {
24 direction: 'PARTICIPANT_TO_BUSINESS',
25 visibility: 'BUSINESS',
26 content: {
27 previewText: 'New Like',
28 minimal: {
29 text: 'New Like',
30 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'
31 }
32 }
33 }
34
35 try {
36 const conversationId = await getOrCreateConversationIdFromVisitor(anonymousVisitorId);
37
38 const elevatedSendMessage = elevate(messages.sendMessage);
39 const minimalMessage = await elevatedSendMessage(conversationId, message);
40 console.log(`The message object returned is ${minimalMessage}`);
41
42 return minimalMessage;
43 } catch (error) {
44 console.error(error);
45 //Handle the error
46 }
47});
48
49export const getOrCreateConversationIdFromVisitor = webMethod(Permissions.Anyone, async (anonymousVisitorId) => {
50 const participantId = {
51 anonymousVisitorId: anonymousVisitorId
52 };
53
54 try {
55 const elevatedGetOrCreateConversation = elevate(conversations.getOrCreateConversation);
56 const conversationObject = await elevatedGetOrCreateConversation(participantId);
57
58 return conversationObject.conversation._id;
59 } catch (error) {
60 console.error(error);
61 //Handle the error
62 }
63});
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.web.js *
3 ****************************/
4
5import { Permissions, webMethod } from 'wix-web-module';
6import { conversations, messages } from 'wix-inbox.v2';
7import { currentMember } from 'wix-members-backend'
8import { elevate } from 'wix-auth';
9
10export 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.firstName
23 },
24 {
25 name: 'Last Name',
26 value: formData.lastName
27 },
28 {
29 name: 'email',
30 value: formData.email
31 },
32 {
33 name: 'Wants Newsletter',
34 value: formData.subscribeNewsletter.toString()
35 }
36 ],
37 }
38 }
39 }
40
41 try {
42 const conversationId = await getOrCreateConversationIdFromMember();
43
44 const elevatedSendMessage = elevate(messages.sendMessage);
45 const formMessage = await elevatedSendMessage(conversationId, message);
46
47 return formMessage;
48 } catch (error) {
49 console.error(error);
50 //Handle the error
51 }
52});
53
54export const getOrCreateConversationIdFromMember = webMethod(Permissions.Anyone, async () => {
55 const member = await currentMember.getMember();
56 const participantId = {
57 memberId: member._id
58 };
59
60 try {
61 const elevatedGetOrCreateConversation = elevate(conversations.getOrCreateConversation);
62 const conversationObject = await elevatedGetOrCreateConversation(participantId);
63
64 return conversationObject.converation._id;
65 } catch (error) {
66 console.error(error);
67 //Handle the error
68 }
69});
70/*************
71 * Page code *
72 *************/
73
74import { sendFormData } from 'backend/inbox.web';
75
76$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 subscribeNewsletter
87 };
88
89 const formData = await sendFormData(dataToInsert);
90 console.log(`The form data is ${formData}`);
91 });
92});