Skip to contentSkip to navigationSkip to topbar
On this pageProducts used

Use Twilio Messaging as a Channel


(warning)

Twilio Alpha - Developer Preview

AI Assistants is a Twilio Alpha(link takes you to an external page) project that's in Developer Preview.

View the current limitations 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. To get started, you will need the following:


Setup

setup page anchor

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

deploy-the-webhook-code page anchor

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(link takes you to an external page) and deploy the Twilio Functions into your Twilio account.

Install and deploy the webhooks

install-and-deploy-the-webhooks page anchor
1
git clone git@github.com:twilio-labs/ai-assistants-samples.git
2
cd ai-assistants-samples
3
npm install
4
cp .env.example .env
5
# you can ignore editing the .env if you don't intend to use other tools in the project
6
7
twilio serverless:deploy

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

Example deployment output

example-deployment-output page anchor
1
Deployment Details
2
Domain: ai-assistants-samples-1111-dev.twil.io
3
Service:
4
ai-assistants-samples (ZSf3510841424c854e3f3b282550211111)
5
Environment:
6
dev (ZE94900e7f2a2c330b15cf6e1c9fd11111)
7
Build SID:
8
ZB2743d62d52d42ccd55873a0bcd511111
9
Runtime:
10
node18
11
View Live Logs:
12
https://www.twilio.com/console/functions/editor/ZSf3510841424c854e3f3b282550211111/environment/ZE94900e7f2a2c330b15cf6e1c9fd11111
13
Functions:
14
[protected] https://ai-assistants-samples-1111-dev.twil.io/channels/conversations/messageAdded
15
https://ai-assistants-samples-1111-dev.twil.io/channels/messaging/incoming
16
[protected] https://ai-assistants-samples-1111-dev.twil.io/channels/conversations/response
17
https://ai-assistants-samples-1111-dev.twil.io/channels/messaging/response
18
https://ai-assistants-samples-1111-dev.twil.io/tools/flex-handover
19
https://ai-assistants-samples-1111-dev.twil.io/tools/google-maps
20
https://ai-assistants-samples-1111-dev.twil.io/tools/internet-search
21
https://ai-assistants-samples-1111-dev.twil.io/tools/studio-handover
22
Assets:

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

Configure your webhook for SMS

configure-your-webhook-for-sms page anchor

If you use the Twilio Console, open your SMS-capable phone number(link takes you to an external page) of choice or Messaging Service(link takes you to an external page) 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:

Set an Incoming Message webhook

set-an-incoming-message-webhook page anchor
1
twilio phone_number <your-twilio-number> \
2
--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(link takes you to an external page).

Configure your webhook for the Sandbox for WhatsApp

configure-your-webhook-for-the-sandbox-for-whatsapp page anchor

If you are using the WhatsApp Sandbox for this guide, configure your When a message comes in webhook in the WhatsApp Sandbox Settings(link takes you to an external page) 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.

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


Need some help?

Terms of service

Copyright © 2024 Twilio Inc.