Skip to contentSkip to navigationSkip to topbar
On this page

Integrate a Custom Chat Client with Flex


(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.

You may have already built a custom chat experience with Programmable Chat or from scratch. You can integrate these chat experiences with Flex and hand off incoming Chat messages to your agents.

(information)

Info

Custom chat integrations require a Flex Flow ChannelType of custom.


How to Integrate Your Chat Application with Flex

how-to-integrate-your-chat-application-with-flex page anchor

First, initialize the Twilio Chat SDK using an access token that links a user with a unique identity - for example: abc123. The Chat SDK is going to help you pass messages back and forth with Flex.

(warning)

Warning

Make sure this identity uniquely identifies your end user and avoid using personally identifiable information like names.

Creating an Access Token (Chat)Link to code sample: Creating an Access Token (Chat)
1
const AccessToken = require('twilio').jwt.AccessToken;
2
const ChatGrant = AccessToken.ChatGrant;
3
4
// Used when generating any kind of tokens
5
// To set up environmental variables, see http://twil.io/secure
6
const twilioAccountSid = process.env.TWILIO_ACCOUNT_SID;
7
const twilioApiKey = process.env.TWILIO_API_KEY;
8
const twilioApiSecret = process.env.TWILIO_API_SECRET;
9
10
// Used specifically for creating Chat tokens
11
const serviceSid = process.env.TWILIO_CHAT_SERVICE_SID;
12
const identity = 'user@example.com';
13
14
// Create a "grant" which enables a client to use Chat as a given user,
15
// on a given device
16
const chatGrant = new ChatGrant({
17
serviceSid: serviceSid,
18
});
19
20
// Create an access token which we will sign and return to the client,
21
// containing the grant we just created
22
const token = new AccessToken(
23
twilioAccountSid,
24
twilioApiKey,
25
twilioApiSecret,
26
{identity: identity}
27
);
28
29
token.addGrant(chatGrant);
30
31
// Serialize the token to a JWT string
32
console.log(token.toJwt());

Now you need to:

  1. Ensure you have a relevant Message Handler. You can add Studio to the communication flow, immediately create a task, or do something else entirely depending on how you've configured your Flex Flow .
  2. Create a Flex Chat Channel (or reuse one if this is a return customer and you're using Long Lived channels)

    1. Creating a Channel will also automatically create a Chat User and place it in the Channel.

This configuration could require a minimum of four API requests, but with Flex you can do everything with a single request to Flex Chat Channels API.

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 abc123",
13
chatUserFriendlyName: "Jane",
14
flexFlowSid: "FOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
15
identity: "abc123",
16
target: "abc123",
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
}

Bringing it all together

bringing-it-all-together page anchor

Now you can use the Twilio Chat SDK to manage communications on the Chat Channel you created with Flex:

  • When a message comes in , you'll receive a messageAdded event and can render it in your custom UI.
  • When you need to send a message , the SDK includes a sendMessage() method that you can use to publish media over the Twilio Chat Channel.

Need some help?

Terms of service

Copyright © 2024 Twilio Inc.