mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
commit
14cc15c711
@ -8,6 +8,9 @@ bot = telebot.TeleBot("")
|
|||||||
|
|
||||||
@bot.message_handler(commands=['start'])
|
@bot.message_handler(commands=['start'])
|
||||||
def start_ex(message):
|
def start_ex(message):
|
||||||
|
"""
|
||||||
|
Start command. Here we are starting state
|
||||||
|
"""
|
||||||
bot.set_state(message.chat.id, 1)
|
bot.set_state(message.chat.id, 1)
|
||||||
bot.send_message(message.chat.id, 'Hi, write me a name')
|
bot.send_message(message.chat.id, 'Hi, write me a name')
|
||||||
|
|
||||||
@ -15,11 +18,17 @@ def start_ex(message):
|
|||||||
|
|
||||||
@bot.message_handler(state="*", commands='cancel')
|
@bot.message_handler(state="*", commands='cancel')
|
||||||
def any_state(message):
|
def any_state(message):
|
||||||
|
"""
|
||||||
|
Cancel state
|
||||||
|
"""
|
||||||
bot.send_message(message.chat.id, "Your state was cancelled.")
|
bot.send_message(message.chat.id, "Your state was cancelled.")
|
||||||
bot.delete_state(message.chat.id)
|
bot.delete_state(message.chat.id)
|
||||||
|
|
||||||
@bot.message_handler(state=1)
|
@bot.message_handler(state=1)
|
||||||
def name_get(message):
|
def name_get(message):
|
||||||
|
"""
|
||||||
|
State 1. Will process when user's state is 1.
|
||||||
|
"""
|
||||||
bot.send_message(message.chat.id, f'Now write me a surname')
|
bot.send_message(message.chat.id, f'Now write me a surname')
|
||||||
bot.set_state(message.chat.id, 2)
|
bot.set_state(message.chat.id, 2)
|
||||||
with bot.retrieve_data(message.chat.id) as data:
|
with bot.retrieve_data(message.chat.id) as data:
|
||||||
@ -28,21 +37,28 @@ def name_get(message):
|
|||||||
|
|
||||||
@bot.message_handler(state=2)
|
@bot.message_handler(state=2)
|
||||||
def ask_age(message):
|
def ask_age(message):
|
||||||
|
"""
|
||||||
|
State 2. Will process when user's state is 2.
|
||||||
|
"""
|
||||||
bot.send_message(message.chat.id, "What is your age?")
|
bot.send_message(message.chat.id, "What is your age?")
|
||||||
bot.set_state(message.chat.id, 3)
|
bot.set_state(message.chat.id, 3)
|
||||||
with bot.retrieve_data(message.chat.id) as data:
|
with bot.retrieve_data(message.chat.id) as data:
|
||||||
data['surname'] = message.text
|
data['surname'] = message.text
|
||||||
|
|
||||||
|
# result
|
||||||
@bot.message_handler(state=3, is_digit=True)
|
@bot.message_handler(state=3, is_digit=True)
|
||||||
def ready_for_answer(message):
|
def ready_for_answer(message):
|
||||||
with bot.retrieve_data(message.chat.id) as data:
|
with bot.retrieve_data(message.chat.id) as data:
|
||||||
bot.send_message(message.chat.id, "Ready, take a look:\n<b>Name: {name}\nSurname: {surname}\nAge: {age}</b>".format(name=data['name'], surname=data['surname'], age=message.text), parse_mode="html")
|
bot.send_message(message.chat.id, "Ready, take a look:\n<b>Name: {name}\nSurname: {surname}\nAge: {age}</b>".format(name=data['name'], surname=data['surname'], age=message.text), parse_mode="html")
|
||||||
bot.delete_state(message.chat.id)
|
bot.delete_state(message.chat.id)
|
||||||
|
|
||||||
|
#incorrect number
|
||||||
@bot.message_handler(state=3, is_digit=False)
|
@bot.message_handler(state=3, is_digit=False)
|
||||||
def age_incorrect(message):
|
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')
|
bot.send_message(message.chat.id, 'Looks like you are submitting a string in the field age. Please enter a number')
|
||||||
|
|
||||||
|
# register filters
|
||||||
|
|
||||||
bot.add_custom_filter(custom_filters.StateFilter(bot))
|
bot.add_custom_filter(custom_filters.StateFilter(bot))
|
||||||
bot.add_custom_filter(custom_filters.IsDigitFilter())
|
bot.add_custom_filter(custom_filters.IsDigitFilter())
|
||||||
bot.infinity_polling(skip_pending=True)
|
bot.infinity_polling(skip_pending=True)
|
@ -159,8 +159,9 @@ class StateFilter(AdvancedCustomFilter):
|
|||||||
|
|
||||||
def check(self, message, text):
|
def check(self, message, text):
|
||||||
if self.bot.current_states.current_state(message.from_user.id) is False:return False
|
if self.bot.current_states.current_state(message.from_user.id) is False:return False
|
||||||
elif '*' in text:return True
|
elif text == '*':return True
|
||||||
return self.bot.current_states.current_state(message.from_user.id) in text
|
elif type(text) is list:return self.bot.current_states.current_state(message.from_user.id) in text
|
||||||
|
return self.bot.current_states.current_state(message.from_user.id) == text
|
||||||
|
|
||||||
class IsDigitFilter(SimpleCustomFilter):
|
class IsDigitFilter(SimpleCustomFilter):
|
||||||
"""
|
"""
|
||||||
|
Loading…
Reference in New Issue
Block a user