From 5fd01119de06f0cb38358ffe1c9782ca3d141006 Mon Sep 17 00:00:00 2001 From: bivashy Date: Mon, 27 Nov 2023 16:50:21 +0600 Subject: [PATCH] Send message event answer to CallbackButtonEvent --- .../vk/command/DispatchCommandListener.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/core/src/main/java/me/mastercapexd/auth/vk/command/DispatchCommandListener.java b/core/src/main/java/me/mastercapexd/auth/vk/command/DispatchCommandListener.java index 5a66ebdf..4e2eb7b0 100644 --- a/core/src/main/java/me/mastercapexd/auth/vk/command/DispatchCommandListener.java +++ b/core/src/main/java/me/mastercapexd/auth/vk/command/DispatchCommandListener.java @@ -3,6 +3,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import com.bivashy.auth.api.AuthPlugin; import com.bivashy.auth.api.config.link.command.LinkCustomCommandSettings; import com.bivashy.auth.api.link.command.context.CustomCommandExecutionContext; import com.bivashy.lamp.commands.vk.VkCommandHandler; @@ -18,8 +19,11 @@ import com.bivashy.messenger.vk.message.keyboard.VkKeyboard; import com.google.gson.Gson; import com.ubivashka.vk.api.parsers.objects.CallbackButtonEvent; +import com.vk.api.sdk.exceptions.ApiException; +import com.vk.api.sdk.exceptions.ClientException; import com.vk.api.sdk.objects.messages.Keyboard; +import me.mastercapexd.auth.hooks.VkPluginHook; import me.mastercapexd.auth.link.vk.VKCommandActorWrapper; import me.mastercapexd.auth.link.vk.VKLinkType; import me.mastercapexd.auth.messenger.commands.custom.BaseCustomCommandExecutionContext; @@ -28,8 +32,10 @@ import revxrsal.commands.command.CommandActor; public abstract class DispatchCommandListener { + private static final ExecutorService EXECUTOR_SERVICE = Executors.newCachedThreadPool(); private static final VKLinkType LINK_TYPE = VKLinkType.getInstance(); + private static final VkPluginHook VK_HOOK = AuthPlugin.instance().getHook(VkPluginHook.class); private static final Gson GSON = new Gson(); private static final double CONVERSATION_PEER_ID_OFFSET = 2e9; @@ -46,6 +52,8 @@ protected void onMessage(com.vk.api.sdk.objects.messages.Message vkMessage, int protected void onButtonClick(CallbackButtonEvent buttonEvent) { EXECUTOR_SERVICE.execute(() -> VkHandler.getInstances().forEach((commandHandler) -> { + responseToButtonClick(buttonEvent); + CallbackButton callbackButton = GSON.fromJson(GSON.toJson(buttonEvent), CallbackButton.class); handleCommandDispatch(commandHandler, new ButtonDispatchSource(callbackButton)); CustomCommandExecutionContext executionContext = new BaseCustomCommandExecutionContext(buttonEvent.getPayload()); @@ -60,6 +68,17 @@ protected void onButtonClick(CallbackButtonEvent buttonEvent) { })); } + private void responseToButtonClick(CallbackButtonEvent event) { + try { + VK_HOOK.getClient() + .messages() + .sendMessageEventAnswer(VK_HOOK.getActor(), event.getEventID(), event.getUserID(), event.getPeerID()) + .execute(); + } catch (ApiException | ClientException e) { + throw new RuntimeException(e); + } + } + private void handleCommandDispatch(VkCommandHandler handler, DispatchSource source) { if (LINK_TYPE.getSettings().shouldDisableConversationCommands() && isConversationPeerId(source.getPeerId())) return; @@ -80,4 +99,5 @@ private Message createMessage(LinkCustomCommandSettings customCommand) { private boolean isConversationPeerId(int peerId) { return peerId > CONVERSATION_PEER_ID_OFFSET; } + }