'InlineKeyboardButton' 类型的对象不可 JSON 序列化
Object of type 'InlineKeyboardButton' is not JSON serializable
我的代码在这里
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)
当客户端向bot发送关键词时,bot利用关键词在另一个文件txt文件中找到相关数据。
它将使用回调查询逐页转发回复消息给客户端。
有3个函数,
- def message_handle(用于 MessageHandler)
2.def 转发(那是回调查询处理程序,正如“转发”(callback_data“加号”)按钮单击后转发页面的名称所说)
- def backwards(对于回调查询处理程序,正如“向后”(回调数据“减去”)按钮单击后向后页面的名称所说)
嗯...
message_handle 显示(在函数 message_handle 中)很好..
但是点击内联按钮(前进)后出现错误..
TypeError: Object of type 'InlineKeyboardButton' is not JSON serializable
下面是正文
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()
所以它有什么问题..?
感谢您的关注
您的问题正是您收到的错误所说的那样。您正试图在消息中包含 InlineKeyboardButton
class 的实例。消息是序列化的字节串。因此,要将该实例作为消息的一部分发送,必须对其进行序列化。显然,InlineKeyboardButton
不可序列化。这意味着 class 具有无法在 JSON 中表示的字段。快速查看该对象的文档,我发现在构造时,它可以采用回调函数引用。这可能是问题所在。函数引用不可序列化。这可能是由于其他一些类似的问题。您通常不想尝试在消息中发送像这样的复杂对象,或者尝试序列化它。
我的猜测是您不想发送该对象,而是发送封装在该对象中的一些数据。我不确切地知道你想要完成什么,我也不熟悉你正在使用的消息传递机制,但是在消息中发送按钮的想法通常没有意义。我会重新考虑您要在消息中放入哪些数据,并确保它全部由可以序列化为 JSON 结构的简单值(字符串、数字等)组成。
我的代码在这里
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)
当客户端向bot发送关键词时,bot利用关键词在另一个文件txt文件中找到相关数据。 它将使用回调查询逐页转发回复消息给客户端。
有3个函数,
- def message_handle(用于 MessageHandler)
2.def 转发(那是回调查询处理程序,正如“转发”(callback_data“加号”)按钮单击后转发页面的名称所说)
- def backwards(对于回调查询处理程序,正如“向后”(回调数据“减去”)按钮单击后向后页面的名称所说)
嗯... message_handle 显示(在函数 message_handle 中)很好.. 但是点击内联按钮(前进)后出现错误..
TypeError: Object of type 'InlineKeyboardButton' is not JSON serializable
下面是正文
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()
所以它有什么问题..? 感谢您的关注
您的问题正是您收到的错误所说的那样。您正试图在消息中包含 InlineKeyboardButton
class 的实例。消息是序列化的字节串。因此,要将该实例作为消息的一部分发送,必须对其进行序列化。显然,InlineKeyboardButton
不可序列化。这意味着 class 具有无法在 JSON 中表示的字段。快速查看该对象的文档,我发现在构造时,它可以采用回调函数引用。这可能是问题所在。函数引用不可序列化。这可能是由于其他一些类似的问题。您通常不想尝试在消息中发送像这样的复杂对象,或者尝试序列化它。
我的猜测是您不想发送该对象,而是发送封装在该对象中的一些数据。我不确切地知道你想要完成什么,我也不熟悉你正在使用的消息传递机制,但是在消息中发送按钮的想法通常没有意义。我会重新考虑您要在消息中放入哪些数据,并确保它全部由可以序列化为 JSON 结构的简单值(字符串、数字等)组成。