Skip to content

Commit d624adf

Browse files
authored
Merge pull request #2 from murtaza98/uikit-review
apply suggestions from code review
2 parents 84f836d + ce4725b commit d624adf

7 files changed

Lines changed: 40 additions & 22 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ You can find all these credentials in a JSON file, which u can get from [here](h
6161
- The Bot will send this message to Visitor if service is unavailable like suppose if no agents are online.
6262
9. Close Chat Message (optional)
6363
- This message will be sent automatically when a chat is closed
64+
10. Hide Quick Replies (required)
65+
- If enabled, then all quick-replies will hide when a visitor clicks on any one of them
6466

6567
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.
6668

app.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "21b7d3ba-031b-41d9-8ff2-fbbfa081ae90",
3-
"version": "1.0.0",
3+
"version": "1.2.0",
44
"requiredApiVersion": "^1.15.0",
55
"iconFile": "icon.png",
66
"author": {

config/Settings.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export enum AppSetting {
1010
DialogflowHandoverMessage = 'dialogflow_handover_message',
1111
DialogflowServiceUnavailableMessage = 'dialogflow_service_unavailable_message',
1212
DialogflowCloseChatMessage = 'dialogflow_close_chat_message',
13+
DialogflowHideQuickReplies = 'dialogflow_hide_quick_replies',
1314
}
1415

1516
export enum DefaultMessage {
@@ -97,4 +98,14 @@ export const settings: Array<ISetting> = [
9798
i18nDescription: 'dialogflow_close_chat_message_description',
9899
required: false,
99100
},
101+
{
102+
id: AppSetting.DialogflowHideQuickReplies,
103+
public: true,
104+
type: SettingType.BOOLEAN,
105+
packageValue: true,
106+
value: true,
107+
i18nLabel: 'dialogflow_hide_quick_replies',
108+
i18nDescription: 'dialogflow_hide_quick_replies_description',
109+
required: true,
110+
},
100111
];

enum/Dialogflow.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ export interface IDialogflowMessage {
88

99
export interface IDialogflowQuickReplies {
1010
text: string;
11-
options: Array<IDialogflowQuickRepliesOptions>;
11+
options: Array<IDialogflowQuickReplyOptions>;
1212
}
1313

14-
export interface IDialogflowQuickRepliesOptions {
14+
export interface IDialogflowQuickReplyOptions {
1515
text: string;
1616
actionId?: string;
1717
buttonStyle?: ButtonStyle;

handler/ExecuteLivechatBlockActionHandler.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,27 @@ export class ExecuteLivechatBlockActionHandler {
2020
try {
2121
const interactionData = this.context.getInteractionData();
2222

23-
const { visitor: { token }, room, container: { id, type }, value } = interactionData;
23+
const { visitor, room, container: { id, type }, value } = interactionData;
2424

25-
if (type !== UIKitIncomingInteractionContainerType.MESSAGE || !room) { return this.context.getInteractionResponder().successResponse(); }
25+
if (type !== UIKitIncomingInteractionContainerType.MESSAGE) {
26+
return this.context.getInteractionResponder().successResponse();
27+
}
2628

29+
const DialogflowBotUsername: string = await getAppSettingValue(this.read, AppSetting.DialogflowBotUsername);
2730
const { servedBy: { username = null } = {}, id: rid } = room as ILivechatRoom;
28-
if (!username) { return this.context.getInteractionResponder().successResponse(); }
2931

30-
const DialogflowBotUsername: string = await getAppSettingValue(this.read, AppSetting.DialogflowBotUsername);
31-
if (DialogflowBotUsername !== username) { return this.context.getInteractionResponder().successResponse(); }
32+
if (!username || DialogflowBotUsername !== username) {
33+
return this.context.getInteractionResponder().successResponse();
34+
}
3235

33-
const visitor = await this.read.getLivechatReader().getLivechatVisitorByToken(token);
34-
if (!visitor) { return this.context.getInteractionResponder().successResponse(); }
3536
const appUser = await this.read.getUserReader().getAppUser(this.app.getID()) as IUser;
3637

3738
await createLivechatMessage(rid, this.read, this.modify, { text: value }, visitor);
3839

39-
await deleteAllActionBlocks(this.modify, appUser, id);
40+
const { value: hideQuickRepliesSetting } = await this.read.getEnvironmentReader().getSettings().getById(AppSetting.DialogflowHideQuickReplies);
41+
if (hideQuickRepliesSetting) {
42+
await deleteAllActionBlocks(this.modify, appUser, id);
43+
}
4044

4145
return this.context.getInteractionResponder().successResponse();
4246
} catch (error) {

i18n/en.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@
1212
"dialogflow_service_unavailable_message": "Service Unavailable Message",
1313
"dialogflow_service_unavailable_message_description": "The Bot will send this message to Visitor if service is unavailable",
1414
"dialogflow_close_chat_message": "Close Chat Message",
15-
"dialogflow_close_chat_message_description": "This message will be sent automatically when a chat is closed"
15+
"dialogflow_close_chat_message_description": "This message will be sent automatically when a chat is closed",
16+
"dialogflow_hide_quick_replies": "Hide Quick Replies",
17+
"dialogflow_hide_quick_replies_description": "If enabled, then all quick-replies will hide when a visitor clicks on any one of them"
1618
}

lib/Message.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IVisitor } from '@rocket.chat/apps-engine/definition/livechat';
33
import { BlockElementType, BlockType, IActionsBlock, IButtonElement, TextObjectType } from '@rocket.chat/apps-engine/definition/uikit';
44
import { IUser } from '@rocket.chat/apps-engine/definition/users';
55
import { AppSetting } from '../config/Settings';
6-
import { IDialogflowMessage, IDialogflowQuickReplies, IDialogflowQuickRepliesOptions } from '../enum/Dialogflow';
6+
import { IDialogflowMessage, IDialogflowQuickReplies, IDialogflowQuickReplyOptions } from '../enum/Dialogflow';
77
import { Logs } from '../enum/Logs';
88
import { getAppSettingValue } from './Settings';
99

@@ -14,21 +14,21 @@ export const createDialogflowMessage = async (rid: string, read: IRead, modify:
1414
const { text, options } = message as IDialogflowQuickReplies;
1515

1616
if (text && options) {
17-
18-
const elements: Array<IButtonElement> = options.map((payload: IDialogflowQuickRepliesOptions) => ({
17+
const elements: Array<IButtonElement> = options.map((payload: IDialogflowQuickReplyOptions) => ({
1918
type: BlockElementType.BUTTON,
2019
text: {
2120
type: TextObjectType.PLAINTEXT,
2221
text: payload.text,
2322
},
2423
value: payload.text,
25-
actionId: payload.actionId ? payload.actionId : payload.text,
24+
actionId: payload.actionId ? payload.actionId : String(Date.now()),
2625
...payload.buttonStyle && { style: payload.buttonStyle },
2726
} as IButtonElement));
2827

2928
const actionsBlock: IActionsBlock = { type: BlockType.ACTIONS, elements };
3029

31-
await createMessage(rid, read, modify, { text, actionsBlock });
30+
await createMessage(rid, read, modify, { text });
31+
await createMessage(rid, read, modify, { actionsBlock });
3232
} else {
3333
// message is instanceof string
3434
if ((message as string).trim().length > 0) {
@@ -98,7 +98,7 @@ export const createLivechatMessage = async (rid: string, read: IRead, modify: I
9898

9999
const room = await read.getRoomReader().getById(rid);
100100
if (!room) {
101-
this.app.getLogger().error(`${Logs.INVALID_ROOM_ID} ${rid}`);
101+
this.app.getLogger().error(`${ Logs.INVALID_ROOM_ID } ${ rid }`);
102102
return;
103103
}
104104

@@ -121,9 +121,8 @@ export const createLivechatMessage = async (rid: string, read: IRead, modify: I
121121
});
122122
};
123123

124-
export const deleteAllActionBlocks = async (modify: IModify, appUser: IUser, msgId: string) => {
124+
export const deleteAllActionBlocks = async (modify: IModify, appUser: IUser, msgId: string): Promise<void> => {
125125
const msgBuilder = await modify.getUpdater().message(msgId, appUser);
126-
msgBuilder.setEditor(appUser);
127-
msgBuilder.setBlocks(modify.getCreator().getBlockBuilder().getBlocks());
128-
await modify.getUpdater().finish(msgBuilder);
126+
msgBuilder.setEditor(appUser).setBlocks(modify.getCreator().getBlockBuilder().getBlocks());
127+
return modify.getUpdater().finish(msgBuilder);
129128
};

0 commit comments

Comments
 (0)