Programmable Chat for Autopilot
Programmable Chat will no longer be available or supported after July 25, 2022. Instead, we'll be focusing on the next generation of chat: Twilio Conversations. Find out more about the EOL process here.
If you're starting a new project, please visit the Programmable Conversations Docs to begin. If you've already built on Programmable Chat, please visit our Migration Guide to learn about how to switch.
Learn how to build web and mobile chat bots with Autopilot and Programmable Chat. First, you need setup Programmable Chat, then you can connect to Autopilot. We'll walk you through both steps.
Setting up Programmable Chat
To get started with Programmable Chat check out the Quickstarts for Javascript, iOS and Android:
- Programmable Chat Javascript Quickstart
- Programmable Chat iOS Quickstart
- Programmable Chat Android Quickstart
After the quickstart,you should have a working Chat Service Instance.
Connecting Chat to Autopilot
To use Autopilot with Programmable Chat follow the steps below:
- Initialize Chat
- Create a Channel and join it
- Add Autopilot URL to Channel Webhook
- Programmable Chat Standalone
- Programmable Chat through Studio
- Handle Autopilot messages in your Chat UI
- Test it!
- When finished, remove the Bot from the channel
1. Initialize Chat Client
For every end user session you need to initialize a chat client with an identity following the instructions below:
2. Create a channel and join channel
Once you have the chat client initiated, you need to create a new channel. You should create a channel for every interaction with the bot so that the user starts with an empty chat. Once you create the channel you need to join it.
Create a channel
- Javascript SDK
- iOS SDK - Objective-C - Swift
- Android SDK
- REST API
Join Channel
- Javascript SDK
- iOS SDK - Objective-C - Swift
- Android SDK
- REST API
3. Add Autopilot URL to Channel
You can use Autopilot with Programmable Chat directly or through Studio. Below are the instructions to configure Programmable Chat both ways. You should configure one or the other, but not both.
3.1. Programmable Chat
For every Programmable Chat Channel, you need to add a webhook that points to Autopilot so that Autopilot get's the end-user's message and knows where to respond.
POST /Services/ISXXX/Channels/CHXXX/Webhooks/
request parameters
Attribute |
Value |
Type |
webhook |
Configuration.Url |
{Your Autopilot Programmable Chat URL} |
Configuration.Method |
POST |
Configuration.Filters |
onMessageSent |
Make sure to replace:
ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - with the Chat Service Instance Sid you are using
CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - with the Channel you created for the user
ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - with the Account Sid
{ProgrammableChatURL} - with your Assistant's Programmable Chat URL (Channels -> Programmable Chat in Console)
3.2 Programmable Chat through Studio
For every Programmable Chat Channel, you need to add a webhook that points to Studio so that the Autopilot widget in Studio get's the end-user's message and knows where to respond. To enable incoming Chat messages, add the Studio Flow's webhook URL as a Chat Channel webhook.
POST /Services/ISXXX/Channels/CHXXX/Webhooks/
request parameters
Attribute |
Value |
Type |
studio |
Configuration.FlowSid |
|
Make sure to replace:
ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - with the Chat Service Instance Sid you are using
CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - with the Channel you created for the user
ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - with the Account Sid
FWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX - with the Studio Flow Sid
New messages posted in the Chat Channel will create a new Studio Execution, enabling the Studio Flow to interact with the Chat user.
Autopilot URL Parameters
Parameter | Description |
TargetTask |
A string representing the unique name of the destination Task. If no TargetTask is provided, the destination task is determined by the natural language understanding. TargetTask is useful when you want to control what task a user is routed to. You may want users that write in from different web pages to land in different tasks |
Debug=true |
Adds the memory to the Programmable Chat Message attributes. |
Memory |
Lets you send Inbound Context to Autopilot. Should be a JSON string containing key-value pairs to insert into the Autopilot memory before starting a dialogue with the bot. Useful for passing data stored in third party systems like a CRM into Autopilot to deliver a more contextual experience. |
Example 1: Using TargetTask parameter at the end of the URL
https://channels.autopilot.twilio.com/v1/AC005c432c1f038a6fcf60e7708db371f4/UAbe96379c4e0ff73bd6b1d5e345de172e/twilio-chat?TargetTask=life_insurance_home
Any message sent to a bot with this URL will land in the life_insurance_home task.
Example 2: Passing Inbound Context with Memory
https://channels.autopilot.twilio.com/v1/<ACCOUNT_SID>/<ASSISTANT_SID>/twilio-chat?Memory={"CarModel":"Diablo","CarMake":"Lamborghini","CarYear":"2019"}
Any message sent to a bot with this URL will insert CarModel, CarMake and CarYear into the Autopilot Memory.
Triggering Webhooks for REST API Events
Upon configuration, only actions from SDK-driven clients (like mobile phones or browsers) will cause webhooks without further action on your part. This includes both Service-level webhooks and Channel-Scoped Webhooks, and as a default behavior helps avoid infinite feedback loops.
Learn more about how webhooks work with Programmable Chat here.
Headers are not currently supported by the Twilio REST Helper Libraries. If you want to enable webhooks from REST, you will need to construct Twilio REST requests in a native HTTP library.
4. Handle Autopilot messages in your Chat UI
Behind the scenes, Autopilot uses the Programmable Chat REST API to publish messages from your Assistant to the Chat Channel using the default value of system
as the from
parameter on the message. To change this, replace this value with the name of your Assistant or any other value you want when implementing the SDK on the front end. Learn more about Android, Javascript and iOS SDKs here.
5. Test it!
You are all set. Send a message to the channel, this will go to Autopilot and Autopilot will respond. You can check the Autopilot Logs -> Queries to make sure the message is getting to Autopilot. You should see which task it is hitting. Then you can program each Tasks with the the desired conversational experience.
6. When finished, remove the Bot from the Channel
When the interaction s complete or you want a human to take over you just need to remove the webhook from the Channel.
Need some help?
We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.