Commit 25299736 authored by murtaza98's avatar murtaza98

apply suggestions from code review and add a new setting for default handover department name

parent 19e22a45
......@@ -11,5 +11,5 @@
"name": "Rasa",
"nameSlug": "rasa",
"classFile": "RasaApp.ts",
"description": "Integration between Rocket.Chat and the RASA Chatbot platform. Documentation available at: https://github.com/RocketChat/Apps.Rasa"
"description": "Integration between Rocket.Chat and the RASA Chatbot platform."
}
......@@ -7,12 +7,13 @@ export enum AppSetting {
RasaHandoverMessage = 'rasa_handover_message',
RasaCloseChatMessage = 'rasa_close_chat_message',
RasaEnableCallbacks = 'rasa_enable_callbacks',
RasaDefaultHandoverDepartment = 'rasa_target_handover_department',
}
export enum DefaultMessage {
DEFAULT_DialogflowServiceUnavailableMessage = 'Sorry, I\'m having trouble answering your question.',
DEFAULT_DialogflowHandoverMessage = 'Transferring to an online agent',
DEFAULT_DialogflowCloseChatMessage = 'Closing the chat, Goodbye',
DEFAULT_RasaServiceUnavailableMessage = 'Sorry, I\'m having trouble answering your question.',
DEFAULT_RasaHandoverMessage = 'Transferring to an online agent',
DEFAULT_RasaCloseChatMessage = 'Closing the chat, Goodbye',
}
export const settings: Array<ISetting> = [
......@@ -59,6 +60,15 @@ export const settings: Array<ISetting> = [
i18nDescription: 'rasa_handover_message_description',
required: false,
},
{
id: AppSetting.RasaDefaultHandoverDepartment,
public: true,
type: SettingType.STRING,
packageValue: '',
i18nLabel: 'rasa_default_handover_department',
i18nDescription: 'rasa_default_handover_department_description',
required: true,
},
{
id: AppSetting.RasaEnableCallbacks,
public: true,
......@@ -67,6 +77,6 @@ export const settings: Array<ISetting> = [
value: false,
i18nLabel: 'rasa_callback_message',
i18nDescription: 'rasa_callback_message_description',
required: false,
required: true,
},
];
......@@ -38,10 +38,13 @@ export class IncomingEndpoint extends ApiEndpoint {
await closeChat(modify, read, sessionId);
break;
case EndpointActionNames.HANDOVER:
const { actionData: { targetDepartment = '' } = {} } = endpointContent;
const { actionData: { targetDepartment = null } = {} } = endpointContent;
const room = await read.getRoomReader().getById(sessionId) as ILivechatRoom;
if (!room) { throw new Error(Logs.INVALID_SESSION_ID); }
const { visitor: { token: visitorToken } } = room;
const { visitor: { token: visitorToken }, department: { name = null } = {} } = room;
if (targetDepartment && name && targetDepartment === name) {
throw new Error(Logs.INVALID_ACTION_USER_ALREADY_IN_DEPARTMENT);
}
await performHandover(modify, read, sessionId, visitorToken, targetDepartment);
break;
default:
......
......@@ -7,6 +7,7 @@ export enum Logs {
INVALID_RASA_SERVER_URL_SETTING = 'Error! Rasa server url not valid',
INVALID_VISITOR_TOKEN = 'Error: Visitor Token not valid',
INVALID_DEPARTMENT_NAME = 'Error: Department Name is not valid',
INVALID_ACTION_USER_ALREADY_IN_DEPARTMENT = 'Error! Invalid request. User is already present in the specified department',
ENDPOINT_REQUEST_PROCESSING_ERROR = 'Error occurred while processing the request. Details:- ',
INVALID_ENDPOINT_ACTION = 'Error!! Invalid Action type',
EMPTY_BOT_USERNAME_SETTING = 'The Bot Username setting is not defined.',
......@@ -15,4 +16,5 @@ export enum Logs {
INVALID_RESPONSE_FROM_RASA_CONTENT_UNDEFINED = 'Error Parsing RASA\'s Response. Content is undefined',
CLOSE_CHAT_REQUEST_FAILED_ERROR = 'Error: Internal Server Error. Could not close the chat',
HANDOVER_REQUEST_FAILED_ERROR = 'Error occurred while processing handover. Details',
INVALID_DEPARTMENT_NAME_IN_APP_SETTING = 'Error: Department Name is not valid in app setting',
}
......@@ -54,7 +54,7 @@ export class PostMessageSentHandler {
const serviceUnavailable: string = await getAppSettingValue(this.read, AppSetting.RasaServiceUnavailableMessage);
await createMessage(rid, this.read, this.modify, {
text: serviceUnavailable ? serviceUnavailable : DefaultMessage.DEFAULT_DialogflowServiceUnavailableMessage,
text: serviceUnavailable ? serviceUnavailable : DefaultMessage.DEFAULT_RasaServiceUnavailableMessage,
});
return;
......
......@@ -8,5 +8,7 @@
"rasa_close_chat_message": "Close Chat Message",
"rasa_close_chat_message_description": "This message will be sent automatically when a chat is closed",
"rasa_callback_message": "Enable Callbacks",
"rasa_callback_message_description": "Enabling this setting will allow the app to use only callback messages. This feature is useful when you are using Reminder messages in your RASA bot."
"rasa_callback_message_description": "Enabling this setting will allow the app to use only callback messages. This feature is useful when you are using Reminder messages in your RASA bot.",
"rasa_default_handover_department": "Default Handover Department Name",
"rasa_default_handover_department_description": "Enter the target department name where you want to transfer the visitor upon handover"
}
......@@ -38,14 +38,14 @@ export const closeChat = async (modify: IModify, read: IRead, rid: string) => {
const closeChatMessage = await getAppSettingValue(read, AppSetting.RasaCloseChatMessage);
const result = await modify.getUpdater().getLivechatUpdater()
.closeRoom(room, closeChatMessage ? closeChatMessage : DefaultMessage.DEFAULT_DialogflowCloseChatMessage);
.closeRoom(room, closeChatMessage ? closeChatMessage : DefaultMessage.DEFAULT_RasaCloseChatMessage);
if (!result) { throw new Error(Logs.CLOSE_CHAT_REQUEST_FAILED_ERROR); }
};
export const performHandover = async (modify: IModify, read: IRead, rid: string, visitorToken: string, targetDepartmentName?: string) => {
export const performHandover = async (modify: IModify, read: IRead, rid: string, visitorToken: string, targetDepartmentName?: string | null) => {
const handoverMessage: string = await getAppSettingValue(read, AppSetting.RasaHandoverMessage);
await createMessage(rid, read, modify, { text: handoverMessage ? handoverMessage : DefaultMessage.DEFAULT_DialogflowHandoverMessage });
await createMessage(rid, read, modify, { text: handoverMessage ? handoverMessage : DefaultMessage.DEFAULT_RasaHandoverMessage });
const room: ILivechatRoom = (await read.getRoomReader().getById(rid)) as ILivechatRoom;
if (!room) { throw new Error(Logs.INVALID_ROOM_ID); }
......@@ -57,11 +57,18 @@ export const performHandover = async (modify: IModify, read: IRead, rid: string,
currentRoom: room,
};
// Fill livechatTransferData.targetDepartment param if required
// Fill livechatTransferData.targetDepartment param
if (targetDepartmentName) {
const targetDepartment: IDepartment = (await read.getLivechatReader().getLivechatDepartmentByIdOrName(targetDepartmentName)) as IDepartment;
if (!targetDepartment) { throw new Error(Logs.INVALID_DEPARTMENT_NAME); }
livechatTransferData.targetDepartment = targetDepartment.id;
} else {
const defaultTargetDepartmentName = await getAppSettingValue(read, AppSetting.RasaDefaultHandoverDepartment);
if (!defaultTargetDepartmentName) { throw new Error(Logs.INVALID_DEPARTMENT_NAME_IN_APP_SETTING); }
const targetDepartment: IDepartment = (await read.getLivechatReader().getLivechatDepartmentByIdOrName(defaultTargetDepartmentName)) as IDepartment;
if (!targetDepartment) { throw new Error(Logs.INVALID_DEPARTMENT_NAME_IN_APP_SETTING); }
livechatTransferData.targetDepartment = targetDepartment.id;
}
const result = await modify.getUpdater().getLivechatUpdater().transferVisitor(visitor, livechatTransferData)
......@@ -71,6 +78,6 @@ export const performHandover = async (modify: IModify, read: IRead, rid: string,
if (!result) {
const offlineMessage: string = await getAppSettingValue(read, AppSetting.RasaServiceUnavailableMessage);
await createMessage(rid, read, modify, { text: offlineMessage ? offlineMessage : DefaultMessage.DEFAULT_DialogflowServiceUnavailableMessage });
await createMessage(rid, read, modify, { text: offlineMessage ? offlineMessage : DefaultMessage.DEFAULT_RasaServiceUnavailableMessage });
}
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment