import os from typing import Any, List from vkbottle_types.objects import UsersUserXtrCounters from vkbottle import ABCHandler, ABCView, BaseMiddleware, MiddlewareResponse, CtxStorage from vkbottle.bot import Bot, Message bot = Bot(os.environ["token"]) dummy_db = CtxStorage() class NoBotMiddleware(BaseMiddleware): async def pre(self, message: Message): return MiddlewareResponse(message.from_id > 0) class RegistrationMiddleware(BaseMiddleware): async def pre(self, message: Message): user = dummy_db.get(message.from_id) if user is None: user = (await bot.api.users.get(message.from_id))[0] dummy_db.set(message.from_id, user) return {"info": user} class InfoMiddleware(BaseMiddleware): async def post( self, message: Message, view: "ABCView",
from vkbottle import API, VKAPIError, ABCRequestRescheduler, CtxStorage from vkbottle.tools.test_utils import with_mocked_api import pytest import typing USERS_GET_RESPONSE = ( '{"response":[{"first_name":"Павел","id":1,"last_name":"Дуров",' '"can_access_closed":true,"is_closed":false}]}') ctx_storage = CtxStorage() class MockedRescheduler(ABCRequestRescheduler): def __init__(self, recent_response: typing.Any, final_response: dict): self.recent_response = recent_response self.final_response = final_response async def reschedule(self, *args) -> dict: assert args[3] == self.recent_response return self.final_response @pytest.mark.asyncio @with_mocked_api(USERS_GET_RESPONSE) async def test_api_raw_response(api: API): response = await api.request("users.get", {"user_ids": 1}) assert isinstance(response["response"], list) assert response["response"][0]["first_name"] == "Павел" @pytest.mark.asyncio
from vkbottle import CtxStorage ctx_storage = CtxStorage() ctx_storage.set("a", 100) # In any part of code in runtime print(CtxStorage().get("a")) # 100