feat(bot): proceseaza doar comenzi, ignora restul mesajelor
handle_command trata orice mesaj din grup: la un simplu emoji raspundea cu "COMANDA NECUNOSCUTA" sau, pentru useri neautorizati, cu "ACCES INTERZIS" - spam in grup si verificari de membership inutile. Acum se proceseaza doar mesajele care incep cu "/". Singura exceptie e raspunsul numeric la meniul de tranzactii, si doar pentru userul care are o selectie pendinte. Mesajele fara text (poze, stickere) nu mai ajung in ramura de comanda necunoscuta. In plus, comenzile adresate explicit altui bot din grup (/scrape@alt_bot) sunt ignorate - username-ul propriu se afla la pornire prin getMe. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YbuWZ699EFeYfPA3nNraa9
This commit is contained in:
@@ -61,9 +61,26 @@ class TelegramTriggerBot:
|
|||||||
logging.info(f"Grup autorizat: {self.allowed_group_id}")
|
logging.info(f"Grup autorizat: {self.allowed_group_id}")
|
||||||
logging.info(f"Long polling timeout: {self.poll_timeout}s")
|
logging.info(f"Long polling timeout: {self.poll_timeout}s")
|
||||||
|
|
||||||
|
# Username-ul botului - necesar ca sa ignoram comenzile adresate
|
||||||
|
# explicit altui bot din acelasi grup (/scrape@alt_bot)
|
||||||
|
self.bot_username = self._get_bot_username()
|
||||||
|
|
||||||
# Înregistrare comenzi în meniul Telegram
|
# Înregistrare comenzi în meniul Telegram
|
||||||
self._register_commands()
|
self._register_commands()
|
||||||
|
|
||||||
|
def _get_bot_username(self):
|
||||||
|
"""Returnează username-ul botului (fără @) sau None dacă apelul eșuează"""
|
||||||
|
try:
|
||||||
|
response = requests.get(f"{self.base_url}/getMe", timeout=10)
|
||||||
|
if response.status_code == 200 and response.json().get('ok'):
|
||||||
|
username = response.json()['result'].get('username')
|
||||||
|
logging.info(f"Bot username: @{username}")
|
||||||
|
return username
|
||||||
|
logging.warning(f"Nu am putut afla username-ul botului: {response.text}")
|
||||||
|
except Exception as e:
|
||||||
|
logging.warning(f"Eroare getMe: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
def _register_commands(self):
|
def _register_commands(self):
|
||||||
"""Înregistrează comenzile bot în meniul Telegram (pentru DM și grupuri)"""
|
"""Înregistrează comenzile bot în meniul Telegram (pentru DM și grupuri)"""
|
||||||
try:
|
try:
|
||||||
@@ -773,16 +790,36 @@ class TelegramTriggerBot:
|
|||||||
chat_title = message['chat'].get('title', 'DM')
|
chat_title = message['chat'].get('title', 'DM')
|
||||||
user_id = message['from']['id']
|
user_id = message['from']['id']
|
||||||
username = message['from'].get('username', 'Unknown')
|
username = message['from'].get('username', 'Unknown')
|
||||||
text = message.get('text', '')
|
text = (message.get('text') or '').strip()
|
||||||
message_id = message.get('message_id')
|
message_id = message.get('message_id')
|
||||||
|
|
||||||
# Normalizează comanda - elimină @username pentru grupuri (ex: /scrape@botname → /scrape)
|
|
||||||
if '@' in text:
|
|
||||||
text = text.split('@')[0]
|
|
||||||
|
|
||||||
# Log context
|
|
||||||
context = (f"grup '{chat_title}' ({chat_id})"
|
context = (f"grup '{chat_title}' ({chat_id})"
|
||||||
if chat_type in ['group', 'supergroup'] else f"DM ({chat_id})")
|
if chat_type in ['group', 'supergroup'] else f"DM ({chat_id})")
|
||||||
|
|
||||||
|
# Botul reactioneaza DOAR la comenzi. Singura exceptie: raspunsul numeric
|
||||||
|
# la meniul de tranzactii, cerut explicit de bot userului respectiv.
|
||||||
|
# Restul (mesaje de conversatie, emoji, poze, stickere) sunt ignorate fara
|
||||||
|
# niciun raspuns - altfel botul spamuia grupul cu "COMANDA NECUNOSCUTA"
|
||||||
|
# sau "ACCES INTERZIS" la fiecare mesaj.
|
||||||
|
is_command = text.startswith('/')
|
||||||
|
awaiting_selection = (user_id in self.pending_account_selection
|
||||||
|
and text[:1].isdigit())
|
||||||
|
|
||||||
|
if not is_command and not awaiting_selection:
|
||||||
|
logging.debug(f"Ignorat (nu e comanda) de la {username} (ID: {user_id}) în {context}")
|
||||||
|
return
|
||||||
|
|
||||||
|
# Normalizează comanda - elimină @username (ex: /scrape@botname → /scrape)
|
||||||
|
# si ignora comenzile adresate explicit altui bot din grup
|
||||||
|
if is_command:
|
||||||
|
first_token = text.split()[0]
|
||||||
|
command, sep, target = first_token.partition('@')
|
||||||
|
if sep:
|
||||||
|
if self.bot_username and target.lower() != self.bot_username.lower():
|
||||||
|
logging.debug(f"Ignorat - comanda adresata lui @{target} în {context}")
|
||||||
|
return
|
||||||
|
text = command + text[len(first_token):]
|
||||||
|
|
||||||
logging.info(f"Mesaj de la {username} (ID: {user_id}) în {context}: {text}")
|
logging.info(f"Mesaj de la {username} (ID: {user_id}) în {context}: {text}")
|
||||||
|
|
||||||
# Verifică autorizare
|
# Verifică autorizare
|
||||||
|
|||||||
Reference in New Issue
Block a user