|
| 1 | +"""Group settings handlers: setwelcome, etc.""" |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +from telegram import Update |
| 6 | +from telegram.constants import ChatMemberStatus |
| 7 | +from telegram.ext import CommandHandler, ContextTypes |
| 8 | + |
| 9 | +from ..services.captcha import CaptchaService |
| 10 | + |
| 11 | +logger = logging.getLogger(__name__) |
| 12 | + |
| 13 | + |
| 14 | +async def _is_admin( |
| 15 | + context: ContextTypes.DEFAULT_TYPE, chat_id: int, user_id: int |
| 16 | +) -> bool: |
| 17 | + """Check if user is admin in the chat.""" |
| 18 | + try: |
| 19 | + member = await context.bot.get_chat_member(chat_id, user_id) |
| 20 | + return member.status in ( |
| 21 | + ChatMemberStatus.ADMINISTRATOR, |
| 22 | + ChatMemberStatus.OWNER, |
| 23 | + ) |
| 24 | + except Exception: |
| 25 | + return False |
| 26 | + |
| 27 | + |
| 28 | +def create_settings_handlers() -> list: |
| 29 | + """Create group settings handlers.""" |
| 30 | + return [ |
| 31 | + CommandHandler("setwelcome", _handle_setwelcome), |
| 32 | + CommandHandler("resetwelcome", _handle_resetwelcome), |
| 33 | + CommandHandler("getwelcome", _handle_getwelcome), |
| 34 | + ] |
| 35 | + |
| 36 | + |
| 37 | +async def _handle_setwelcome( |
| 38 | + update: Update, context: ContextTypes.DEFAULT_TYPE |
| 39 | +) -> None: |
| 40 | + """Set custom welcome message for the group. |
| 41 | + |
| 42 | + Usage: /setwelcome <message> |
| 43 | + |
| 44 | + Supports placeholders: |
| 45 | + - {username}: @username or full name |
| 46 | + - {chatname}: group name |
| 47 | + |
| 48 | + Supports button syntax: |
| 49 | + - [Button Text](buttonurl://URL) |
| 50 | + """ |
| 51 | + captcha_service: CaptchaService = context.bot_data["captcha_service"] |
| 52 | + message = update.message |
| 53 | + if message is None or message.from_user is None: |
| 54 | + return |
| 55 | + |
| 56 | + chat = update.effective_chat |
| 57 | + if chat is None or chat.type == "private": |
| 58 | + await message.reply_text("Questo comando funziona solo nei gruppi.") |
| 59 | + return |
| 60 | + |
| 61 | + if not await _is_admin(context, chat.id, message.from_user.id): |
| 62 | + await message.reply_text( |
| 63 | + "Solo gli amministratori possono usare questo comando." |
| 64 | + ) |
| 65 | + return |
| 66 | + |
| 67 | + if message.text is None: |
| 68 | + return |
| 69 | + |
| 70 | + parts = message.text.split(maxsplit=1) |
| 71 | + if len(parts) < 2: |
| 72 | + await message.reply_text( |
| 73 | + "Uso: /setwelcome <messaggio>\n\n" |
| 74 | + "Placeholder disponibili:\n" |
| 75 | + " {username} - @username o nome completo\n" |
| 76 | + " {chatname} - nome del gruppo\n\n" |
| 77 | + "Sintassi bottoni:\n" |
| 78 | + " [Testo](buttonurl://URL)" |
| 79 | + ) |
| 80 | + return |
| 81 | + |
| 82 | + welcome_message = parts[1] |
| 83 | + await captcha_service.set_welcome_message(chat.id, welcome_message) |
| 84 | + await message.reply_text("Messaggio di benvenuto impostato!") |
| 85 | + logger.info( |
| 86 | + "Welcome message set for chat %s by admin %s", |
| 87 | + chat.id, |
| 88 | + message.from_user.id, |
| 89 | + ) |
| 90 | + |
| 91 | + |
| 92 | +async def _handle_resetwelcome( |
| 93 | + update: Update, context: ContextTypes.DEFAULT_TYPE |
| 94 | +) -> None: |
| 95 | + """Reset welcome message to default.""" |
| 96 | + captcha_service: CaptchaService = context.bot_data["captcha_service"] |
| 97 | + message = update.message |
| 98 | + if message is None or message.from_user is None: |
| 99 | + return |
| 100 | + |
| 101 | + chat = update.effective_chat |
| 102 | + if chat is None or chat.type == "private": |
| 103 | + await message.reply_text("Questo comando funziona solo nei gruppi.") |
| 104 | + return |
| 105 | + |
| 106 | + if not await _is_admin(context, chat.id, message.from_user.id): |
| 107 | + await message.reply_text( |
| 108 | + "Solo gli amministratori possono usare questo comando." |
| 109 | + ) |
| 110 | + return |
| 111 | + |
| 112 | + await captcha_service.set_welcome_message(chat.id, None) |
| 113 | + await message.reply_text("Messaggio di benvenuto ripristinato al default.") |
| 114 | + logger.info( |
| 115 | + "Welcome message reset for chat %s by admin %s", |
| 116 | + chat.id, |
| 117 | + message.from_user.id, |
| 118 | + ) |
| 119 | + |
| 120 | + |
| 121 | +async def _handle_getwelcome( |
| 122 | + update: Update, context: ContextTypes.DEFAULT_TYPE |
| 123 | +) -> None: |
| 124 | + """Show current welcome message.""" |
| 125 | + captcha_service: CaptchaService = context.bot_data["captcha_service"] |
| 126 | + message = update.message |
| 127 | + if message is None or message.from_user is None: |
| 128 | + return |
| 129 | + |
| 130 | + chat = update.effective_chat |
| 131 | + if chat is None or chat.type == "private": |
| 132 | + await message.reply_text("Questo comando funziona solo nei gruppi.") |
| 133 | + return |
| 134 | + |
| 135 | + if not await _is_admin(context, chat.id, message.from_user.id): |
| 136 | + await message.reply_text( |
| 137 | + "Solo gli amministratori possono usare questo comando." |
| 138 | + ) |
| 139 | + return |
| 140 | + |
| 141 | + custom_message = await captcha_service.get_welcome_message(chat.id) |
| 142 | + if custom_message: |
| 143 | + await message.reply_text( |
| 144 | + f"Messaggio di benvenuto attuale:\n\n{custom_message}" |
| 145 | + ) |
| 146 | + else: |
| 147 | + bot_username = (await context.bot.get_me()).username or "bot" |
| 148 | + default = captcha_service.get_default_welcome_template(bot_username) |
| 149 | + await message.reply_text( |
| 150 | + f"Nessun messaggio personalizzato. Default:\n\n{default}" |
| 151 | + ) |
0 commit comments