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

Send Outbound Messages via SMS, WhatsApp and Other Channels


(information)

Info

This guide is for Flex UI 1.x and channels that use Programmable Chat and Proxy. If you are using Flex UI 2.x or you are starting out, we recommend that you build with Flex Conversations.

Flex allows your agents to initiate conversations with your customers via any Twilio Messaging Channel, including SMS, WhatsApp, and Facebook Messenger.

Currently, outbound messages require additional development work. This page outlines some of the work you'll need to do for the backend but outbound messages will also require a Flex UI Plugin to expose the functionality in the Flex interface.


Managing Task Creation

managing-task-creation page anchor

There are two ways to handle task creation for outbound messages: when the agent messages the customer (immediately) or when the customer responds to the outbound message (delayed).

(information)

Info

Outbound messages require a Flex Flow ChannelType of sms, whatsapp, facebook, or custom.


Strategy 1: Create Task When Agent Messages Customer

strategy-1-create-task-when-agent-messages-customer page anchor

The following steps will create a task when you send an outbound message to your customer.

1. Define a Flex Flow

1-define-a-flex-flow page anchor

First, define an outbound Flex Flow with an Integration type of task. You only need to do this once. Please ensure you are also defining your Flex Flow to use a Messaging capable Task Channel such as sms or chat. If you already have a Flex Flow that's using Studio, then the enabled parameter must be set to false.

Strategy 1: Create a Flex Flow

strategy-1-create-a-flex-flow page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_20
// Download the helper library from https://www.twilio.com/docs/node/install
_20
// Find your Account SID and Auth Token at twilio.com/console
_20
// and set the environment variables. See http://twil.io/secure
_20
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_20
const authToken = process.env.TWILIO_AUTH_TOKEN;
_20
const client = require('twilio')(accountSid, authToken);
_20
_20
client.flexApi.v1.flexFlow
_20
.create({
_20
contactIdentity: '+12XXXXXXXXX',
_20
enabled: ,
_20
integrationType: 'task',
_20
'integration.workflowSid': 'WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
_20
'integration.workspaceSid': 'WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
_20
'integration.channel': 'TCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
_20
friendlyName: 'Outbound SMS',
_20
chatServiceSid: 'ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXNone',
_20
channelType: 'sms'
_20
})
_20
.then(flex_flow => console.log(flex_flow.sid));

Output

_19
{
_19
"sid": "FOXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_19
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_19
"date_created": "2016-08-01T22:10:40Z",
_19
"date_updated": "2016-08-01T22:10:40Z",
_19
"friendly_name": "Outbound SMS",
_19
"chat_service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_19
"channel_type": "sms",
_19
"contact_identity": "+12XXXXXXXXX",
_19
"enabled": ,
_19
"integration_type": "task",
_19
"integration": {
_19
"flow_sid": "FWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_19
"retry_count": 1
_19
},
_19
"long_lived": true,
_19
"janitor_enabled": true,
_19
"url": "https://flex-api.twilio.com/v1/FlexFlows/FOXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_19
}

2. Create a Flex Chat Channel

2-create-a-flex-chat-channel page anchor

Next, create a Chat Channel using the Flex API to initiate messaging with the customer. In this example, the API call also adds Task Attributes, which will allow the channel to route the task to a specific agent (using the targetWorker Workflow expression).

Make sure to save the SID of the Chat Channel - you'll need it later!

Outbound Flex Channel with TaskRouter integration

outbound-flex-channel-with-taskrouter-integration page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_24
// Download the helper library from https://www.twilio.com/docs/node/install
_24
// Find your Account SID and Auth Token at twilio.com/console
_24
// and set the environment variables. See http://twil.io/secure
_24
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_24
const authToken = process.env.TWILIO_AUTH_TOKEN;
_24
const client = require('twilio')(accountSid, authToken);
_24
_24
client.flexApi.v1.channel
_24
.create({
_24
target: '+18001231234',
_24
taskAttributes: JSON.stringify({
_24
to: '+18001231234',
_24
direction: 'outbound',
_24
name: 'John',
_24
from: '+18005555555',
_24
targetWorker: 'reference_worker_attribute_contact_uri_here',
_24
autoAnswer: 'true'
_24
}),
_24
identity: 'sms_18001231234',
_24
chatFriendlyName: 'Outbound Chat with John',
_24
flexFlowSid: 'FOXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
_24
chatUserFriendlyName: 'chat_user_friendly_name'
_24
})
_24
.then(channel => console.log(channel.sid));

Output

_10
{
_10
"flex_flow_sid": "FOXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10
"sid": "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10
"task_sid": "WTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10
"user_sid": "USXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10
"date_created": "2016-08-01T22:10:40Z",
_10
"date_updated": "2016-08-01T22:10:40Z",
_10
"url": "https://flex-api.twilio.com/v1/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_10
}

Once the Flex Channel API is executed successfully, two things happen:

  • A new chat channel is created
  • A new task is created and assigned to the worker specified in targetWorker

Then, if you open the Flex UI for targetWorker, you should see the following new reservation:

3. Set up a Twilio Proxy Session

3-set-up-a-twilio-proxy-session page anchor

Now that you have a channel, you'll want to associate it with a Proxy Session. Sessions offer a useful layer of state and metadata that Flex uses to ensure that your customers' messages stay properly threaded as they are processed by bots, TaskRouter and agents.

You'll need to make two requests:

The first creates the Proxy Session, and includes the person you're trying to reach.

The second request adds the chat channel to the session, and maps it to the Contact Center number.

(information)

Info

Consider enabling the Channel Janitor to help manage any stale chat sessions.

Strategy 1: Create a Proxy Session for Chat Channel

strategy-1-create-a-proxy-session-for-chat-channel page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_15
// Download the helper library from https://www.twilio.com/docs/node/install
_15
// Find your Account SID and Auth Token at twilio.com/console
_15
// and set the environment variables. See http://twil.io/secure
_15
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_15
const authToken = process.env.TWILIO_AUTH_TOKEN;
_15
const client = require('twilio')(accountSid, authToken);
_15
_15
client.proxy.v1.services('KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_15
.sessions
_15
.create({
_15
uniqueName: 'SID_FROM_CHANNELS_API',
_15
mode: 'message-only',
_15
participants: [{'Identifier': 'CUSTOMER_NUMBER'}]
_15
})
_15
.then(session => console.log(session.sid));

Output

_21
{
_21
"service_sid": "KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_21
"status": "open",
_21
"unique_name": "SID_FROM_CHANNELS_API",
_21
"date_started": "2015-07-30T20:00:00Z",
_21
"date_ended": "2015-07-30T20:00:00Z",
_21
"date_last_interaction": "2015-07-30T20:00:00Z",
_21
"date_expiry": "2015-07-30T20:00:00Z",
_21
"ttl": 3600,
_21
"mode": "message-only",
_21
"closed_reason": "",
_21
"sid": "KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_21
"date_updated": "2015-07-30T20:00:00Z",
_21
"date_created": "2015-07-30T20:00:00Z",
_21
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_21
"url": "https://proxy.twilio.com/v1/Services/KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Sessions/KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_21
"links": {
_21
"interactions": "https://proxy.twilio.com/v1/Services/KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Sessions/KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Interactions",
_21
"participants": "https://proxy.twilio.com/v1/Services/KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Sessions/KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants"
_21
}
_21
}

In the sample code, you need to replace:

AttributeDescription
{{CUSTOMER_NUMBER}}The phone number of the customer you are trying to reach
{{SID_FROM_CHANNELS_API}}The SID for the Chat Channel created in Step 2. Here, it is being used as a name for the Proxy Session, which helps you (and Flex) find it.

You'll also need to add the agent to the Proxy Session, using their Chat Channel SID as their identifier and the Contact Center number as their Proxy identifier, since messages from the agent should look like they're coming from the Contact Center.

Finally, if you want the agent to see the Customer's number when they respond to the chat, make the Agent's friendly name the customer's number.

Strategy 1: Add the Agent to the Proxy Session

strategy-1-add-the-agent-to-the-proxy-session page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_16
// Download the helper library from https://www.twilio.com/docs/node/install
_16
// Find your Account SID and Auth Token at twilio.com/console
_16
// and set the environment variables. See http://twil.io/secure
_16
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_16
const authToken = process.env.TWILIO_AUTH_TOKEN;
_16
const client = require('twilio')(accountSid, authToken);
_16
_16
client.proxy.v1.services('KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_16
.sessions('KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_16
.participants
_16
.create({
_16
proxyIdentifier: 'CONTACT_CENTER_NUMBER',
_16
friendlyName: 'CUSTOMER_NUMBER',
_16
identifier: 'CHAT_CHANNEL_SID'
_16
})
_16
.then(participant => console.log(participant.sid));

Output

_17
{
_17
"sid": "KPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_17
"session_sid": "KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_17
"service_sid": "KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_17
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_17
"identifier": "CHAT_CHANNEL_SID",
_17
"proxy_identifier": "CONTACT_CENTER_NUMBER",
_17
"proxy_identifier_sid": "PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_17
"friendly_name": "CUSTOMER_NUMBER",
_17
"date_deleted": "2015-07-30T20:00:00Z",
_17
"date_updated": "2015-07-30T20:00:00Z",
_17
"date_created": "2015-07-30T20:00:00Z",
_17
"url": "https://proxy.twilio.com/v1/Services/KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Sessions/KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants/KPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_17
"links": {
_17
"message_interactions": "https://proxy.twilio.com/v1/Services/KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Sessions/KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants/KPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/MessageInteractions"
_17
}
_17
}

In the sample code, you need to replace:

CONTACT_CENTER_NUMBERThis is the contact center phone number, channel number, or agent number from which the message will be sent. This is likely the same number you used as contactIdentity when you created the Flex Flow.
CHAT_CHANNEL_SIDThe SID of the chat channel you created using the Channel API in Step 2. This tells Proxy that the agent will be sending messages over this channel.

3. Update the Chat Channel Attributes

3-update-the-chat-channel-attributes page anchor

Update the Chat Channel attributes to include the Proxy Session SID you created in Step 3 if it's not already there. The chat channel attributes should be a JSON string:

{proxySession: "KCXXXXXXXXXXXXXXXXXXXXXXXX"}

Make sure that you retrieve the existing attributes object, add the proxySession key, and then update the parameter as a string.

Node.js Example:


_10
.then(
_10
attributes => {
_10
return Object.assign(JSON.parse(attributes.attributes), { proxySession: proxySession.sid })
_10
}
_10
))

Strategy 1: Update Chat Channel Attributes

strategy-1-update-chat-channel-attributes page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_13
// Download the helper library from https://www.twilio.com/docs/node/install
_13
// Find your Account SID and Auth Token at twilio.com/console
_13
// and set the environment variables. See http://twil.io/secure
_13
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_13
const authToken = process.env.TWILIO_AUTH_TOKEN;
_13
const client = require('twilio')(accountSid, authToken);
_13
_13
client.chat.v2.services('ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_13
.channels('CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_13
.update({attributes: JSON.stringify({
_13
proxySession: 'KCXXXXXXXXXXXXXXXXXXXXXXXX'
_13
})})
_13
.then(channel => console.log(channel.friendlyName));

Output

_24
{
_24
"sid": "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"friendly_name": "friendly_name",
_24
"unique_name": "unique_name",
_24
"attributes": {
_24
"proxySession": "KCXXXXXXXXXXXXXXXXXXXXXXXX"
_24
},
_24
"type": "public",
_24
"date_created": "2015-12-16T22:18:37Z",
_24
"date_updated": "2015-12-16T22:18:38Z",
_24
"created_by": "username",
_24
"members_count": 0,
_24
"messages_count": 0,
_24
"url": "https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"links": {
_24
"members": "https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Members",
_24
"messages": "https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages",
_24
"invites": "https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Invites",
_24
"webhooks": "https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Webhooks",
_24
"last_message": null
_24
}
_24
}

Now, go back to Flex and accept the reservation. Your agent can directly message into the Chat interface, and messages are routed to the customer on their Twilio Channel.


Strategy 2: Create Task When Customer Responds

strategy-2-create-task-when-customer-responds page anchor

The following steps outline an alternate architecture for sending a message to the customer. When the customer responds, the inbound message handler will create a Task and forward it to an agent.

1. Configure inbound messages

1-configure-inbound-messages page anchor

Make sure you set up an inbound message handler, and that it's configured to create a Task. This will handle the customer's response to the outbound message. Please ensure you are also defining your Flex Flow to use a Messaging capable Task Channel such as sms or chat.

If you already handle inbound messages and want to use that logic, you can use the code sample below to retrieve all Flex Flows on the account - simply choose the SID of the one associated with your current message handler for your desired channel (e.g., the SMS message handler).

Strategy 2: Retrieve all Flex Flows on Account

strategy-2-retrieve-all-flex-flows-on-account page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_10
// Download the helper library from https://www.twilio.com/docs/node/install
_10
// Find your Account SID and Auth Token at twilio.com/console
_10
// and set the environment variables. See http://twil.io/secure
_10
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_10
const authToken = process.env.TWILIO_AUTH_TOKEN;
_10
const client = require('twilio')(accountSid, authToken);
_10
_10
client.flexApi.v1.flexFlow
_10
.list({limit: 20})
_10
.then(flexFlow => flexFlow.forEach(f => console.log(f.sid)));

Output

_32
{
_32
"meta": {
_32
"page": 0,
_32
"page_size": 50,
_32
"first_page_url": "https://flex-api.twilio.com/v1/FlexFlows?PageSize=50&Page=0",
_32
"previous_page_url": "https://flex-api.twilio.com/v1/FlexFlows?PageSize=50&Page=0",
_32
"url": "https://flex-api.twilio.com/v1/FlexFlows?PageSize=50&Page=0",
_32
"next_page_url": "https://flex-api.twilio.com/v1/FlexFlows?PageSize=50&Page=1",
_32
"key": "flex_flows"
_32
},
_32
"flex_flows": [
_32
{
_32
"sid": "FOXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_32
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_32
"date_created": "2016-08-01T22:10:40Z",
_32
"date_updated": "2016-08-01T22:10:40Z",
_32
"friendly_name": "friendly_name",
_32
"chat_service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_32
"channel_type": "sms",
_32
"contact_identity": "12345",
_32
"enabled": true,
_32
"integration_type": "studio",
_32
"integration": {
_32
"flow_sid": "FWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_32
"retry_count": 1
_32
},
_32
"long_lived": true,
_32
"janitor_enabled": true,
_32
"url": "https://flex-api.twilio.com/v1/FlexFlows/FOXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_32
}
_32
]
_32
}

Create a chat channel using the Flex Channel Resource. You'll need the Flex Flow for outbound messages. You'll need a new channel for each interaction.

(information)

Info

You may see multiple SIDs associated with a Flow - some might be for Twilio Studio. You need to use a Flex Flow SID, which will begin with the letters FO.

Create an outbound message handler without a task

create-an-outbound-message-handler-without-a-task page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_16
// Download the helper library from https://www.twilio.com/docs/node/install
_16
// Find your Account SID and Auth Token at twilio.com/console
_16
// and set the environment variables. See http://twil.io/secure
_16
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_16
const authToken = process.env.TWILIO_AUTH_TOKEN;
_16
const client = require('twilio')(accountSid, authToken);
_16
_16
client.flexApi.v1.channel
_16
.create({
_16
target: '+18001231234',
_16
identity: 'sms_18001231234',
_16
chatUserFriendlyName: 'Jane',
_16
chatFriendlyName: 'Chat with Jane',
_16
flexFlowSid: 'FOXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
_16
})
_16
.then(channel => console.log(channel.sid));

Output

_10
{
_10
"flex_flow_sid": "FOXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10
"sid": "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10
"task_sid": "WTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10
"user_sid": "USXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_10
"date_created": "2016-08-01T22:10:40Z",
_10
"date_updated": "2016-08-01T22:10:40Z",
_10
"url": "https://flex-api.twilio.com/v1/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
_10
}

3. Create a Proxy Session

3-create-a-proxy-session page anchor

Now you'll need to create a Proxy Session in order to associate the Chat Channel and the Customer's Phone number together. Sessions offer a useful layer of state and metadata that Flex uses to ensure that your customers' messages stay properly threaded as they are processed by bots, TaskRouter and agents. It will help tell Flex that, when the customer responds, they should be talking to this Agent over this Channel. Pass the channel you created in Step 2 as the Unique Name of the Session.

Make sure that you inclued the 'FriendlyName' in the participants array.

In the sample code, you need to replace:

AttributeDescription
{{CHAT_CHANNEL_SID}}The chat channel that the agent will be using to respond to the customer, which you created in Step 2.
{{CONTACT_CENTER_NUMBER}}The contact center/agent ID (the number the SMS will be sent from). This is the same number you used as contactIdentity when you created the Flex Flow. This could be a WhatsApp Number, or any other identifier for another messaging channel.
{{FRIENDLY_NAME}}The friendly name needs to be included in the participants in order to correctly create the proxy session. We recommend including the customer number ( {{CUSTOMER_NUMBER }}) as the friendly name.
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_15
// Download the helper library from https://www.twilio.com/docs/node/install
_15
// Find your Account SID and Auth Token at twilio.com/console
_15
// and set the environment variables. See http://twil.io/secure
_15
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_15
const authToken = process.env.TWILIO_AUTH_TOKEN;
_15
const client = require('twilio')(accountSid, authToken);
_15
_15
client.proxy.v1.services('KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_15
.sessions
_15
.create({
_15
uniqueName: 'CHXXXXXXXXXXXXXXXXXXXXXXXXX',
_15
participants: [{'identifier': 'CHAT_CHANNEL_SID', 'proxyIdentifier': 'CONTACT_CENTER_NUMBER', 'friendlyName': 'SOME_FRIENDLY_NAME_<CUSTOMER_NUMBER>'}],
_15
mode: 'message-only'
_15
})
_15
.then(session => console.log(session.sid));

Output

_21
{
_21
"service_sid": "KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_21
"status": "open",
_21
"unique_name": "CHXXXXXXXXXXXXXXXXXXXXXXXXX",
_21
"date_started": "2015-07-30T20:00:00Z",
_21
"date_ended": "2015-07-30T20:00:00Z",
_21
"date_last_interaction": "2015-07-30T20:00:00Z",
_21
"date_expiry": "2015-07-30T20:00:00Z",
_21
"ttl": 3600,
_21
"mode": "message-only",
_21
"closed_reason": "",
_21
"sid": "KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_21
"date_updated": "2015-07-30T20:00:00Z",
_21
"date_created": "2015-07-30T20:00:00Z",
_21
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_21
"url": "https://proxy.twilio.com/v1/Services/KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Sessions/KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_21
"links": {
_21
"interactions": "https://proxy.twilio.com/v1/Services/KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Sessions/KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Interactions",
_21
"participants": "https://proxy.twilio.com/v1/Services/KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Sessions/KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants"
_21
}
_21
}

Use another request to add the customer, using their number as the identifier. Proxy can use its own logic to select the Proxy identifier.

Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_15
// Download the helper library from https://www.twilio.com/docs/node/install
_15
// Find your Account SID and Auth Token at twilio.com/console
_15
// and set the environment variables. See http://twil.io/secure
_15
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_15
const authToken = process.env.TWILIO_AUTH_TOKEN;
_15
const client = require('twilio')(accountSid, authToken);
_15
_15
client.proxy.v1.services('KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_15
.sessions('KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_15
.participants
_15
.create({
_15
friendlyName: 'This is the Customer',
_15
identifier: 'CUSTOMER_NUMBER'
_15
})
_15
.then(participant => console.log(participant.sid));

Output

_17
{
_17
"sid": "KPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_17
"session_sid": "KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_17
"service_sid": "KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_17
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_17
"identifier": "CUSTOMER_NUMBER",
_17
"proxy_identifier": "+14155559999",
_17
"proxy_identifier_sid": "PNXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_17
"friendly_name": "This is the Customer",
_17
"date_deleted": "2015-07-30T20:00:00Z",
_17
"date_updated": "2015-07-30T20:00:00Z",
_17
"date_created": "2015-07-30T20:00:00Z",
_17
"url": "https://proxy.twilio.com/v1/Services/KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Sessions/KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants/KPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_17
"links": {
_17
"message_interactions": "https://proxy.twilio.com/v1/Services/KSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Sessions/KCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Participants/KPXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/MessageInteractions"
_17
}
_17
}

4. Update the Chat Channel Attributes

4-update-the-chat-channel-attributes page anchor

Update the Chat Channel attributes to include the Proxy Session SID you created in Step 3 if it's not already there. The chat channel attributes should be a JSON string:

{proxySession: "KCXXXXXXXXXXXXXXXXXXXXXXXX"}

Make sure that you retrieve the existing attributes object, add the proxySession key, and then update the parameter as a string.

Update Chat Channel Attributes

update-chat-channel-attributes page anchor
Node.js
Python
C#
Java
Go
PHP
Ruby
twilio-cli
curl

_13
// Download the helper library from https://www.twilio.com/docs/node/install
_13
// Find your Account SID and Auth Token at twilio.com/console
_13
// and set the environment variables. See http://twil.io/secure
_13
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_13
const authToken = process.env.TWILIO_AUTH_TOKEN;
_13
const client = require('twilio')(accountSid, authToken);
_13
_13
client.chat.v2.services('ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_13
.channels('CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
_13
.update({attributes: JSON.stringify({
_13
proxySession: 'KCXXXXXXXXXXXXXXXXXXXXXXXX'
_13
})})
_13
.then(channel => console.log(channel.friendlyName));

Output

_24
{
_24
"sid": "CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"account_sid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"friendly_name": "friendly_name",
_24
"unique_name": "unique_name",
_24
"attributes": {
_24
"proxySession": "KCXXXXXXXXXXXXXXXXXXXXXXXX"
_24
},
_24
"type": "public",
_24
"date_created": "2015-12-16T22:18:37Z",
_24
"date_updated": "2015-12-16T22:18:38Z",
_24
"created_by": "username",
_24
"members_count": 0,
_24
"messages_count": 0,
_24
"url": "https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
_24
"links": {
_24
"members": "https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Members",
_24
"messages": "https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Messages",
_24
"invites": "https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Invites",
_24
"webhooks": "https://chat.twilio.com/v2/Services/ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Channels/CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Webhooks",
_24
"last_message": null
_24
}
_24
}

Now you can manually send your outbound message through the chat channel. This will trigger a post-event webhook, creating the proxy interaction or the outbound message. For example, if you're using Studio, then it will trigger the Studio Flow as an outbound message to your customer.

We advise you to send the message through the channel SID as opposed to the Proxy SID to keep a full record of the chat interaction.

chatServiceSidThe Service associated with your Chat Channel.
channelSidThe Chat Channel for the Flex Channel Resource. This will tell the Channel to send a message from the Agent to the Customer using the identifiers stored in Proxy.
fromThe contact center/agent ID (the number the SMS will be sent from).
messageTextThis is the message content that you can send in the message. Note that this content may not appear (e.g., if you're using a Studio Flow that doesn't use the Trigger Widget's body property.

Send the message to the chat channel

send-the-message-to-the-chat-channel page anchor

_16
const sendMessage = async function(channelSid, chatServiceSid, client, from, messageText){
_16
try {
_16
console.log("Send Message Function")
_16
return client
_16
.chat
_16
.services(chatServiceSid)
_16
.channels(channelSid)
_16
.messages
_16
.create({
_16
body : messageText,
_16
from : from
_16
})
_16
} catch(error) {
_16
console.log(error);
_16
}
_16
}

Your customer should now receive a message. When they respond, a Task will be created, and you can route it to an agent who can accept and handle the response.


Rate this page: