Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Connecting Twilio Studio to Conversations


Twilio Studio is a flexible low-code/no-code application builder for creating your own chatbots and interaction flows triggered by an incoming message from an end-user. You can leverage Studio to automate some level of interaction with your users.

By connecting to Studio via Conversations, you create a distinct message thread for that interaction and unlock the possibility of adding human Participants from other channels after you complete your automated handling. For example, you could add an agent from a web or mobile app that implements the Conversations SDK, or you could connect them to someone via SMS or WhatsApp.

Let's get started!


Building a Studio Flow

building-a-studio-flow page anchor

First, let's create a basic Studio Flow:

  • Log into your Twilio account in the Twilio Console(link takes you to an external page)
  • Navigate to the Studio Flow section(link takes you to an external page) in the Console
  • Click the + icon underneath the Flows heading to create a new Flow

    Create a new Studio Flow.
  • Name your Flow and click Next
    Create a new Flow.
  • You'll see a list of possible templates you can use. Select the Start from scratch option and click Next

    Template Flow.

Once you've created the Flow, you'll see the Flow's Canvas. Let's build out the rest of the project's logic.


Add Widgets to the Canvas

add-widgets-to-the-canvas page anchor

Studio Flows are built from Widgets. These items represent pieces of logic that allow you to handle incoming actions and respond accordingly by performing specific tasks, like sending a message.

Let's start by adding a Send & Wait For Reply Widget to the "Incoming Conversation" Trigger.

Add Widgets to the Canvas.

Then, click on the Send & Wait For Reply Widget to show its Inspector Panel. In the Config tab, add a message body (i.e. "Hi, do you like bots?") and click Save. This tells Studio to receive the incoming message, and then reply with the message you see there in the Widget.

Add body message to the widget.

Next, add to the "Reply" transition the "Split Based On…" Widget to parse the user response.

Then, open the Split Based On... Widget Inspect Panel and in the Config tab, select the "inbound.Body" option for the "Variable to test" field. Click Save.

Split Based On Widget.

In the Split Based On... Transitions tab, let's add conditions that match the possible responses you want to test for:

Conditions that match possible responses to test.

The Split Based On... Widget lets you access a variable and test conditions on it to determine how to react. In this case, we're testing the body of the message the user sent in response to your "Hi, do you like bots?" message.

Finally, let's add Send Message Widgets on each transition with the response you want to send:

Send Message Widget.

This will reply with different messages depending on how the user replied to your initial message. Your Canvas is now set up! To publish the Flow, click Publish from the top Canvas menu.


Connecting to Conversations

connecting-to-conversations page anchor

There are two ways to connect Twilio Studio to Conversations. First, you can set a particular "sender" (e.g. Twilio SMS number or Twilio WhatsApp sender) to automatically create a new Conversation when it receives a message that wouldn't be mapped to an existing Conversation. Second, you can add the Studio Flow to a Conversation that already exists.

Autocreating Conversations

autocreating-conversations page anchor
(information)

Info

You'll need your Studio Flow's SID (FWXXX). You can get this in a few places, like the Studio Flow Console page(link takes you to an external page) or in the URL when you're editing the Flow.

(information)

Info

Your Twilio Phone Number should be in E.164 format, like this: +12345678901

For this example, we'll use the Address Configuration API. We're using SMS here, but you could also use other channels we support, like WhatsApp or Messenger.


_10
twilio api:conversations:v1:configuration:addresses:create \
_10
--type sms \
_10
--address your_twilio_number \
_10
--auto-creation.enabled \
_10
--auto-creation.type studio \
_10
--auto-creation.studio-flow-sid FWXXXXXXX

Well done! Now, inbound messages to this address (Twilio phone number) will create a new Conversation (if there isn't one for that number pair) and add the Studio Flow to it.

Note: To disable inbound conversation autocreation, delete the Address Configuration.

Alternatively, you can set this up manually on a specific Conversation. First, create a Conversation:


_10
twilio api:conversations:v1:conversations:create \
_10
—-friendly-name "studio_test"

Next, add an external Participant (for this example, we'll use SMS, but it could be any channel):

(information)

Info

Make sure to replace CHXXXXX with your Conversations SID for the Conversation you created in the step above.


_10
twilio api:conversations:v1:conversations:participants:create \
_10
--conversation-sid CHXXXXXXXXXXXXX \
_10
--messaging-binding.address +15558675310 \
_10
--messaging-binding.proxy-address your_twilio_number

Finally, add a Conversation-Scoped Webhook that points to Studio:


_10
twilio api:conversations:v1:conversations:webhooks:create \
_10
--target studio \
_10
--conversation-sid CHXXXXXXXXXXX \
_10
--configuration.flow-sid FWXXXXXXXXXXX

Send a text to the ProxyAddress you specified in your MessagingBinding using the phone number you used as the Address and watch the magic happen! 🎉

Conversations-example-new.

Optional: Importing Flow Data from JSON

optional-importing-flow-data-from-json page anchor

Instead of adding Widgets to the Canvas, you can import this basic tutorial Flow. You can do this by creating a new Flow, selecting Import from JSON from the list of templates in the selection modal, and pasting the JSON Flow definition into the import window.

Example JSON Flow definition:


_178
{
_178
"description": "A New Flow",
_178
"states": [
_178
{
_178
"name": "Trigger",
_178
"type": "trigger",
_178
"transitions": [
_178
{
_178
"event": "incomingMessage"
_178
},
_178
{
_178
"event": "incomingCall"
_178
},
_178
{
_178
"next": "send_and_reply_1",
_178
"event": "incomingConversationMessage"
_178
},
_178
{
_178
"event": "incomingRequest"
_178
},
_178
{
_178
"event": "incomingParent"
_178
}
_178
],
_178
"properties": {
_178
"offset": {
_178
"x": 0,
_178
"y": 0
_178
}
_178
}
_178
},
_178
{
_178
"name": "send_and_reply_1",
_178
"type": "send-and-wait-for-reply",
_178
"transitions": [
_178
{
_178
"next": "split_1",
_178
"event": "incomingMessage"
_178
},
_178
{
_178
"event": "timeout"
_178
},
_178
{
_178
"event": "deliveryFailure"
_178
}
_178
],
_178
"properties": {
_178
"offset": {
_178
"x": 70,
_178
"y": 170
_178
},
_178
"service": "{{trigger.message.InstanceSid}}",
_178
"channel": "{{trigger.message.ChannelSid}}",
_178
"from": "{{flow.channel.address}}",
_178
"body": "Hi, do you like bots?",
_178
"timeout": "3600"
_178
}
_178
},
_178
{
_178
"name": "send_message_1",
_178
"type": "send-message",
_178
"transitions": [
_178
{
_178
"event": "sent"
_178
},
_178
{
_178
"event": "failed"
_178
}
_178
],
_178
"properties": {
_178
"offset": {
_178
"x": -280,
_178
"y": 600
_178
},
_178
"service": "{{trigger.message.InstanceSid}}",
_178
"channel": "{{trigger.message.ChannelSid}}",
_178
"from": "{{flow.channel.address}}",
_178
"to": "{{contact.channel.address}}",
_178
"body": "👻"
_178
}
_178
},
_178
{
_178
"name": "send_message_2",
_178
"type": "send-message",
_178
"transitions": [
_178
{
_178
"event": "sent"
_178
},
_178
{
_178
"event": "failed"
_178
}
_178
],
_178
"properties": {
_178
"offset": {
_178
"x": 30,
_178
"y": 600
_178
},
_178
"service": "{{trigger.message.InstanceSid}}",
_178
"channel": "{{trigger.message.ChannelSid}}",
_178
"from": "{{flow.channel.address}}",
_178
"to": "{{contact.channel.address}}",
_178
"body": "🤖"
_178
}
_178
},
_178
{
_178
"name": "send_message_3",
_178
"type": "send-message",
_178
"transitions": [
_178
{
_178
"event": "sent"
_178
},
_178
{
_178
"event": "failed"
_178
}
_178
],
_178
"properties": {
_178
"offset": {
_178
"x": 340,
_178
"y": 590
_178
},
_178
"service": "{{trigger.message.InstanceSid}}",
_178
"channel": "{{trigger.message.ChannelSid}}",
_178
"from": "{{flow.channel.address}}",
_178
"to": "{{contact.channel.address}}",
_178
"body": "👽"
_178
}
_178
},
_178
{
_178
"name": "split_1",
_178
"type": "split-based-on",
_178
"transitions": [
_178
{
_178
"next": "send_message_1",
_178
"event": "noMatch"
_178
},
_178
{
_178
"next": "send_message_2",
_178
"event": "match",
_178
"conditions": [
_178
{
_178
"friendly_name": "If value matches_any_of yes,Yes",
_178
"arguments": [
_178
"{{widgets.send_and_reply_1.inbound.Body}}"
_178
],
_178
"type": "matches_any_of",
_178
"value": "yes,Yes"
_178
}
_178
]
_178
},
_178
{
_178
"next": "send_message_3",
_178
"event": "match",
_178
"conditions": [
_178
{
_178
"friendly_name": "If value matches_any_of no,No",
_178
"arguments": [
_178
"{{widgets.send_and_reply_1.inbound.Body}}"
_178
],
_178
"type": "matches_any_of",
_178
"value": "no,No"
_178
}
_178
]
_178
}
_178
],
_178
"properties": {
_178
"input": "{{widgets.send_and_reply_1.inbound.Body}}",
_178
"offset": {
_178
"x": -100,
_178
"y": 370
_178
}
_178
}
_178
}
_178
],
_178
"initial_state": "Trigger",
_178
"flags": {
_178
"allow_concurrent_calls": true
_178
}
_178
}


Well done! You've learned how to connect Twilio Studio with Conversations. To continue, check out these other resources:


Rate this page: