Skip to content

Commit 2e5a070

Browse files
committed
feat(id-handler): add /id command to retrieve chat and user IDs
- Implemented a new handler for the /id command that responds with the current chat ID and user ID. - Integrated the ID handler into the main application initialization process.
1 parent a47a286 commit 2e5a070

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""ID handler: returns chat and user IDs."""
2+
3+
from telegram import Update
4+
from telegram.ext import CommandHandler, ContextTypes
5+
6+
7+
def create_id_handlers() -> list:
8+
"""Create the /id command handler."""
9+
return [CommandHandler("id", _handle_id)]
10+
11+
12+
async def _handle_id(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
13+
"""Reply with the current chat ID and user ID."""
14+
chat = update.effective_chat
15+
user = update.effective_user
16+
message = update.message
17+
18+
if chat is None or user is None or message is None:
19+
return
20+
21+
await message.reply_text(f"ID chat: {chat.id}\nID utente: {user.id}")

src/python_italy_bot/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from .config import Settings
88
from .db import create_repository
9+
from .handlers.id import create_id_handlers
910
from .handlers.moderation import create_moderation_handlers
1011
from .handlers.spam import create_spam_handler
1112
from .handlers.welcome import create_welcome_handlers
@@ -43,6 +44,8 @@ async def _post_init(application) -> None:
4344
application.add_handler(create_spam_handler(spam_detector))
4445
for handler in create_moderation_handlers(moderation_service):
4546
application.add_handler(handler)
47+
for handler in create_id_handlers():
48+
application.add_handler(handler)
4649

4750

4851
async def _post_shutdown(application) -> None:

src/python_italy_bot/services/spam_detector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(
2525
) -> None:
2626
self._max_messages_per_minute = max_messages_per_minute
2727
self._duplicate_threshold_seconds = duplicate_threshold_seconds
28-
self._message_history: dict[int, list[datetime]] = defaultdict(list)
28+
self._message_history: dict[int, list[datetime]] = defaultdict[int, list[datetime]](list)
2929
self._last_message_text: dict[int, tuple[str, datetime]] = {}
3030

3131
def _clean_old_entries(self, user_id: int) -> None:

0 commit comments

Comments
 (0)