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

91 lines
3.0 KiB
Python
Raw Normal View History

2021-11-27 21:41:39 +03:00
from telebot import asyncio_filters
from telebot.async_telebot import AsyncTeleBot
2022-01-24 20:24:56 +03:00
# list of storages, you can use any storage
2022-01-24 22:38:35 +03:00
from telebot.asyncio_storage import StateMemoryStorage
2022-01-24 20:24:56 +03:00
# new feature for states.
from telebot.asyncio_handler_backends import State, StatesGroup
2022-01-24 20:28:10 +03:00
# default state storage is statememorystorage
2022-01-24 20:28:56 +03:00
bot = AsyncTeleBot('TOKEN', state_storage=StateMemoryStorage())
2021-11-27 21:41:39 +03:00
2022-01-24 20:24:56 +03:00
# Just create different statesgroup
class MyStates(StatesGroup):
name = State() # statesgroup should contain states
surname = State()
age = State()
2021-11-27 21:41:39 +03:00
2022-01-24 20:34:50 +03:00
# set_state -> sets a new state
# delete_state -> delets state if exists
# get_state -> returns state if exists
2021-11-27 21:41:39 +03:00
@bot.message_handler(commands=['start'])
async def start_ex(message):
"""
Start command. Here we are starting state
"""
2022-01-24 20:24:56 +03:00
await bot.set_state(message.from_user.id, MyStates.name, message.chat.id)
2021-11-27 21:41:39 +03:00
await bot.send_message(message.chat.id, 'Hi, write me a name')
@bot.message_handler(state="*", commands='cancel')
async def any_state(message):
"""
Cancel state
"""
await bot.send_message(message.chat.id, "Your state was cancelled.")
2022-01-24 20:24:56 +03:00
await bot.delete_state(message.from_user.id, message.chat.id)
2021-11-27 21:41:39 +03:00
@bot.message_handler(state=MyStates.name)
async def name_get(message):
"""
2022-01-24 20:34:50 +03:00
State 1. Will process when user's state is MyStates.name.
2021-11-27 21:41:39 +03:00
"""
await bot.send_message(message.chat.id, f'Now write me a surname')
2022-01-24 20:24:56 +03:00
await bot.set_state(message.from_user.id, MyStates.surname, message.chat.id)
async with bot.retrieve_data(message.from_user.id, message.chat.id) as data:
2021-11-27 21:41:39 +03:00
data['name'] = message.text
@bot.message_handler(state=MyStates.surname)
async def ask_age(message):
"""
2022-01-24 20:34:50 +03:00
State 2. Will process when user's state is MyStates.surname.
2021-11-27 21:41:39 +03:00
"""
await bot.send_message(message.chat.id, "What is your age?")
2022-01-24 20:24:56 +03:00
await bot.set_state(message.from_user.id, MyStates.age, message.chat.id)
async with bot.retrieve_data(message.from_user.id, message.chat.id) as data:
2021-11-27 21:41:39 +03:00
data['surname'] = message.text
# result
@bot.message_handler(state=MyStates.age, is_digit=True)
async 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
async with bot.retrieve_data(message.from_user.id, message.chat.id) as data:
2021-11-27 21:41:39 +03:00
await 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
await bot.delete_state(message.from_user.id, message.chat.id)
2021-11-27 21:41:39 +03:00
#incorrect number
@bot.message_handler(state=MyStates.age, is_digit=False)
async def age_incorrect(message):
2022-01-24 20:34:50 +03:00
"""
Will process for wrong input when state is MyState.age
"""
2021-11-27 21:41:39 +03:00
await 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(asyncio_filters.StateFilter(bot))
bot.add_custom_filter(asyncio_filters.IsDigitFilter())
2021-12-12 13:07:30 +03:00
import asyncio
asyncio.run(bot.polling())