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

Using Twilio Conversations with Studio


Twilio Conversations enables back-and-forth messaging across various communication channels. This means multiple users from different supported messaging platforms are able to seamlessly communicate with one another. Use Studio to handle interactions or logic for incoming Conversation messages and leverage the powerful capabilities of Conversations' cross-channel and multi-user support.


Why should you use Conversations with Studio

why-should-you-use-conversations-with-studio page anchor

Conversations provides the ability to add or remove Participants that are interacting with a Flow's Execution, creating the opportunity to add support agents at any point to assist users with your Studio application. Additionally, the Conversations integration enables collaborative capabilities within your application as multiple Participants can provide input and move the same Flow Execution forward.


Connecting a Conversation to your Flow

connecting-a-conversation-to-your-flow page anchor

To connect a Twilio Conversation to your Studio Flow, check out the following tutorials:


Detecting incoming Conversation messages

detecting-incoming-conversation-messages page anchor

A Studio Flow's Execution will start when a message is created in the Conversation that is connected to the Flow. Use the Incoming Conversation transition within the Trigger (Start) Widget. Once a message is sent in a Conversation, the Widget connected to the Incoming Conversation transition will be run.

(warning)

Warning

If you are using Flex Conversations, remember to handle certain terminal TaskRouter events according to you application's needs, as conversations may stay orphaned with an open state.

See Conversations Best Practices for details.


Example: Add a Support Agent

example-add-a-support-agent page anchor

You can modify Conversations Participants using the Run Function Widget along with a Twilio Function. A Twilio Function will run code that makes a request to the Conversations API to add or remove Participants.

In this example, customers will send the keyword "support" as a Conversation message. The Studio Flow will detect that the keyword was sent and add a Participant to the Conversation. The Participant will be the support agent in this case.

Prerequisites

prerequisites page anchor

1. Check if the "support" keyword is sent as a message in the Conversation

1-check-if-the-support-keyword-is-sent-as-a-message-in-the-conversation page anchor

The Incoming Conversation transition from the Trigger (Start) Widget is used to detect and start the Studio Flow when a new message is sent in the Conversation the Studio Flow is connected to. All messages sent within the Studio Flow will be delivered to all Participants in the Conversation.

Add a Split Based On… Widget to the Canvas to check if the keyword "support" is sent within the Conversation. Rename the Widget to check-support. Liquid variables are values stored within a Studio Flow that can be used by your Widgets. When checking for the "support" keyword, the Split Based On… Widget evaluates the {{trigger.conversation.Body}} variable. This variable holds the message that was sent in the Conversation. In the Variable to Test field, enter {{trigger.conversation.Body}}.

Twilio Studio Tutorial Using Conversations Split Based On Widget... on the Canvas checking the Liquid variable for the support keyword.

_25
exports.handler = function(context, event, callback) {
_25
_25
// get the Twilio client
_25
const client = context.getTwilioClient();
_25
_25
// store the Conversation ID passed in from the Studio Flow
_25
const conversationsSid = event.conversationID;
_25
_25
// add a participant to the Conversation
_25
client.conversations
_25
.conversations(conversationsSid)
_25
.participants
_25
.create({
_25
'messagingBinding.address': 'enter-support-agent-phone-number',
_25
'messagingBinding.proxyAddress': 'enter-twilio-phone-number'
_25
})
_25
.then(participant => {
_25
console.log(participant.sid);
_25
return callback(null, "Success");
_25
})
_25
.catch(err => {
_25
console.log(err);
_25
callback(err)
_25
})
_25
};

3. Invoke the Twilio Function using the Run Function Widget

3-invoke-the-twilio-function-using-the-run-function-widget page anchor

Add a Run Function Widget, which runs code you write in a Twilio Function, to the Canvas. Rename the Run Function Widget to add-support-agent. Connect the Support transition you created in the check-support Widget to this Widget by clicking the dangling dot under the Support transition and dragging it to the Run Function Widget before letting go.

Twilio Studio Tutorial Conversations Run Function Widget on the Canvas with the Configuration panel shown.Rate this page:

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.