getOrCreateConversation( )
Retrieves a conversation for the specified visitor, contact, or member ID, or creates one if it doesn't exist.
This function requires elevated permissions to run. This function is not universal and runs only on the backend.
Syntax
function getOrCreateConversation(participantId: ParticipantId): Promise<GetOrCreateConversationResponse>
getOrCreateConversation Parameters
NAME
TYPE
DESCRIPTION
ID of the visitor, contact, or member chatting with the business.
Required for 3rd-party apps.
Returns
Return Type:
NAME
TYPE
DESCRIPTION
Created or retrieved conversation.
Indicates whether the conversation was just created.
If true
, the conversation was just created.
If false
, the conversation already existed.
Was this helpful?
1import { conversations } from 'wix-inbox.v2';2import { elevate } from 'wix-auth';34/* Sample participantId value:5 * {6 * memberId: 'b17c523c-6ec9-4b56-9d9d-123cc6978bdf'7 * }8 */910export async function myGetOrCreateConversationFunction(participantId) {11 try {12 const elevatedGetOrCreateConversation = elevate(conversations.getOrCreateConversation);13 const conversationObject = await elevatedGetOrCreateConversation(participantId);14 const id = conversationObject.conversation._id;15 const channels = conversationObject.conversation.channels;16 const siteName = conversationObject.conversation.businessDisplayData.name;1718 return conversationObject;19 } catch (error) {20 console.error(error);21 // Handle the error22 }23}2425/* Promise resolves to:26 * {27 * "conversation": {28 * "_id": "092d1135-99cf-3ca9-a0bf-378e1df4539b",29 * "businessDisplayData": {30 * "name": "Classic Cars"31 * },32 * "channels": [33 * "CHAT",34 * EMAIL35 * ],36 * "participant": {37 * "memberId": "b17c523c-6ec9-4b56-9d9d-123cc6978bdf"38 * },39 * "participantDisplayData": {40 * "name": "alevine@email.com"41 * }42 * "newConversation": false43 * }44 */45
1import { Permissions, webMethod } from 'wix-web-module';2import { conversations } from 'wix-inbox.v2';3import { elevate } from 'wix-auth';45/* Sample participantId value:6 * {7 * memberId: 'b17c523c-6ec9-4b56-9d9d-123cc6978bdf'8 * }9 */1011export const myGetOrCreateConversationFunction = webMethod(Permissions.Anyone, async (participantId) => {1213 try {14 const elevatedGetOrCreateConversation = elevate(conversations.getOrCreateConversation);15 const conversationObject = await elevatedGetOrCreateConversation(participantId);16 const id = conversationObject.conversation._id;17 const channels = conversationObject.conversation.channels;18 const siteName = conversationObject.conversation.businessDisplayData.name;1920 return conversationObject;21 } catch (error) {22 console.error(error);23 // Handle the error24 }25});2627/* Promise resolves to:28 * {29 * "conversation": {30 * "_id": "092d1135-99cf-3ca9-a0bf-378e1df4539b",31 * "businessDisplayData": {32 * "name": "Classic Cars"33 * },34 * "channels": [35 * "CHAT",36 * EMAIL37 * ],38 * "participant": {39 * "memberId": "b17c523c-6ec9-4b56-9d9d-123cc6978bdf"40 * },41 * "participantDisplayData": {42 * "name": "alevine@email.com"43 * }44 * "newConversation": false45 * }46 */47
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});
This code registers an event listener in the events.js
file that triggers when a new contact is created. The event handler calls getOrCreateConversation()
to generate or retrieve a conversation. It then sends a thank-you message in the wix-chat
widget.
1/****************************2 * Backend code - events.js *3 ****************************/45import { sendThankYou } from 'backend/inbox';67export function wixCrm_onContactCreated(event) {8 const contactId = event.metadata.entityId;9 sendThankYou(contactId);10}1112/*******************************13 * Backend code - inbox.web.js *14 ******************************/1516import { Permissions, webMethod } from 'wix-web-module';17import { conversations, messages } from 'wix-inbox.v2';18import { elevate } from 'wix-auth';1920export const sendThankYou = webMethod(Permissions.Anyone, async (contactId) => {21 const participantId = {22 contactId: contactId23 };24 const message = {25 direction: 'BUSINESS_TO_PARTICIPANT',26 visibility: 'BUSINESS_AND_PARTICIPANT',27 content: {28 previewText: 'Thank You',29 basic: {30 items: [31 { text: 'Thank you for your input' },32 ]33 }34 },35 sourceChannel: 'CHAT',36 targetChannels: ['CHAT']37 }3839 try {40 const elevatedGetOrCreateConversation = elevate(conversations.getOrCreateConversation);41 const conversationObject = await elevatedGetOrCreateConversation(participantId);42 const conversationId = conversationObject.conversation._id;4344 const elevatedSendMessage = elevate(messages.sendMessage);45 const thankYouMessage = await elevatedSendMessage(conversationId, message);4647 return thankYouMessage;48 } catch (error) {49 console.error(error);50 //Handle the error51 }52});53
1import { Permissions, webMethod } from 'wix-web-module';2import { conversations } from 'wix-inbox.v2';3import { elevate } from 'wix-auth';45/* Sample participantId value:6 * {7 * anonymousVisitorId: 'af63e767-caeb-4435-bf8c-f14ebd413060'8 * }9 */1011export const getConversationFromVisitor = webMethod(Permissions.Anyone, async (participantId) => {12 try {13 const elevatedGetOrCreateConversation = elevate(conversations.getOrCreateConversation)14 const conversationObject = await elevatedGetOrCreateConversation(participantId);15 const id = conversationObject.conversation._id;16 const firstChannel = conversationObject.conversation.channels[0];1718 return conversationObject;19 } catch (error) {20 console.error(error);21 //Handle the error22 }23});2425/* Promise resolves to:26 * {27 * "conversation": {28 * "_id": "c4999cbd-33ea-37cb-a374-75600f409d0b",29 * "businessDisplayData": {30 * "name": "Peak Fitness"31 * },32 * "channels": [33 * "CHAT"34 * ],35 * "participant": {36 * "anonymousVisitorId": "5dc5855b-0e34-495b-9762-3a9f591baa37"37 * },38 * "participantDisplayData": {39 * "name": "Visitor #8145",40 * "imageUrl": "https://static.parastorage.com/services/engage-web/1.6076.0/assets/visitor-avatars-faces/Avatar1Orange.png"41 * }42 * },43 * "newConversation": false44 * }45 */46
1import { Permissions, webMethod } from 'wix-web-module';2import { conversations } from 'wix-inbox.v2';3import { elevate } from 'wix-auth';45/* Sample participantId value:6 * {7 * contactId: 'c5ef5c35-3b53-41c9-aeeb-214424e5f6e5'8 * }9 */1011export const myGetOrCreateConversationFunction = webMethod(Permissions.Anyone, async (participantId) => {1213 try {14 const elevatedGetOrCreateConversation = elevate(conversations.getOrCreateConversation);15 const conversationObject = await conversations.getOrCreateConversation(participantId);16 const id = conversationObject.conversation._id;17 const channels = conversationObject.conversation.channels;18 const siteName = conversationObject.conversation.businessDisplayData.name;1920 return conversationObject;21 } catch (error) {22 console.error(error);23 //Handle the error24 }25});2627/* Promise resolves to:28 * {29 * "conversation": {30 * "_id": "7d313780-d237-3d5e-bfdb-f348bf615140",31 * "businessDisplayData": {32 * "name": "Ticket Express"33 * },34 * "channels": [35 * "CHAT",36 * "EMAIL"37 * ],38 * "participant": {39 * "contactId": "c5ef5c35-3b53-41c9-aeeb-214424e5f6e5"40 * },41 * "participantDisplayData": {42 * "name": "Connor Dawson"43 * }44 * },45 * "newConversation": true46 * }47 */48
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';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});63