Skip to content

Commit

Permalink
Send message event answer to CallbackButtonEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
bivashy committed Nov 27, 2023
1 parent d565fae commit 5fd0111
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -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());
Expand All @@ -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;
Expand All @@ -80,4 +99,5 @@ private Message createMessage(LinkCustomCommandSettings customCommand) {
private boolean isConversationPeerId(int peerId) {
return peerId > CONVERSATION_PEER_ID_OFFSET;
}

}

0 comments on commit 5fd0111

Please sign in to comment.