2021-09-25 15:12:32 +03:00
import telebot
2021-10-01 13:56:54 +03:00
from telebot import custom_filters
2021-09-25 15:12:32 +03:00
bot = telebot . TeleBot ( " " )
2021-10-01 13:56:54 +03:00
2021-11-06 10:52:41 +03:00
class MyStates :
name = 1
surname = 2
age = 3
2021-10-01 13:56:54 +03:00
2021-09-25 15:12:32 +03:00
@bot.message_handler ( commands = [ ' start ' ] )
def start_ex ( message ) :
2021-10-01 21:29:59 +03:00
"""
Start command . Here we are starting state
"""
2021-11-06 11:08:49 +03:00
bot . set_state ( message . from_user . id , MyStates . name )
2021-09-25 15:12:32 +03:00
bot . send_message ( message . chat . id , ' Hi, write me a name ' )
2021-10-01 13:56:54 +03:00
2021-09-25 15:12:32 +03:00
2021-10-01 13:56:54 +03:00
@bot.message_handler ( state = " * " , commands = ' cancel ' )
def any_state ( message ) :
2021-10-01 21:29:59 +03:00
"""
Cancel state
"""
2021-10-01 13:56:54 +03:00
bot . send_message ( message . chat . id , " Your state was cancelled. " )
2021-11-06 11:06:43 +03:00
bot . delete_state ( message . from_user . id )
2021-10-01 13:56:54 +03:00
2021-11-06 10:52:41 +03:00
@bot.message_handler ( state = MyStates . name )
2021-10-01 13:56:54 +03:00
def name_get ( message ) :
2021-10-01 21:29:59 +03:00
"""
State 1. Will process when user ' s state is 1.
"""
2021-09-25 15:12:32 +03:00
bot . send_message ( message . chat . id , f ' Now write me a surname ' )
2021-11-06 11:08:49 +03:00
bot . set_state ( message . from_user . id , MyStates . surname )
2021-11-06 11:06:43 +03:00
with bot . retrieve_data ( message . from_user . id ) as data :
2021-09-25 15:12:32 +03:00
data [ ' name ' ] = message . text
2021-11-06 10:52:41 +03:00
@bot.message_handler ( state = MyStates . surname )
2021-10-01 13:56:54 +03:00
def ask_age ( message ) :
2021-10-01 21:29:59 +03:00
"""
State 2. Will process when user ' s state is 2.
"""
2021-09-25 15:12:32 +03:00
bot . send_message ( message . chat . id , " What is your age? " )
2021-11-06 11:08:49 +03:00
bot . set_state ( message . from_user . id , MyStates . age )
2021-11-06 11:06:43 +03:00
with bot . retrieve_data ( message . from_user . id ) as data :
2021-09-25 15:12:32 +03:00
data [ ' surname ' ] = message . text
2021-10-01 21:29:59 +03:00
# result
2021-11-06 10:52:41 +03:00
@bot.message_handler ( state = MyStates . age , is_digit = True )
2021-10-01 13:56:54 +03:00
def ready_for_answer ( message ) :
2021-11-06 11:06:43 +03:00
with bot . retrieve_data ( message . from_user . id ) as data :
2021-09-25 15:12:32 +03:00
bot . send_message ( message . chat . id , " Ready, take a look: \n <b>Name: {name} \n Surname: {surname} \n Age: {age} </b> " . format ( name = data [ ' name ' ] , surname = data [ ' surname ' ] , age = message . text ) , parse_mode = " html " )
2021-11-06 11:06:43 +03:00
bot . delete_state ( message . from_user . id )
2021-10-01 13:56:54 +03:00
2021-10-01 21:29:59 +03:00
#incorrect number
2021-11-06 10:52:41 +03:00
@bot.message_handler ( state = MyStates . age , is_digit = False )
2021-10-01 13:56:54 +03:00
def age_incorrect ( message ) :
bot . send_message ( message . chat . id , ' Looks like you are submitting a string in the field age. Please enter a number ' )
2021-09-25 15:12:32 +03:00
2021-10-01 21:29:59 +03:00
# register filters
2021-10-01 13:56:54 +03:00
bot . add_custom_filter ( custom_filters . StateFilter ( bot ) )
bot . add_custom_filter ( custom_filters . IsDigitFilter ( ) )
2021-10-13 16:34:36 +03:00
# set saving states into file.
bot . enable_saving_states ( ) # you can delete this if you do not need to save states
2021-10-01 13:56:54 +03:00
bot . infinity_polling ( skip_pending = True )