1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00
pyTelegramBotAPI/examples/custom_states.py

102 lines
3.2 KiB
Python
Raw Normal View History

2022-01-24 20:24:56 +03:00
import telebot # telebot
2021-09-25 15:12:32 +03:00
from telebot import custom_filters
2022-01-24 20:24:56 +03:00
from telebot.handler_backends import State, StatesGroup #States
2021-09-25 15:12:32 +03:00
2022-01-24 20:24:56 +03:00
# States storage
2022-01-24 22:38:35 +03:00
from telebot.storage import StateMemoryStorage
2021-09-25 15:12:32 +03:00
2022-01-24 20:24:56 +03:00
# Beginning from version 4.4.0+, we support storages.
# StateRedisStorage -> Redis-based storage.
# StatePickleStorage -> Pickle-based storage.
# For redis, you will need to install redis.
# Pass host, db, password, or anything else,
# if you need to change config for redis.
# Pickle requires path. Default path is in folder .state-saves.
# If you were using older version of pytba for pickle,
# you need to migrate from old pickle to new by using
# StatePickleStorage().convert_old_to_new()
# Now, you can pass storage to bot.
state_storage = StateMemoryStorage() # you can init here another storage
bot = telebot.TeleBot("TOKEN",
state_storage=state_storage)
# States group.
class MyStates(StatesGroup):
# Just name variables differently
name = State() # creating instances of State class is enough from now
surname = State()
age = State()
2021-11-06 10:52:41 +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
"""
2022-01-24 20:24:56 +03:00
bot.set_state(message.from_user.id, MyStates.name, message.chat.id)
2021-09-25 15:12:32 +03:00
bot.send_message(message.chat.id, 'Hi, write me a name')
2022-01-24 20:24:56 +03:00
# Any state
@bot.message_handler(state="*", commands='cancel')
def any_state(message):
2021-10-01 21:29:59 +03:00
"""
Cancel state
"""
bot.send_message(message.chat.id, "Your state was cancelled.")
2022-01-24 20:24:56 +03:00
bot.delete_state(message.from_user.id, message.chat.id)
2021-11-06 10:52:41 +03:00
@bot.message_handler(state=MyStates.name)
def name_get(message):
2021-10-01 21:29:59 +03:00
"""
2022-01-24 20:34:50 +03:00
State 1. Will process when user's state is MyStates.name.
2021-10-01 21:29:59 +03:00
"""
2021-09-25 15:12:32 +03:00
bot.send_message(message.chat.id, f'Now write me a surname')
2022-01-24 20:24:56 +03:00
bot.set_state(message.from_user.id, MyStates.surname, message.chat.id)
with bot.retrieve_data(message.from_user.id, message.chat.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)
def ask_age(message):
2021-10-01 21:29:59 +03:00
"""
2022-01-24 20:34:50 +03:00
State 2. Will process when user's state is MyStates.surname.
2021-10-01 21:29:59 +03:00
"""
2021-09-25 15:12:32 +03:00
bot.send_message(message.chat.id, "What is your age?")
2022-01-24 20:24:56 +03:00
bot.set_state(message.from_user.id, MyStates.age, message.chat.id)
with bot.retrieve_data(message.from_user.id, message.chat.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)
def ready_for_answer(message):
2022-01-24 20:34:50 +03:00
"""
State 3. Will process when user's state is MyStates.age.
"""
2022-01-24 20:24:56 +03:00
with bot.retrieve_data(message.from_user.id, message.chat.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}\nSurname: {surname}\nAge: {age}</b>".format(name=data['name'], surname=data['surname'], age=message.text), parse_mode="html")
2022-01-24 20:24:56 +03:00
bot.delete_state(message.from_user.id, message.chat.id)
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)
def age_incorrect(message):
2022-01-24 20:34:50 +03:00
"""
Wrong response for MyStates.age
"""
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
bot.add_custom_filter(custom_filters.StateFilter(bot))
bot.add_custom_filter(custom_filters.IsDigitFilter())
bot.infinity_polling(skip_pending=True)