Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send message event answer to CallbackButtonEvent #124

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

}
Loading