Menu

Actions Framework

Auto-Generated Documentation for the Flex UI is now available as a beta distribution. The auto-generated documentation is accurate and comprehensive, and so may differ from what you see in the official Flex UI documentation.

Flex UI constantly emits asynchronous events and event payloads that describe how the user is interacting with the platform. With the Flex UI Actions Framework, you can manually invoke certain actions such as completing tasks, transferring tasks, holding a current call, and many more. Each action fires a before and after event, which allows you to handle your own logic before and after the action occurs. You can also replace the default behavior of an Action with your own custom logic. As you develop Plugins, the Actions Framework will allow you to describe how you want to interact with Flex UI or any CRM Data.

Register and Invoke an Action

Actions.registerAction registers a named action and allows you to invoke the action later. The second argument is an ActionFunction to be executed when the action is invoked. You can implement your logic in the ActionFunction.

import { Actions } from "@twilio/flex-ui";

Actions.registerAction("AcceptTask", (payload) => {
   // Custom Logic Goes Here
});

Actions.invokeAction invokes actions that come out of the box with Flex, and the ones that you register. You can pass in an optional payload as a second parameter if the action needs any data when being invoked. When the named Action is invoked, it fires a before and after event.

import { Actions } from "@twilio/flex-ui";
Actions.invokeAction("AcceptTask", { sid: "WRXXXXXXXXXXXXXXXXX" });

Replace an Action

Actions.replaceAction replaces the default implementation of a named Action with your own. The replaced Action will be called with the same parameters as the original implementation, so you can add additional logic, then invoke the original action in your replacement function.

import { Actions } from "@twilio/flex-ui";

Actions.replaceAction("AcceptTask", (payload, original) => {
    return new Promise((resolve, reject) => {
        alert("I have replaced this Action");
        resolve();
    }).then(() => original(payload));
});

Add and Remove Event Listeners

You can add and remove event listeners to events fired when an action is invoked. Every event name is the invoked Action’s name prefixed with either “before” or “after”.

import { Actions } from "@twilio/flex-ui";

Actions.addListener("beforeAcceptTask", (payload, cancelActionInvocation) => {
  // Implement logic before the AcceptTask action has been invoked
  if (someCondition) {
     // use this function to prevent the actual AcceptTask action from being invoked
     cancelActionInvocation();
  }
});

Actions.addListener("afterAcceptTask", (payload) => {
  // Implement logic after AcceptTask action has stopped executing
});

To remove a listener, you must provide the same function you used as an argument when registering the event. If you need to remove listeners, you should register your event listeners with a named function instead of an anonymous function.

Registering event listeners with named functions

import { Actions } from "@twilio/flex-ui";
const handleBeforeAcceptTask = (payload, cancelActionInvocation) => {
  // Implement logic before the AcceptTask action has been invoked
};

const handleAfterAcceptTask = (payload) => {
  // Implement logic after the AcceptTask action has stopped executing
};

// Register beforeAcceptTask and afterAcceptTask events

Actions.addListener("beforeAcceptTask", handleBeforeAcceptTask);
Actions.addListener("afterAcceptTask", handleAfterAcceptTask);

Removing event listeners with named functions

You can now remove the event listeners by passing the name of the event with the original function used to register the event.

Actions.removeListener("beforeAcceptTask", handleBeforeAcceptTask);
Actions.removeListener("afterAcceptTask", handleAfterAcceptTask);

Events Supported

For a list of all actions, visit the Actions part of the Flex UI 1.x docs. All event names take the name of the Action, prefixed with before or after. Every event name is also in camel case.

before[eventName] (for example "beforeAcceptTask")
Called when a named action is triggered, but before the action body is run. You can abort the action that is provided with event body.

after[eventName]
Called after the action has stopped executing.

The afterLogout event is not supported. Once the Worker has logged out, they will be returned to the Flex login screen.

Common use cases and examples

Add an Action after a Task is accepted

Raises a javascript alert after an Agent has clicked to accept any task.

flex.Actions.addListener("afterAcceptTask", (payload) => alert("Triggered after event AcceptTask"));

Ask for confirmation before accepting a Task

Generates a prompt before Task Acceptance; prevent that Action from running with an abort command if the user doesn't confirm.

flex.Actions.addListener("beforeAcceptTask", (payload, abortFunction) => {
    alert("Triggered before event AcceptTask");
        if (!window.confirm("Are you sure you want to accept the task?")) {
            abortFunction();
        }
});

Customizing an Existing Action

Replaces the original Action for AcceptTask. Injects custom logic to alert about the replacement, but executes the original Action.

flex.Actions.replaceAction("AcceptTask", (payload, original) => {
    return new Promise<void>((resolve, reject) => {
        alert("I have replaced this Action");
        resolve();
    }).then(() => original(payload));
});

Registering a Custom Action

Registers a custom Action called MyAction, which makes a HTTP request. We then add a listener to the action CompleteTask which then invokes this custom Action. For example this could be used to update your CRM system.

flex.Actions.registerAction("MyAction", (payload) => {
    return 
        fetch("https://my.server.backend.com/test")
        .then(response => {
            alert("Triggered MyAction with response " + JSON.stringify(response));
        })
        .catch(error => {
            console.log(error);
            throw error;
        });       
});


flex.Actions.addListener("afterCompleteTask", (payload) => {return flex.Actions.invokeAction("MyAction")});

Sending a message after a Task is completed

Sends a post-conversation message once the task is in a wrap-up state. Could be used to send a survey, or notify a user that the agent closed the session.

flex.Actions.replaceAction("WrapupTask", (payload, original) => {
    // Only alter chat tasks:
    if( payload.task.taskChannelUniqueName !== "chat" ) {
        original(payload);
    } else {
        return new Promise(function(resolve, reject) {
          // Send the message:
          flex.Actions.invokeAction("SendMessage", {
            body: 'Thanks for chatting. Your session is now closed.',
            channelSid: payload.task.attributes.channelSid
          }).then(response => {
            // Wait until the message is sent to wrap-up the task:
            resolve(original(payload));
        });

     });

   }

 });

Next Steps

Rate this page:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Stack Overflow Collective or browsing the Twilio tag on Stack Overflow.

Thank you for your feedback!

Please select the reason(s) for your feedback. The additional information you provide helps us improve our documentation:

Sending your feedback...
🎉 Thank you for your feedback!
Something went wrong. Please try again.

Thanks for your feedback!

Refer us and get $10 in 3 simple steps!

Step 1

Get link

Get a free personal referral link here

Step 2

Give $10

Your user signs up and upgrade using link

Step 3

Get $10

1,250 free SMSes
OR 1,000 free voice mins
OR 12,000 chats
OR more