W
W
Werner Heisenberg2021-05-25 14:25:50
Java
Werner Heisenberg, 2021-05-25 14:25:50

How can I add keyboards (InlineKeyboard and ReplyKeyboard) to telegram bot?

I'm trying to add two kinds of keyboards to a telegram bot so that when I click on ReplyKeyboard , InlineKeyboard is called . And at the same time, the dialog state of the bot did not go astray. When I somehow added InlineKeyboard, the bot responded only to the button, and ignored the rest of the requests.
How can I integrate buttons so that everything doesn't break?
The picture is as it should be.
60acdc88347b8250572745.png
Here is the source in which I came to a dead end (

import java.io.*;
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;
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"))
            {
                Random randseed=new Random();
                int briseed=randseed.nextInt(2);
                String[] welcomemsg=new String[2];
                welcomemsg[0]="Hello!";
                welcomemsg[1]="Hi!";
                response = welcomemsg[briseed];
            }
            else if(request.contains("Wiki") ||request.contains("Wikipedia"))
            {
                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("/joke"))
            {
                response="Try to don't talk :)";
            }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;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mimocodil, 2021-05-25
@arkjjj

When the user clicks on the button (reply keyboard), it sends a message to the chat, the text of which is indicated on the button. Currently, these buttons do not have other functions.
Next, you just need to do a check like you checked the /start, /joke, etc. commands. for the phrase "Feed Back", in which make the bot send the message. In the sent message, set the button with the link, as in the illustration, and that's it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question