fix(BATON-007): add validate_bot_token() for startup detection and fix test mocks
- Add validate_bot_token() to backend/telegram.py: calls getMe on startup, logs ERROR if token is invalid (never raises per #1215 contract) - Call validate_bot_token() in lifespan() after db.init_db() for early detection - Update conftest.py make_app_client() to mock getMe endpoint - Add 3 tests for validate_bot_token (200, 401, network error cases) Root cause: CHAT_ID=5190015988 (positive) was wrong — fixed to -5190015988 on server per decision #1212. Group "Big Red Button" confirmed via getChat. Service restarted. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
cbc15eeedc
commit
a2b38ef815
4 changed files with 75 additions and 1 deletions
|
|
@ -14,6 +14,25 @@ logger = logging.getLogger(__name__)
|
|||
_TELEGRAM_API = "https://api.telegram.org/bot{token}/{method}"
|
||||
|
||||
|
||||
async def validate_bot_token() -> bool:
|
||||
"""Validate BOT_TOKEN by calling getMe. Logs ERROR if invalid. Never raises."""
|
||||
url = _TELEGRAM_API.format(token=config.BOT_TOKEN, method="getMe")
|
||||
async with httpx.AsyncClient(timeout=10) as client:
|
||||
try:
|
||||
resp = await client.get(url)
|
||||
if resp.status_code == 200:
|
||||
bot_name = resp.json().get("result", {}).get("username", "?")
|
||||
logger.info("Telegram token valid, bot: @%s", bot_name)
|
||||
return True
|
||||
logger.error(
|
||||
"BOT_TOKEN invalid — getMe returned %s: %s", resp.status_code, resp.text
|
||||
)
|
||||
return False
|
||||
except Exception as exc:
|
||||
logger.error("BOT_TOKEN validation failed (network): %s", exc)
|
||||
return False
|
||||
|
||||
|
||||
async def send_message(text: str) -> None:
|
||||
url = _TELEGRAM_API.format(token=config.BOT_TOKEN, method="sendMessage")
|
||||
async with httpx.AsyncClient(timeout=10) as client:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue