Skip to content
Merged
18 changes: 18 additions & 0 deletions functions/captureChannelWithBot.protected.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/no-dynamic-require */
/**
* Copyright (C) 2021-2023 Technology Matters
* This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -33,6 +34,8 @@ type EnvVars = {
ASELO_APP_ACCESS_KEY: string;
ASELO_APP_SECRET_KEY: string;
AWS_REGION: string;
TWILIO_WORKSPACE_SID: string;
SURVEY_WORKFLOW_SID: string;
};

type Body = {
Expand All @@ -47,6 +50,7 @@ export const handler = async (
event: Body,
callback: ServerlessCallback,
) => {
console.log('===== captureChannelWithBot handler =====');
const response = responseWithCors();
const resolve = bindResolve(callback)(response);

Expand Down Expand Up @@ -124,6 +128,20 @@ export const handler = async (

const updatedChannelAttributes = JSON.parse(updated.attributes);

// Cleanup task for captured channel by the bot
await context
.getTwilioClient()
.taskrouter.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks.create({
workflowSid: context.SURVEY_WORKFLOW_SID,
attributes: JSON.stringify({
isChatCaptureControl: true,
channelSid,
}),
taskChannel: 'survey',
timeout: 45600, // 720 minutes or 12 hours
});

// ==============
/**
* TODO: Factor out shared chunk of code
Expand Down
23 changes: 18 additions & 5 deletions functions/taskrouterListeners/janitorListener.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,19 @@ type EnvVars = {
FLEX_PROXY_SERVICE_SID: string;
};

const isCleanupPostSurvey = (eventType: EventType, taskAttributes: { isSurveyTask?: boolean }) =>
(eventType === TASK_CANCELED || eventType === TASK_WRAPUP) && taskAttributes.isSurveyTask;
// This applies to both pre-survey(isChatCaptureControl) and post-survey
const isCleanupBotCapture = (
eventType: EventType,
taskAttributes: { isSurveyTask?: boolean; isChatCaptureControl?: boolean },
) => {
if (taskAttributes.isSurveyTask) {
return eventType === TASK_CANCELED || eventType === TASK_WRAPUP;
}
if (taskAttributes.isChatCaptureControl) {
return eventType === TASK_CANCELED;
}
return false;
};

const isCleanupCustomChannel = (eventType: EventType, taskAttributes: { channelType?: string }) => {
if (
Expand Down Expand Up @@ -83,15 +94,17 @@ export const handleEvent = async (context: Context<EnvVars>, event: EventFields)

const taskAttributes = JSON.parse(taskAttributesString);

if (isCleanupPostSurvey(eventType, taskAttributes)) {
console.log('Handling clean up post-survey...');
if (isCleanupBotCapture(eventType, taskAttributes)) {
const cleanupType = taskAttributes.isChatCaptureControl ? 'pre-survey' : 'post-survey';

await wait(3000); // wait 3 seconds just in case some bot message is pending

const handlerPath = Runtime.getFunctions()['helpers/chatChannelJanitor'].path;
const chatChannelJanitor = require(handlerPath).chatChannelJanitor as ChatChannelJanitor;
await chatChannelJanitor(context, { channelSid: taskAttributes.channelSid });

console.log('Finished handling clean up post-survey.');
console.log(`Finished handling clean up for ${cleanupType}.`);

return;
}

Expand Down
7 changes: 7 additions & 0 deletions functions/webhooks/chatbotCallback.protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export const handler = async (
event: Body,
callback: ServerlessCallback,
) => {
console.log('===== chatbotCallback handler =====');

const response = responseWithCors();
const resolve = bindResolve(callback)(response);

Expand Down Expand Up @@ -138,6 +140,11 @@ export const handler = async (
channel.update({
attributes: JSON.stringify(releasedChannelAttributes),
}),
// Move control task to complete state
client.taskrouter.v1
.workspaces('WORKFLOW_SID')
.tasks(channelAttributes.controlTaskSid)
.update({ assignmentStatus: 'completed' }),
// Remove this webhook from the channel
channel
.webhooks()
Expand Down