Search code examples
python-3.xpython-telegram-bot

How to make a custom callback_data in pyTelegramBotApi?


I have an inline keyboard in my message, so I want to transmit some data with callback, like in this example (obv, that's not working, cause callback_data must be a string)

item1 = types.InlineKeyboardButton('DO IT!', callback_data={'tp':'adm', 'com':'addf', 'con':info})

I want my callback handler to handle some data, but I don't really know how to write it


Solution

  • You can make string from your data despite of type you have. For python dict you can use json library:

    import json
    data = {'tp':'adm', 'com':'addf', 'con':info}
    item1 = types.InlineKeyboardButton('DO IT!', callback_data=json.dumps(data))
    

    But FYI it supports max length of only 64 characters (source). So either you'll fit it or find the way how to compress your data.