feat: drop non-private Telegram updates early

This commit is contained in:
3252a8
2026-06-05 11:16:16 +03:00
parent 6dc43182ab
commit 516d699cf3
3 changed files with 52 additions and 0 deletions
@@ -77,6 +77,17 @@ class UpdateAntiFloodMiddleware(BaseMiddleware):
event: Update,
data: Dict[str, Any],
) -> Any:
if bool(getattr(self.settings, "TELEGRAM_DROP_NON_PRIVATE_UPDATES", True)):
chat_type = _message_or_callback_chat_type(event)
if chat_type is not None and chat_type != "private":
logger.info(
"Telegram update dropped outside private chat: chat_type=%s update_type=%s",
chat_type,
getattr(event, "event_type", "unknown"),
)
data["antiflood_dropped"] = True
return None
if not bool(getattr(self.settings, "TELEGRAM_ANTIFLOOD_ENABLED", True)):
return await handler(event, data)
@@ -159,6 +170,18 @@ def _update_actor_key(update: Update) -> Optional[str]:
return None
def _message_or_callback_chat_type(update: Update) -> Optional[str]:
if update.message and update.message.chat:
return str(update.message.chat.type)
if (
update.callback_query
and update.callback_query.message
and update.callback_query.message.chat
):
return str(update.callback_query.message.chat.type)
return None
def _update_action_key(update: Update) -> str:
if update.message:
text = update.message.text or ""