【发布时间】:2021-08-09 23:09:01
【问题描述】:
我正在尝试使用 2 种快速回复的方法。这两种方法是 cmd_language 和 cmd_crypto。问题是第一种方法(cmd_language)效果很好,但第二种方法不行。
我希望程序像这样工作: 我键入 /start,键入“欢迎”,然后自动执行 cmd_language,显示“选择语言”和选项(现在可以了)。然后我输入/crypto,显示选项,但是当我选择一个选项时,我得到一个错误。
from telegram.ext import *
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
#-------------------- START ------------------------
def cmd_start(update, context:CallbackContext):
update.message.reply_text("Welcome")
cmd_language(update, context)
#-------------------LANGUAGE---------------------------
def cmd_language(update, context:CallbackContext):
languages= [[InlineKeyboardButton("Español????????", callback_data="ES")],
[InlineKeyboardButton("English????????", callback_data="EN")]]
menuLanguages = InlineKeyboardMarkup(languages)
update.message.reply_text("Select the language", reply_markup=menuLanguages)
def selectionLanguage(update, context):
query = update.callback_query
language = query.data
query.edit_message_text(text=f"You have selected {language}")
#-----------------------CRYTO-------------------
def cmd_crypto(update, context:CallbackContext):
cryptos= [[InlineKeyboardButton("ADA - Cardano", callback_data="ADA")],
[InlineKeyboardButton("BTC - Bitcoin", callback_data="BTC")]]
menuCryptos= InlineKeyboardMarkup(cryptos)
update.message.reply_text("Select the cryptocurrency:", reply_markup=menuCryptos)
def selectionCrypto(update, context):
query = update.callback_query
crypto= query.data
query.edit_message_text(text=f"You have selected {crypto}"))
#----------------ERROR-------------------------
def error(update, context):
print(f"Update {update} caused error {context.error}")
#--------------MAIN-------------------------------
def main():
updater = Updater("TOKEN", use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler("start", cmd_start))
dp.add_handler(CommandHandler("language", cmd_language))
dp.add_handler(CallbackQueryHandler(selectionLanguage))
dp.add_handler(CommandHandler("crypto", cmd_crypto))
dp.add_handler(CallbackQueryHandler(selectionCrypto))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == "__main__":
main()
【问题讨论】:
标签: python python-3.x telegram python-telegram-bot