4

I just got acquainted with the keyboard buttons for telegram bots using the .KeyboardButton from the Telegram API documentation but I have an issue; so far I've only been able to design the buttons such that the output after clicking on the button is the same as it's caption/placeholder which is really not helpful for what I want to do.

I have commands set and all already so for example I want the output of a button labeled "Rules" to be /rules in order to initiate the command action instead of the bot output being "Rules". I'm working with Python although I'm open to anyone working on the same stuff in other languages.

halfer
  • 19,824
  • 17
  • 99
  • 186
Emmanuel Batse
  • 153
  • 1
  • 6

2 Answers2

8

Clicking on a KeyboardButton will always result in sending a message containing the very same caption it holds. That's just how KeyboardButton in telegram works. So you would have to label your button "/rules".

As an alternative you can use InlineKeyboardButton instead. You can chose any label text you wish and provide additional callback_data. Clicking inlineKeyboardButton will not print any message but instead a callback query will be sent to your bot's script. This query can then be answered any way you wish.

newsha
  • 1,288
  • 1
  • 10
  • 13
  • Hey can you give me a sample code line of InlineKeyboardButton in use? I tried that at first but I think I didn’t get it right so I’ll love a sample line thanks. – Emmanuel Batse Jun 08 '18 at 13:52
  • @EmmanuelBatse there are two simple examples of inline bots [1](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard.py) [2](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/inlinekeyboard2.py) – Ivan Talalaev Oct 12 '21 at 15:43
-1

you may add a space character to the KeyboardButton text and then parse it from the message, or, if you don't like spaces to appear at the start, just add them to the end. heh, that's not enough right, so soft hyphen might be used, it's hex is \xC2\xAD (it doesn't show up, at least at my telegrams).

so, that works for me, hope mr.durov won't change that..

I've got pinged at SF because of downvote.. ye, okay, The question is about keyboard_markup, which creates buttons below text input area, they produce instant input, which prints button caption in the chat, the bot may parse them as commands (which normally start with "/" character), so the question is about how to distinguish them properly.. This is not recommended route (as it seems).

With inline_markup, you create buttons that produce callbacks with custom data, so there will be no user input in the chat.

Michael Quad
  • 149
  • 6