Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. This Example will show you how to use register_next_step handler.
  4. """
  5.  
  6. import telebot
  7. from telebot import types
  8.  
  9. API_TOKEN = '<token>'
  10.  
  11. bot = telebot.TeleBot(API_TOKEN)
  12.  
  13. user_dict = {}
  14.  
  15.  
  16. class User:
  17.     def __init__(self, name):
  18.         self.name = name
  19.         self.age = None
  20.         self.sex = None
  21.  
  22.  
  23. # Handle '/start' and '/help'
  24. @bot.message_handler(commands=['help', 'start'])
  25. def send_welcome(message):
  26.     msg = bot.reply_to(message, """\
  27. Hi there, I am Example bot.
  28. What's your name?
  29. """)
  30.     bot.register_next_step_handler(msg, process_name_step)
  31.  
  32.  
  33. def process_name_step(message):
  34.     try:
  35.         chat_id = message.chat.id
  36.         name = message.text
  37.         user = User(name)
  38.         user_dict[chat_id] = user
  39.         msg = bot.reply_to(message, 'How old are you?')
  40.         bot.register_next_step_handler(msg, process_age_step)
  41.     except Exception as e:
  42.         bot.reply_to(message, 'oooops')
  43.  
  44.  
  45. def process_age_step(message):
  46.     try:
  47.         chat_id = message.chat.id
  48.         age = message.text
  49.         if not age.isdigit():
  50.             msg = bot.reply_to(message, 'Age should be a number. How old are you?')
  51.             bot.register_next_step_handler(msg, process_age_step)
  52.             return
  53.         user = user_dict[chat_id]
  54.         user.age = age
  55.         markup = types.ReplyKeyboardMarkup(one_time_keyboard=True)
  56.         markup.add('Male', 'Female')
  57.         msg = bot.reply_to(message, 'What is your gender', reply_markup=markup)
  58.         bot.register_next_step_handler(msg, process_sex_step)
  59.     except Exception as e:
  60.         bot.reply_to(message, 'oooops')
  61.  
  62.  
  63. def process_sex_step(message):
  64.     try:
  65.         chat_id = message.chat.id
  66.         sex = message.text
  67.         user = user_dict[chat_id]
  68.         if (sex == u'Male') or (sex == u'Female'):
  69.             user.sex = sex
  70.         else:
  71.             raise Exception()
  72.         bot.send_message(chat_id, 'Nice to meet you ' + user.name + '\n Age:' + str(user.age) + '\n Sex:' + user.sex)
  73.     except Exception as e:
  74.         bot.reply_to(message, 'oooops')
  75.  
  76.  
  77. # Enable saving next step handlers to file "./.handlers-saves/step.save".
  78. # Delay=2 means that after any change in next step handlers (e.g. calling register_next_step_handler())
  79. # saving will hapen after delay 2 seconds.
  80. bot.enable_save_next_step_handlers(delay=2)
  81.  
  82. # Load next_step_handlers from save file (default "./.handlers-saves/step.save")
  83. # WARNING It will work only if enable_save_next_step_handlers was called!
  84. bot.load_next_step_handlers()
  85.  
  86. bot.polling()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement