You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
3.0 KiB
77 lines
3.0 KiB
|
3 weeks ago
|
package org.telegram.commands;
|
||
|
|
|
||
|
|
import org.telegram.database.DatabaseManager;
|
||
|
|
import org.telegram.telegrambots.extensions.bots.commandbot.commands.BotCommand;
|
||
|
|
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
||
|
|
import org.telegram.telegrambots.meta.api.objects.Chat;
|
||
|
|
import org.telegram.telegrambots.meta.api.objects.User;
|
||
|
|
import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
|
||
|
|
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;
|
||
|
|
import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardRow;
|
||
|
|
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
||
|
|
import org.telegram.telegrambots.meta.generics.TelegramClient;
|
||
|
|
|
||
|
|
import java.util.ArrayList;
|
||
|
|
import java.util.List;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* This command shows a keyboard with two options:
|
||
|
|
* 1. Show an image
|
||
|
|
* 2. Trigger success message
|
||
|
|
*
|
||
|
|
* @author Your Name
|
||
|
|
*/
|
||
|
|
public class KeyboardCommand extends BotCommand {
|
||
|
|
|
||
|
|
public static final String LOGTAG = "KEYBOARDCOMMAND";
|
||
|
|
public static final String IMAGE_URL = "https://globecoin.s3.ap-southeast-1.amazonaws.com/echo2/uploads/2025-03-05/381300c82a6a4ba1b48800e292ca150c.jpg";
|
||
|
|
|
||
|
|
public KeyboardCommand() {
|
||
|
|
super("keyboard", "Show interactive keyboard with options");
|
||
|
|
}
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public void execute(TelegramClient telegramClient, User user, Chat chat, String[] arguments) {
|
||
|
|
|
||
|
|
if (!DatabaseManager.getInstance().getUserStateForCommandsBot(user.getId())) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
String userName = user.getFirstName() + " " + user.getLastName();
|
||
|
|
|
||
|
|
// Create inline keyboard buttons
|
||
|
|
InlineKeyboardButton showImageButton = InlineKeyboardButton.builder()
|
||
|
|
.text("🖼️ 展示图片")
|
||
|
|
.callbackData("show_image")
|
||
|
|
.build();
|
||
|
|
|
||
|
|
InlineKeyboardButton successButton = InlineKeyboardButton.builder()
|
||
|
|
.text("✅ 跳转成功")
|
||
|
|
.callbackData("show_success:" + userName)
|
||
|
|
.build();
|
||
|
|
|
||
|
|
// Create keyboard rows
|
||
|
|
// List<InlineKeyboardRow> rows = new ArrayList<>();
|
||
|
|
// rows.add(new InlineKeyboardRow(showImageButton));
|
||
|
|
// rows.add(new InlineKeyboardRow(successButton));
|
||
|
|
|
||
|
|
// Create keyboard rows - 两个按钮在同一行
|
||
|
|
List<InlineKeyboardRow> rows = new ArrayList<>();
|
||
|
|
InlineKeyboardRow row = new InlineKeyboardRow(showImageButton, successButton);
|
||
|
|
rows.add(row);
|
||
|
|
|
||
|
|
// Create the message with inline keyboard
|
||
|
|
SendMessage message = new SendMessage(chat.getId().toString(),
|
||
|
|
"请选择一个选项:\n\n1. 展示图片\n2. 查看成功信息");
|
||
|
|
InlineKeyboardMarkup keyboardMarkup = new InlineKeyboardMarkup(rows);
|
||
|
|
message.setReplyMarkup(keyboardMarkup);
|
||
|
|
|
||
|
|
try {
|
||
|
|
telegramClient.execute(message);
|
||
|
|
} catch (TelegramApiException e) {
|
||
|
|
// log.error("Error", e);
|
||
|
|
System.out.println(e);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|