44
55from telegram import Update
66from telegram .constants import ChatMemberStatus
7- from telegram .ext import ChatMemberHandler , ContextTypes , MessageHandler , filters
7+ from telegram .ext import (
8+ ChatMemberHandler ,
9+ CommandHandler ,
10+ ContextTypes ,
11+ MessageHandler ,
12+ filters ,
13+ )
814
915from ..services .captcha import CaptchaService
1016
@@ -18,8 +24,9 @@ def create_welcome_handlers(captcha_service: CaptchaService) -> list:
1824 _handle_new_member ,
1925 ChatMemberHandler .CHAT_MEMBER ,
2026 ),
27+ CommandHandler ("start" , _handle_start ),
2128 MessageHandler (
22- filters .TEXT & filters .ChatType .PRIVATE ,
29+ filters .TEXT & filters .ChatType .PRIVATE & ~ filters . COMMAND ,
2330 _handle_private_message ,
2431 ),
2532 ]
@@ -38,7 +45,6 @@ async def _handle_new_member(
3845 new_status = result .new_chat_member .status
3946 old_status = result .old_chat_member .status if result .old_chat_member else None
4047
41- # Only handle new joins (not status changes from restricted to member, etc.)
4248 if new_status not in (ChatMemberStatus .MEMBER , ChatMemberStatus .RESTRICTED ):
4349 return
4450 if old_status in (
@@ -53,15 +59,12 @@ async def _handle_new_member(
5359 if user is None or chat is None :
5460 return
5561
56- # Skip if bot
5762 if user .is_bot :
5863 return
5964
60- # Skip if already verified
61- if await captcha_service .is_verified (user .id , chat .id ):
65+ if await captcha_service .is_globally_verified (user .id ):
6266 return
6367
64- # Restrict new member
6568 try :
6669 await context .bot .restrict_chat_member (
6770 chat_id = chat .id ,
@@ -72,25 +75,79 @@ async def _handle_new_member(
7275 logger .warning ("Could not restrict user %s in chat %s: %s" , user .id , chat .id , e )
7376 return
7477
75- # Record pending verification
7678 await captcha_service .add_pending (user .id , chat .id )
7779
78- # Send welcome message
79- welcome = captcha_service .get_welcome_message ()
80+ bot_me = await context .bot .get_me ()
81+ bot_username = bot_me .username or "bot"
82+
83+ custom_template = await captcha_service .get_welcome_message (chat .id )
84+ if custom_template :
85+ template = custom_template
86+ else :
87+ template = captcha_service .get_default_welcome_template (bot_username )
88+
89+ formatted = captcha_service .format_welcome_message (template , user , chat , bot_username )
90+ text , keyboard = captcha_service .parse_button_urls (formatted )
91+
8092 try :
8193 await context .bot .send_message (
8294 chat_id = chat .id ,
83- text = welcome ,
95+ text = text ,
96+ reply_markup = keyboard ,
8497 )
8598 except Exception as e :
8699 logger .warning ("Could not send welcome to chat %s: %s" , chat .id , e )
87100
88101
102+ async def _handle_start (
103+ update : Update ,
104+ context : ContextTypes .DEFAULT_TYPE ,
105+ ) -> None :
106+ """Handle /start command, including deep link for verification."""
107+ captcha_service : CaptchaService = context .bot_data ["captcha_service" ]
108+ message = update .message
109+ if message is None :
110+ return
111+
112+ chat = update .effective_chat
113+ if chat is None or chat .type != "private" :
114+ return
115+
116+ user = update .effective_user
117+ if user is None :
118+ return
119+
120+ args = context .args
121+ if args and args [0 ] == "verify" :
122+ rules_url = captcha_service .get_rules_url ()
123+ if rules_url :
124+ await message .reply_text (
125+ f"Per completare la verifica, leggi il regolamento:\n { rules_url } \n \n "
126+ "Dopo averlo letto, invia il comando segreto che troverai."
127+ )
128+ else :
129+ captcha_content = captcha_service .get_captcha_file_content ()
130+ if captcha_content :
131+ await message .reply_text (
132+ "Ecco il regolamento. Leggilo e invia il comando segreto che troverai:\n \n "
133+ f"{ captcha_content [:4000 ]} "
134+ )
135+ else :
136+ await message .reply_text (
137+ "Invia il comando segreto per completare la verifica."
138+ )
139+ else :
140+ await message .reply_text (
141+ "Ciao! Sono il bot di Python Italia.\n "
142+ "Se devi completare la verifica per un gruppo, usa il pulsante nel messaggio di benvenuto."
143+ )
144+
145+
89146async def _handle_private_message (
90147 update : Update ,
91148 context : ContextTypes .DEFAULT_TYPE ,
92149) -> None :
93- """Handle private messages: check for secret command and verify user."""
150+ """Handle private messages: check for secret command and verify user globally ."""
94151 captcha_service : CaptchaService = context .bot_data ["captcha_service" ]
95152 message = update .message
96153 if message is None or message .text is None :
@@ -102,21 +159,28 @@ async def _handle_private_message(
102159
103160 if not captcha_service .is_secret_command (message .text ):
104161 await message .reply_text (
105- "Comando non riconosciuto. Leggi il file delle regole del gruppo "
162+ "Comando non riconosciuto. Leggi il regolamento "
106163 "e invia il comando segreto che troverai."
107164 )
108165 return
109166
167+ if await captcha_service .is_globally_verified (user .id ):
168+ await message .reply_text (
169+ "Sei già verificato! Puoi partecipare alle discussioni in tutti i gruppi."
170+ )
171+ return
172+
110173 pending_chats = await captcha_service .get_pending_chats (user .id )
111174 if not pending_chats :
112175 await message .reply_text (
113- "Sei già verificato oppure non hai gruppi in attesa. "
114- "Se hai appena fatto il captcha, potrebbe essere già stato applicato ."
176+ "Non hai gruppi in attesa di verifica . "
177+ "Se hai appena inviato il comando, la verifica potrebbe essere già stata applicata ."
115178 )
116179 return
117180
181+ await captcha_service .verify_user_globally (user .id )
182+
118183 for chat_id in pending_chats :
119- await captcha_service .verify_user (user .id , chat_id )
120184 try :
121185 await context .bot .restrict_chat_member (
122186 chat_id = chat_id ,
@@ -129,5 +193,6 @@ async def _handle_private_message(
129193 )
130194
131195 await message .reply_text (
132- "Verifica completata! Ora puoi partecipare alle discussioni nei gruppi Python Italia."
196+ "Verifica completata! Ora puoi partecipare alle discussioni "
197+ "in tutti i gruppi Python Italia."
133198 )
0 commit comments