Browse Source

内联键盘交互

master
La123123 3 weeks ago
parent
commit
0b8c66ba2e
  1. 40
      src/main/java/org/telegram/updateshandlers/CommandsHandler.java

40
src/main/java/org/telegram/updateshandlers/CommandsHandler.java

@ -2,6 +2,7 @@ package org.telegram.updateshandlers;
import org.telegram.commands.HelloCommand; import org.telegram.commands.HelloCommand;
import org.telegram.commands.HelpCommand; import org.telegram.commands.HelpCommand;
import org.telegram.commands.KeyboardCommand;
import org.telegram.commands.StartCommand; import org.telegram.commands.StartCommand;
import org.telegram.commands.StopCommand; import org.telegram.commands.StopCommand;
import org.telegram.commands.SuccessCommand; import org.telegram.commands.SuccessCommand;
@ -12,8 +13,13 @@ import org.telegram.telegrambots.extensions.bots.commandbot.CommandLongPollingTe
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.message.Message; import org.telegram.telegrambots.meta.api.objects.message.Message;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.methods.send.SendPhoto;
import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import static org.telegram.commands.KeyboardCommand.IMAGE_URL;
/** /**
* This handler mainly works with commands to demonstrate the Commands feature of the API * This handler mainly works with commands to demonstrate the Commands feature of the API
* *
@ -26,6 +32,7 @@ public class CommandsHandler extends CommandLongPollingTelegramBot {
public CommandsHandler(String botToken, String botUsername) { public CommandsHandler(String botToken, String botUsername) {
super(new OkHttpTelegramClient(botToken), true, () -> botUsername); super(new OkHttpTelegramClient(botToken), true, () -> botUsername);
register(new HelloCommand()); register(new HelloCommand());
register(new KeyboardCommand());
register(new StartCommand()); register(new StartCommand());
register(new StopCommand()); register(new StopCommand());
register(new SuccessCommand()); register(new SuccessCommand());
@ -38,7 +45,6 @@ public class CommandsHandler extends CommandLongPollingTelegramBot {
try { try {
telegramClient.execute(commandUnknownMessage); telegramClient.execute(commandUnknownMessage);
} catch (TelegramApiException e) { } catch (TelegramApiException e) {
// log.error("Error sending message in commands bot", e);
System.out.println("Error sending message in commands bot" + e); System.out.println("Error sending message in commands bot" + e);
} }
helpCommand.execute(telegramClient, message.getFrom(), message.getChat(), new String[] {}); helpCommand.execute(telegramClient, message.getFrom(), message.getChat(), new String[] {});
@ -47,6 +53,12 @@ public class CommandsHandler extends CommandLongPollingTelegramBot {
@Override @Override
public void processNonCommandUpdate(Update update) { public void processNonCommandUpdate(Update update) {
// 处理回调查询
if (update.hasCallbackQuery()) {
onCallbackQueryReceived(update.getCallbackQuery());
return;
}
if (update.hasMessage()) { if (update.hasMessage()) {
Message message = update.getMessage(); Message message = update.getMessage();
@ -65,4 +77,30 @@ public class CommandsHandler extends CommandLongPollingTelegramBot {
} }
} }
} }
public void onCallbackQueryReceived(CallbackQuery callbackQuery) {
System.out.println("收到回调查询:" + callbackQuery.getData());
String data = callbackQuery.getData();
long chatId = callbackQuery.getMessage().getChatId();
if (data.equals("show_image")) {
// Send photo
SendPhoto sendPhoto = new SendPhoto(String.valueOf(chatId), new InputFile(IMAGE_URL));
sendPhoto.setCaption("这是展示的图片");
try {
telegramClient.execute(sendPhoto);
} catch (TelegramApiException e) {
System.out.println("Error sending photo: " + e);
}
} else if (data.startsWith("show_success:")) {
// Extract username and show success message
String userName = data.substring("show_success:".length());
SendMessage successMessage = new SendMessage(String.valueOf(chatId), userName + " success");
try {
telegramClient.execute(successMessage);
} catch (TelegramApiException e) {
System.out.println("Error sending success message: " + e);
}
}
}
} }
Loading…
Cancel
Save