Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Telegram Bot Error - telegram.error.BadRequest: Button_data_invalid

I'm trying to make a telegram bot with the telegram and telegram.ext libraries on Python. I try to pass callback_data for a button as a parameter for inline keyboard, but when it comes to add the keyboard as reply_markup, it sends the following error:

Traceback (most recent call last):
    File "C:\Users\micke\AppData\Local\Programs\Python\Python36-32\lib\site-packages\telegram\ext\dispatcher.py", line 279, in process_update
    handler.handle_update(update, self)
    File "C:\Users\micke\AppData\Local\Programs\Python\Python36-32\lib\site-packages\telegram\ext\callbackqueryhandler.py", line 143, in handle_update
    return self.callback(dispatcher.bot, update, **optional_args)
    File ".\__init__.py", line 121, in button
    reply_markup=reply_markup
    File "C:\Users\micke\AppData\Local\Programs\Python\Python36-32\lib\site-packages\telegram\bot.py", line 60, in decorator
    result = func(self, *args, **kwargs)
    File "C:\Users\micke\AppData\Local\Programs\Python\Python36-32\lib\site-packages\telegram\bot.py", line 85, in decorator
    result = self._request.post(url, data, timeout=kwargs.get('timeout'))
    File "C:\Users\micke\AppData\Local\Programs\Python\Python36-32\lib\site-packages\telegram\utils\request.py", line 273, in post
    **urlopen_kwargs)
    File "C:\Users\micke\AppData\Local\Programs\Python\Python36-32\lib\site-packages\telegram\utils\request.py", line 210, in _request_wrapper
    raise BadRequest(message)
telegram.error.BadRequest: Button_data_invalid

The code is the following:

p1 = "%s(@%s)" % (query.from_user.first_name, query.from_user.username)
text = "<b>⚔Duel</b>\nChallenger %s is ready!\nWaiting for an opponent..." % p1
args = '{"next":"player2","p1":"%s","text":"%s"}' % (p1,text)
reply_markup = InlineKeyboardMarkup(kb.kb(op = "data", args = args))
bot.edit_message_text(
                      text=text,
                      inline_message_id=query.inline_message_id,
                      parse_mode=ParseMode.HTML,
                      reply_markup=reply_markup
                      )

Also, the code for the keyboard is:

class kb:
    def kb(op = None, args = None):
        if op == 'data':
            keyboard = [[InlineKeyboardButton("Join", callback_data = args)]]

I don't really know where am I mistaken. It works well when instead of args I assign static text to callback_data

What do you think it could be?

like image 786
Mickey Rodriguez Avatar asked Jul 16 '26 03:07

Mickey Rodriguez


1 Answers

Probably data size, you passing, is exceeding the maximum allowed bytes of 64. Here is the docs. You can get size of data in bytes as followed.

len('YOUR DATA'.encode('utf-8'))
like image 153
Darkhan ZD Avatar answered Jul 17 '26 22:07

Darkhan ZD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!