-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMessage.ts
More file actions
161 lines (136 loc) · 5.74 KB
/
Message.ts
File metadata and controls
161 lines (136 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { IModify, IRead } from '@rocket.chat/apps-engine/definition/accessors';
import { IVisitor } from '@rocket.chat/apps-engine/definition/livechat';
import { BlockElementType, BlockType, IActionsBlock, IButtonElement, TextObjectType } from '@rocket.chat/apps-engine/definition/uikit';
import { IUploadDescriptor } from '@rocket.chat/apps-engine/definition/uploads/IUploadDescriptor';
import { IUser } from '@rocket.chat/apps-engine/definition/users';
import { Buffer } from 'buffer';
import { AppSetting } from '../config/Settings';
import { DialogflowJWT, IDialogflowMessage, IDialogflowQuickReplies, IDialogflowQuickReplyOptions } from '../enum/Dialogflow';
import { Logs } from '../enum/Logs';
import { getAppSettingValue } from './Settings';
export const createDialogflowMessage = async (rid: string, read: IRead, modify: IModify, dialogflowMessage: IDialogflowMessage): Promise<any> => {
const { messages = [], audio } = dialogflowMessage;
const room = await read.getRoomReader().getById(rid);
if (!room) {
throw new Error(`${Logs.INVALID_ROOM_ID} ${rid}`);
}
const botUserName = await getAppSettingValue(read, AppSetting.DialogflowBotUsername);
if (!botUserName) {
throw new Error(Logs.EMPTY_BOT_USERNAME_SETTING);
}
const sender = await read.getUserReader().getByUsername(botUserName);
if (!sender) {
throw new Error(Logs.INVALID_BOT_USERNAME_SETTING);
}
if (audio) {
const buffer = Buffer.from(audio, DialogflowJWT.BASE_64);
const uploadDescriptor: IUploadDescriptor = {
filename: 'audio.wav',
room,
user: sender,
};
await modify.getCreator().getUploadCreator().uploadBuffer(buffer, uploadDescriptor);
}
for (const message of messages) {
const { text, options } = message as IDialogflowQuickReplies;
if (text && options) {
const elements: Array<IButtonElement> = options.map((payload: IDialogflowQuickReplyOptions) => ({
type: BlockElementType.BUTTON,
text: {
type: TextObjectType.PLAINTEXT,
text: payload.text,
},
value: payload.text,
actionId: payload.actionId || uuid(),
...payload.buttonStyle && { style: payload.buttonStyle },
} as IButtonElement));
const actionsBlock: IActionsBlock = { type: BlockType.ACTIONS, elements };
await createMessage(rid, read, modify, { text });
await createMessage(rid, read, modify, { actionsBlock });
} else {
// message is instanceof string
if ((message as string).trim().length > 0) {
await createMessage(rid, read, modify, { text: message });
}
}
}
};
export const createMessage = async (rid: string, read: IRead, modify: IModify, message: any ): Promise<any> => {
if (!message) {
return;
}
const botUserName = await getAppSettingValue(read, AppSetting.DialogflowBotUsername);
if (!botUserName) {
this.app.getLogger().error(Logs.EMPTY_BOT_USERNAME_SETTING);
return;
}
const sender = await read.getUserReader().getByUsername(botUserName);
if (!sender) {
this.app.getLogger().error(Logs.INVALID_BOT_USERNAME_SETTING);
return;
}
const room = await read.getRoomReader().getById(rid);
if (!room) {
this.app.getLogger().error(`${Logs.INVALID_ROOM_ID} ${rid}`);
return;
}
const msg = modify.getCreator().startMessage().setRoom(room).setSender(sender);
const { text, actionsBlock, attachment } = message;
if (text) {
msg.setText(text);
}
if (attachment) {
msg.addAttachment(attachment);
}
if (actionsBlock) {
const { elements } = actionsBlock as IActionsBlock;
msg.addBlocks(modify.getCreator().getBlockBuilder().addActionsBlock({ elements }));
}
return new Promise(async (resolve) => {
modify.getCreator().finish(msg)
.then((result) => resolve(result))
.catch((error) => console.error(error));
});
};
export const createLivechatMessage = async (rid: string, read: IRead, modify: IModify, message: any, visitor: IVisitor ): Promise<any> => {
if (!message) {
return;
}
const botUserName = await getAppSettingValue(read, AppSetting.DialogflowBotUsername);
if (!botUserName) {
this.app.getLogger().error(Logs.EMPTY_BOT_USERNAME_SETTING);
return;
}
const room = await read.getRoomReader().getById(rid);
if (!room) {
this.app.getLogger().error(`${ Logs.INVALID_ROOM_ID } ${ rid }`);
return;
}
const msg = modify.getCreator().startLivechatMessage().setRoom(room).setVisitor(visitor);
const { text, attachment } = message;
if (text) {
msg.setText(text);
}
if (attachment) {
msg.addAttachment(attachment);
}
return new Promise(async (resolve) => {
modify.getCreator().finish(msg)
.then((result) => resolve(result))
.catch((error) => console.error(error));
});
};
export const deleteAllActionBlocks = async (modify: IModify, appUser: IUser, msgId: string): Promise<void> => {
const msgBuilder = await modify.getUpdater().message(msgId, appUser);
msgBuilder.setEditor(appUser).setBlocks(modify.getCreator().getBlockBuilder().getBlocks());
return modify.getUpdater().finish(msgBuilder);
};
export const uuid = (): string => {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
// tslint:disable-next-line: no-bitwise
const r = Math.random() * 16 | 0;
// tslint:disable-next-line: no-bitwise
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
};