Skip to content

Commit 23b764e

Browse files
committed
feat(moderation): enhance report handling and notification system
- Improved the report handling process by introducing a dedicated function to notify admins of reports. - Added user display name retrieval and message link generation for better report context. - Enhanced error handling when fetching chat administrators for report notifications. - Updated the report response to include detailed information about the reported user and the reporter.
1 parent 6882c4c commit 23b764e

1 file changed

Lines changed: 78 additions & 2 deletions

File tree

src/python_italy_bot/handlers/moderation.py

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,14 @@ async def _handle_report(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
276276

277277
reported_user_id: int | None = None
278278
message_id: int | None = None
279+
reported_user = None
279280

280281
if message.reply_to_message and message.reply_to_message.from_user:
281-
reported_user_id = message.reply_to_message.from_user.id
282+
reported_user = message.reply_to_message.from_user
283+
reported_user_id = reported_user.id
282284
message_id = message.reply_to_message.message_id
283285

284-
if reported_user_id is None:
286+
if reported_user_id is None or reported_user is None:
285287
await message.reply_text(
286288
"Rispondi al messaggio da segnalare con /report [motivo]"
287289
)
@@ -295,6 +297,15 @@ async def _handle_report(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
295297
reason=reason,
296298
)
297299

300+
await _notify_admins_of_report(
301+
context=context,
302+
chat=chat,
303+
reporter=message.from_user,
304+
reported_user=reported_user,
305+
message_id=message_id,
306+
reason=reason,
307+
)
308+
298309
await message.reply_text(
299310
"Segnalazione inviata. Gli amministratori la esamineranno."
300311
)
@@ -306,6 +317,71 @@ async def _handle_report(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
306317
)
307318

308319

320+
def _get_user_display_name(user) -> str:
321+
"""Get display name for a user (full name or username)."""
322+
if user.first_name:
323+
name = user.first_name
324+
if user.last_name:
325+
name += f" {user.last_name}"
326+
return name
327+
return user.username or str(user.id)
328+
329+
330+
def _build_message_link(chat, message_id: int | None) -> str | None:
331+
"""Build a link to a message in a chat."""
332+
if message_id is None:
333+
return None
334+
if chat.username:
335+
return f"https://t.me/{chat.username}/{message_id}"
336+
chat_id_str = str(chat.id)
337+
if chat_id_str.startswith("-100"):
338+
chat_id_str = chat_id_str[4:]
339+
return f"https://t.me/c/{chat_id_str}/{message_id}"
340+
341+
342+
async def _notify_admins_of_report(
343+
context: ContextTypes.DEFAULT_TYPE,
344+
chat,
345+
reporter,
346+
reported_user,
347+
message_id: int | None,
348+
reason: str | None,
349+
) -> None:
350+
"""Send report notification to all chat admins via private message."""
351+
try:
352+
admins = await context.bot.get_chat_administrators(chat.id)
353+
except Exception as e:
354+
logger.warning("Failed to get admins for report notification: %s", e)
355+
return
356+
357+
chat_title = chat.title or "Chat"
358+
reporter_name = _get_user_display_name(reporter)
359+
reported_name = _get_user_display_name(reported_user)
360+
message_link = _build_message_link(chat, message_id)
361+
362+
report_text = f"<b>{chat_title}:</b>\n"
363+
report_text += f'Reported user: <a href="tg://user?id={reported_user.id}">{reported_name}</a> ({reported_user.id})\n'
364+
report_text += f'Reported by: <a href="tg://user?id={reporter.id}">{reporter_name}</a> ({reporter.id})\n'
365+
if message_link:
366+
report_text += f'Link: <a href="{message_link}">qui</a>\n'
367+
if reason:
368+
report_text += f"Reason: {reason}"
369+
370+
for admin in admins:
371+
if admin.user.is_bot:
372+
continue
373+
try:
374+
await context.bot.send_message(
375+
chat_id=admin.user.id,
376+
text=report_text,
377+
parse_mode="HTML",
378+
)
379+
except Exception as e:
380+
logger.debug(
381+
"Could not send report to admin %s: %s", admin.user.id, e
382+
)
383+
384+
309385
async def _resolve_user_id(
310386
context: ContextTypes.DEFAULT_TYPE,
311387
chat_id: int,

0 commit comments

Comments
 (0)