如何解决updater.dispatcher.add\u文件夹在python电报bot API中

2024-05-14 21:49:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我在写一个python电报机器人,我想用内联键盘底部。 我写下这段代码:

from telegram import *
from telegram.ext import *
mbti_message = 'This is a test massage that should sent in MBTI part'

def startx(bot, update):
    keyboard = [[InlineKeyboardButton("Option 1", callback_data='1'),
                 InlineKeyboardButton("Option 2", callback_data='2')],

                [InlineKeyboardButton("Option 3", callback_data='3')]]

    reply_markup = InlineKeyboardMarkup(keyboard)
    chat_id = update.message.chat_id
    bot.sendMessage(chat_id, "Message Sent From Function STARTX", reply_markup=reply_markup)

def buttomx(bot, update):

    query = update.callback_query

    bot.edit_message_text(text="Selected option: %s" % query.data,
                          chat_id=query.message.chat_id,
                          message_id=query.message.message_id)

def start (bot,update):

    keyboard_list = [[InlineKeyboardButton("ABOUT US", callback_data='1')],
                     [InlineKeyboardButton("MBTI Test Start", callback_data='2')]]
    reply_markup = InlineKeyboardMarkup(keyboard_list)
    chat_id = update.message.chat_id
    start_message_sent = "Welcome To My Bot Please Chose Your Options"
    bot.sendMessage(chat_id,start_message_sent, reply_markup=reply_markup)
    bot.sendMessage(chat_id,start_message_sent_global,reply_markup=reply_markup)

def bottom(bot, update):

    counter = 0
    query = update.callback_query
    chat_id = query.message.chat_id
    selected_option = int(query.data)

    if selected_option==1 :
        bot.sendMessage(chat_id,"You Chose About Us Section Thanks For Choosing Us")
    elif selected_option==2:
        bot.sendMessage(chat_id,"You Are Ready To Start Test ...")
        command = 'mbti'
        updater.dispatcher.add_handler(CommandHandler(command=command, callback=startx))
        updater.dispatcher.add_handler(CallbackQueryHandler(buttomx))
        updater.start_polling()

当用户按下底部MBTI时,将命令设置为MBTI并将其传递给命令处理程序,当命令处理程序获取MBTI命令时,命令开始运行starx函数。在

但是当我在if条件下使用下面的代码时,它会在检查if条件之前发送它

^{pr2}$

在这种情况下我该怎么办?在


Tags: 命令idmessagedatabotcallbackchatupdate
1条回答
网友
1楼 · 发布于 2024-05-14 21:49:10

使用ConversationHandler进行深度交互菜单。另一种方法可以存储userState,并使用State调用函数。在

有关更多信息,请参阅docsexample,它还支持user_data和RegExp,以便强大地处理来自用户的消息。在

别忘了:数据存储在内存中,在某些情况下,您可能丢失或不正确。很好的想法是在入口点清除user_data

每个函数可以有多个returns到另一个条目,并且与另一个条目在不同的匹配情况下具有许多不同的函数。在

相关问题 更多 >

    热门问题