Skip to content
Open
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
14 changes: 12 additions & 2 deletions DialogflowApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@rocket.chat/apps-engine/definition/accessors';
import { ApiSecurity, ApiVisibility } from '@rocket.chat/apps-engine/definition/api';
import { App } from '@rocket.chat/apps-engine/definition/App';
import { ILivechatMessage } from '@rocket.chat/apps-engine/definition/livechat';
import { ILivechatMessage, ILivechatEventContext, IPostLivechatAgentAssigned } from '@rocket.chat/apps-engine/definition/livechat';
import { IPostMessageSent } from '@rocket.chat/apps-engine/definition/messages';
import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata';
import { ISetting } from '@rocket.chat/apps-engine/definition/settings';
Expand All @@ -20,9 +20,10 @@ import { FulfillmentsEndpoint } from './endpoints/FulfillmentsEndpoint';
import { IncomingEndpoint } from './endpoints/IncomingEndpoint';
import { ExecuteLivechatBlockActionHandler } from './handler/ExecuteLivechatBlockActionHandler';
import { OnSettingUpdatedHandler } from './handler/OnSettingUpdatedHandler';
import { OnAgentAssignedHandler } from './handler/OnAgentAssignedHandler';
import { PostMessageSentHandler } from './handler/PostMessageSentHandler';

export class DialogflowApp extends App implements IPostMessageSent, IUIKitLivechatInteractionHandler {
export class DialogflowApp extends App implements IPostMessageSent, IPostLivechatAgentAssigned, IUIKitLivechatInteractionHandler {
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
super(info, logger, accessors);
}
Expand All @@ -45,6 +46,15 @@ export class DialogflowApp extends App implements IPostMessageSent, IUIKitLivech
await handler.run();
}

public async executePostLivechatAgentAssigned(context: ILivechatEventContext,
read: IRead,
http: IHttp,
persis: IPersistence,
modify: IModify): Promise<void> {
const handler = new OnAgentAssignedHandler(this, context, read, http, persis, modify);
await handler.run();
}

public async onSettingUpdated(setting: ISetting, configurationModify: IConfigurationModify, read: IRead, http: IHttp): Promise<void> {
const onSettingUpdatedHandler: OnSettingUpdatedHandler = new OnSettingUpdatedHandler(this, read, http);
await onSettingUpdatedHandler.run();
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ You can find all these credentials in a JSON file, which u can get from [here](h
- This message will be sent automatically when a chat is closed
10. Hide Quick Replies (required)
- If enabled, then all quick-replies will hide when a visitor clicks on any one of them
11. Send Welcome Intent (required)
- If enabled, then a welcome event will be sent to bot, when the bot is assigned to any visitor

4. (Optional Step) Lastly you can test your Dialogflow Connection by viewing App Logs. To view the logs, goto App Page (`Setting > Apps > Dialogflow`). There click on menu item (3 vertical dots icon) and then select `View Logs`. There select the **most recent** `onSettingUpdated` title. If you see `------------------ Google Credentials validation Success ----------------` message, then it means your setup is fine. If you don't see this message, then recheck your Dialogflow credentials.

Expand Down
31 changes: 31 additions & 0 deletions config/Settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ export enum AppSetting {
DialogflowServiceUnavailableMessage = 'dialogflow_service_unavailable_message',
DialogflowCloseChatMessage = 'dialogflow_close_chat_message',
DialogflowHideQuickReplies = 'dialogflow_hide_quick_replies',
DialogflowEnableWelcomeMessage = 'dialogflow_enable_welcome_message',
DialogflowWelcomeMessage = 'dialogflow_welcome_message',
DialogflowWelcomeIntentOnStart = 'dialogflow_welcome_intent_on_start',
}

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_DialogflowWelcomeMessage = 'Thanks for connecting',
}

export const settings: Array<ISetting> = [
Expand Down Expand Up @@ -108,4 +112,31 @@ export const settings: Array<ISetting> = [
i18nDescription: 'dialogflow_hide_quick_replies_description',
required: true,
},
{
id: AppSetting.DialogflowEnableWelcomeMessage,
public: true,
type: SettingType.BOOLEAN,
packageValue: false,
i18nLabel: 'dialogflow_enable_welcome_message',
i18nDescription: 'dialogflow_enable_welcome_message_description',
required: false,
},
{
id: AppSetting.DialogflowWelcomeMessage,
public: true,
type: SettingType.STRING,
packageValue: DefaultMessage.DEFAULT_DialogflowWelcomeMessage,
i18nLabel: 'dialogflow_welcome_message',
i18nDescription: 'dialogflow_welcome_message_description',
required: false,
},
{
id: AppSetting.DialogflowWelcomeIntentOnStart,
public: true,
type: SettingType.BOOLEAN,
packageValue: false,
i18nLabel: 'dialogflow_welcome_intent_on_start',
i18nDescription: 'dialogflow_welcome_intent_on_start_description',
required: true,
},
];
73 changes: 73 additions & 0 deletions handler/OnAgentAssignedHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { IHttp, IModify, IPersistence, IRead } from '@rocket.chat/apps-engine/definition/accessors';
import { IApp } from '@rocket.chat/apps-engine/definition/IApp';
import { ILivechatEventContext, ILivechatRoom } from '@rocket.chat/apps-engine/definition/livechat';
import { RoomType } from '@rocket.chat/apps-engine/definition/rooms';
import { AppSetting, DefaultMessage } from '../config/Settings';
import { DialogflowRequestType, IDialogflowMessage } from '../enum/Dialogflow';
import { Logs } from '../enum/Logs';
import { Dialogflow } from '../lib/Dialogflow';
import { createDialogflowMessage, createMessage } from '../lib/Message';
import { updateRoomCustomFields } from '../lib/Room';
import { getAppSettingValue } from '../lib/Settings';

export class OnAgentAssignedHandler {
constructor(private readonly app: IApp,
private readonly context: ILivechatEventContext,
private readonly read: IRead,
private readonly http: IHttp,
private readonly persis: IPersistence,
private readonly modify: IModify) {}

public async run() {
const { room } = this.context;
const livechatRoom = room as ILivechatRoom;

const { id: rid, type, servedBy, isOpen, customFields = {} } = livechatRoom;
const { welcomeEventSent = false } = customFields;

const DialogflowBotUsername: string = await getAppSettingValue(this.read, AppSetting.DialogflowBotUsername);
const { value: sendWelcomeEvent } = await this.read.getEnvironmentReader().getSettings().getById(AppSetting.DialogflowWelcomeIntentOnStart);
const { value: sendWelcomeMessage } = await this.read.getEnvironmentReader().getSettings().getById(AppSetting.DialogflowEnableWelcomeMessage);

if (!type || type !== RoomType.LIVE_CHAT) {
return;
}

if (!isOpen || !sendWelcomeEvent) {
return;
}

if (!servedBy || servedBy.username !== DialogflowBotUsername) {
return;
}

if (welcomeEventSent) {
return;
}

if (sendWelcomeMessage) {
const welcomeMessage: string = await getAppSettingValue(this.read, AppSetting.DialogflowWelcomeMessage);
await createMessage(rid, this.read, this.modify, { text: welcomeMessage || DefaultMessage.DEFAULT_DialogflowWelcomeMessage });
}

await updateRoomCustomFields(rid, { welcomeEventSent: true }, this.read, this.modify);

try {
const event = { name: "Welcome", languageCode: "en" };
Copy link
Contributor

@murtaza98 murtaza98 Sep 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can u highlight this event-name somewhere, maybe in the setting description or App's README?

const response: IDialogflowMessage = await Dialogflow.sendRequest(this.http, this.read, this.modify, rid, event, DialogflowRequestType.EVENT);

await createDialogflowMessage(rid, this.read, this.modify, response);
} catch (error) {
this.app.getLogger().error(`${Logs.DIALOGFLOW_REST_API_ERROR} ${error.message}`);

const serviceUnavailable: string = await getAppSettingValue(this.read, AppSetting.DialogflowServiceUnavailableMessage);

await createMessage(rid,
this.read,
this.modify,
{ text: serviceUnavailable ? serviceUnavailable : DefaultMessage.DEFAULT_DialogflowServiceUnavailableMessage });

return;
}
}
}
8 changes: 7 additions & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@
"dialogflow_close_chat_message": "Close Chat Message",
"dialogflow_close_chat_message_description": "This message will be sent automatically when a chat is closed",
"dialogflow_hide_quick_replies": "Hide Quick Replies",
"dialogflow_hide_quick_replies_description": "If enabled, then all quick-replies will hide when a visitor clicks on any one of them"
"dialogflow_hide_quick_replies_description": "If enabled, then all quick-replies will hide when a visitor clicks on any one of them",
"dialogflow_enable_welcome_message": "Enable Custom Welcome Message",
"dialogflow_enable_welcome_message_description": "If enabled, then a custom welcome message will be sent when bot is assigned to any visitor",
"dialogflow_welcome_message": "Dialogflow Message to send on start",
"dialogflow_welcome_message_description": "Custom welcome message to send to visitor",
"dialogflow_welcome_intent_on_start": "Send Welcome Intent",
"dialogflow_welcome_intent_on_start_description": "If enabled, then a welcome event will be sent when bot is assigned to any visitor"
}