diff --git a/apps/dashboard/pages/api/integrations/crisp/config-update.ts b/apps/dashboard/pages/api/integrations/crisp/config-update.ts index 0565cdcd3..2cb694b59 100644 --- a/apps/dashboard/pages/api/integrations/crisp/config-update.ts +++ b/apps/dashboard/pages/api/integrations/crisp/config-update.ts @@ -22,8 +22,7 @@ export const updateCrispConfig = async ( req: AppNextApiRequest, res: NextApiResponse ) => { - const data = req.body as z.infer; - + const data = schema.parse(req.body) // 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 563e9b7b2..432b86629 100644 --- a/apps/dashboard/pages/api/integrations/crisp/hooks.ts +++ b/apps/dashboard/pages/api/integrations/crisp/hooks.ts @@ -308,13 +308,13 @@ export const hook = async (req: AppNextApiRequest, res: NextApiResponse) => { (one: any) => one.selected ); - if ( - metadata?.choice === 'request_human' && - newChoice?.value !== 'enable_ai' - ) { - return 'User has requested a human operator, do not handle.'; - } + // if ( + // metadata?.choice === 'request_human' && + // newChoice?.value !== 'enable_ai' + // ) { + // return 'User has requested a human operator, do not handle.'; + // } switch (body.event) { case 'message:send': if ( @@ -369,62 +369,78 @@ export const hook = async (req: AppNextApiRequest, res: NextApiResponse) => { switch (selected?.value) { case 'request_human': - await CrispClient.website.updateConversationMetas( - body.website_id, - body.data.session_id, - { - data: { - choice: '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] + } + ); } - ); - - // const data = - // await CrispClient.website.listLastActiveWebsiteOperators( - // body.website_id - // ); - - // await CrispClient.website.sendMessageInConversation( - // body.website_id, - // body.data.session_id, - // { - // type: 'text', - // from: 'operator', - // origin: 'chat', - - // content: 'An operator will get back to you shortly.', - // user: { - // type: 'participant', - // // nickname: agent?.name || 'Chaindesk', - // avatar: 'https://chaindesk.ai/app-rounded-bg-white.png', - // }, - // // mentions: [data?.[0]?.user_id], - // } - // ); - - 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, - }, - ], - }, + 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: 'Unfortunetly, all the operators are not available at the moment.', + choices: [ + { + value: 'resolved', + icon: '✅', + label: 'Mark as resolved', + selected: false, + } + ], + }, + } + ); } - ); - break; case 'resolved': await CrispClient.website.changeConversationState(