# Use Twilio Messaging as a Channel

> \[!WARNING]
>
> AI Assistants is a [Twilio Alpha](https://twilioalpha.com) project that's in **Developer Preview**.
>
> View the [current limitations](/docs/alpha/ai-assistants/pricing-and-limits) for details about feature limits during developer preview.

This guide will show you how to trigger your AI Assistant with incoming messages from end users by connecting your Assistant with [Twilio Messaging](/docs/messaging). To get started, you will need the following:

* A configured AI Assistant. [See the Quickstart guide to setting up an Assistant](/docs/alpha/ai-assistants/quickstart) if you have not configured one yet.
* A Twilio phone number or WhatsApp business number. Alternatively, you can use the [Twilio Sandbox for WhatsApp](/docs/whatsapp/sandbox) to get started.
* The [Twilio CLI](/docs/twilio-cli/quickstart) installed on your machine.
* The [Serverless Toolkit](/docs/labs/serverless-toolkit) installed on your machine.

## Setup

To trigger your AI Assistant via incoming messages, you will configure an incoming message webhook on your Twilio phone number to forward messages to your AI Assistant.

### Deploy the webhook code

You only have to deploy this webhook into your Twilio account once, regardless of how many AI Assistants or phone numbers you intend to use.

To deploy the webhooks, clone the [AI Assistants Samples repository](https://github.com/twilio-labs/ai-assistants-samples) and deploy the [Twilio Functions](/docs/serverless/functions-assets) into your Twilio account.

```bash {title="Install and deploy the webhooks"}
git clone git@github.com:twilio-labs/ai-assistants-samples.git
cd ai-assistants-samples
npm install
cp .env.example .env
# you can ignore editing the .env if you don't intend to use other tools in the project

twilio serverless:deploy
```

After running the above commands, your output should look similar to this:

```text {title="Example deployment output"}
Deployment Details
Domain: ai-assistants-samples-1111-dev.twil.io
Service:
   ai-assistants-samples (ZSf3510841424c854e3f3b282550211111)
Environment:
   dev (ZE94900e7f2a2c330b15cf6e1c9fd11111)
Build SID:
   ZB2743d62d52d42ccd55873a0bcd511111
Runtime:
   node18
View Live Logs:
   https://www.twilio.com/console/functions/editor/ZSf3510841424c854e3f3b282550211111/environment/ZE94900e7f2a2c330b15cf6e1c9fd11111
Functions:
   [protected] https://ai-assistants-samples-1111-dev.twil.io/channels/conversations/messageAdded
   https://ai-assistants-samples-1111-dev.twil.io/channels/messaging/incoming
   [protected] https://ai-assistants-samples-1111-dev.twil.io/channels/conversations/response
   https://ai-assistants-samples-1111-dev.twil.io/channels/messaging/response
   https://ai-assistants-samples-1111-dev.twil.io/tools/flex-handover
   https://ai-assistants-samples-1111-dev.twil.io/tools/google-maps
   https://ai-assistants-samples-1111-dev.twil.io/tools/internet-search
   https://ai-assistants-samples-1111-dev.twil.io/tools/studio-handover
Assets:
```

Make sure you take note of the output behind `Domain`; this is your Functions URL.

### Configure your webhook for SMS

If you use the Twilio Console, open your [SMS-capable phone number](https://www.twilio.com/console/phone-numbers/incoming) of choice or [Messaging Service](https://www.twilio.com/console/sms/services) in the Twilio Console and configure the **When a message comes in webhook** to point to: `https://<your-functions-domain>.twil.io/channels/messaging/incoming?AssistantSid=<your-assistant-sid>`.
You can find your Assistant's SID in the Twilio Console under your Assistant's configuration.

Alternatively, you can use the Twilio CLI to configure the webhook by running the following command:

```bash {title="Set an Incoming Message webhook"}
twilio phone_number <your-twilio-number> \
    --sms-url=https://<your-functions-domain>.twil.io/channels/messaging/incoming?AssistantSid=<your-assistant-sid>
```

Make sure you replace `<your-functions-domain>` and `<your-assistant-sid>` with the respective values from the steps above. You can find your [Assistant's SID in the AI Assistants section of the Twilio Console](https://console.twilio.com/us1/develop/ai-assistants/assistants).

### Configure your webhook for the Sandbox for WhatsApp

If you are using the WhatsApp Sandbox for this guide, configure your **When a message comes in** webhook in the [WhatsApp Sandbox Settings](https://console.twilio.com/us1/develop/sms/try-it-out/whatsapp-learn?frameUrl=%2Fconsole%2Fsms%2Fwhatsapp%2Flearn%3Fx-target-region%3Dus1) to point to `https://<your-functions-domain>.twil.io/channels/messaging/incoming?AssistantSid=<your-assistant-sid>`

Make sure you replace `<your-functions-domain>` and `<your-assistant-sid>` with the respective values from the steps above.

### Test your configuration

Send a message to your Twilio phone number or WhatsApp sandbox, and you should receive a response from your AI Assistant.

## Learn more

* [Learn how to connect your AI Assistant to Twilio Conversations](/docs/alpha/ai-assistants/code-samples/channel-conversations)
