Search code examples
callbackbotstelegramtelethon

When should I use 'b' (bytes) prefix defining Callback data in Telethon?


I saw the next code:

markup = [
  [Button.inline("Restart the test", b'restart')],
  [Button.inline("Continue", b'continue')]
]
@client.on(events.CallbackQuery(data=b'restart'))
...

What is the purpose of using this 'b' before the callback data definition?
Does it make the bot work faster or what?


Solution

  • Telethon v1 will encode str data as bytes under the hood, and use that for comparisons when callback queries are received. Telegram's API supports arbitrary bytes as the data stored inside inline buttons, which is why Telethon will compare the bytes directly without decodeing them (as they do not necessarily need to be valid str).

    The support for passing str is merely there for convenience.