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
6 changes: 6 additions & 0 deletions enum/Dialogflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ export interface IDialogflowMessage {
export interface IDialogflowQuickReplies {
text: string;
options: Array<IDialogflowQuickReplyOptions>;
customFields?: IDialogflowCustomFields;
}

export interface IDialogflowCustomFields {
disableInput?: boolean;
disableInputMessage?: string;
}

export interface IDialogflowQuickReplyOptions {
Expand Down
17 changes: 15 additions & 2 deletions lib/Dialogflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IHttp, IHttpRequest, IModify, IPersistence, IRead } from '@rocket.chat/
import { IRoom } from '@rocket.chat/apps-engine/definition/rooms';
import { createSign } from 'crypto';
import { AppSetting } from '../config/Settings';
import { DialogflowJWT, DialogflowRequestType, DialogflowUrl, IDialogflowAccessToken, IDialogflowEvent, IDialogflowMessage, IDialogflowQuickReplies, LanguageCode } from '../enum/Dialogflow';
import { DialogflowJWT, DialogflowRequestType, DialogflowUrl, IDialogflowAccessToken, IDialogflowEvent, IDialogflowMessage, IDialogflowQuickReplies, IDialogflowCustomFields, LanguageCode } from '../enum/Dialogflow';
import { Headers } from '../enum/Http';
import { Logs } from '../enum/Logs';
import { base64urlEncode } from './Helper';
Expand Down Expand Up @@ -89,9 +89,11 @@ class DialogflowClass {
};

const messages: Array<string | IDialogflowQuickReplies> = [];
// customFields should be sent as the response of last message on client side
let msgCustomFields: IDialogflowCustomFields = {};

fulfillmentMessages.forEach((message) => {
const { text, payload: { quickReplies = null } = {} } = message;
const { text, payload: { quickReplies = null, customFields = null } = {} } = message;
if (text) {
const { text: textMessageArray } = text;
messages.push(textMessageArray[0]);
Expand All @@ -102,8 +104,19 @@ class DialogflowClass {
messages.push(quickReplies);
}
}
if (customFields) {
msgCustomFields.disableInput = !!customFields.disableInput;
msgCustomFields.disableInputMessage = customFields.disableInputMessage;
}
});


if (messages.length > 0) {
if (Object.keys(msgCustomFields).length > 0) {
let lastObj = messages[messages.length - 1];
lastObj = Object.assign(lastObj, { customFields: msgCustomFields });
messages[messages.length - 1] = lastObj;
}
parsedMessage.messages = messages;
}

Expand Down
16 changes: 10 additions & 6 deletions lib/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const createDialogflowMessage = async (rid: string, read: IRead, modify:
const { messages = [] } = dialogflowMessage;

for (const message of messages) {
const { text, options } = message as IDialogflowQuickReplies;
const { text, options, customFields = null } = message as IDialogflowQuickReplies;
if (text && options) {
const elements: Array<IButtonElement> = options.map((payload: IDialogflowQuickReplyOptions) => ({
type: BlockElementType.BUTTON,
Expand All @@ -27,12 +27,11 @@ export const createDialogflowMessage = async (rid: string, read: IRead, modify:

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

await createMessage(rid, read, modify, { text });
await createMessage(rid, read, modify, { actionsBlock });
await createMessage(rid, read, modify, { text, actionsBlock, customFields });
} else {
// message is instanceof string
if ((message as string).trim().length > 0) {
await createMessage(rid, read, modify, { text: message });
await createMessage(rid, read, modify, { text: message, customFields });
}
}
}
Expand Down Expand Up @@ -61,9 +60,14 @@ export const createMessage = async (rid: string, read: IRead, modify: IModify,
return;
}

const msg = modify.getCreator().startMessage().setRoom(room).setSender(sender);
const { text, actionsBlock, attachment, customFields } = message;
let data = { room, sender };

const { text, actionsBlock, attachment } = message;
if (customFields) {
data = Object.assign(data, { customFields });
}

const msg = modify.getCreator().startMessage(data);

if (text) {
msg.setText(text);
Expand Down