Python电报bot不执行操作

2024-04-25 22:46:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我将此代码用于显示打开/启动按钮。

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, ConversationHandler
MENU, HELP = range(2)

def start(bot, update):
    keyboard = [
                 [InlineKeyboardButton('Help', callback_data='help')]
               ]

    # Create initial message:
    message = 'Welcome.'

    update.message.reply_text(message, reply_markup=InlineKeyboardMarkup(keyboard))

def help(bot, update):

    keyboard = [
                 [InlineKeyboardButton('Leave', callback_data='cancel')]
               ]


    bot.edit_message_text(
    text='Help ... help..',
    chat_id=update.callback_query.message.chat_id,
    message_id=update.callback_query.message.message_id,
    reply_markup=InlineKeyboardMarkup(keyboard)
)
    bot.answer_callback_query(update.callback_query.id, text='')

def cancel(bot, update):

    bot.edit_message_text(
    text='Bye',
    chat_id=update.callback_query.message.chat_id,
    message_id=update.callback_query.message.message_id,
)
    bot.answer_callback_query(update.callback_query.id, text='')

    return ConversationHandler.END     


# Create the EventHandler and pass it your bot's token.
updater = Updater(token="tokencode", use_context=True)

# Get the dispatcher to register handlers:
dispatcher = updater.dispatcher

dispatcher.add_handler(CommandHandler('start', start))
dispatcher.add_handler(CallbackQueryHandler(help, pattern='help'))
dispatcher.add_handler(CallbackQueryHandler(cancel, pattern='cancel'))

updater.start_polling()

updater.idle()

代码之所以有效,是因为如果我们在方法中编写一些调试打印(“hi”)并执行bot,我们将看到我们进入了这些方法。无按钮或消息不会显示。

代码的结果应该是这样,但不是这样工作的 https://i.stack.imgur.com/c0wyM.gif

我已经花了好几个小时在这上面了,非常感谢你的帮助


Tags: 代码textidmessagebotcallbackchathelp