I’m using the Aiotdlib library, which uses Telegram’s TDLib as a base, and when trying to send a message with buttons using a bot and the InlineKeyboardButtonTypeCallback function, I receive the following error:
[Error 400] Failed to parse JSON object as TDLib request: Wrong padding length
Here is my example code where I send the message whenever someone interacts with the bot:
from aiotdlib.api import API, UpdateNewMessage, InlineKeyboardButtonTypeCallback, ReplyMarkupInlineKeyboard, InlineKeyboardButton
from aiotdlib import Client
import asyncio
API_ID = 0
API_HASH = "..."
PHONE_NUMBER = "..."
BOT_TOKEN = "..."
async def on_update_new_message(client: Client, update: UpdateNewMessage):
chat_id = update.message.chat_id
chat = await client.api.get_chat(chat_id)
print(f'Message received in chat {chat.title}')
# button_data = bytes("button", "utf-8")
button_data = "button".encode("utf-8")
buttons = ReplyMarkupInlineKeyboard(rows=[
[InlineKeyboardButton(text="button", type=InlineKeyboardButtonTypeCallback(data=button_data))]
])
await client.send_text(chat_id, "start", reply_markup=buttons)
async def main():
client = Client(
api_id=API_ID,
api_hash=API_HASH,
bot_token=BOT_TOKEN,
# phone_number=PHONE_NUMBER,
)
client.add_event_handler(on_update_new_message, update_type=API.Types.UPDATE_NEW_MESSAGE)
async with client:
await client.idle()
if __name__ == '__main__':
asyncio.run(main())
This error only happens using InlineKeyboardButtonTypeCallback
, I don’t have the same error with InlineKeyboardButtonTypeUrl
for example.
Everything I looked for related to this error suggests that the error is in the string/bytes passed in the data
parameter of the InlineKeyboardButtonTypeCallback
function.