Skip to content

Commit

Permalink
feat: 🎸 optimize crisp human assistance
Browse files Browse the repository at this point in the history
  • Loading branch information
OdapX committed Oct 9, 2023
1 parent e0c8ffd commit ec07331
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 56 deletions.
3 changes: 1 addition & 2 deletions apps/dashboard/pages/api/integrations/crisp/config-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ export const updateCrispConfig = async (
req: AppNextApiRequest,
res: NextApiResponse
) => {
const data = req.body as z.infer<typeof schema>;

const data = schema.parse(req.body)
// const websites = await getConnectedWebsites();

let metadata = {} as any;
Expand Down
191 changes: 137 additions & 54 deletions apps/dashboard/pages/api/integrations/crisp/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,69 @@ export const hook = async (req: AppNextApiRequest, res: NextApiResponse) => {
metadata?.choice === 'request_human' &&
newChoice?.value !== 'enable_ai'
) {
const agent = await getAgent(body.data.website_id);
const conversation = await prisma.conversation.findFirst({
where: {
AND: [{ visitorId: body.data.session_id }],
},
include: {
messages: {
orderBy: {
createdAt: 'desc',
},
},
},
});

// 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);
}
}
}
return 'User has requested a human operator, do not handle.';
}

Expand Down Expand Up @@ -369,62 +432,82 @@ 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],
user:{
type:'website',
nickname:'chaindesk'
}
}
);
}
);

// 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(
Expand Down

0 comments on commit ec07331

Please sign in to comment.