-
-
Notifications
You must be signed in to change notification settings - Fork 806
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[3.0] Bot API 5.1 + FSM + Utils (#525)
* Regenerate corresponding to Bot API 5.1 * Added base of FSM. Markup constructor and small refactoring * Fix dependencies * Fix mypy windows error * Move StatesGroup.get_root() from meta to class * Fixed chat and user constraints * Update pipeline * Remove docs pipeline * Added GLOBAL_USER FSM strategy * Reformat code * Fixed Dispatcher._process_update * Bump Bot API 5.2. Added integration with MagicFilter * Coverage
- Loading branch information
1 parent
a6f824a
commit 0e72d8e
Showing
265 changed files
with
2,921 additions
and
1,324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
4.9 | ||
5.1 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Any, Dict, Optional | ||
|
||
from aiogram.dispatcher.fsm.storage.base import BaseStorage, StateType | ||
|
||
|
||
class FSMContext: | ||
def __init__(self, storage: BaseStorage, chat_id: int, user_id: int) -> None: | ||
self.storage = storage | ||
self.chat_id = chat_id | ||
self.user_id = user_id | ||
|
||
async def set_state(self, state: StateType = None) -> None: | ||
await self.storage.set_state(chat_id=self.chat_id, user_id=self.user_id, state=state) | ||
|
||
async def get_state(self) -> Optional[str]: | ||
return await self.storage.get_state(chat_id=self.chat_id, user_id=self.user_id) | ||
|
||
async def set_data(self, data: Dict[str, Any]) -> None: | ||
await self.storage.set_data(chat_id=self.chat_id, user_id=self.user_id, data=data) | ||
|
||
async def get_data(self) -> Dict[str, Any]: | ||
return await self.storage.get_data(chat_id=self.chat_id, user_id=self.user_id) | ||
|
||
async def update_data( | ||
self, data: Optional[Dict[str, Any]] = None, **kwargs: Any | ||
) -> Dict[str, Any]: | ||
if data: | ||
kwargs.update(data) | ||
return await self.storage.update_data( | ||
chat_id=self.chat_id, user_id=self.user_id, data=kwargs | ||
) | ||
|
||
async def clear(self) -> None: | ||
await self.set_state(state=None) | ||
await self.set_data({}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from typing import Any, Awaitable, Callable, Dict, Optional | ||
|
||
from aiogram.dispatcher.fsm.context import FSMContext | ||
from aiogram.dispatcher.fsm.storage.base import BaseStorage | ||
from aiogram.dispatcher.fsm.strategy import FSMStrategy, apply_strategy | ||
from aiogram.dispatcher.middlewares.base import BaseMiddleware | ||
from aiogram.types import Update | ||
|
||
|
||
class FSMContextMiddleware(BaseMiddleware[Update]): | ||
def __init__( | ||
self, | ||
storage: BaseStorage, | ||
strategy: FSMStrategy = FSMStrategy.USER_IN_CHAT, | ||
isolate_events: bool = True, | ||
) -> None: | ||
self.storage = storage | ||
self.strategy = strategy | ||
self.isolate_events = isolate_events | ||
|
||
async def __call__( | ||
self, | ||
handler: Callable[[Update, Dict[str, Any]], Awaitable[Any]], | ||
event: Update, | ||
data: Dict[str, Any], | ||
) -> Any: | ||
context = self._resolve_context(data) | ||
data["fsm_storage"] = self.storage | ||
if context: | ||
data.update({"state": context, "raw_state": await context.get_state()}) | ||
if self.isolate_events: | ||
async with self.storage.lock(): | ||
return await handler(event, data) | ||
return await handler(event, data) | ||
|
||
def _resolve_context(self, data: Dict[str, Any]) -> Optional[FSMContext]: | ||
user = data.get("event_from_user") | ||
chat = data.get("event_chat") | ||
chat_id = chat.id if chat else None | ||
user_id = user.id if user else None | ||
|
||
if chat_id is None: | ||
chat_id = user_id | ||
|
||
if chat_id is not None and user_id is not None: | ||
chat_id, user_id = apply_strategy( | ||
chat_id=chat_id, user_id=user_id, strategy=self.strategy | ||
) | ||
return self.get_context(chat_id=chat_id, user_id=user_id) | ||
return None | ||
|
||
def get_context(self, chat_id: int, user_id: int) -> FSMContext: | ||
return FSMContext(storage=self.storage, chat_id=chat_id, user_id=user_id) |
Oops, something went wrong.