首页 > 解决方案 > Python电报机器人不执行动作操作

问题描述

我制作了这段代码来显示 /start 上的按钮。

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()

代码有效,因为如果我们在方法中编写一些调试 print("hi") 并执行机器人,我们将看到我们输入了这些方法。Neverthless 按钮或消息不显示。

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

我已经有好几个小时了,所以任何帮助都将不胜感激。

标签: pythontelegram-botpython-telegram-bot

解决方案


您必须在函数定义中切换参数。除此之外,我宁愿称context你所称的为bot.

这是有效的代码

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

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

    # Create initial message:
    message = 'Welcome.'

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

def help(update, context):

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


    context.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)
)
    context.bot.answer_callback_query(update.callback_query.id, text='')

def cancel(update, context):

    context.bot.edit_message_text(
    text='Bye',
    chat_id=update.callback_query.message.chat_id,
    message_id=update.callback_query.message.message_id,
)
    context.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()

推荐阅读