-
Notifications
You must be signed in to change notification settings - Fork 6k
Description
Steps to reproduce
- Run the code below
bot = Bot(TOKEN)
callback_data = ''.join(chr(i) for i in range(128, 128+64))
reply_markup = InlineKeyboardMarkup([[InlineKeyboardButton('Test', callback_data=callback_data)]])
bot.send_message(chat_id=CHAT_ID, text="text", reply_markup=reply_markup)Expected behaviour
The code should send a message to CHAT_ID with the text "text" and a button "Test" with the callback_data set to \x80\x81\x82 and so on up to \xbf.
Actual behaviour
telegram.error.BadRequest: Button_data_invalid
Note that the code below works as expected:
bot = Bot(TOKEN)
callback_data = bytes(bytearray(j for j in range(128, 128+64)))
bot._request._request_wrapper('POST', '{0}/sendMessage'.format(bot.base_url),
body=b'{"chat_id": "CHAT_ID", "text": "test", "reply_markup": "{\\"inline_keyboard\\": [[{\\"text\\": \\"Test\\", \\"callback_data\\": \\"' + callback_data + b'\\"}]]}"}',
headers={'Content-Type': 'application/json'})This means that it is indeed possible to send bytes \x80 through \xbf as the callback_data of a button.
The problem originates from the from
python-telegram-bot/telegram/utils/request.py
Line 323 in 9d99660
| body=json.dumps(data).encode('utf-8'), |
where we encode all the data (including the callback_data) using utf-8, which mangles all characters above \x80 (128). This means that \x80 through \xbf actually turns into 128 bytes instead of the expected 64 (and therefore the BadRequest)
Bug?
Is this a bug we even really care about?
I figured I just might as well report it.
One optimal fix would be to allow bytes as the input to InlineKeyboardButton, that would then be excluded from the .encode('utf-8') but I wouldn't even know how to begin implementing that.
We could also just ignore it - I highly doubt this bug really effects anyone tbh.
Configuration
Operating System:
Windows 10 version 1803 build 17134.285
Version of Python, python-telegram-bot & dependencies:
python-telegram-bot 11.1.0
certifi 2018.08.24
future 0.16.0
Python 3.7.0 (default, Jun 28 2018, 08:04:48) [MSC v.1912 64 bit (AMD64)]
NOTE actually using master branch.
Logs
With
import http.client
http.client.HTTPConnection.debuglevel = 5so we can see the data we send.
send: b'POST /botTOKEN/sendMessage HTTP/1.1\r\nHost: api.telegram.org\r\nAccept-Encoding: identity\r\nContent-Length: 579\r\nContent-Type: application/json\r\nconnection: keep-alive\r\nuser-agent: Python Telegram Bot (https://github.com/python-telegram-bot/python-telegram-bot)\r\n\r\n'
send: b'{"chat_id": "CHAT_ID", "text": "text", "reply_markup": "{\\"inline_keyboard\\": [[{\\"text\\": \\"Test\\", \\"callback_data\\": \\"\\\\u0080\\\\u0081\\\\u0082\\\\u0083\\\\u0084\\\\u0085\\\\u0086\\\\u0087\\\\u0088\\\\u0089\\\\u008a\\\\u008b\\\\u008c\\\\u008d\\\\u008e\\\\u008f\\\\u0090\\\\u0091\\\\u0092\\\\u0093\\\\u0094\\\\u0095\\\\u0096\\\\u0097\\\\u0098\\\\u0099\\\\u009a\\\\u009b\\\\u009c\\\\u009d\\\\u009e\\\\u009f\\\\u00a0\\\\u00a1\\\\u00a2\\\\u00a3\\\\u00a4\\\\u00a5\\\\u00a6\\\\u00a7\\\\u00a8\\\\u00a9\\\\u00aa\\\\u00ab\\\\u00ac\\\\u00ad\\\\u00ae\\\\u00af\\\\u00b0\\\\u00b1\\\\u00b2\\\\u00b3\\\\u00b4\\\\u00b5\\\\u00b6\\\\u00b7\\\\u00b8\\\\u00b9\\\\u00ba\\\\u00bb\\\\u00bc\\\\u00bd\\\\u00be\\\\u00bf\\"}]]}"}'
Traceback (most recent call last):
*SNIP*
File "C:/Users/bomja_000/.PyCharm2018.2/config/scratches/scratch_22.py", line 77, in <module>
bot.send_message(chat_id=95205500, text="text", reply_markup=reply_markup)
File "C:\Anaconda3\envs\githubbot\lib\site-packages\telegram\bot.py", line 65, in decorator
result = func(self, *args, **kwargs)
File "C:\Anaconda3\envs\githubbot\lib\site-packages\telegram\bot.py", line 90, in decorator
result = self._request.post(url, data, timeout=kwargs.get('timeout'))
File "C:\Anaconda3\envs\githubbot\lib\site-packages\telegram\utils\request.py", line 325, in post
headers={'Content-Type': 'application/json'})
File "C:\Anaconda3\envs\githubbot\lib\site-packages\telegram\utils\request.py", line 239, in _request_wrapper
raise BadRequest(message)
telegram.error.BadRequest: Button_data_invalid