Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugin-hrm-form/src/HrmFormPlugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ const setUpActions = (
// bind setupObject to the functions that requires some initialization
const wrapupOverride = ActionFunctions.wrapupTask(setupObject, getMessage);

Flex.Actions.addListener('beforeAcceptTask', ActionFunctions.beforeAcceptTask(setupObject, getMessage));
Flex.Actions.addListener('afterAcceptTask', ActionFunctions.afterAcceptTask(featureFlags, setupObject, getMessage));

setUpTransferActions(setupObject);
Expand Down
53 changes: 36 additions & 17 deletions plugin-hrm-form/src/utils/setUpActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,20 @@ const sendMessageOfKey = (messageKey: string) => (
conversation: Conversation,
getMessage: (key: string) => (key: string) => Promise<string>,
): ActionFunction => async (payload: ActionPayload) => {
const taskLanguage = getTaskLanguage(setupObject)(payload.task);
const message = await getMessage(messageKey)(taskLanguage);
const res = await conversation.sendMessage(message);
console.log(
`Successfully sent message '${message}' from key ${messageKey} translated to ${taskLanguage}, added as index ${res}`,
);
try {
console.log('>>>>>> sendMessageOfKey executed');
const taskLanguage = getTaskLanguage(setupObject)(payload.task);
const message = await getMessage(messageKey)(taskLanguage);
console.log('>>>>>> sendMessageOfKey message', message);
const res = await conversation.sendMessage(message);
console.log('>>>>>> sendMessageOfKey result', res);
console.log(
`Successfully sent message '${message}' from key ${messageKey} translated to ${taskLanguage}, added as index ${res}`,
);
} catch (err) {
console.error('>>>>>> sendMessageOfKey error', err);
throw err;
}
};

const sendSystemMessageOfKey = (messageKey: string) => (
Expand Down Expand Up @@ -108,21 +116,23 @@ const sendWelcomeMessageOnConversationJoined = (
getMessage: GetMessage,
payload: ActionPayload,
) => {
console.log('>>>>>> sendWelcomeMessageOnConversationJoined');
const manager = Manager.getInstance();
const trySendWelcomeMessage = (convo: Conversation, ms: number, retries: number) => {
console.log('>>>>>> trySendWelcomeMessage');
setTimeout(async () => {
try {
const convoState = manager.store.getState().flex.chat.conversations[convo.sid];
if (!convoState) {
console.warn(
`Conversation ${convo.sid}, which should be for task ${payload.task.taskSid} not found in redux store.`,
`>>>>>> Conversation ${convo.sid}, which should be for task ${payload.task.taskSid} not found in redux store.`,
);
return;
}
// if channel is not ready, wait 200ms and retry
if (convoState.isLoadingParticipants || convoState.isLoadingConversation || convoState.isLoadingMessages) {
if (retries < 10) trySendWelcomeMessage(convo, 200, retries + 1);
else console.error('Failed to send welcome message: max retries reached.');
else console.error('>>>>>> Failed to send welcome message: max retries reached.');
} else {
sendWelcomeMessage(setupObject, convo, getMessage)(payload);
}
Expand All @@ -131,13 +141,30 @@ const sendWelcomeMessageOnConversationJoined = (
if (retries < 10) {
trySendWelcomeMessage(convo, 200, retries + 1);
} else {
console.error('Failed to send welcome message: max retries reached due to error.', error);
console.error('>>>>>> Failed to send welcome message: max retries reached due to error.', error);
}
}
}, ms);
};
// Ignore event payload as we already have everything we want in afterAcceptTask arguments. Start at 0ms as many users are able to send the message right away
console.log('>>>>>> subscribing to event conversationJoined');
manager.conversationsClient.once('conversationJoined', (c: Conversation) => trySendWelcomeMessage(c, 0, 0));
console.log('>>>>>> subscribed to event conversationJoined');
};

export const beforeAcceptTask = (setupObject: SetupObject, getMessage: GetMessage) => async (
payload: ActionPayload,
) => {
const { task } = payload;

if (TaskHelper.isChatBasedTask(task)) {
subscribeAlertOnConversationJoined(task);
}

// If this is the first counsellor that gets the task, say hi
if (TaskHelper.isChatBasedTask(task) && !TransferHelpers.hasTransferStarted(task)) {
sendWelcomeMessageOnConversationJoined(setupObject, getMessage, payload);
}
};

export const afterAcceptTask = (featureFlags: FeatureFlags, setupObject: SetupObject, getMessage: GetMessage) => async (
Expand All @@ -158,14 +185,6 @@ export const afterAcceptTask = (featureFlags: FeatureFlags, setupObject: SetupOb
}
}
});
if (TaskHelper.isChatBasedTask(task)) {
subscribeAlertOnConversationJoined(task);
}

// If this is the first counsellor that gets the task, say hi
if (TaskHelper.isChatBasedTask(task) && !TransferHelpers.hasTransferStarted(task)) {
sendWelcomeMessageOnConversationJoined(setupObject, getMessage, payload);
}

if (TransferHelpers.hasTransferStarted(task)) {
await handleTransferredTask(task);
Expand Down
Loading