Skip to content

Commit

Permalink
Log oout conversation state on post survey listener - serialise concu…
Browse files Browse the repository at this point in the history
…rrent logic for conversations
  • Loading branch information
stephenhand committed Jun 7, 2024
1 parent f0751a2 commit 6f030f1
Showing 1 changed file with 45 additions and 35 deletions.
80 changes: 45 additions & 35 deletions functions/channelCapture/channelCaptureHandlers.private.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,56 +434,66 @@ export const handleChannelCapture = async (
const parsedAdditionalControlTaskAttributes = additionControlTaskAttributes
? JSON.parse(additionControlTaskAttributes)
: {};

const [, , controlTask] = await Promise.all([
// Remove the studio trigger webhooks to prevent this channel to trigger subsequent Studio flows executions
!conversationSid &&
let controlTask: TaskInstance;
if (conversationSid) {
const conversationContext = await context
.getTwilioClient()
.conversations.conversations(conversationSid);
console.log('conversation state:', (await conversationContext.fetch()).state);
// Create control task to prevent channel going stale
controlTask = await context
.getTwilioClient()
.taskrouter.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks.create({
workflowSid: context.SURVEY_WORKFLOW_SID,
taskChannel: 'survey',
attributes: JSON.stringify({
isChatCaptureControl: true,
conversationSid,
...parsedAdditionalControlTaskAttributes,
}),
timeout: controlTaskTTL || 45600, // 720 minutes or 12 hours
});
const webhooks = await conversationContext.webhooks.list();
for (const webhook of webhooks) {
if (webhook.target === 'studio') {
// eslint-disable-next-line no-await-in-loop
await webhook.remove();
}
}
} else {
[, controlTask] = await Promise.all([
// Remove the studio trigger webhooks to prevent this channel to trigger subsequent Studio flows executions
context
.getTwilioClient()
.chat.services(context.CHAT_SERVICE_SID)
.channels(channelSid)
.webhooks.list()
.then((channelWebhooks) =>
channelWebhooks.map(async (w) => {
.then((webhooks) =>
webhooks.map(async (w) => {
if (w.type === 'studio') {
await w.remove();
}
}),
),

/*
* Doing the "same" as above but for Conversations. Differences to the Studio Webhook in this case:
* - It's NOT found under the channel webhooks, but under the conversation webhooks
* - It uses the property 'target' instead of 'type'
*/
conversationSid &&
// Create control task to prevent channel going stale
context
.getTwilioClient()
.conversations.conversations(conversationSid)
.webhooks.list()
.then((channelWebhooks) =>
channelWebhooks.map(async (w) => {
if (w.target === 'studio') {
await w.remove();
}
.taskrouter.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks.create({
workflowSid: context.SURVEY_WORKFLOW_SID,
taskChannel: 'survey',
attributes: JSON.stringify({
isChatCaptureControl: true,
channelSid,
conversationSid,
...parsedAdditionalControlTaskAttributes,
}),
),

// Create control task to prevent channel going stale
context
.getTwilioClient()
.taskrouter.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks.create({
workflowSid: context.SURVEY_WORKFLOW_SID,
taskChannel: 'survey',
attributes: JSON.stringify({
isChatCaptureControl: true,
channelSid,
...parsedAdditionalControlTaskAttributes,
timeout: controlTaskTTL || 45600, // 720 minutes or 12 hours
}),
timeout: controlTaskTTL || 45600, // 720 minutes or 12 hours
}),
]);
]);
}

const { ENVIRONMENT, HELPLINE_CODE } = context;
let languageSanitized = language.replace('-', '_'); // Lex doesn't accept '-'
Expand Down

0 comments on commit 6f030f1

Please sign in to comment.