Обновлены примеры использования Middleware
This commit is contained in:
parent
a7173b4371
commit
8d62d0d20a
@ -1,7 +1,7 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from typing import Any, Dict
|
from typing import Any, Awaitable, Callable, Dict
|
||||||
|
|
||||||
from maxapi import Bot, Dispatcher
|
from maxapi import Bot, Dispatcher
|
||||||
from maxapi.types import MessageCreated, Command, UpdateUnion
|
from maxapi.types import MessageCreated, Command, UpdateUnion
|
||||||
@ -15,14 +15,15 @@ dp = Dispatcher()
|
|||||||
|
|
||||||
class CustomDataForRouterMiddleware(BaseMiddleware):
|
class CustomDataForRouterMiddleware(BaseMiddleware):
|
||||||
async def __call__(
|
async def __call__(
|
||||||
self,
|
self,
|
||||||
event: UpdateUnion,
|
handler: Callable[[Any, Dict[str, Any]], Awaitable[Any]],
|
||||||
data: Dict[str, Any]
|
event_object: UpdateUnion,
|
||||||
):
|
data: Dict[str, Any],
|
||||||
|
) -> Any:
|
||||||
|
|
||||||
data['custom_data'] = f'Это ID того кто вызвал команду: {event.from_user.user_id}'
|
data['custom_data'] = f'Это ID того кто вызвал команду: {event_object.from_user.user_id}'
|
||||||
|
result = await handler(event_object, data)
|
||||||
return data
|
return result
|
||||||
|
|
||||||
|
|
||||||
@dp.message_created(Command('custom_data'))
|
@dp.message_created(Command('custom_data'))
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from typing import Any, Dict
|
from typing import Any, Awaitable, Callable, Dict
|
||||||
|
|
||||||
from maxapi import Bot, Dispatcher
|
from maxapi import Bot, Dispatcher
|
||||||
from maxapi.filters.middleware import BaseMiddleware
|
from maxapi.filters.middleware import BaseMiddleware
|
||||||
from maxapi.types import MessageCreated, Command, UpdateUnion
|
from maxapi.types import MessageCreated, Command, UpdateUnion
|
||||||
from maxapi.types.command import Command
|
|
||||||
|
|
||||||
logging.basicConfig(level=logging.INFO)
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
@ -16,30 +15,34 @@ dp = Dispatcher()
|
|||||||
|
|
||||||
class CheckChatTitleMiddleware(BaseMiddleware):
|
class CheckChatTitleMiddleware(BaseMiddleware):
|
||||||
async def __call__(
|
async def __call__(
|
||||||
self,
|
self,
|
||||||
event: UpdateUnion,
|
handler: Callable[[Any, Dict[str, Any]], Awaitable[Any]],
|
||||||
):
|
event_object: UpdateUnion,
|
||||||
|
data: Dict[str, Any],
|
||||||
|
) -> Any:
|
||||||
|
|
||||||
return event.chat.title == 'MAXApi'
|
if event_object.chat.title == 'MAXApi':
|
||||||
|
return await handler(event_object, data)
|
||||||
|
|
||||||
|
|
||||||
|
class CustomDataMiddleware(BaseMiddleware):
|
||||||
|
async def __call__(
|
||||||
|
self,
|
||||||
|
handler: Callable[[Any, Dict[str, Any]], Awaitable[Any]],
|
||||||
|
event_object: UpdateUnion,
|
||||||
|
data: Dict[str, Any],
|
||||||
|
) -> Any:
|
||||||
|
|
||||||
|
data['custom_data'] = f'Это ID того кто вызвал команду: {event_object.from_user.user_id}'
|
||||||
|
|
||||||
|
await handler(event_object, data)
|
||||||
|
|
||||||
|
|
||||||
@dp.message_created(Command('start'), CheckChatTitleMiddleware())
|
@dp.message_created(Command('start'), CheckChatTitleMiddleware())
|
||||||
async def start(event: MessageCreated):
|
async def start(event: MessageCreated):
|
||||||
await event.message.answer('Это сообщение было отправлено, так как ваш чат называется "MAXApi"!')
|
await event.message.answer('Это сообщение было отправлено, так как ваш чат называется "MAXApi"!')
|
||||||
|
|
||||||
|
|
||||||
class CustomDataMiddleware(BaseMiddleware):
|
|
||||||
async def __call__(
|
|
||||||
self,
|
|
||||||
event: UpdateUnion,
|
|
||||||
data: Dict[str, Any]
|
|
||||||
):
|
|
||||||
|
|
||||||
data['custom_data'] = f'Это ID того кто вызвал команду: {event.from_user.user_id}'
|
|
||||||
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@dp.message_created(Command('custom_data'), CustomDataMiddleware())
|
@dp.message_created(Command('custom_data'), CustomDataMiddleware())
|
||||||
async def custom_data(event: MessageCreated, custom_data: str):
|
async def custom_data(event: MessageCreated, custom_data: str):
|
||||||
await event.message.answer(custom_data)
|
await event.message.answer(custom_data)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user