Skip to contentSkip to navigationSkip to topbar
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 FlowLink to code sample: Strategy 1: Create a Flex Flow
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createFlexFlow() {
11
const flexFlow = await client.flexApi.v1.flexFlow.create({
12
channelType: "sms",
13
chatServiceSid: "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXNone",
14
contactIdentity: "+12XXXXXXXXX",
15
enabled: false,
16
friendlyName: "Outbound SMS",
17
"integration.channel": "TCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
18
"integration.workflowSid": "WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
19
"integration.workspaceSid": "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
20
integrationType: "task",
21
});
22
23
console.log(flexFlow.accountSid);
24
}
25
26
createFlexFlow();

Output

1
{
2
"sid": "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"date_created": "2016-08-01T22:10:40Z",
5
"date_updated": "2016-08-01T22:10:40Z",
6
"friendly_name": "Outbound SMS",
7
"chat_service_sid": "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXNone",
8
"channel_type": "sms",
9
"contact_identity": "+12XXXXXXXXX",
10
"enabled": false,
11
"integration_type": "task",
12
"integration": {
13
"flow_sid": "FWaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
14
"retry_count": 1
15
},
16
"long_lived": true,
17
"janitor_enabled": true,
18
"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 integrationLink to code sample: Outbound Flex Channel with TaskRouter integration
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createChannel() {
11
const channel = await client.flexApi.v1.channel.create({
12
chatFriendlyName: "Outbound Chat with John",
13
chatUserFriendlyName: "ChatUserFriendlyName",
14
flexFlowSid: "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15
identity: "sms_18001231234",
16
target: "+18001231234",
17
taskAttributes: JSON.stringify({
18
to: "+18001231234",
19
direction: "outbound",
20
name: "John",
21
from: "+18005555555",
22
targetWorker: "reference_worker_attribute_contact_uri_here",
23
autoAnswer: "true",
24
}),
25
});
26
27
console.log(channel.accountSid);
28
}
29
30
createChannel();

Output

1
{
2
"flex_flow_sid": "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"sid": "CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"task_sid": "WTaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
6
"user_sid": "USaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7
"date_created": "2016-08-01T22:10:40Z",
8
"date_updated": "2016-08-01T22:10:40Z",
9
"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 ChannelLink to code sample: Strategy 1: Create a Proxy Session for Chat Channel
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createSession() {
11
const session = await client.proxy.v1
12
.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
13
.sessions.create({
14
mode: "message-only",
15
participants: [
16
{
17
Identifier: "CUSTOMER_NUMBER",
18
},
19
],
20
uniqueName: "SID_FROM_CHANNELS_API",
21
});
22
23
console.log(session.sid);
24
}
25
26
createSession();

Output

1
{
2
"service_sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"status": "open",
4
"unique_name": "SID_FROM_CHANNELS_API",
5
"date_started": "2015-07-30T20:00:00Z",
6
"date_ended": "2015-07-30T20:00:00Z",
7
"date_last_interaction": "2015-07-30T20:00:00Z",
8
"date_expiry": "2015-07-30T20:00:00Z",
9
"ttl": 3600,
10
"mode": "message-only",
11
"closed_reason": "",
12
"sid": "KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
13
"date_updated": "2015-07-30T20:00:00Z",
14
"date_created": "2015-07-30T20:00:00Z",
15
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
16
"url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
17
"links": {
18
"interactions": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Interactions",
19
"participants": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants"
20
}
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 SessionLink to code sample: Strategy 1: Add the Agent to the Proxy Session
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createParticipant() {
11
const participant = await client.proxy.v1
12
.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
13
.sessions("KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
14
.participants.create({
15
friendlyName: "CUSTOMER_NUMBER",
16
identifier: "CHAT_CHANNEL_SID",
17
proxyIdentifier: "CONTACT_CENTER_NUMBER",
18
});
19
20
console.log(participant.sid);
21
}
22
23
createParticipant();

Output

1
{
2
"sid": "KPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"session_sid": "KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"service_sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
6
"identifier": "CHAT_CHANNEL_SID",
7
"proxy_identifier": "CONTACT_CENTER_NUMBER",
8
"proxy_identifier_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
9
"friendly_name": "CUSTOMER_NUMBER",
10
"date_deleted": "2015-07-30T20:00:00Z",
11
"date_updated": "2015-07-30T20:00:00Z",
12
"date_created": "2015-07-30T20:00:00Z",
13
"url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants/KPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
14
"links": {
15
"message_interactions": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants/KPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/MessageInteractions"
16
}
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:

1
.then(
2
attributes => {
3
return Object.assign(JSON.parse(attributes.attributes), { proxySession: proxySession.sid })
4
}
5
))
Strategy 1: Update Chat Channel AttributesLink to code sample: Strategy 1: Update Chat Channel Attributes
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function updateChannel() {
11
const channel = await client.chat.v2
12
.services("ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
13
.channels("Sid")
14
.update({
15
attributes: JSON.stringify({
16
proxySession: "KCXXXXXXXXXXXXXXXXXXXXXXXX",
17
}),
18
});
19
20
console.log(channel.sid);
21
}
22
23
updateChannel();

Output

1
{
2
"sid": "Sid",
3
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"service_sid": "ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"friendly_name": "friendly_name",
6
"unique_name": "unique_name",
7
"attributes": "{\"proxySession\": \"KCXXXXXXXXXXXXXXXXXXXXXXXX\"}",
8
"type": "public",
9
"date_created": "2015-12-16T22:18:37Z",
10
"date_updated": "2015-12-16T22:18:38Z",
11
"created_by": "username",
12
"members_count": 0,
13
"messages_count": 0,
14
"url": "https://chat.twilio.com/v2/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15
"links": {
16
"members": "https://chat.twilio.com/v2/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Members",
17
"messages": "https://chat.twilio.com/v2/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages",
18
"invites": "https://chat.twilio.com/v2/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Invites",
19
"webhooks": "https://chat.twilio.com/v2/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Webhooks",
20
"last_message": null
21
}
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 - 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 AccountLink to code sample: Strategy 2: Retrieve all Flex Flows on Account
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function listFlexFlow() {
11
const flexFlows = await client.flexApi.v1.flexFlow.list({ limit: 20 });
12
13
flexFlows.forEach((f) => console.log(f.accountSid));
14
}
15
16
listFlexFlow();

Output

1
{
2
"meta": {
3
"page": 0,
4
"page_size": 50,
5
"first_page_url": "https://flex-api.twilio.com/v1/FlexFlows?PageSize=50&Page=0",
6
"previous_page_url": null,
7
"url": "https://flex-api.twilio.com/v1/FlexFlows?PageSize=50&Page=0",
8
"next_page_url": null,
9
"key": "flex_flows"
10
},
11
"flex_flows": [
12
{
13
"sid": "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
14
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15
"date_created": "2016-08-01T22:10:40Z",
16
"date_updated": "2016-08-01T22:10:40Z",
17
"friendly_name": "friendly_name",
18
"chat_service_sid": "ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
19
"channel_type": "sms",
20
"contact_identity": "12345",
21
"enabled": true,
22
"integration_type": "studio",
23
"integration": {
24
"flow_sid": "FWaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
25
"retry_count": 1
26
},
27
"long_lived": true,
28
"janitor_enabled": true,
29
"url": "https://flex-api.twilio.com/v1/FlexFlows/FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
30
}
31
]
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 taskLink to code sample: Create an outbound message handler without a task
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createChannel() {
11
const channel = await client.flexApi.v1.channel.create({
12
chatFriendlyName: "Chat with Jane",
13
chatUserFriendlyName: "Jane",
14
flexFlowSid: "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15
identity: "sms_18001231234",
16
target: "+18001231234",
17
});
18
19
console.log(channel.accountSid);
20
}
21
22
createChannel();

Output

1
{
2
"flex_flow_sid": "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"sid": "CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"task_sid": "WTaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
6
"user_sid": "USaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
7
"date_created": "2016-08-01T22:10:40Z",
8
"date_updated": "2016-08-01T22:10:40Z",
9
"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.
1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createSession() {
11
const session = await client.proxy.v1
12
.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
13
.sessions.create({
14
mode: "message-only",
15
participants: [
16
{
17
identifier: "CHAT_CHANNEL_SID",
18
proxyIdentifier: "CONTACT_CENTER_NUMBER",
19
friendlyName: "SOME_FRIENDLY_NAME_<CUSTOMER_NUMBER>",
20
},
21
],
22
uniqueName: "CHXXXXXXXXXXXXXXXXXXXXXXXXX",
23
});
24
25
console.log(session.sid);
26
}
27
28
createSession();

Output

1
{
2
"service_sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"status": "open",
4
"unique_name": "CHXXXXXXXXXXXXXXXXXXXXXXXXX",
5
"date_started": "2015-07-30T20:00:00Z",
6
"date_ended": "2015-07-30T20:00:00Z",
7
"date_last_interaction": "2015-07-30T20:00:00Z",
8
"date_expiry": "2015-07-30T20:00:00Z",
9
"ttl": 3600,
10
"mode": "message-only",
11
"closed_reason": "",
12
"sid": "KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
13
"date_updated": "2015-07-30T20:00:00Z",
14
"date_created": "2015-07-30T20:00:00Z",
15
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
16
"url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
17
"links": {
18
"interactions": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Interactions",
19
"participants": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants"
20
}
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.

1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function createParticipant() {
11
const participant = await client.proxy.v1
12
.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
13
.sessions("KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
14
.participants.create({
15
friendlyName: "This is the Customer",
16
identifier: "CUSTOMER_NUMBER",
17
});
18
19
console.log(participant.sid);
20
}
21
22
createParticipant();

Output

1
{
2
"sid": "KPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
3
"session_sid": "KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"service_sid": "KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
6
"identifier": "CUSTOMER_NUMBER",
7
"proxy_identifier": "+14155559999",
8
"proxy_identifier_sid": "PNaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
9
"friendly_name": "This is the Customer",
10
"date_deleted": "2015-07-30T20:00:00Z",
11
"date_updated": "2015-07-30T20:00:00Z",
12
"date_created": "2015-07-30T20:00:00Z",
13
"url": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants/KPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
14
"links": {
15
"message_interactions": "https://proxy.twilio.com/v1/Services/KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Sessions/KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Participants/KPaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/MessageInteractions"
16
}
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.

1
// Download the helper library from https://www.twilio.com/docs/node/install
2
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";
3
4
// Find your Account SID and Auth Token at twilio.com/console
5
// and set the environment variables. See http://twil.io/secure
6
const accountSid = process.env.TWILIO_ACCOUNT_SID;
7
const authToken = process.env.TWILIO_AUTH_TOKEN;
8
const client = twilio(accountSid, authToken);
9
10
async function updateChannel() {
11
const channel = await client.chat.v2
12
.services("ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
13
.channels("Sid")
14
.update({
15
attributes: JSON.stringify({
16
proxySession: "KCXXXXXXXXXXXXXXXXXXXXXXXX",
17
}),
18
});
19
20
console.log(channel.sid);
21
}
22
23
updateChannel();

Output

1
{
2
"sid": "Sid",
3
"account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
4
"service_sid": "ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
5
"friendly_name": "friendly_name",
6
"unique_name": "unique_name",
7
"attributes": "{\"proxySession\": \"KCXXXXXXXXXXXXXXXXXXXXXXXX\"}",
8
"type": "public",
9
"date_created": "2015-12-16T22:18:37Z",
10
"date_updated": "2015-12-16T22:18:38Z",
11
"created_by": "username",
12
"members_count": 0,
13
"messages_count": 0,
14
"url": "https://chat.twilio.com/v2/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15
"links": {
16
"members": "https://chat.twilio.com/v2/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Members",
17
"messages": "https://chat.twilio.com/v2/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Messages",
18
"invites": "https://chat.twilio.com/v2/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Invites",
19
"webhooks": "https://chat.twilio.com/v2/Services/ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Channels/CHaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Webhooks",
20
"last_message": null
21
}
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
1
const sendMessage = async function(channelSid, chatServiceSid, client, from, messageText){
2
try {
3
console.log("Send Message Function")
4
return client
5
.chat
6
.services(chatServiceSid)
7
.channels(channelSid)
8
.messages
9
.create({
10
body : messageText,
11
from : from
12
})
13
} catch(error) {
14
console.log(error);
15
}
16
}
17

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.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.