u.context = context return await u.save() await UserState.create(uid=uid, branch=branch, context=context) async def all_users(self): """This method should return user_ids of all stated users""" return [u.uid async for u in UserState.all()] async def delete_user(self, uid: int): """This method should delete the user's bot from the database""" u = await UserState.get(uid=uid) await u.delete() bot = Bot(os.environ.get("token")) bot.branch = SqliteBranch() class StoredBranch(ClsBranch): @rule_disposal(VBMLRule("/говорить <word>")) async def say(self, ans: Message, word: str): self.context["word"] = word return f"Теперь я буду говорить слово: {word}" @rule_disposal(VBMLRule("/остановить")) async def stop(self, ans: Message): await bot.branch.exit(ans.peer_id) return "Бранч остановлен!" async def branch(self, ans: Message, *args): if "word" not in self.context:
from vkbottle import Bot, Message from vkbottle import PhotoUploader from dotenv import load_dotenv from config import Config from routes import main, mentors, news, faq, schedule from models import DBStoredBranch, UserState from utils import init_db from vkbottle.ext import Middleware import os load_dotenv() token = os.environ.get('TOKEN') bot = Bot(tokens=[token], debug=Config.DEBUG) bot.branch = DBStoredBranch() photo_uploader = PhotoUploader(bot.api, generate_attachment_strings=True) @bot.middleware.middleware_handler() class CheckUserStateMiddleware(Middleware): async def pre(self, msg: Message, *args): if not Config.USE_MIDDLEWARE: return u = await UserState.filter(uid=msg.from_id).get_or_none() if u is None: u = await UserState.create( uid=msg.from_id, branch='main', context={}