Skip to content

Commit

Permalink
Merge pull request #725 from techmatters/gian_CHI-3075
Browse files Browse the repository at this point in the history
fix: handle conversatiosn in sendMessageAndRunJanitor function [CHI-3075]
  • Loading branch information
GPaoloni authored Nov 25, 2024
2 parents 9b27d1e + 910482d commit 24c003e
Showing 1 changed file with 49 additions and 21 deletions.
70 changes: 49 additions & 21 deletions functions/sendMessageAndRunJanitor.protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ export const handler = async (
const resolve = bindResolve(callback)(response);

try {
const { channelSid } = event;
const { channelSid, conversationSid } = event;

if (channelSid === undefined) {
resolve(error400('ChannelSid parameter is missing'));
if (channelSid === undefined && conversationSid === undefined) {
resolve(error400('none of and channelSid provided, exactly one expected.'));
return;
}

Expand All @@ -51,28 +51,56 @@ export const handler = async (
const chatChannelJanitor = require(Runtime.getFunctions()['helpers/chatChannelJanitor'].path)
.chatChannelJanitor as ChatChannelJanitor;

const channelWebhooks = await context
.getTwilioClient()
.chat.services(context.CHAT_SERVICE_SID)
.channels(channelSid)
.webhooks.list();
if (conversationSid) {
const conversationWebhooks = await context
.getTwilioClient()
.conversations.v1.conversations(conversationSid)
.webhooks.list();

// Remove the studio trigger webhooks to prevent this channel to trigger subsequent Studio flows executions
await Promise.all(
channelWebhooks.map(async (w) => {
if (w.type === 'studio') {
await w.remove();
}
}),
);
// Remove the studio trigger webhooks to prevent this channel to trigger subsequent Studio flows executions
await Promise.all(
conversationWebhooks.map(async (w) => {
if (w.target === 'studio') {
await w.remove();
}
}),
);

// Send message
const result = await sendSystemMessage(context, event);
// Send message
const result = await sendSystemMessage(context, event);

// Deactivate channel and proxy
await chatChannelJanitor(context, { channelSid });
// Deactivate channel and proxy
await chatChannelJanitor(context, { conversationSid });

resolve(send(result.status)(result.message));
resolve(send(result.status)(result.message));
return;
}
// TODO: remove once all accounts have been migrated to conversations
if (channelSid) {
const channelWebhooks = await context
.getTwilioClient()
.chat.services(context.CHAT_SERVICE_SID)
.channels(channelSid)
.webhooks.list();

// Remove the studio trigger webhooks to prevent this channel to trigger subsequent Studio flows executions
await Promise.all(
channelWebhooks.map(async (w) => {
if (w.type === 'studio') {
await w.remove();
}
}),
);

// Send message
const result = await sendSystemMessage(context, event);

// Deactivate channel and proxy
await chatChannelJanitor(context, { channelSid });

resolve(send(result.status)(result.message));
return;
}
} catch (err: any) {
resolve(error500(err));
}
Expand Down

0 comments on commit 24c003e

Please sign in to comment.