diff --git a/src/commands/CommandUtility.ts b/src/commands/CommandUtility.ts index 3c07bdd..254b3dc 100644 --- a/src/commands/CommandUtility.ts +++ b/src/commands/CommandUtility.ts @@ -77,7 +77,13 @@ export class CommandUtility implements ICommandUtility { break; } default: { - await handler.sendDefault(); + const subCommand = this.params[0].toLowerCase(); + if(subCommand === CommandParam.CREATE){ + await this.handleQuickCreate(handler,this.params); + }else{ + await handler.sendDefault(); + } + break; } } } @@ -107,4 +113,23 @@ export class CommandUtility implements ICommandUtility { } } } + + private async handleQuickCreate(handler: Handler, args: string[]): Promise { + if (!args || args.length === 0) { + await handler.sendDefault(); + return; + } + + switch (args[0].toLowerCase()) { + case CommandParam.CREATE: { + await handler.CreateQuickReply(args); + break; + } + default: { + await handler.sendDefault(); + break; + } + } + } + } diff --git a/src/handlers/Handler.ts b/src/handlers/Handler.ts index 5f4c0ca..bda928e 100644 --- a/src/handlers/Handler.ts +++ b/src/handlers/Handler.ts @@ -22,6 +22,7 @@ import { Language } from '../lib/Translation/translation'; import { ReplyAIModal } from '../modal/AIreplyModal'; import { AIstorage } from '../storage/AIStorage'; import { UserPreferenceStorage } from '../storage/userPreferenceStorage'; +import { createQuickReply } from '../modal/quickCommands/createquick'; export class Handler implements IHandler { public app: QuickRepliesApp; @@ -206,4 +207,20 @@ export class Handler implements IHandler { return; } } + public async CreateQuickReply(args: string[]): Promise { + try { + await createQuickReply( + this.app, + this.sender, + this.room, + args, + this.read, + this.modify, + this.persis, + ); + } catch (error) { + this.app.getLogger().error(`Error in CreateQuickReply: ${error.message}`); + } + } + } diff --git a/src/modal/quickCommands/createquick.ts b/src/modal/quickCommands/createquick.ts new file mode 100644 index 0000000..e719125 --- /dev/null +++ b/src/modal/quickCommands/createquick.ts @@ -0,0 +1,49 @@ +import { + IModify, + IPersistence, + IRead, +} from '@rocket.chat/apps-engine/definition/accessors'; +import { IRoom } from '@rocket.chat/apps-engine/definition/rooms'; +import { IUser } from '@rocket.chat/apps-engine/definition/users'; +import { QuickRepliesApp } from '../../../QuickRepliesApp'; +import { ReplyStorage } from '../../storage/ReplyStorage'; +import { t } from '../../lib/Translation/translation'; +import { getUserPreferredLanguage } from '../../helper/userPreference'; +import { sendNotification } from '../../helper/notification'; +export async function createQuickReply( + app: QuickRepliesApp, + sender: IUser, + room: IRoom, + args: string[], + read: IRead, + modify: IModify, + persistence: IPersistence, +): Promise { + const language = await getUserPreferredLanguage( + read.getPersistenceReader(), + persistence, + sender.id, + ); + + const replyName = args[1]; + const replyBody = args.slice(2).join(' '); + + const replyStorage = new ReplyStorage(persistence, read.getPersistenceReader()); + + const result = await replyStorage.createReply(sender, replyName, replyBody, language); + + if (!result.success) { + const errorMessage = `${t('Fail_Create_Reply', language, { + name: sender.name, + })} \n\n ${result.error}`; + await sendNotification(read, modify, sender, room, { message: errorMessage }); + return; + } + + const successMessage = `${t('Success_Create_Reply', language, { + name: sender.name, + replyname: replyName, + })}`; + await sendNotification(read, modify, sender, room, { message: successMessage }); + +}