【问题标题】:How to have multiple methods with InlinKeyBoardMarkup - telegram bot如何使用 InlinKeyBoardMarkup 使用多种方法 - 电报机器人
【发布时间】: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


    【解决方案1】:

    这里的关键部分是Dispatcher.add_handler 文档中的“顺序和优先级计数”行:您的第一个CallbackQueryHandler 将只处理任何传入的CallbackQuery,而第二个将永远不会被解雇。

    作为问题的解决方案,我建议:

    请查看inlinekeyboard2.py 示例,该示例展示了两者。


    免责声明:我目前是python-telegram-bot 的维护者。

    【讨论】:

    • 感谢您的回答,但这并不是我想要的。我可以让 /cmd_language 和 /cmd_crypto 独立工作吗?我的意思是我可以输入 /language 并显示选项,我可以输入 /crypto 并显示选项
    • 当然,这行得通——即使你同时使用ConversationHandler
    猜你喜欢
    • 1970-01-01
    • 2018-02-11
    • 2015-07-21
    • 2023-01-20
    • 2017-06-09
    • 2017-08-18
    • 2023-01-12
    • 2021-10-22
    • 1970-01-01
    相关资源
    最近更新 更多