bot register next step handler

Подскажите пожалуйста, как выйти из любого шага при использовании данного метода, что бы на каждом из них, к примеру, можно было нажать кнопку, и цикл завершался.
Примерный код:

При нажатии на ОТМЕНА, все работает, кнопка пропадает и заменяется на текст, но сам цикл не завершается.

Проблему саму не удалось решить, но мне помогло это:

one_time_keyboard=True
Клавиатура сразу убирается когда пользователь выбрал нужный пункт меню. Благодаря этому пользователь не сможет много кратно и быстро переключаться по меню.

Please note that GitHub no longer supports your web browser.

We recommend upgrading to the latest Google Chrome or Firefox.

Join GitHub today

GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

pyTelegramBotAPI / examples / step_example.py

Users who have contributed to this file

# -*- coding: utf-8 -*-
«»»
This Example will show you how to use register_next_step handler.
«»»
import telebot
from telebot import types
API_TOKEN = ‘ ‘
bot = telebot.TeleBot( API_TOKEN )
user_dict = <>
class User :
def __init__ ( self , name ):
self .name = name
self .age = None
self .sex = None
# Handle ‘/start’ and ‘/help’
@bot.message_handler ( commands = [ ‘ help ‘ , ‘ start ‘ ])
def send_welcome ( message ):
msg = bot.reply_to(message, «»»
Hi there, I am Example bot.
What’s your name?
«»» )
bot.register_next_step_handler(msg, process_name_step)
def process_name_step ( message ):
try :
chat_ >= message.chat.id
name = message.text
user = User(name)
user_dict[chat_ >= user
msg = bot.reply_to(message, ‘ How old are you? ‘ )
bot.register_next_step_handler(msg, process_age_step)
except Exception as e:
bot.reply_to(message, ‘ oooops ‘ )
def process_age_step ( message ):
try :
chat_ >= message.chat.id
age = message.text
if not age.isdigit():
msg = bot.reply_to(message, ‘ Age should be a number. How old are you? ‘ )
bot.register_next_step_handler(msg, process_age_step)
return
user = user_dict[chat_id]
user.age = age
markup = types.ReplyKeyboardMarkup( one_time_keyboard = True )
markup.add( ‘ Male ‘ , ‘ Female ‘ )
msg = bot.reply_to(message, ‘ What is your gender ‘ , reply_markup = markup)
bot.register_next_step_handler(msg, process_sex_step)
except Exception as e:
bot.reply_to(message, ‘ oooops ‘ )
def process_sex_step ( message ):
try :
chat_ >= message.chat.id
sex = message.text
user = user_dict[chat_id]
if (sex == u ‘ Male ‘ ) or (sex == u ‘ Female ‘ ):
user.sex = sex
else :
raise Exception ()
bot.send_message(chat_ > ‘ Nice to meet you ‘ + user.name + ‘
Age: ‘ + str (user.age) + ‘
Sex: ‘ + user.sex)
except Exception as e:
bot.reply_to(message, ‘ oooops ‘ )
# Enable saving next step handlers to file «./.handlers-saves/step.save».
# Delay=2 means that after any change in next step handlers (e.g. calling register_next_step_handler())
# saving will hapen after delay 2 seconds.
bot.enable_save_next_step_handlers( delay = 2 )
# Load next_step_handlers from save file (default «./.handlers-saves/step.save»)
# WARNING It will work only if enable_save_next_step_handlers was called!
bot.load_next_step_handlers()
bot.polling()
  • © 2019 GitHub , Inc.
  • Terms
  • Privacy
  • Security
  • Status
  • Help

You can’t perform that action at this time.

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.

Оцените статью