python - 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
我已经有好几个小时了,所以任何帮助都将不胜感激。
解决方案
您必须在函数定义中切换参数。除此之外,我宁愿称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()
推荐阅读
- c++ - 如何在可变参数模板函数中使用 source_location?
- html - 如何在导航中垂直居中徽标?
- python - 在一次试验中更新数据并再次使用更新数据的功能
- django - 如何正确更新 django 中的用户配置文件
- python - Beautifulsoup 是否有可以删除所有空格的功能
- node.js - 我在使用 npm install express 时遇到问题
- docker - 无法连接到 docker 容器
- node.js - 业务逻辑nodejs放在哪里
- npm - 端口 4200 已在使用中。使用“--port”指定不同的端口
- python - ValueError:检查目标时出错:预期dense_1的形状为(16、1、2)但得到的数组形状为(2、1、1)