Skip to content

Commit d51eb48

Browse files
authored
Merge pull request #42 from PrajvalRaval/master
[NEW] Add support for UIKit components
2 parents 98a92df + 15672f7 commit d51eb48

14 files changed

Lines changed: 186 additions & 41 deletions

DialogflowApp.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,28 @@ import { ILivechatMessage } from '@rocket.chat/apps-engine/definition/livechat';
1414
import { IPostMessageSent } from '@rocket.chat/apps-engine/definition/messages';
1515
import { IAppInfo } from '@rocket.chat/apps-engine/definition/metadata';
1616
import { ISetting } from '@rocket.chat/apps-engine/definition/settings';
17+
import { IUIKitLivechatInteractionHandler, IUIKitResponse, UIKitLivechatBlockInteractionContext } from '@rocket.chat/apps-engine/definition/uikit';
1718
import { settings } from './config/Settings';
1819
import { FulfillmentsEndpoint } from './endpoints/FulfillmentsEndpoint';
1920
import { IncomingEndpoint } from './endpoints/IncomingEndpoint';
21+
import { ExecuteLivechatBlockActionHandler } from './handler/ExecuteLivechatBlockActionHandler';
2022
import { OnSettingUpdatedHandler } from './handler/OnSettingUpdatedHandler';
2123
import { PostMessageSentHandler } from './handler/PostMessageSentHandler';
2224

23-
export class DialogflowApp extends App implements IPostMessageSent {
25+
export class DialogflowApp extends App implements IPostMessageSent, IUIKitLivechatInteractionHandler {
2426
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
2527
super(info, logger, accessors);
2628
}
2729

30+
public async executeLivechatBlockActionHandler(context: UIKitLivechatBlockInteractionContext,
31+
read: IRead,
32+
http: IHttp,
33+
persistence: IPersistence,
34+
modify: IModify): Promise<IUIKitResponse> {
35+
const handler = new ExecuteLivechatBlockActionHandler(this, context, read, http, persistence, modify);
36+
return await handler.run();
37+
}
38+
2839
public async executePostMessageSent(message: ILivechatMessage,
2940
read: IRead,
3041
http: IHttp,

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "21b7d3ba-031b-41d9-8ff2-fbbfa081ae90",
3-
"version": "1.0.0",
4-
"requiredApiVersion": "^1.15.0",
3+
"version": "1.2.0",
4+
"requiredApiVersion": "^1.17.0",
55
"iconFile": "icon.png",
66
"author": {
77
"name": "Rocket.Chat",

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
];

docs/QuickReplies.md

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1-
### Quick Replies Structure
1+
# Quick Replies
2+
3+
## Required Structure
24
```
35
{
46
"quickReplies": {
57
"text": string,
68
"options": [
7-
string
9+
array of objects
810
]
911
}
1012
}
1113
```
1214

13-
### Field description
14-
15-
1. `text`
15+
## Params
1616

17-
- **Type**: string
18-
- **Description**: The title of the collection of quick replies
17+
| **Param Name** | **Param Type** | **Description** | **Dependency** | **Example** |
18+
|:--------------:|:--------------:|:---------------------------------------------:|:--------------:|:-----------------------------------------------------------------------------------------------------------------------------------------------------------:|
19+
| `text` | String | The title of the collection of quick replies. | Required | ``` "text": "Sorry I don't know the answer. Please select one of the following options:" ``` |
20+
| `options` | Array | The collection of quick replies objects. | Required | ``` "options": [ { "text": "Start chat with agent", "actionId": "sflaia-start-chat", "buttonStyle" : "primary" } ] ``` |
1921

20-
2. `options[]`
21-
- **Type**: Array of string
22-
- **Description**: The collection of quick replies.
22+
## `options` Params
2323

24+
| **Param Name** | **Param Type** | **Description** | **Dependency** | **Acceptable Values** | **Example** |
25+
|:--------------:|:--------------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------:|:--------------:|:---------------------:|:---------------------------------------:|
26+
| `text` | String | Title of the quick replies action. | Required | Any | ``` "text": "Start Chat" ``` |
27+
| `actionId` | String | Id of the quick replies action. | Optional | Any | ``` "actionId": "sflaia-start-chat" ``` |
28+
| `buttonStyle` | String | Button style of your quick replies action. Use `danger` to render a red colour action and `primary` for an action that matches your Livechat Bar colour. | Optional | `danger` or `primary` | ``` "buttonStyle": "primary" ``` |
2429
### Example
2530

26-
In this example, there are 2 simple text messages and one `quickReply` message
27-
28-
#### Dialogflow Dashboard
29-
30-
![image](./images/QuickRepliesDialogflowDashboard.png)
31-
32-
#### Corresponding Livechat widget
33-
34-
![image](./images/QuickRepliesLivechatWidget.png)
31+
![Quick Replies Example](https://user-images.githubusercontent.com/41849970/91997140-62939c00-ed57-11ea-8b27-82e650a502f0.png)
-30.1 KB
Binary file not shown.
-36 KB
Binary file not shown.

enum/Dialogflow.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ButtonStyle } from '@rocket.chat/apps-engine/definition/uikit';
2+
13
export interface IDialogflowMessage {
24
messages?: Array<string | IDialogflowQuickReplies>;
35
isFallback: boolean;
@@ -6,7 +8,13 @@ export interface IDialogflowMessage {
68

79
export interface IDialogflowQuickReplies {
810
text: string;
9-
options: Array<string>;
11+
options: Array<IDialogflowQuickReplyOptions>;
12+
}
13+
14+
export interface IDialogflowQuickReplyOptions {
15+
text: string;
16+
actionId?: string;
17+
buttonStyle?: ButtonStyle;
1018
}
1119

1220
export interface IDialogflowAccessToken {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { IHttp, IModify, IPersistence, IRead } from '@rocket.chat/apps-engine/definition/accessors';
2+
import { IApp } from '@rocket.chat/apps-engine/definition/IApp';
3+
import { ILivechatRoom } from '@rocket.chat/apps-engine/definition/livechat';
4+
import { IUIKitResponse, UIKitLivechatBlockInteractionContext } from '@rocket.chat/apps-engine/definition/uikit';
5+
import { UIKitIncomingInteractionContainerType } from '@rocket.chat/apps-engine/definition/uikit/UIKitIncomingInteractionContainer';
6+
import { IUser } from '@rocket.chat/apps-engine/definition/users';
7+
import { AppSetting } from '../config/Settings';
8+
import { createLivechatMessage, deleteAllActionBlocks } from '../lib/Message';
9+
import { getAppSettingValue } from '../lib/Settings';
10+
11+
export class ExecuteLivechatBlockActionHandler {
12+
constructor(private readonly app: IApp,
13+
private context: UIKitLivechatBlockInteractionContext,
14+
private read: IRead,
15+
private http: IHttp,
16+
private persistence: IPersistence,
17+
private modify: IModify) {}
18+
19+
public async run(): Promise<IUIKitResponse> {
20+
try {
21+
const interactionData = this.context.getInteractionData();
22+
23+
const { visitor, room, container: { id, type }, value } = interactionData;
24+
25+
if (type !== UIKitIncomingInteractionContainerType.MESSAGE) {
26+
return this.context.getInteractionResponder().successResponse();
27+
}
28+
29+
const DialogflowBotUsername: string = await getAppSettingValue(this.read, AppSetting.DialogflowBotUsername);
30+
const { servedBy: { username = null } = {}, id: rid } = room as ILivechatRoom;
31+
32+
if (!username || DialogflowBotUsername !== username) {
33+
return this.context.getInteractionResponder().successResponse();
34+
}
35+
36+
const appUser = await this.read.getUserReader().getAppUser(this.app.getID()) as IUser;
37+
38+
await createLivechatMessage(rid, this.read, this.modify, { text: value }, visitor);
39+
40+
const { value: hideQuickRepliesSetting } = await this.read.getEnvironmentReader().getSettings().getById(AppSetting.DialogflowHideQuickReplies);
41+
if (hideQuickRepliesSetting) {
42+
await deleteAllActionBlocks(this.modify, appUser, id);
43+
}
44+
45+
return this.context.getInteractionResponder().successResponse();
46+
} catch (error) {
47+
this.app.getLogger().error(error);
48+
return this.context.getInteractionResponder().errorResponse();
49+
}
50+
}
51+
}

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
}

0 commit comments

Comments
 (0)