diff --git a/functions/captureChannelWithBot.protected.ts b/functions/captureChannelWithBot.protected.ts index 20b7c9d0..d766a8e7 100644 --- a/functions/captureChannelWithBot.protected.ts +++ b/functions/captureChannelWithBot.protected.ts @@ -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 @@ -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 = { @@ -47,6 +50,7 @@ export const handler = async ( event: Body, callback: ServerlessCallback, ) => { + console.log('===== captureChannelWithBot handler ====='); const response = responseWithCors(); const resolve = bindResolve(callback)(response); @@ -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 diff --git a/functions/taskrouterListeners/janitorListener.private.ts b/functions/taskrouterListeners/janitorListener.private.ts index ec420bb1..935dfd2f 100644 --- a/functions/taskrouterListeners/janitorListener.private.ts +++ b/functions/taskrouterListeners/janitorListener.private.ts @@ -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 ( @@ -83,15 +94,17 @@ export const handleEvent = async (context: Context, 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; } diff --git a/functions/webhooks/chatbotCallback.protected.ts b/functions/webhooks/chatbotCallback.protected.ts index 36bcfc3a..fc0eba30 100644 --- a/functions/webhooks/chatbotCallback.protected.ts +++ b/functions/webhooks/chatbotCallback.protected.ts @@ -44,6 +44,8 @@ export const handler = async ( event: Body, callback: ServerlessCallback, ) => { + console.log('===== chatbotCallback handler ====='); + const response = responseWithCors(); const resolve = bindResolve(callback)(response); @@ -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()