# 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

First, let's create a basic Studio Flow:

* Log into your Twilio account in the [Twilio Console](https://www.twilio.com/console)
* Navigate to the [Studio Flow section](https://www.twilio.com/console/studio/flows) in the Console
* Click the **+** icon underneath the **Flows** heading to create a new Flow

  ![Click the plus icon to create a new flow in the Flows section.](https://docs-resources.prod.twilio.com/fdaf3d691d56c3e9444323c5d5ccfc9501da69d721db585059b56fea9f70883a.png)
* Name your Flow and click **Next ![New Flow selection with options for starting from scratch, appointment reminders, IVR, and messaging chatbot.](https://docs-resources.prod.twilio.com/478f3d0bfcbda629433e791ce7451971f309c682246de5ae0763e183eb6a3c09.png)**
* You'll see a list of possible templates you can use. Select the **Start from scratch** option and click **Next**

  ![Template Flow.](https://docs-resources.prod.twilio.com/b7401b9c3f099abf5beb8421b42eea43cd3cce68e8b09f90ed398adb47545a50.png)

Once you've created the Flow, you'll see the Flow's [Canvas](/docs/studio/user-guide/get-started#navigate-the-canvas). Let's build out the rest of the project's logic.

## Add Widgets to the Canvas

Studio Flows are built from [Widgets](/docs/studio/user-guide/get-started#work-with-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](/docs/studio/widget-library/send-wait-reply) Widget to the "Incoming Conversation" [Trigger](/docs/studio/widget-library/trigger-start).

![Flowchart with trigger, send and reply, and split based on widgets configuration.](https://docs-resources.prod.twilio.com/cb31009f08b670d8912980e28db449ebf0f89c49eb1ef8df62a2fa9caaefee5a.png)

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.

![Configuring message body 'Hi, do you like bots?' in send and reply widget.](https://docs-resources.prod.twilio.com/62dedc7fae89cd10390160a7e3e69ebbcdc8ad62f127b6625541b8bb965c84e0.png)

Next, add to the "Reply" [transition](/docs/studio/user-guide/get-started#define-widget-transitions) 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.](https://docs-resources.prod.twilio.com/a91d0e59e7f78780884508391ea3db3a162512ff2ab48f8519948d3153f315e6.png)

In the [Split Based On... Transitions](/docs/studio/widget-library/split-based-on#split-based-on-transitions) tab, let's add conditions that match the possible responses you want to test for:

![Flowchart showing response conditions to 'Hi, do you like bots?' with yes or no paths.](https://docs-resources.prod.twilio.com/74b2a9d306236e76774866fbf6386643966c51cf7e7b467878cc7f9ee495e5d2.png)

The [Split Based On... Widget](/docs/studio/widget-library/split-based-on) 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:

![Flowchart showing message sending logic with split conditions and outcomes.](https://docs-resources.prod.twilio.com/d9d6a1261bbf3c74852f0e513c2eae363e5b1808a19082e132069bac8e53f449.png)

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

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

> \[!NOTE]
>
> You'll need your Studio Flow's SID `(FWXXX)`. You can get this in a few places, like the [Studio Flow Console page](https://twilio.com/console/studio/flows) or in the URL when you're editing the Flow.

> \[!NOTE]
>
> Your Twilio Phone Number should be in [E.164 format](/docs/glossary/what-e164), like this: `+12345678901`

For this example, we'll use the [Address Configuration API](/docs/conversations/api/address-configuration-resource). We're using SMS here, but you could also use other channels we support, like WhatsApp or Messenger.

```bash
twilio api:conversations:v1:configuration:addresses:create \
--type sms \
--address your_twilio_number \
--auto-creation.enabled \
--auto-creation.type studio \
--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](/docs/conversations/api/address-configuration-resource#delete-an-addressconfiguration-resource).

### Existing Conversations

Alternatively, you can set this up manually on a specific Conversation. First, [create a Conversation](/docs/conversations/api/conversation-resource):

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

```

Next, add an external [Participant](/docs/conversations/api/conversation-participant-resource#add-a-conversation-participant-sms) (for this example, we'll use SMS, but it could be any channel):

> \[!NOTE]
>
> Make sure to replace `CHXXXXX` with your Conversations SID for the Conversation you created in the step above.

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

Finally, add a [Conversation-Scoped Webhook](/docs/conversations/api/conversation-scoped-webhook-resource) that points to Studio:

```bash
twilio api:conversations:v1:conversations:webhooks:create \
--target studio \
--conversation-sid CHXXXXXXXXXXX \
--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! 🎉

![Text conversation with a bot asking if the user likes bots, user replies yes.](https://docs-resources.prod.twilio.com/3114df9ffd5a71d98a413b236ff533224eccc6c54afe31887165969cebf7a924.jpg)

## Optional: Importing Flow Data from JSON

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:

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

```

## What's Next?

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

* Learn how to use [Facebook Messenger with Twilio Conversations](/docs/conversations/facebook-messenger)
* Explore [Conversations API Quickstart](/docs/conversations/quickstart)
