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.x and channels that use Programmable Chat and Proxy. If you are using Flex UI 2.x.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

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

Output

_19
{
_19
"sid": "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_19
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_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": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXNone",
_19
"channel_type": "sms",
_19
"contact_identity": "+12XXXXXXXXX",
_19
"enabled": false,
_19
"integration_type": "task",
_19
"integration": {
_19
"flow_sid": "FWaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_19
"retry_count": 1
_19
},
_19
"long_lived": true,
_19
"janitor_enabled": true,
_19
"url": "https://flex-api.twilio.com/v1/FlexFlows/FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
_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

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

Output

_10
{
_10
"flex_flow_sid": "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_10
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_10
"sid": "CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_10
"task_sid": "WTaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_10
"user_sid": "USaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_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/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
_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

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

Output

_21
{
_21
"service_sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_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": "KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_21
"date_updated": "2015-07-30T20:00:00Z",
_21
"date_created": "2015-07-30T20:00:00Z",
_21
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_21
"url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_21
"links": {
_21
"interactions": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Interactions",
_21
"participants": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/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

_23
// Download the helper library from https://www.twilio.com/docs/node/install
_23
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_23
_23
// Find your Account SID and Auth Token at twilio.com/console
_23
// and set the environment variables. See http://twil.io/secure
_23
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_23
const authToken = process.env.TWILIO_AUTH_TOKEN;
_23
const client = twilio(accountSid, authToken);
_23
_23
async function createParticipant() {
_23
const participant = await client.proxy.v1
_23
.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_23
.sessions("KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_23
.participants.create({
_23
friendlyName: "CUSTOMER_NUMBER",
_23
identifier: "CHAT_CHANNEL_SID",
_23
proxyIdentifier: "CONTACT_CENTER_NUMBER",
_23
});
_23
_23
console.log(participant.sid);
_23
}
_23
_23
createParticipant();

Output

_17
{
_17
"sid": "KPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_17
"session_sid": "KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_17
"service_sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_17
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_17
"identifier": "CHAT_CHANNEL_SID",
_17
"proxy_identifier": "CONTACT_CENTER_NUMBER",
_17
"proxy_identifier_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_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/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants/KPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_17
"links": {
_17
"message_interactions": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants/KPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/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

_23
// Download the helper library from https://www.twilio.com/docs/node/install
_23
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_23
_23
// Find your Account SID and Auth Token at twilio.com/console
_23
// and set the environment variables. See http://twil.io/secure
_23
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_23
const authToken = process.env.TWILIO_AUTH_TOKEN;
_23
const client = twilio(accountSid, authToken);
_23
_23
async function updateChannel() {
_23
const channel = await client.chat.v2
_23
.services("ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_23
.channels("Sid")
_23
.update({
_23
attributes: JSON.stringify({
_23
proxySession: "KCXXXXXXXXXXXXXXXXXXXXXXXX",
_23
}),
_23
});
_23
_23
console.log(channel.sid);
_23
}
_23
_23
updateChannel();

Output

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

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

_16
// Download the helper library from https://www.twilio.com/docs/node/install
_16
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_16
_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 = twilio(accountSid, authToken);
_16
_16
async function listFlexFlow() {
_16
const flexFlows = await client.flexApi.v1.flexFlow.list({ limit: 20 });
_16
_16
flexFlows.forEach((f) => console.log(f.accountSid));
_16
}
_16
_16
listFlexFlow();

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": null,
_32
"url": "https://flex-api.twilio.com/v1/FlexFlows?PageSize=50&Page=0",
_32
"next_page_url": null,
_32
"key": "flex_flows"
_32
},
_32
"flex_flows": [
_32
{
_32
"sid": "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_32
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_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": "ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_32
"channel_type": "sms",
_32
"contact_identity": "12345",
_32
"enabled": true,
_32
"integration_type": "studio",
_32
"integration": {
_32
"flow_sid": "FWaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_32
"retry_count": 1
_32
},
_32
"long_lived": true,
_32
"janitor_enabled": true,
_32
"url": "https://flex-api.twilio.com/v1/FlexFlows/FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
_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

_22
// Download the helper library from https://www.twilio.com/docs/node/install
_22
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_22
_22
// Find your Account SID and Auth Token at twilio.com/console
_22
// and set the environment variables. See http://twil.io/secure
_22
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_22
const authToken = process.env.TWILIO_AUTH_TOKEN;
_22
const client = twilio(accountSid, authToken);
_22
_22
async function createChannel() {
_22
const channel = await client.flexApi.v1.channel.create({
_22
chatFriendlyName: "Chat with Jane",
_22
chatUserFriendlyName: "Jane",
_22
flexFlowSid: "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_22
identity: "sms_18001231234",
_22
target: "+18001231234",
_22
});
_22
_22
console.log(channel.accountSid);
_22
}
_22
_22
createChannel();

Output

_10
{
_10
"flex_flow_sid": "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_10
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_10
"sid": "CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_10
"task_sid": "WTaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_10
"user_sid": "USaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_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/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
_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 included 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

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

Output

_21
{
_21
"service_sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_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": "KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_21
"date_updated": "2015-07-30T20:00:00Z",
_21
"date_created": "2015-07-30T20:00:00Z",
_21
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_21
"url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_21
"links": {
_21
"interactions": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Interactions",
_21
"participants": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/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

_22
// Download the helper library from https://www.twilio.com/docs/node/install
_22
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_22
_22
// Find your Account SID and Auth Token at twilio.com/console
_22
// and set the environment variables. See http://twil.io/secure
_22
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_22
const authToken = process.env.TWILIO_AUTH_TOKEN;
_22
const client = twilio(accountSid, authToken);
_22
_22
async function createParticipant() {
_22
const participant = await client.proxy.v1
_22
.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_22
.sessions("KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_22
.participants.create({
_22
friendlyName: "This is the Customer",
_22
identifier: "CUSTOMER_NUMBER",
_22
});
_22
_22
console.log(participant.sid);
_22
}
_22
_22
createParticipant();

Output

_17
{
_17
"sid": "KPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_17
"session_sid": "KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_17
"service_sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_17
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_17
"identifier": "CUSTOMER_NUMBER",
_17
"proxy_identifier": "+14155559999",
_17
"proxy_identifier_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_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/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants/KPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
_17
"links": {
_17
"message_interactions": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants/KPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/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

_23
// Download the helper library from https://www.twilio.com/docs/node/install
_23
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
_23
_23
// Find your Account SID and Auth Token at twilio.com/console
_23
// and set the environment variables. See http://twil.io/secure
_23
const accountSid = process.env.TWILIO_ACCOUNT_SID;
_23
const authToken = process.env.TWILIO_AUTH_TOKEN;
_23
const client = twilio(accountSid, authToken);
_23
_23
async function updateChannel() {
_23
const channel = await client.chat.v2
_23
.services("ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
_23
.channels("Sid")
_23
.update({
_23
attributes: JSON.stringify({
_23
proxySession: "KCXXXXXXXXXXXXXXXXXXXXXXXX",
_23
}),
_23
});
_23
_23
console.log(channel.sid);
_23
}
_23
_23
updateChannel();

Output

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

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: