diff --git a/app.json b/app.json index ac25ae3..5e1fa09 100644 --- a/app.json +++ b/app.json @@ -1,54 +1,54 @@ { - "id": "e664d2cb-7beb-413a-837a-80fd840c387b", - "version": "0.0.1", - "requiredApiVersion": "^1.44.0", - "iconFile": "icon.png", - "author": { - "name": "Vipin Chaudhary", - "homepage": "https://github.com/RocketChat/Apps.QuickReplies", - "support": "https://github.com/RocketChat/Apps.QuickReplies/issues" - }, - "name": "QuickReplies", - "nameSlug": "quickreplies", - "classFile": "QuickRepliesApp.ts", - "description": "Instantly craft and send customizable responses within Rocket.Chat.", - "implements": [], - "permissions": [ - { - "name": "ui.registerButtons" - }, - { - "name": "api" - }, - { - "name": "slashcommand" - }, - { - "name": "server-setting.read" - }, - { - "name": "room.read" - }, - { - "name": "persistence" - }, - { - "name": "ui.interact" - }, - { - "name": "networking" - }, - { - "name": "message.write" - }, - { - "name": "user.read" - }, - { - "name": "room.write" - }, - { - "name": "message.read" - } - ] + "id": "e664d2cb-7beb-413a-837a-80fd840c387b", + "version": "0.0.1", + "requiredApiVersion": "^1.44.0", + "iconFile": "icon.png", + "author": { + "name": "Vipin Chaudhary", + "homepage": "https://github.com/RocketChat/Apps.QuickReplies", + "support": "https://github.com/RocketChat/Apps.QuickReplies/issues" + }, + "name": "QuickReplies", + "nameSlug": "quickreplies", + "classFile": "QuickRepliesApp.ts", + "description": "Instantly craft and send customizable responses within Rocket.Chat.", + "implements": [], + "permissions": [ + { + "name": "ui.registerButtons" + }, + { + "name": "api" + }, + { + "name": "slashcommand" + }, + { + "name": "server-setting.read" + }, + { + "name": "room.read" + }, + { + "name": "persistence" + }, + { + "name": "ui.interact" + }, + { + "name": "networking" + }, + { + "name": "message.write" + }, + { + "name": "user.read" + }, + { + "name": "room.write" + }, + { + "name": "message.read" + } + ] } \ No newline at end of file diff --git a/src/enum/modals/listContextualBar.ts b/src/enum/modals/listContextualBar.ts index ded7256..13400bd 100644 --- a/src/enum/modals/listContextualBar.ts +++ b/src/enum/modals/listContextualBar.ts @@ -10,4 +10,6 @@ export enum ListContextualBarEnum { CLOESE_BUTTON_TEXT = 'Close', SEARCH_BLOCK_ID = 'list-reply-search-block-id', SEARCH_ACTION_ID = 'list-reply-search-action-id', + SEND_BLOCK_ID = 'send_reply_block', + SEND_ACTION_ID = 'send_reply_action', } diff --git a/src/handlers/ExecuteBlockActionHandler.ts b/src/handlers/ExecuteBlockActionHandler.ts index 5912997..bb21028 100644 --- a/src/handlers/ExecuteBlockActionHandler.ts +++ b/src/handlers/ExecuteBlockActionHandler.ts @@ -110,148 +110,142 @@ export class ExecuteBlockActionHandler { const command = value.split(' : ')[0].trim(); const replyId = value.split(' : ')[1].trim(); - const replyStorage = new ReplyStorage( - this.persistence, - persistenceRead, - ); + const replyStorage = new ReplyStorage( + this.persistence, + persistenceRead, + ); - const reply = await replyStorage.getReplyById( - user, - replyId, - ); + const reply = await replyStorage.getReplyById( + user, + replyId, + ); - if (!reply) { - return this.context - .getInteractionResponder() - .errorResponse(); - } - const language = await getUserPreferredLanguage( - this.read.getPersistenceReader(), - this.persistence, - user.id, - ); - if (room) { - switch (command) { - case ListContextualBarEnum.SEND: - const sendModal = await SendReplyModal( - this.app, - user, - this.read, - this.persistence, - this.modify, - room, - reply, - language, - ); + if (!reply) { + return this.context + .getInteractionResponder() + .errorResponse(); + } + const language = await getUserPreferredLanguage( + this.read.getPersistenceReader(), + this.persistence, + user.id, + ); + if (room) { + switch (command) { + case ListContextualBarEnum.SEND: + const sendModal = await SendReplyModal( + this.app, + user, + this.read, + this.persistence, + this.modify, + room, + reply, + language, + ); + return this.context + .getInteractionResponder() + .openModalViewResponse(sendModal); + case ListContextualBarEnum.EDIT: + const editModal = await EditReplyModal( + this.app, + user, + this.read, + this.persistence, + this.modify, + room, + reply, + language, + ); + return this.context + .getInteractionResponder() + .openModalViewResponse(editModal); + case ListContextualBarEnum.DELETE: + const confirmModal = await confirmDeleteModal( + this.app, + user, + this.read, + this.persistence, + this.modify, + room, + reply, + language, + ); + return this.context + .getInteractionResponder() + .openModalViewResponse(confirmModal); + default: + break; + } + } + } + break; + } + case MessageActionButton.CREATE_REPLY_ACTION_ID: { + await handler.CreateReply(); + break; + } + case MessageActionButton.LIST_REPLY_ACTION_ID: { + await handler.ListReply(); + break; + } + case MessageActionButton.CONFIGURE_PREFERENCES_ACTION_ID: + await handler.Configure(); + break; + case MessageActionButton.NEED_MORE_ACTION_ID: + await handler.Help(); + break; + case ListContextualBarEnum.SEARCH_ACTION_ID: + if (value) { + const UpdatedListBar = await listReplyContextualBar( + this.app, + user, + this.read, + this.persistence, + this.modify, + room, + userReplies, + language, + value, + ); + return this.context + .getInteractionResponder() + .updateContextualBarViewResponse(UpdatedListBar); + } else { + const UpdatedListBar = await listReplyContextualBar( + this.app, + user, + this.read, + this.persistence, + this.modify, + room, + userReplies, + language, + ); + return this.context + .getInteractionResponder() + .updateContextualBarViewResponse(UpdatedListBar); + } + case ReplyAIModalEnum.PROMPT_INPUT_ACTION_ID: + const aistorage = new AIstorage( + this.persistence, + this.read.getPersistenceReader(), + user.id, + ); + if (value) { + await aistorage.updatePrompt(value); + } + break; + case ReplyAIModalEnum.GENERATE_BUTTON_ACTION_ID: + const aiStorage = new AIstorage( + this.persistence, + this.read.getPersistenceReader(), + user.id, + ); + const message = await aiStorage.getMessage(); + const prompt = await aiStorage.getPrompt(); - return this.context - .getInteractionResponder() - .openModalViewResponse(sendModal); - - break; - case ListContextualBarEnum.EDIT: - const editModal = await EditReplyModal( - this.app, - user, - this.read, - this.persistence, - this.modify, - room, - reply, - language, - ); - return this.context - .getInteractionResponder() - .openModalViewResponse(editModal); - - break; - case ListContextualBarEnum.DELETE: - const confirmModal = await confirmDeleteModal( - this.app, - user, - this.read, - this.persistence, - this.modify, - room, - reply, - language, - ); - return this.context - .getInteractionResponder() - .openModalViewResponse(confirmModal); - - default: - } - } - } - break; - } - case MessageActionButton.CREATE_REPLY_ACTION_ID: { - await handler.CreateReply(); - break; - } - case MessageActionButton.LIST_REPLY_ACTION_ID: { - await handler.ListReply(); - break; - } - case MessageActionButton.CONFIGURE_PREFERENCES_ACTION_ID: - await handler.Configure(); - break; - case MessageActionButton.NEED_MORE_ACTION_ID: - await handler.Help(); - break; - case ListContextualBarEnum.SEARCH_ACTION_ID: - if (value) { - const UpdatedListBar = await listReplyContextualBar( - this.app, - user, - this.read, - this.persistence, - this.modify, - room, - userReplies, - language, - value, - ); - return this.context - .getInteractionResponder() - .updateContextualBarViewResponse(UpdatedListBar); - } else { - const UpdatedListBar = await listReplyContextualBar( - this.app, - user, - this.read, - this.persistence, - this.modify, - room, - userReplies, - language, - ); - return this.context - .getInteractionResponder() - .updateContextualBarViewResponse(UpdatedListBar); - } - case ReplyAIModalEnum.PROMPT_INPUT_ACTION_ID: - const aistorage = new AIstorage( - this.persistence, - this.read.getPersistenceReader(), - user.id, - ); - if (value) { - await aistorage.updatePrompt(value); - } - break; - - case ReplyAIModalEnum.GENERATE_BUTTON_ACTION_ID: - const aiStorage = new AIstorage( - this.persistence, - this.read.getPersistenceReader(), - user.id, - ); - const message = await aiStorage.getMessage(); - const prompt = await aiStorage.getPrompt(); - - const Preference = await userPreference.getUserPreference(); + const Preference = await userPreference.getUserPreference(); const response = await new AIHandler( this.app, @@ -259,91 +253,117 @@ export class ExecuteBlockActionHandler { Preference, ).handleResponse(user, message, prompt); - await aiStorage.updateResponse(response); + await aiStorage.updateResponse(response); - const updatedModal = await ReplyAIModal( - this.app, - user, - this.read, - this.persistence, - this.modify, - room, - language, - message, - response, - ); + const updatedModal = await ReplyAIModal( + this.app, + user, + this.read, + this.persistence, + this.modify, + room, + language, + message, + response, + ); - return this.context - .getInteractionResponder() - .updateModalViewResponse(updatedModal); - case UserPreferenceModalEnum.AI_PREFERENCE_DROPDOWN_ACTION_ID: - if (value === AIusagePreferenceEnum.Personal) { - existingPreference.AIusagePreference = - AIusagePreferenceEnum.Personal; - await userPreference.storeUserPreference( - existingPreference, - ); - const updatedPreference = - await userPreference.getUserPreference(); + return this.context + .getInteractionResponder() + .updateModalViewResponse(updatedModal); + case UserPreferenceModalEnum.AI_PREFERENCE_DROPDOWN_ACTION_ID: + if (value === AIusagePreferenceEnum.Personal) { + existingPreference.AIusagePreference = + AIusagePreferenceEnum.Personal; + await userPreference.storeUserPreference( + existingPreference, + ); + const updatedPreference = + await userPreference.getUserPreference(); - const updatedModal = await UserPreferenceModal({ - app: this.app, - modify: this.modify, - existingPreference: updatedPreference, - }); + const updatedModal = await UserPreferenceModal({ + app: this.app, + modify: this.modify, + existingPreference: updatedPreference, + }); - return this.context - .getInteractionResponder() - .updateModalViewResponse(updatedModal); - } else { - existingPreference.AIusagePreference = - AIusagePreferenceEnum.Workspace; - await userPreference.storeUserPreference( - existingPreference, - ); - const updatedPreference = - await userPreference.getUserPreference(); + return this.context + .getInteractionResponder() + .updateModalViewResponse(updatedModal); + } else { + existingPreference.AIusagePreference = + AIusagePreferenceEnum.Workspace; + await userPreference.storeUserPreference( + existingPreference, + ); + const updatedPreference = + await userPreference.getUserPreference(); - const updatedModal = await UserPreferenceModal({ - app: this.app, - modify: this.modify, - existingPreference: updatedPreference, - }); + const updatedModal = await UserPreferenceModal({ + app: this.app, + modify: this.modify, + existingPreference: updatedPreference, + }); - return this.context - .getInteractionResponder() - .updateModalViewResponse(updatedModal); - } - break; - case UserPreferenceModalEnum.AI_OPTION_DROPDOWN_ACTION_ID: - const option = value as AIProviderEnum; - if (value) { - if (Object.values(AIProviderEnum).includes(option)) { - existingPreference.AIconfiguration.AIProvider = option; - await userPreference.storeUserPreference( - existingPreference, - ); - const updatedPreference = - await userPreference.getUserPreference(); + return this.context + .getInteractionResponder() + .updateModalViewResponse(updatedModal); + } + break; + case UserPreferenceModalEnum.AI_OPTION_DROPDOWN_ACTION_ID: + const option = value as AIProviderEnum; + if (value) { + if (Object.values(AIProviderEnum).includes(option)) { + existingPreference.AIconfiguration.AIProvider = option; + await userPreference.storeUserPreference( + existingPreference, + ); + const updatedPreference = + await userPreference.getUserPreference(); - const updatedModal = await UserPreferenceModal({ - app: this.app, - modify: this.modify, - existingPreference: updatedPreference, - }); + const updatedModal = await UserPreferenceModal({ + app: this.app, + modify: this.modify, + existingPreference: updatedPreference, + }); - return this.context - .getInteractionResponder() - .updateModalViewResponse(updatedModal); - } else { - console.log('value is not part of AIProviderEnum enum'); - } - } else { - console.log('no value'); - } - break; - } + return this.context + .getInteractionResponder() + .updateModalViewResponse(updatedModal); + } else { + console.log('value is not part of AIProviderEnum enum'); + } + } else { + console.log('no value'); + } + break; + } + + + if (actionId.startsWith(ListContextualBarEnum.SEND_ACTION_ID)) { + const replyId = actionId.split('_').pop(); + if (!replyId) { + this.app.getLogger().error(`Invalid actionId format: ${actionId}`); + return this.context.getInteractionResponder().errorResponse(); + } + + const reply = await replyStorage.getReplyById(user, replyId); + if (!reply) { + return this.context.getInteractionResponder().errorResponse(); + } + + const sendModal = await SendReplyModal( + this.app, + user, + this.read, + this.persistence, + this.modify, + room, + reply, + language, + ); + return this.context.getInteractionResponder().openModalViewResponse(sendModal); + } return this.context.getInteractionResponder().successResponse(); } -} +} \ No newline at end of file diff --git a/src/modal/listContextualBar.ts b/src/modal/listContextualBar.ts index 59ab473..7e9a701 100644 --- a/src/modal/listContextualBar.ts +++ b/src/modal/listContextualBar.ts @@ -94,14 +94,6 @@ export async function listReplyContextualBar( const accessoryElement = elementBuilder.createOverflow( { options: [ - { - text: { - type: 'plain_text', - text: t('Send_Text', language), - emoji: true, - }, - value: `${ListContextualBarEnum.SEND} : ${reply.id}`, - }, { text: { type: 'plain_text', @@ -126,6 +118,18 @@ export async function listReplyContextualBar( }, ); + const sendButton = elementBuilder.addButton( + { + text: t('Send_Text', language), + style: ButtonStyle.PRIMARY, + value: `${ListContextualBarEnum.SEND} : ${reply.id}`, + }, + { + blockId: `${ListContextualBarEnum.SEND_BLOCK_ID}_${reply.id}`, + actionId: `${ListContextualBarEnum.SEND_ACTION_ID}_${reply.id}`, + } + ); + const name = reply.name.slice(0, 40); const body = reply.body.slice(0, 60); const replySection = blockBuilder.createSectionBlock({ @@ -133,8 +137,9 @@ export async function listReplyContextualBar( accessory: accessoryElement, }); - const replyBody = blockBuilder.createContextBlock({ - contextElements: [body], + const replyBody = blockBuilder.createSectionBlock({ + text: body, + accessory: sendButton, }); blocks.push(replySection, replyBody, divider);