Twilio Flex WebChat
Flex WebChat must be loaded from the CDN. Glance does not provide an NPM package, which is required for React deployments.
- Install Flex WebChat.
- The following code example contains Flex WebChat and Glance Presence functionality. Update the code with your Account SID and Flex Flow SID:
<script>
const appConfig = {
accountSid: "[YOUR_TWILIO_ACCOUNT_SID]",
flexFlowSid: "[YOUR_FLEX_FLOW_SID]",
context: {
friendlyName: "[YOUR_FIRENDLY_NAME]",
},
startEngagementOnInit: true
};
let sessionid = undefined;
Twilio.FlexWebChat.createWebChat(appConfig)
.then(webchat => {
const { manager } = webchat;
Twilio.FlexWebChat.Actions.on("afterSendMessage", () => {
const {channelSid} = manager.store.getState().flex.session;
if (!sessionid || sessionid !== channelSid) {
manager
.chatClient.getChannelBySid(channelSid)
.then(channel => {
let visitor = new GLANCE.Presence.Visitor({
groupid: document.getElementById("glance-cobrowse").getAttribute("data-groupid"),
visitorid: channel.sid
});
visitor.onerror = function (e) {
console.log("presence error:", e);
};
visitor.presence({
onsuccess: function () {
console.log("presence success");
}
});
visitor.onsignal = function (msg) {
console.log("received signal:", msg);
};
visitor.connect();
});
sessionid = channelSid;
}
});
webchat.init();
});
</script>
-
Set up a Flow for Flex WebChat:
- From the Twilio Console > Studio > Manage Flows, select Webchat Flow.
- In the Widget Library > Flow Control, select and drag Set Variables onto the canvas. Click the widget to edit.
- Click the plus sign on the right side of Variables.
- For Key, input
glance_visitor_id
. - For Value, input
{{trigger.message.ChannelSid}}
- Save the variable.
- Save the widget.
- Wire the widget to the Incoming Message Trigger.
- In the Widget Library > Connect Other Products, select and drag Send to Flex onto the canvas. Click the widget to edit.
- For Workflow, select Assign to Anyone.
- For Channel, select Programmable Chat.
- For Attributes, input
{"name": "{{trigger.message.ChannelAttributes.from}}", "channelType": "{{trigger.message.ChannelAttributes.channel_type}}", "channelSid": "{{trigger.message.ChannelSid}}", "glance_visitor_id": "{{flow.variables.glance_visitor_id}}"}
- Save the widget.
- Wire the widget to the Set Variable Widget that you just created.
- Select Publish.