Профиль пользователя заблокирован сроком с 30 сентября 2021 г. и навсегда по причине: Создание нескольких аккаунтов.
  • Как я могу добавить клавиатуры (InlineKeyboard и ReplyKeyboard) в telegram bot?

    @arkjjj Автор вопроса
    import java.io.*;
    import org.telegram.telegrambots.api.methods.updatingmessages.EditMessageText;
    import org.telegram.telegrambots.api.objects.Message;
    import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup;
    import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboardMarkup;
    import org.telegram.telegrambots.api.objects.replykeyboard.buttons.InlineKeyboardButton;
    import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardButton;
    import org.telegram.telegrambots.api.objects.replykeyboard.buttons.KeyboardRow;
    import wiki_work.SearchWiki;
    import com.vdurmont.emoji.EmojiParser;
    import org.alicebot.ab.*;
    import org.telegram.telegrambots.bots.TelegramLongPollingBot;
    import java.util.*;
    import org.telegram.telegrambots.api.methods.send.SendMessage;
    import org.telegram.telegrambots.api.objects.Update;
    import org.telegram.telegrambots.exceptions.TelegramApiException;
    
    import static java.lang.Math.toIntExact;
    
    class BotHandler extends TelegramLongPollingBot
    {
        private static final boolean TRACE_MODE = false;
        private final static int SIZE = 2000;
        private String smiley_emoji = EmojiParser.parseToUnicode(":smiley:");
        private String wink_emoji = EmojiParser.parseToUnicode(":wink:");
        private String share_number_emoji = EmojiParser.parseToUnicode(":phone: share your number");
        private String money_emoji = EmojiParser.parseToUnicode(":moneybag:");
        static String botName = "super";
        String response;
        String textLine;
        Bot bot;
        Chat chatSession;
        String resourcesPath;
        public BotHandler()
        {
            String resourcesPath = getResourcesPath();
            bot = new Bot("super", resourcesPath);
            chatSession = new Chat(bot);
            bot.brain.nodeStats();
            MagicBooleans.trace_mode = TRACE_MODE;
        }
    
    
    
        public void onUpdateReceived(Update update)
        {
            if (update.hasMessage() && update.getMessage().hasText()) {
    
                String textLine = update.getMessage().getText();
                long chat_id = update.getMessage().getChatId();
                if ((textLine == null) || (textLine.length() < 1))
                    textLine = MagicStrings.null_input;
                String request = textLine;
                if (MagicBooleans.trace_mode)
                    System.out.println("STATE=" + request + ":THAT=" + ((History) chatSession.thatHistory.get(0)).get(0) + ":TOPIC=" + chatSession.predicates.get("topic"));
                if (request.equals("/start")) {
                    if (update.getMessage().getText().equals("/start")) {
                        SendMessage message = new SendMessage() // Create a message object object
                                .setChatId(chat_id)
                                .setText("Hello!");
                        // Create ReplyKeyboardMarkup object
                        ReplyKeyboardMarkup keyboardMarkup = new ReplyKeyboardMarkup();
                        // Create the keyboard (list of keyboard rows)
                        List<KeyboardRow> keyboard = new ArrayList<>();
                        // Create a keyboard row
                        KeyboardRow row = new KeyboardRow();
                        // Set each button, you can also use KeyboardButton objects if you need something else than text
                        row.add("/FeedBack");
                        row.add("/PreOrder");
                        // Add the first row to the keyboard
                        keyboard.add(row);
    
                        keyboardMarkup.setKeyboard(keyboard);
                        // Add it to the message
                        message.setReplyMarkup(keyboardMarkup);
                        try {
                            sendMessage(message); // Sending our message object to user
                        } catch (TelegramApiException e) {
                            e.printStackTrace();
                        }
                    }
                }
    
                if (request.equals("/FeedBack")) {
    
                    if (update.getMessage().getText().equals("/FeedBack")) {
    
    
                        SendMessage message = new SendMessage() // Create a message object object
                                .setChatId(chat_id)
                                .setText("Thank you so much for leaving feedback on me. It means a lot to my creators and to me.");
                        InlineKeyboardMarkup markupInline = new InlineKeyboardMarkup();
                        List<List<InlineKeyboardButton>> rowsInline = new ArrayList<>();
                        List<InlineKeyboardButton> rowInline = new ArrayList<>();
                        rowInline.add(new InlineKeyboardButton().setUrl("https://mail.google.com").setCallbackData("Feed Back").setText("Feed Back"));
                        // Set the keyboard to the markup
                        rowsInline.add(rowInline);
                        // Add it to the message
                        markupInline.setKeyboard(rowsInline);
                        message.setReplyMarkup(markupInline);
                        try {
                            sendMessage(message); // Sending our message object to user
                        } catch (TelegramApiException e) {
                            e.printStackTrace();
                        }
                    } else {
    
                    }
    
                } else if (update.hasCallbackQuery()) {
                    // Set variables
                    String call_data = update.getCallbackQuery().getData();
                    long message_id = update.getCallbackQuery().getMessage().getMessageId();
                    long chat_id1 = update.getCallbackQuery().getMessage().getChatId();
    
                    if (call_data.equals("Feed Back")) {
                        String answer = "Updated message text";
                        EditMessageText new_message = new EditMessageText()
                                .setChatId(chat_id1)
                                .setMessageId(toIntExact(message_id))
                                .setText(answer);
                        try {
                            editMessageText(new_message);
                        } catch (TelegramApiException e) {
                            e.printStackTrace();
                        }
                    }
                }
    
                // The search on Wikipedia does not work,
                // you need a detailed approach to the case.
                //It is necessary that the program does a search
                //using the keyword "What" or "Who" or "When" or "Were".
                //Now it returns its answer to the previous question,
                //without answering the request to Wikipedia.
                else if(request.contains("Wiki search") ||request.contains("Wikipedia search"))
                {
                    String query = update.getMessage().getText();
                    SearchWiki searchWiki = new SearchWiki();
                    String messageText = searchWiki.run(query);
                    long chatId = update.getMessage().getChatId();
    
                    if(messageText.length() > SIZE)
                        messageText = Redactor.cut(messageText, SIZE);
    
                    SendMessage message = new SendMessage()
                            .setChatId(chatId)
                            .setText(messageText);
                }
                else if(request.equals("/cmds"))
                {
                    response="Try to talk to me, I give my word that you won't regret that you started :)";
                }else if(request.equals("/help"))
                {
                    response="What's wrong? ";
                }
                else
                {
                    response = chatSession.multisentenceRespond(request);
                    if(response.contains("<"))
                    {
                        response="Mmmmm"+ wink_emoji;
                    }
    
                }
                SendMessage message = new SendMessage().setChatId(chat_id).setText(response);
                try {
                    execute(message);
                }
                catch (TelegramApiException e)
                {
                    e.printStackTrace();
                }
            }
        }
    
    
        public String getBotUsername()
        {
            return "Bot_Bot-Bot";
        }
    
        @Override
        public String getBotToken()
        {
            return "56565595+5+55";
        }
    
        private static String getResourcesPath()
        {
            File currDir = new File(".");
            String path = currDir.getAbsolutePath();
            path = path.substring(0, path.length() - 2);
            System.out.println(path);
            String resourcesPath = path + File.separator + "src" + File.separator + "main" + File.separator + "resources";
            return resourcesPath;
        }
    }
  • Как я могу добавить клавиатуры (InlineKeyboard и ReplyKeyboard) в telegram bot?

    @arkjjj Автор вопроса
    Спасибо Богдан, если вам не сложно, можете на примере кода написать решение.
    Я просто, пытался что-то похожее сделать, и у меня бот кроме кнопки не на что больше не реагировал. Не знаю с чем это связано..