fix(telegram): async webhook handler + deduplicate callback responses

- Webhook returns 200 immediately, processing runs in background task
  → Telegram no longer retries, eliminates duplicate callbacks and 400 errors
- Consolidate answer_callback_query call to top of handler (before heavy work)
- Add logger.info/error for callback actions to aid debugging

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
OliverGiertz 2026-03-21 11:08:32 +00:00
parent 1020526e76
commit e9c472b722
2 changed files with 31 additions and 15 deletions

View file

@ -677,7 +677,14 @@ def api_n8n_ingest(request: Request) -> dict:
@app.post("/telegram/webhook")
async def telegram_webhook(request: Request) -> dict:
"""Receive updates from Telegram Bot API."""
"""Receive updates from Telegram Bot API.
Returns 200 immediately so Telegram never retries the same update.
Actual processing runs in a background task.
"""
import asyncio
import logging
# Verify secret token
secret = settings.telegram_webhook_secret
if secret:
@ -691,12 +698,14 @@ async def telegram_webhook(request: Request) -> dict:
except Exception:
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Invalid JSON")
try:
handle_update(update)
except Exception as exc:
import logging
logging.getLogger(__name__).error("Telegram update handler error: %s", exc)
async def _process():
loop = asyncio.get_event_loop()
try:
await loop.run_in_executor(None, lambda: handle_update(update))
except Exception as exc:
logging.getLogger(__name__).error("Telegram update handler error: %s", exc)
asyncio.create_task(_process())
return {"ok": True}