Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "id" property to State instances #2398

Closed
wants to merge 1 commit into from

Conversation

Artin-GH
Copy link
Contributor

Description

Added "id" property to State instances to prevent potential bugs

Describe your tests

I have provided screenshots from the bot in the next sections

Python version: 3.11.0

OS: macOS Sonoma 14.6.1

Example Scenario

image

File: menus/__init__.py

from . import main, admin

File: __main__.py

from telebot import TeleBot, custom_filters, types
from telebot.states.sync.context import StateContext
from telebot.storage import StateMemoryStorage
from telebot.states.sync.middleware import StateMiddleware
import menus

state_storage = StateMemoryStorage()
bot = TeleBot(
    "1913942518:AAEWPg2_WOUmKswDGudTellqHGfNZaEoCak",
    state_storage=state_storage,
    use_class_middlewares=True)
bot.add_custom_filter(custom_filters.StateFilter(bot))
bot.setup_middleware(StateMiddleware(bot))

@bot.message_handler(commands=["main"])
def main_menu(msg: types.Message, state: StateContext):
    new_state = menus.main.MenuState.ANY_MSG
    state.set(new_state)
    bot.reply_to(msg, f'State changed to main.MenuState.ANY_MSG\nName: {new_state.name}')

@bot.message_handler(commands=["admin"])
def admin_menu(msg: types.Message, state: StateContext):
    new_state = menus.admin.MenuState.ANY_MSG
    state.set(new_state)
    bot.reply_to(msg, f'State changed to admin.MenuState.ANY_MSG\nName: {new_state.name}')

menus.main.register_handlers(bot)
menus.admin.register_handlers(bot)

bot.infinity_polling()

File: menus/main.py

from telebot import TeleBot, types
from telebot.states import State, StatesGroup
from telebot.states.sync.context import StateContext

class MenuState(StatesGroup):
    ANY_MSG = State()

def register_handlers(bot: TeleBot):
    bot.register_message_handler(
        lambda msg, state: send_info(bot, msg, state),
        state=MenuState.ANY_MSG)

def send_info(bot: TeleBot, msg: types.Message, state: StateContext):
    bot.reply_to(msg, 'This is send_info from menus.main')
    state.delete()

File: menus/admin.py

from telebot import TeleBot, types
from telebot.states import State, StatesGroup
from telebot.states.sync.context import StateContext

class MenuState(StatesGroup):
    ANY_MSG = State()

def register_handlers(bot: TeleBot):
    bot.register_message_handler(
        lambda msg, state: send_info(bot, msg, state),
        state=MenuState.ANY_MSG)

def send_info(bot: TeleBot, msg: types.Message, state: StateContext):
    bot.reply_to(msg, 'This is send_info from menus.admin')
    state.delete()

Before

image

After

image

@coder2020official
Copy link
Collaborator

useless

@Artin-GH Artin-GH deleted the patch-2 branch September 26, 2024 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants