-
Notifications
You must be signed in to change notification settings - Fork 1
/
middlewares.py
55 lines (43 loc) · 1.57 KB
/
middlewares.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import asyncio
from aiogram.types import Message, CallbackQuery
from typing import Any, Callable, Dict, Awaitable
from aiogram import types, BaseMiddleware
from pymongo import ReturnDocument
from mongo import db
import secrets
import traceback
class UserMiddleware(BaseMiddleware):
def __init__(self) -> None:
super().__init__()
async def __call__(
self,
handler: Callable[[Message or CallbackQuery, Dict[str, Any]], Awaitable[Any]],
event: Message or CallbackQuery,
data: Dict[str, Any]
) -> Any:
chat_id = event.chat.id if isinstance(event, Message) else event.message.chat.id if event.message else int(event.chat_instance)
user: types.User = event.from_user
db_user = await db.users.find_one_and_update(
{"chat_id": chat_id},
{"$setOnInsert": {
},
"$set": {
'first_name': user.first_name,
'last_name': user.last_name,
'username': user.username,
},
},
upsert=True,
return_document=ReturnDocument.AFTER)
data['omi_id'] = db_user.get('omi_id')
return await handler(event, data)
class QueryAnswerMiddleware(BaseMiddleware):
async def __call__(
self,
handler: Callable[[CallbackQuery, Dict[str, Any]], Awaitable[Any]],
event: CallbackQuery,
data: Dict[str, Any]
) -> Any:
result = await handler(event, data)
await event.answer()
return result