diff --git a/apps/dashboard/pages/api/integrations/crisp/config-update.ts b/apps/dashboard/pages/api/integrations/crisp/config-update.ts index 2cb694b59..f08d35d78 100644 --- a/apps/dashboard/pages/api/integrations/crisp/config-update.ts +++ b/apps/dashboard/pages/api/integrations/crisp/config-update.ts @@ -22,7 +22,7 @@ export const updateCrispConfig = async ( req: AppNextApiRequest, res: NextApiResponse ) => { - const data = schema.parse(req.body) + const data = req.body as z.infer; // const websites = await getConnectedWebsites(); let metadata = {} as any; diff --git a/apps/dashboard/pages/api/integrations/crisp/hooks.ts b/apps/dashboard/pages/api/integrations/crisp/hooks.ts index 860d883d2..186faee1d 100644 --- a/apps/dashboard/pages/api/integrations/crisp/hooks.ts +++ b/apps/dashboard/pages/api/integrations/crisp/hooks.ts @@ -325,57 +325,33 @@ export const hook = async (req: AppNextApiRequest, res: NextApiResponse) => { }, }, }); - - // save the new message - const conversationManager = new ConversationManager({ - organizationId: agent?.organizationId!, - channel: ConversationChannel.crisp, - agentId: agent?.id!, - visitorId: body.data.session_id, - conversationId:conversation?.id, - }); - conversationManager.push({ - from: MessageFrom.human, - text: body.data.content, - }); - conversationManager.save() - - const mostRecentMessage = conversation?.messages[0] - if(mostRecentMessage?.from == 'human') { - const oneHourAgo = new Date().getTime() - (60 * 60 * 1000); - - if (new Date(mostRecentMessage.createdAt).getTime() < oneHourAgo) { - CrispClient.website.composeMessageInConversation( - body.website_id, - body.data.session_id, - { - type: 'start', - from: 'operator', - } - ); - await CrispClient.website.updateConversationMetas( - body.website_id, - body.data.session_id, - { - data: { - choice: 'enable_ai', - }, - } - ); - - try { - return handleQuery( - body.website_id, - body.data.session_id, - body.data.content - ); - - } catch (err) { - req.logger.error(err); - } - } + + const mostRecentMessage = conversation?.messages[0]; + const oneHourAgo = new Date().getTime() - 60 * 60 * 1000; + + if (new Date(mostRecentMessage?.createdAt!).getTime() < oneHourAgo) { + CrispClient.website.composeMessageInConversation( + body.website_id, + body.data.session_id, + { + type: 'start', + from: 'operator', + } + ); + await CrispClient.website.updateConversationMetas( + body.website_id, + body.data.session_id, + { + data: { + choice: 'enable_ai', + }, + } + ); + + metadata.choice = 'enable_ai'; + } else { + return 'User has requested a human operator, do not handle.'; } - return 'User has requested a human operator, do not handle.'; } switch (body.event) { @@ -432,82 +408,94 @@ export const hook = async (req: AppNextApiRequest, res: NextApiResponse) => { switch (selected?.value) { case 'request_human': - const availibility = await CrispClient.website.getWebsiteAvailabilityStatus(body.data.website_id); - const status = availibility?.status - - if(status === "online"){ - const active_operators: {user_id:string,avatar:string|null, timestamp:number}[] = await CrispClient.website.listLastActiveWebsiteOperators(body.data.website_id) - const highly_active_operator = active_operators.filter(op => op.timestamp == Math.min(...active_operators.map(o => o.timestamp)))[0]; - await CrispClient.website.updateConversationMetas( - body.website_id, - body.data.session_id, - { - data: { - choice: 'request_human', - }, - } - ); - - await CrispClient.website.sendMessageInConversation( - body.website_id, - body.data.session_id, - { - type: 'picker', - from: 'operator', - origin: 'chat', - content: { - id: 'chaindesk-enable', - text: 'An operator will get back to you shortly.', - choices: [ - { - value: 'enable_ai', - icon: '▶️', - label: 'Re-enable AI', - selected: false, - }, - ] - }, - mentions: [highly_active_operator.user_id], - user:{ - type:'website', - nickname:'chaindesk' - } - } - ); - } - else { - // website offline - await CrispClient.website.updateConversationMetas( - body.website_id, - body.data.session_id, + const availibility = + await CrispClient.website.getWebsiteAvailabilityStatus( + body.data.website_id + ); + const status = availibility?.status; + + if (status === 'online') { + const active_operators: { + user_id: string; + avatar: string | null; + timestamp: number; + }[] = await CrispClient.website.listLastActiveWebsiteOperators( + body.data.website_id + ); + const highly_active_operator = active_operators.filter( + (op) => + op.timestamp == + Math.min(...active_operators.map((o) => o.timestamp)) + )[0]; + await CrispClient.website.updateConversationMetas( + body.website_id, + body.data.session_id, + { + data: { + choice: 'request_human', + }, + } + ); + + await CrispClient.website.sendMessageInConversation( + body.website_id, + body.data.session_id, + { + type: 'picker', + from: 'operator', + origin: 'chat', + content: { + id: 'chaindesk-enable', + text: 'An operator will get back to you shortly.', + choices: [ { - data: { - choice: 'enable_ai', - }, - } - ); - await CrispClient.website.sendMessageInConversation( - body.website_id, - body.data.session_id, + value: 'enable_ai', + icon: '▶️', + label: 'Re-enable AI', + selected: false, + }, + ], + }, + mentions: [highly_active_operator.user_id], + user: { + type: 'website', + nickname: 'chaindesk', + }, + } + ); + } else { + // website offline + await CrispClient.website.updateConversationMetas( + body.website_id, + body.data.session_id, + { + data: { + choice: 'enable_ai', + }, + } + ); + await CrispClient.website.sendMessageInConversation( + body.website_id, + body.data.session_id, + { + type: 'picker', + from: 'operator', + origin: 'chat', + content: { + id: 'chaindesk-enable', + text: 'Unfortunately, no operators are available at the moment.', + choices: [ { - type: 'picker', - from: 'operator', - origin: 'chat', - content: { - id: 'chaindesk-enable', - text: 'Unfortunetly, all the operators are not available at the moment.', - choices: [ - { - value: 'resolved', - icon: '✅', - label: 'Mark as resolved', - selected: false, - } - ], - }, - } - ); - } + value: 'resolved', + icon: '✅', + label: 'Mark as resolved', + selected: false, + }, + ], + }, + } + ); + } break; case 'resolved': await CrispClient.website.changeConversationState(