首页 > 解决方案 > PyTelegramBotApi - 如果其他功能正在运行,如何停止一个功能?

问题描述

我正在尝试进行可选输入,并拥有此代码

 bot.send_chat_action(message.from_user.id, 'typing')
 markup = types.InlineKeyboardMarkup()
 markup.add(types.InlineKeyboardButton("Лечу только в одну сторону", callback_data="one_way"))
 msg = bot.send_message(message.from_user.id, '✅Хорошо. Теперь введите дату возвращения:', reply_markup=markup)
 bot.register_next_step_handler(msg, get_return_date)

此代码向用户发送带有按钮的消息以跳过此步骤,并注册函数 get_return_date(),该函数等待日期值。消息 如果用户单击按钮,查询处理程序注册另一个函数 get_adults(),它等待数值:

@bot.callback_query_handler(func=lambda call: call.data == "one_way")
def is_one_way(call):
    msg = bot.send_message(call.from_user.id,
                           '✅Хорошо. Сколько взрослых (пассажиров старше 12-ти лет на момент полёта) полетят ‍?')
    bot.register_next_step_handler(msg, get_adults)
    return

而且,麻烦的是 - 如果用户单击按钮跳过, get_return_date() 和 get_adults() 都在等待一个值并同时工作: 问题

有什么想法我该怎么办?

标签: pythonpython-3.xtelegrampy-telegram-bot-api

解决方案


你可以这样做:

bot.send_chat_action(message.from_user.id, 'typing')
 msg = bot.send_message(message.from_user.id, '✅Хорошо. Теперь введите дату возвращения: (you can skip this passage with /skip)')
 bot.register_next_step_handler(msg, get_return_date)

def get_retourn_date(message):
    if message.text == '/skip':
        get_adults()
        return
    # write here the actual function code

推荐阅读