Search code examples
pythonpython-telegram-bot

Object of type 'InlineKeyboardButton' is not JSON serializable


my code is here

def forwards(update:Update, context:CallbackContext):
    query = update.callback_query
    next_index = int(query.data.split("_")[1]) + 1
    current_index = int(query.data.split("_")[1])
    html = ""

   # if context.user_data["totalPage"] == current_index:
    for i in range(((current_index - 1)*20), context.user_data["rows"]):
        html += context.user_data["videos"][i]
    c_d = current_index - 1
    button = [[InlineKeyboardButton("⬅️previous page", callback_data="minus_" + str(c_d))]]
    markup = InlineKeyboardButton(button)
    a = query.edit_message_text(text = html, parse_mode = ParseMode.HTML, reply_markup = markup) 


def message_handle(update, context:CallbackContext):
    temp = r.message_handler(update.message)
    context.user_data["videos"] = temp["videos"]
    context.user_data["totalPage"] = temp["totalPage"]
    context.user_data["rows"] = temp["rows"]
    html = ""
    
    if temp["rows"] >= 20:
        count = 0
        for i in temp["videos"]:
            if count == 20:
                break
            html += i
            count += 1
    elif temp["rows"] < 20:
        for i in temp["videos"]:
            html += i
        
    button = [[InlineKeyboardButton("next page➡️", callback_data="plus_2")]]
    markup = InlineKeyboardMarkup(button)
    
    update.message.reply_text(text = html , parse_mode = ParseMode.HTML, 
                                disable_web_page_preview = True, 
                                reply_to_message_id = update.message.message_id,
                                reply_markup = markup)

when cliecnt sends a keyword to the bot, the bot ,using the keyword, finds out relevant data in the other file, txtfile. It will forward reply message to the client page by page , using callbackquery .

there are 3 function,

  1. def message_handle (that's for MessageHandler)

2.def forwards ( thats for callbackQuery handler, as the name said for forward page after "forward"( callback_data "plus" )button click)

  1. def backwards ( for callbackQuery handler, as the name said for backward page after "backward"(callback data "minus") button click)

um... the message_handle shows (in function message_handle) fine.. but there is error showed after the inline button(forward) click..

TypeError: Object of type 'InlineKeyboardButton' is not JSON serializable

and the following is the main part

def main():
    dp.add_handler(MessageHandler(Filters.text, message_handle))
    dp.add_handler(CallbackQueryHandler(forwards, pattern = "plus"))
    dp.add_handler(CallbackQueryHandler(backwards, pattern = "minus"))
    
    updater.start_polling()
    updater.idle()

so whats wrong of it..? thanks for you attention


Solution

  • Your problem is just what the error you are getting says it is. You are attempting to include an instance of the InlineKeyboardButton class in a message. A message is a serialized string of bytes. So to send that instance as part of the message, it has to be serialized. Apparently, InlineKeyboardButton is not serializable. This means that the class has fields that can't be represented in JSON. Looking quickly at the docs for that object, I see that when constructed, it can take callback function references. That could be the problem. A function reference isn't serializable. It could be due to some other similar issue. You generally don't want to try to send a complex object like that in a message, or otherwise attempt to serialize it.

    My guess is that you don't want to send that object, but rather send some of the data encapsulated in that object. I don't know exactly what you're trying to accomplish, nor am I familiar with the message passing mechanism you are using, but the idea of sending a button in a message doesn't generally make sense. I would rethink just what data you want to put in your message, and make sure that it all consists of simple values (strings, numbers, etc.) that can be serialized into a JSON structure.