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.
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).
Outbound messages require a Flex Flow ChannelType
of sms
, whatsapp
, facebook
, or custom.
The following steps will create a task when you send an outbound message to your customer.
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
.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createFlexFlow() {11const flexFlow = await client.flexApi.v1.flexFlow.create({12channelType: "sms",13chatServiceSid: "ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXNone",14contactIdentity: "+12XXXXXXXXX",15enabled: false,16friendlyName: "Outbound SMS",17"integration.channel": "TCXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",18"integration.workflowSid": "WWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",19"integration.workspaceSid": "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",20integrationType: "task",21});2223console.log(flexFlow.accountSid);24}2526createFlexFlow();
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": 115},16"long_lived": true,17"janitor_enabled": true,18"url": "https://flex-api.twilio.com/v1/FlexFlows/FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"19}
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!
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createChannel() {11const channel = await client.flexApi.v1.channel.create({12chatFriendlyName: "Outbound Chat with John",13chatUserFriendlyName: "ChatUserFriendlyName",14flexFlowSid: "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",15identity: "sms_18001231234",16target: "+18001231234",17taskAttributes: JSON.stringify({18to: "+18001231234",19direction: "outbound",20name: "John",21from: "+18005555555",22targetWorker: "reference_worker_attribute_contact_uri_here",23autoAnswer: "true",24}),25});2627console.log(channel.accountSid);28}2930createChannel();
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:
targetWorker
Then, if you open the Flex UI for targetWorker
, you should see the following new reservation:
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.
Consider enabling the Channel Janitor to help manage any stale chat sessions.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createSession() {11const session = await client.proxy.v112.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")13.sessions.create({14mode: "message-only",15participants: [16{17Identifier: "CUSTOMER_NUMBER",18},19],20uniqueName: "SID_FROM_CHANNELS_API",21});2223console.log(session.sid);24}2526createSession();
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:
Attribute | Description |
---|---|
{{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.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createParticipant() {11const participant = await client.proxy.v112.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")13.sessions("KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")14.participants.create({15friendlyName: "CUSTOMER_NUMBER",16identifier: "CHAT_CHANNEL_SID",17proxyIdentifier: "CONTACT_CENTER_NUMBER",18});1920console.log(participant.sid);21}2223createParticipant();
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_NUMBER | This 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_SID | The 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. |
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(2attributes => {3return Object.assign(JSON.parse(attributes.attributes), { proxySession: proxySession.sid })4}5))
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function updateChannel() {11const channel = await client.chat.v212.services("ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")13.channels("Sid")14.update({15attributes: JSON.stringify({16proxySession: "KCXXXXXXXXXXXXXXXXXXXXXXXX",17}),18});1920console.log(channel.sid);21}2223updateChannel();
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": null21}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.
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.
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).
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function listFlexFlow() {11const flexFlows = await client.flexApi.v1.flexFlow.list({ limit: 20 });1213flexFlows.forEach((f) => console.log(f.accountSid));14}1516listFlexFlow();
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": 126},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.
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
.
1// Download the helper library from https://www.twilio.com/docs/node/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createChannel() {11const channel = await client.flexApi.v1.channel.create({12chatFriendlyName: "Chat with Jane",13chatUserFriendlyName: "Jane",14flexFlowSid: "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",15identity: "sms_18001231234",16target: "+18001231234",17});1819console.log(channel.accountSid);20}2122createChannel();
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}
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:
Attribute | Description |
---|---|
{{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/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createSession() {11const session = await client.proxy.v112.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")13.sessions.create({14mode: "message-only",15participants: [16{17identifier: "CHAT_CHANNEL_SID",18proxyIdentifier: "CONTACT_CENTER_NUMBER",19friendlyName: "SOME_FRIENDLY_NAME_<CUSTOMER_NUMBER>",20},21],22uniqueName: "CHXXXXXXXXXXXXXXXXXXXXXXXXX",23});2425console.log(session.sid);26}2728createSession();
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/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function createParticipant() {11const participant = await client.proxy.v112.services("KSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")13.sessions("KCaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")14.participants.create({15friendlyName: "This is the Customer",16identifier: "CUSTOMER_NUMBER",17});1819console.log(participant.sid);20}2122createParticipant();
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}
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/install2const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";34// Find your Account SID and Auth Token at twilio.com/console5// and set the environment variables. See http://twil.io/secure6const accountSid = process.env.TWILIO_ACCOUNT_SID;7const authToken = process.env.TWILIO_AUTH_TOKEN;8const client = twilio(accountSid, authToken);910async function updateChannel() {11const channel = await client.chat.v212.services("ISaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")13.channels("Sid")14.update({15attributes: JSON.stringify({16proxySession: "KCXXXXXXXXXXXXXXXXXXXXXXXX",17}),18});1920console.log(channel.sid);21}2223updateChannel();
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": null21}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.
chatServiceSid | The Service associated with your Chat Channel. |
---|---|
channelSid | The 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. |
from | The contact center/agent ID (the number the SMS will be sent from). |
messageText | This 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. |
1const sendMessage = async function(channelSid, chatServiceSid, client, from, messageText){2try {3console.log("Send Message Function")4return client5.chat6.services(chatServiceSid)7.channels(channelSid)8.messages9.create({10body : messageText,11from : from12})13} catch(error) {14console.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.