Flex Manager
This page applies to Flex UI 1.x.x.
For the Flex UI 2.x.x version of this content, see Manager in the overview of Flex UI programmability options.
The Flex Manager is the access point for controlling your Flex instance and all of the underlying Twilio products used for communications and assigning tasks. This means that within your Flex project, you can access the TaskRouter or the Chat client directly through the Flex Manager.
Aside from Flex itself, Manager also gives you access to the Programmable Chat, Sync, Client, and TaskRouter SDKs.
For a full list of attributes and methods, see the Manager class documentation in the Flex UI API Reference.
You can access the Manager using these methods:
- the
getInstancemethod - the
createmethod - the
initmethod
Flex.Manager.getInstance()
1return Flex2.provideLoginInfo(configuration, "#container")3.then(() => Flex.Manager.create(configuration))4.then(manager => {5// use manager here6})7.catch(error => handleError(error));
You can check out the sample project to learn how to initialize Flex.
1init(flex, manager) {2// use manager here3}
You can use Flex Manager to subscribe to events that occur from Flex. For more details, see Flex Events in the Flex UI API Reference.
1import { Manager } from "@twilio/flex-ui";2const manager = Manager.getInstance();34manager.events.addListener('eventName', (payload) => {5// implement logic here6});7
For example, you can subscribe to the pluginsLoaded event to know when all Flex plugins have loaded.
1manager.events.addListener("pluginsLoaded", () => {2console.log("Plugins have been loaded!");3});4
This example logs connect in the browser's console whenever the agent connects to a call:
1Flex.Manager.getInstance().voiceClient.on('connect', () => {2console.log('connect');3});4
By mixing calls to the Manager with the actions framework, you can perform more complex tasks like this example that automatically accepts all inbound chats for agents:
1Flex.Manager.getInstance().workerClient.on("reservationCreated", reservation => {2if (reservation.task.taskChannelUniqueName === 'chat' && reservation.task.direction === 'inbound') {3Flex.Actions.invokeAction("AcceptTask", {sid: reservation.sid});4Flex.Actions.invokeAction("SelectTask", {sid: reservation.sid});5}6});
The insightsClient provides access to the Twilio Sync SDK. For Flex accounts, this gives access to workers and tasks data through the use of two classes:
- LiveQuery class: to query Flex data and receives pushed updates whenever new (or updated) records would match the given expression
- InstantQuery class: to get a static snapshot of Flex data
Both classes need two arguments:
- Index name: the data set that the query is executed against. Flex supports the following index names:
tr-task,tr-worker,tr-reservation, andtr-queue. - Query: this is the query used to filter the data from the index. The syntax for the query is documented here. The query can be an empty string, in which case, the whole data set is returned (for example, all workers).
In this example, the insightsClient is used to query the workers with activity_name set to Available and subscribe to changes. That means that every time a worker changes their status to Available, the itemUpdated event is fired. If a worker changes their status from Available to any other status, the itemRemoved event is fired.
1Flex.Manager.insightsClient2.liveQuery('tr-worker', 'data.activity_name == "Available"')3.then(function (args) {4console.log(5'Subscribed to live data updates for worker in "Available" activity'6);7args.on('itemRemoved', function (args) {8console.log('Worker ' + args.key + ' is no longer "Available"');9});10args.on('itemUpdated', function (args) {11console.log('Worker ' + args.key + ' is now "Available"');12});13})14.catch(function (err) {15console.log('Error when subscribing to live updates', err);16});17
In this example, the insightsClient is used to query workers with specific skills inside its attributes. This returns an array of workers that can be used to provide static data.
1manager.insightsClient.instantQuery('tr-worker').then((q) => {2q.on('searchResult', (items) => {3// Do something with the results4});5q.search('data.attributes.languages contains "english"');6});