Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpetrov committed Oct 13, 2023
1 parent 4f02648 commit 66e15f4
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof schema>;
// const websites = await getConnectedWebsites();

let metadata = {} as any;
Expand Down
236 changes: 112 additions & 124 deletions apps/dashboard/pages/api/integrations/crisp/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 66e15f4

Please sign in to comment.