> To build or manage a Wix site with an AI agent, follow https://www.wix.com/headless/skill.md
> It builds a new Wix site or puts your own site online, from a prompt, and keeps changing it afterwards. The pages are code in any web technology, and Wix runs the backend.
> It works through the Wix plugin (the Wix MCP and the Wix skills), the Wix CLI or no Wix login, and puts the site live on Wix hosting or on another host.
> Business features include a store, bookings, events and tickets, a restaurant, a blog, forms, members and many more.
> Read the Wix skills online at https://www.wix.com/skills
> 
> Portal Navigation:
> 
> - Append `.md` to any URL under `https://dev.wix.com/docs/` to get its markdown version.
> - Pages are either content pages (article or reference text) or menu pages (a list of links to child pages).
> - To get a menu page, truncate any URL to a parent path and append `.md` (e.g. `https://dev.wix.com/docs/sdk.md`, `https://dev.wix.com/docs/sdk/core-modules.md`).
> - If a reference page seems to be missing its schema (parameters, return types, or type definitions), append `?schema=true` to its `.md` URL (e.g. `https://dev.wix.com/docs/contexts/contexts/stores/category-info/use-category-info-context.md?schema=true`).
> - Top-level index of all portals: https://dev.wix.com/docs/llms.txt
> - Full concatenated docs: https://dev.wix.com/docs/llms-full.txt

# Method name: sendMessage(conversationId: string, message: Message, options: SendMessageOptions)

# Method package: wixInboxV2

# Method menu location: wixInboxV2 --> messages --> sendMessage

# Method Link: https://dev.wix.com/docs/velo/apis/wix-inbox-v2/messages/send-message.md

# Method Description:

Sends a message to the business or participant.


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](#message-types).



# Method Code Examples:

*** Note: do not assume any prop names or enum values other than the ones in the example.

## Send a basic message (dashboard page code)

```javascript
import { messages } from 'wix-inbox.v2';

/* Sample conversationId value: 'e56ed8f7-4bbf-3d99-b2f9-962e555264c3'
 *
 * Sample messsage value:
 * {
 *     content: {
 *       previewText: "Welcome",
 *       basic: {
 *         items: [
 *           {
 *             text: "Welcome to Amy's Shoes!"
 *           }
 *         ]
 *       }
 *     },
 *     visibility: 'BUSINESS_AND_PARTICIPANT',
 *     direction: 'BUSINESS_TO_PARTICIPANT',
 *     sourceChannel: 'CHAT',
 *     targetChannels: ['CHAT']
 * }
 */

export async function mySendMessageFunction(conversationId, message) {
  try {
    const basicMessage = await messages.sendMessage(conversationId, message);
    const messageId = basicMessage.message._id;
    const sender = basicMessage.message.sender;
    const visibility = basicMessage.message.visibility;

    return basicMessage;
  } catch (error) {
    console.error(error);
    //Handle the error
  }

}

/* Promise resolves to:
 * {
 *   "_createdDate": "2023-11-27T16:51:58.040Z"
 *   "_id": "1701103918040190",
 *   "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a",
 *   "badges": [],
 *   "content": {
 *     "previewText": "Welcome",
 *     "contentType": "BASIC"
 *     "basic": {
 *       "items": [
 *         {
 *           "text": "Welcome to Amy's Shoes!"
 *         }
 *       ]
 *     },
 *   },
 *   "direction": "BUSINESS_TO_PARTICIPANT",
 *   "sender": {
 *     "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"
 *   },
 *   "sequence": "1701103918040190",
 *   "sourceChannel": "CHAT",
 *   "targetChannels": [
 *     "CHAT"
 *   ],
 *   "visibility": "BUSINESS_AND_PARTICIPANT",
 * }
 */
```

## Send a basic message (export from backend code)

```javascript
import { Permissions, webMethod } from 'wix-web-module';
import { messages } from 'wix-inbox.v2';
import { elevate } from 'wix-auth';

/* Sample conversationId value: 'e56ed8f7-4bbf-3d99-b2f9-962e555264c3'
 *
 * Sample messsage value:
 * {
 *     content: {
 *       previewText: "Welcome",
 *       basic: {
 *         items: [
 *           {
 *             text: "Welcome to Amy's Shoes!"
 *           }
 *         ]
 *       }
 *     },
 *     visibility: 'BUSINESS_AND_PARTICIPANT',
 *     direction: 'BUSINESS_TO_PARTICIPANT',
 *     sourceChannel: 'CHAT',
 *     targetChannels: ['CHAT']
 * }
 */

export const mySendMessageFunction = webMethod(Permissions.Anyone, async (conversationId, message) => {
  const elevatedSendMessage = elevate(messages.sendMessage);

  try {
    const basicMessage = await elevatedSendMessage(conversationId, message);
    const messageId = basicMessage.message._id;
    const sender = basicMessage.message.sender;
    const visibility = basicMessage.message.visibility;

    return basicMessage;
  } catch (error) {
    console.error(error);
    //Handle the error
  }

});

/* Promise resolves to:
 * {
 *   "_createdDate": "2023-11-27T16:51:58.040Z"
 *   "_id": "1701103918040190",
 *   "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a",
 *   "badges": [],
 *   "content": {
 *     "previewText": "Welcome",
 *     "contentType": "BASIC"
 *     "basic": {
 *       "items": [
 *         {
 *           "text": "Welcome to Amy's Shoes!"
 *         }
 *       ]
 *     },
 *   },
 *   "direction": "BUSINESS_TO_PARTICIPANT",
 *   "sender": {
 *     "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"
 *   },
 *   "sequence": "1701103918040190",
 *   "sourceChannel": "CHAT",
 *   "targetChannels": [
 *     "CHAT"
 *   ],
 *   "visibility": "BUSINESS_AND_PARTICIPANT",
 * }
 */

```

## Send a template message  

```javascript
import { Permissions, webMethod } from 'wix-web-module';
import { messages } from 'wix-inbox.v2';
import { elevate } from 'wix-auth';

/* Sample conversationId value: '74aa9ee1-8e36-3028-b441-57dacfbd841e'
 * 
 * Sample message value: 
 * {
 *     direction: 'BUSINESS_TO_PARTICIPANT',
 *     visibility: 'BUSINESS_AND_PARTICIPANT',
 *     content: {
 *       previewText: "JoJo, you're getting a spa day!",
 *       template: {
 *         title: "JoJo, you're getting a spa day!",
 *         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',
 *         textLines: [
 *           "Thanks for booking with us!",
 *           "You're booked for a massage, mud bath, and facial on May 9, 2021."
 *         ],
 *         buttons: [{
 *           label: 'View My Invoice',
 *           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'
 *         },
 *         {
 *           label: 'Change My Appointment',
 *         postbackProperties: {
 *           interactionId: 'spa_day_54321__change_appointment'
 *           }
 *         }
 *         ]
 *       }
 *     },
 *     sourceChannel: "CHAT",
 *     targetChannels: ['CHAT']
 *   }
 */

export const mySendMessageFunction = webMethod(Permissions.Anyone, async (conversationId, message) => {
  const elevatedSendMessage = elevate(messages.sendMessage);

  try {
    const templateMessage = await elevatedSendMessage(conversationId, message);

    return templateMessage;
  } catch (error) {
    console.log(error);
    //Handle the error
  }

});

/* Promise resolves to:
 * {
 *   "_createdDate": "2023-11-29T10:40:52.822Z",
 *     "_id": "1701254452822623",
 *     "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a",
 *     "badges": [],
 *      "message": {
 *        "content": {
 *          "previewText": "JoJo, you're getting a spa day!",
 *          "template": {
 *            "buttons": 
 *            [
 *              {
 *                "label": "View My Invoice",
 *                "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"
 *              },
 *              {
 *                "label": "Change My Appointment",
 *                "postbackProperties": 
 *                {
 *                  "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a",
 *                  "interactionId": "spa_day_54321__change_appointment"
 *                }
 *              }
 *            ],
 *          "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"
 *          "orientation": "PORTRAIT",
 *          "textLines": 
 *          [
 *            "Thanks for booking with us!",
 *            "You're booked for a massage, mud bath, and facial on May 9, 2021."
 *          ],
 *          "title": "JoJo, you're getting a spa day!"
 *       },
 *     },
 *     "direction": "BUSINESS_TO_PARTICIPANT",
 *       "sender": {
 *       "appId": "151e476a-715e-ec33-db9a-a7ff4d51f70a"
 *     },
 *     "sequence": "1701254452822623",
 *     "sourceChannel": "CHAT",
 *     "targetChannels": [],
 *      "visibility": "BUSINESS_AND_PARTICIPANT",
 *     }
 * }
 */

```

## Send a minimal message to a site visitor 

```javascript
/****************************
 * Backend code - events.js *
 ****************************/

import { sendMinimalMessage } from 'backend/inbox.web';

export function wixBlog_onPostLiked(event) {
  console.log(`New post liked with ID ${event.data.postId}`);
  const anonymousVisitorId = event.data.anonymousVisitorId;
  sendMinimalMessage(anonymousVisitorId);
}

/*******************************
 * Backend code - inbox.web.js *
 *******************************/

import { Permissions, webMethod } from 'wix-web-module';
import { conversations, messages } from 'wix-inbox.v2';
import { elevate } from 'wix-auth';

export const sendMinimalMessage = webMethod(Permissions.Anyone, async (anonymousVisitorId) => {
  const message = {
    direction: 'PARTICIPANT_TO_BUSINESS',
    visibility: 'BUSINESS',
    content: {
      previewText: 'New Like',
      minimal: {
        text: 'New Like',
        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'
      }
    }
  }

  try {
    const conversationId = await getOrCreateConversationIdFromVisitor(anonymousVisitorId);

    const elevatedSendMessage = elevate(messages.sendMessage);
    const minimalMessage = await elevatedSendMessage(conversationId, message);
    console.log(`The message object returned is ${minimalMessage}`);

    return minimalMessage;
  } catch (error) {
    console.error(error);
    //Handle the error
  }
});

export const getOrCreateConversationIdFromVisitor = webMethod(Permissions.Anyone, async (anonymousVisitorId) => {
  const participantId = {
    anonymousVisitorId: anonymousVisitorId
  };

  try {
    const elevatedGetOrCreateConversation = elevate(conversations.getOrCreateConversation);
    const conversationObject = await elevatedGetOrCreateConversation(participantId);

    return conversationObject.conversation._id;
  } catch (error) {
    console.error(error);
    //Handle the error
  }
});
```

## Get or create a new conversation with a member 

```javascript
/*******************************
 * Backend code - inbox.web.js *
 *******************************/

import { Permissions, webMethod } from 'wix-web-module';
import { conversations, messages } from 'wix-inbox.v2';
import { currentMember } from 'wix-members-backend'
import { elevate } from 'wix-auth';

export const sendFormData = webMethod(Permissions.Anyone, async (formData) => {
  const message = {
    direction: 'PARTICIPANT_TO_BUSINESS',
    visibility: 'BUSINESS',
    content: {
      previewText: 'New Contact',
      form: {
        title: 'New Contact',
        description: 'New Contact Form',
        fields: [
          { 
            name: 'First Name', 
            value: formData.firstName
          },
          { 
            name: 'Last Name', 
            value: formData.lastName 
          },
          { 
            name: 'email', 
            value: formData.email 
          },
          { 
            name: 'Wants Newsletter', 
            value: formData.subscribeNewsletter.toString() 
          }
        ],
      }
    }
  }
  
  try {
    const conversationId = await getOrCreateConversationIdFromMember();

    const elevatedSendMessage = elevate(messages.sendMessage);
    const formMessage = await elevatedSendMessage(conversationId, message);

    return formMessage;
  } catch (error) {
    console.error(error);
    //Handle the error
  }
});

export const getOrCreateConversationIdFromMember = webMethod(Permissions.Anyone, async () => {
  const member = await currentMember.getMember();
  const participantId = { 
    memberId: member._id 
  };
  
  try {
    const elevatedGetOrCreateConversation = elevate(conversations.getOrCreateConversation);
    const conversationObject = await elevatedGetOrCreateConversation(participantId);
    
    return conversationObject.converation._id;
  } catch (error) {
    console.error(error);
    //Handle the error
  }
});
/*************
 * Page code *
 *************/

import { sendFormData } from 'backend/inbox.web';

$w.onReady(function () {
  $w('#submitButton').onClick(async () => {
    const firstName = $w('#firstName').value;
    const lastName = $w('#lastName').value;
    const email = $w('#email').value;
    const subscribeNewsletter = $w('#subscribeNewsletter').checked;
    const dataToInsert = {
      firstName,
      lastName,
      email,
      subscribeNewsletter
    };

    const formData = await sendFormData(dataToInsert);
    console.log(`The form data is ${formData}`);
  });
});
```

---