-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.py
More file actions
37 lines (27 loc) · 965 Bytes
/
Copy pathmiddleware.py
File metadata and controls
37 lines (27 loc) · 965 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
25
26
27
28
29
30
31
32
33
34
35
36
# middleware.py
from functools import wraps
def require_session(handler):
"""
Middleware que exige una sesión válida en chat privado.
"""
@wraps(handler)
async def wrapper(update, context, *args, **kwargs):
chat = update.effective_chat
# Solo permitimos chat privado
if chat.type != "private":
return
session_manager = context.application.bot_data.get("session_manager")
if not session_manager:
# Error de configuración del bot
await context.bot.send_message(
chat_id=chat.id,
text="Error interno. Inténtalo más tarde."
)
return
session = session_manager.get(chat.id)
if not session:
return
# Inyectamos la sesión en el contexto para el handler
context.session = session
return await handler(update, context, *args, **kwargs)
return wrapper