我编写了此代码,以在/ 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”)并执行bot,我们将看到我们输入了这些方法。没出现任何按钮或消息。
代码的结果应该是这样,但不能像这样工作 https://i.stack.imgur.com/c0wyM.gif
我已经有好几个小时了,所以我们将不胜感激。