Creating clickable text in a Telegram bot using Java

Hey everyone! I’m new to making Telegram bots but I know Java pretty well. I’m trying to figure out how to make text clickable in my bot’s messages, like the ones in those cool mafia game bots.

I’ve got a basic bot set up that can send messages, but I’m not sure how to make parts of the text into clickable links. Here’s a simplified version of what I’ve got so far:

public class PizzaBot extends TelegramLongPollingBot {
    public void onUpdateReceived(Update update) {
        Message msg = update.getMessage();
        if ('/menu'.equals(msg.getText())) {
            SendMessage reply = new SendMessage();
            reply.setChatId(msg.getChatId().toString());
            reply.setText('Our special today: Mushroom Pizza');
            try {
                execute(reply);
            } catch (TelegramApiException e) {
                e.printStackTrace();
            }
        }
    }

    // Bot username and token methods here
}

How can I modify this to make certain words in the message clickable? Do I need to use some kind of special formatting or a different method to send messages? Any help would be awesome!

I ran into a similar issue when I was learning how to build Telegram bots. Instead of trying to embed clickable text directly into your message, you must use an inline keyboard. In my project, I created an InlineKeyboardMarkup and added an InlineKeyboardButton with text like ‘Order Now’ and assigned it callback data. After that, I attached the markup to the SendMessage object so the button appears under the text. This approach lets users interact with your message without cluttering the main content. I hope this helps.

To create clickable elements in your Telegram bot, you’ll need to utilize the InlineKeyboardMarkup class. This allows you to add interactive buttons beneath your message.

Here’s how you can modify your existing code:

  1. Import necessary classes:
    import org.telegram.telegrambots.meta.api.objects.replykeyboard.InlineKeyboardMarkup;
    import org.telegram.telegrambots.meta.api.objects.replykeyboard.buttons.InlineKeyboardButton;

  2. Create an InlineKeyboardMarkup and add buttons:
    InlineKeyboardMarkup markupInline = new InlineKeyboardMarkup();
    List<List> rowsInline = new ArrayList<>();
    List rowInline = new ArrayList<>();
    rowInline.add(new InlineKeyboardButton().setText(‘Order Now’).setCallbackData(‘order_mushroom’));
    rowsInline.add(rowInline);
    markupInline.setKeyboard(rowsInline);

  3. Attach the markup to your SendMessage:
    reply.setReplyMarkup(markupInline);

This approach creates a button below your message text, which users can click to interact with your bot.

yo Alice, ive been there! try using InlineKeyboardMarkup and InlineKeyboardButton classes. add buttons to ur message with setText and setCallbackData methods. then attach the markup to ur SendMessage. its not exactly clickable text, but it’ll give u those sweet interactive buttons below ur message. good luck!