|
| 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 | +} |
0 commit comments