回调查询是 None 在 python-telegram-bot v20.7 中输入

问题描述 投票:0回答:1

这是我的代码:

from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery
from telegram.constants import ParseMode
from telegram.ext import ApplicationBuilder, CommandHandler
from telegram.ext import filters, MessageHandler, ContextTypes, ConversationHandler, CallbackQueryHandler

async def hello_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
    keyboard = [
        [InlineKeyboardButton("Press me", callback_data='hello')]

    ]

    reply_markup = InlineKeyboardMarkup(keyboard)

    await update.message.reply_text('Press:', reply_markup=reply_markup)

async def button_pressed(update: Update, context: ContextTypes.DEFAULT_TYPE):
    query = update.callback_query
    await query.answer()
    if query.data == 'hello':
        await query.edit_message_text("Hello!")

if __name__ == '__main__':
    app = ApplicationBuilder().token(TOKEN).build()
    app.add_handler(CommandHandler('hello', hello_command))
    app.add_handler(CallbackQueryHandler(callback=button_pressed))
    app.run_polling(0)

问题是,当我按下按钮时,没有任何反应,尽管机器人应该回复“你好!”文本。我尝试了很多不同的选项来处理回调数据,但仍然没有成功。在调试过程中,我得出的结论是,我的代码没有产生预期结果,因为 CallbackQuery 是 NoneType。这很奇怪,因为我正确初始化了callback_data。我还发现,当代码运行时,它永远不会进入button_pressed 函数。我应该怎么做才能让我的代码正常工作?

python-telegram-bot
1个回答
0
投票

你好,我复制了它,我认为如果你提供的实际上是完整的代码,那么我修复了一些导入问题,并使其更加简洁和可读,你不需要等待查询答案部分,认为它可以作为答案显然没有,所以没关系,尽管这是不必要的。您的代码可以正常工作,只需进行调整并添加导入即可。 [链接][https://pastebin.com/20XNqeZg]

© www.soinside.com 2019 - 2024. All rights reserved.