Skip to content

Instantly share code, notes, and snippets.

@johnfischelli
Last active January 16, 2020 16:16
Show Gist options
  • Save johnfischelli/07c719c48b97cdcb8ff36cc32bcbbac5 to your computer and use it in GitHub Desktop.
Save johnfischelli/07c719c48b97cdcb8ff36cc32bcbbac5 to your computer and use it in GitHub Desktop.
Submit a channelSid to this function and have it wrap-up or cancel the associated TaskRouter Task.
// view instructions about the twilio-flex-token-validator here: https://www.npmjs.com/package/twilio-flex-token-validator
const JWEValidator = require('twilio-flex-token-validator').functionValidator;
exports.handler = JWEValidator(async function(context, event, callback) {
// setup twilio client
const client = context.getTwilioClient();
// parse data form the incoming http request
const channelSid = event.channelSid;
// setup a response object
const response = new Twilio.Response();
response.appendHeader('Access-Control-Allow-Origin', '*');
response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS POST');
response.appendHeader('Content-Type', 'application/json');
response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
let task = await client.taskrouter.workspaces(context.TWILIO_WORKSPACE_SID).tasks.list({
evaluateTaskAttributes: `channelSid == "${channelSid}"`,
limit: 1
});
task = task[0];
if (task.assignmentStatus == 'pending' || task.assignmentStatus == 'reserved') {
await client.taskrouter.workspaces(context.TWILIO_WORKSPACE_SID).tasks(task.sid).update({
assignmentStatus: 'canceled',
reason: 'customer left chat'
});
}
if (task.assignmentStatus == 'assigned') {
await client.taskrouter.workspaces(context.TWILIO_WORKSPACE_SID).tasks(task.sid).update({
assignmentStatus: 'wrapping'
});
}
response.setBody({
status: 'success'
});
callback(null, response);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment