-
Notifications
You must be signed in to change notification settings - Fork 0
/
stepExample.py
77 lines (62 loc) · 2.07 KB
/
stepExample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""
This Example will show you how to use register_next_step handler.
"""
import time
import telebot
from telebot import types
API_TOKEN = "402626118:AAF3eS8keQfNAcmQhbAWSkAzY0jpR__HCBI"
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, """\
What's up! I am Friendly Bot.
What's your name?
""")
bot.register_next_step_handler(msg, process_name_step)
def process_name_step(message):
try:
chat_id = message.chat.id
name = message.text
user = User(name)
user_dict[chat_id] = 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_id = 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_id = 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_id, 'Nice to meet you ' + user.name + '\n Age:' + str(user.age) + '\n Sex:' + user.sex)
except Exception as e:
bot.reply_to(message, 'oooops')
bot.polling()