Skip to content

Commit

Permalink
Fix chatbot clean up / save for conversations
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhand committed May 24, 2024
1 parent 30edcaf commit 6e811a7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 29 deletions.
3 changes: 2 additions & 1 deletion functions/channelCapture/chatbotCallback.protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ export const handler = async (

await chatbotCallbackCleanup({
context,
channelOrConversation: conversation || channel!,
conversation,
channel,
channelAttributes,
memory: lexResponse.slots,
lexClient,
Expand Down
57 changes: 29 additions & 28 deletions functions/channelCapture/chatbotCallbackCleanup.protected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,25 @@ export type Body = {
channelSid: string;
};

type ChatbotCallbackCleanupParams = {
channel?: ChannelInstance;
conversation?: ConversationInstance;

context: Context<EnvVars>;
channelAttributes: { [k: string]: any };
memory?: { [key: string]: string };
lexClient: LexClient;
};

export const chatbotCallbackCleanup = async ({
context,
channelOrConversation,
channel,
conversation,
channelAttributes,
memory: lexMemory,
lexClient,
}: {
context: Context<EnvVars>;
channelOrConversation: ChannelInstance | ConversationInstance;
channelAttributes: { [k: string]: any };
memory?: { [key: string]: string };
lexClient: LexClient;
}) => {
}: ChatbotCallbackCleanupParams) => {
const memory = lexMemory || {};
const { isConversation } = channelAttributes;

const capturedChannelAttributes =
channelAttributes.capturedChannelAttributes as CapturedChannelAttributes;
Expand All @@ -86,12 +90,12 @@ export const chatbotCallbackCleanup = async ({
const updateChannelOrConversationAttributes = async (attributesObj: any) => {
const attributes = JSON.stringify(attributesObj);

if (isConversation) {
await (channelOrConversation as ConversationInstance).update({
if (conversation) {
await conversation.update({
attributes,
});
} else {
await (channelOrConversation as ChannelInstance).update({
await channel!.update({
attributes,
});
}
Expand All @@ -100,16 +104,13 @@ export const chatbotCallbackCleanup = async ({
const removeWebhookFromChannelOrConversation = async () => {
if (!capturedChannelAttributes.chatbotCallbackWebhookSid) return;

if (capturedChannelAttributes.chatbotCallbackWebhookSid) {
await (channelOrConversation as ConversationInstance)
.webhooks()
.get(capturedChannelAttributes.chatbotCallbackWebhookSid)
.remove();
} else {
await (channelOrConversation as ChannelInstance)
if (conversation) {
await conversation
.webhooks()
.get(capturedChannelAttributes.chatbotCallbackWebhookSid)
.remove();
} else if (channel) {
await channel.webhooks().get(capturedChannelAttributes.chatbotCallbackWebhookSid).remove();
}
};

Expand All @@ -120,7 +121,7 @@ export const chatbotCallbackCleanup = async ({
lexClient.deleteSession(context, {
botName: capturedChannelAttributes.botName,
botAlias: capturedChannelAttributes.botAlias,
userId: channelOrConversation.sid,
userId: (conversation ?? channel!).sid,
}),
// Update channel attributes (remove channelCapturedByBot and add memory)
updateChannelOrConversationAttributes(releasedChannelAttributes),
Expand All @@ -130,7 +131,7 @@ export const chatbotCallbackCleanup = async ({
capturedChannelAttributes &&
channelCaptureHandlers.handleChannelRelease(
context,
channelOrConversation,
conversation ?? channel!,
capturedChannelAttributes,
memory,
),
Expand All @@ -155,21 +156,21 @@ export const handler = async (
}

const client = context.getTwilioClient();
let channel: ChannelInstance | undefined;
const conversation = await client.conversations.v1.conversations(channelSid).fetch();
const channel = await client.chat
.services(context.CHAT_SERVICE_SID)
.channels(channelSid)
.fetch();

const channelOrConversation = conversation || channel;
if (!conversation) {
channel = await client.chat.services(context.CHAT_SERVICE_SID).channels(channelSid).fetch();
}

const channelAttributes = JSON.parse(channelOrConversation.attributes);
const channelAttributes = JSON.parse((conversation || channel).attributes);

const lexClient = require(Runtime.getFunctions()['channelCapture/lexClient'].path) as LexClient;

await chatbotCallbackCleanup({
context,
channelOrConversation,
channel,
conversation,
channelAttributes,
lexClient,
});
Expand Down

0 comments on commit 6e811a7

Please sign in to comment.