python - 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() 都在等待一个值并同时工作: 问题
有什么想法我该怎么办?
解决方案
你可以这样做:
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
推荐阅读
- node.js - 使用 Node Js 获取 ECONNRESET 消息的 Azure Analytics HTTP Api 调用
- python - 如何使用面对异常(selenium + python)的文本()定位元素?
- react-native - ESLint:要求语句不是导入语句的一部分。(@typescript-eslint/no-var-requires)
- python-3.x - 获取类类型以实例化的字典
- python-3.x - GCloud App Engine 上 Flask 应用执行时间不规律
- git - 通过 xargs util 将配置参数传递给 git 命令
- amazon-cloudwatch - 如何将 cloudwatch 指标数据发送到 kinesis 数据流
- sql - Oracle SQL 中列中的日期之间的差异
- python - 使用 kuberspay 待处理的 Cilium pod
- regex - 使用正则表达式查找特定字符串,后跟两个用空格分隔的字符串