feat: Telegram-Webhook mit JSON-Validierung und Queue-Enqueue

This commit is contained in:
Hermes Agent
2026-06-22 06:37:31 +02:00
parent e382201878
commit 5a72f9ec80
3 changed files with 155 additions and 0 deletions
View File
+44
View File
@@ -0,0 +1,44 @@
"""Telegram-Webhook Blueprint."""
from flask import Blueprint, current_app, request
from pydantic import ValidationError
from src.models.submission import Submission
telegram = Blueprint("telegram", __name__)
@telegram.route("/webhook/telegram", methods=["POST"])
def webhook_telegram() -> tuple[dict[str, str], int]:
"""Empfängt Telegram-Updates via Webhook und reiht sie in die Queue ein."""
data = request.get_json(silent=True)
if data is None:
return {"error": "Ungültiges oder fehlendes JSON"}, 400
message = data.get("message")
if not isinstance(message, dict):
return {"error": "Feld 'message' fehlt oder ist ungültig"}, 400
message_id = message.get("message_id")
if not message_id:
return {"error": "Feld 'message_id' fehlt"}, 400
text = message.get("text")
if not text:
return {"error": "Feld 'text' fehlt oder ist leer"}, 400
chat = message.get("chat", {})
chat_id = chat.get("id") if isinstance(chat, dict) else None
try:
submission = Submission(
message_id=str(message_id),
plattform="telegram",
absender=str(chat_id) if chat_id else "unknown",
text=str(text),
)
except ValidationError:
return {"error": "Validierungsfehler"}, 400
current_app.queue_handler.enqueue(submission) # type: ignore[attr-defined]
return {"status": "ok"}, 200