fix: логирование Telegram listener + strip @botname из команд
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -351,7 +351,7 @@ def _start_telegram_listener(cfg, get_db_fn):
|
|||||||
def handle_command(text: str):
|
def handle_command(text: str):
|
||||||
# Resolve button text to command
|
# Resolve button text to command
|
||||||
mapped = _button_map.get(text.strip().lower(), text.strip())
|
mapped = _button_map.get(text.strip().lower(), text.strip())
|
||||||
cmd = mapped.split()[0].lower()
|
cmd = mapped.split()[0].split("@")[0].lower() # strip @botname
|
||||||
if cmd == "/status":
|
if cmd == "/status":
|
||||||
conn = get_db_fn()
|
conn = get_db_fn()
|
||||||
report = format_status_report(cfg, conn)
|
report = format_status_report(cfg, conn)
|
||||||
@@ -365,14 +365,18 @@ def _start_telegram_listener(cfg, get_db_fn):
|
|||||||
_send_help(cfg)
|
_send_help(cfg)
|
||||||
|
|
||||||
def poll_loop():
|
def poll_loop():
|
||||||
|
import logging
|
||||||
|
log = logging.getLogger(__name__)
|
||||||
offset = 0
|
offset = 0
|
||||||
base_url = f"https://api.telegram.org/bot{cfg.telegram.bot_token}"
|
base_url = f"https://api.telegram.org/bot{cfg.telegram.bot_token}"
|
||||||
|
log.info("Telegram listener started")
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
resp = httpx.get(f"{base_url}/getUpdates",
|
resp = httpx.get(f"{base_url}/getUpdates",
|
||||||
params={"offset": offset, "timeout": 30},
|
params={"offset": offset, "timeout": 30},
|
||||||
timeout=35)
|
timeout=35)
|
||||||
if resp.status_code != 200:
|
if resp.status_code != 200:
|
||||||
|
log.warning(f"Telegram poll: HTTP {resp.status_code}")
|
||||||
time.sleep(5)
|
time.sleep(5)
|
||||||
continue
|
continue
|
||||||
updates = resp.json().get("result", [])
|
updates = resp.json().get("result", [])
|
||||||
@@ -382,8 +386,13 @@ def _start_telegram_listener(cfg, get_db_fn):
|
|||||||
text = msg.get("text", "")
|
text = msg.get("text", "")
|
||||||
chat_id = str(msg.get("chat", {}).get("id", ""))
|
chat_id = str(msg.get("chat", {}).get("id", ""))
|
||||||
if text and chat_id == cfg.telegram.chat_id:
|
if text and chat_id == cfg.telegram.chat_id:
|
||||||
handle_command(text)
|
log.info(f"Telegram cmd: {text}")
|
||||||
except Exception:
|
try:
|
||||||
|
handle_command(text)
|
||||||
|
except Exception as e:
|
||||||
|
log.error(f"Telegram command error: {e}", exc_info=True)
|
||||||
|
except Exception as e:
|
||||||
|
log.error(f"Telegram poll error: {e}")
|
||||||
time.sleep(10)
|
time.sleep(10)
|
||||||
|
|
||||||
t = threading.Thread(target=poll_loop, daemon=True)
|
t = threading.Thread(target=poll_loop, daemon=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user