-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.py
More file actions
24 lines (22 loc) · 890 Bytes
/
middleware.py
File metadata and controls
24 lines (22 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import os
from aiogram import BaseMiddleware
from aiogram.types import Message, CallbackQuery
from typing import Callable, Dict, Any, Awaitable
class AuthMiddleware(BaseMiddleware):
def __init__(self, allowed_ids: list[int]):
self.allowed_ids = allowed_ids
super().__init__()
async def __call__(
self,
handler: Callable[[Message, Dict[str, Any]], Awaitable[Any]],
event: Message,
data: Dict[str, Any]
) -> Any:
user_id = getattr(event, "from_user", None) and event.from_user.id
if user_id not in self.allowed_ids:
if isinstance(event, Message):
await event.answer("Access denied, do not do it again")
elif isinstance(event, CallbackQuery):
await event.answer("Access denied, do not do it again")
return
return await handler(event, data)