feat: Message-Intake-Service mit Kommando-Erkennung (closes #2)
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
"""Message-Intake-Service."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from enum import StrEnum
|
||||
|
||||
from src.models.submission import Submission
|
||||
|
||||
|
||||
class KommandoTyp(StrEnum):
|
||||
"""Bekannte Benutzer-Kommandos."""
|
||||
|
||||
SPIEL_SUCHEN = "spiel_suchen"
|
||||
SPIEL_BEWERTEN = "spiel_bewerten"
|
||||
HILFE = "hilfe"
|
||||
UNBEKANNT = "unbekannt"
|
||||
|
||||
|
||||
def erkenne_kommando(text: str) -> KommandoTyp:
|
||||
"""Erkennt das Kommando aus dem Nachrichtentext.
|
||||
|
||||
Args:
|
||||
text: Der Nachrichtentext.
|
||||
|
||||
Returns:
|
||||
Das erkannte KommandoTyp-Enum.
|
||||
"""
|
||||
text_lower = text.lower().strip()
|
||||
|
||||
if text_lower.startswith("suche") or text_lower.startswith("finde"):
|
||||
return KommandoTyp.SPIEL_SUCHEN
|
||||
if text_lower.startswith("bewerte") or text_lower.startswith("bewertung"):
|
||||
return KommandoTyp.SPIEL_BEWERTEN
|
||||
if text_lower in ("hilfe", "help", "?", "commands"):
|
||||
return KommandoTyp.HILFE
|
||||
|
||||
return KommandoTyp.UNBEKANNT
|
||||
|
||||
|
||||
def extrahiere_suchbegriff(text: str) -> str | None:
|
||||
"""Extrahiert den Suchbegriff aus einer Such-Nachricht.
|
||||
|
||||
Args:
|
||||
text: Der Nachrichtentext (z.B. "suche Catan").
|
||||
|
||||
Returns:
|
||||
Der Suchbegriff ohne Kommando-Präfix, oder None.
|
||||
"""
|
||||
for praefix in ("suche ", "finde "):
|
||||
if text.lower().startswith(praefix):
|
||||
begriff = text[len(praefix) :].strip()
|
||||
if begriff:
|
||||
return begriff
|
||||
return None
|
||||
|
||||
|
||||
def verarbeite_submission(submission: Submission) -> dict[str, object]:
|
||||
"""Verarbeitet eine Submission und gibt eine Antwort-Struktur zurück.
|
||||
|
||||
Args:
|
||||
submission: Die eingegangene Submission.
|
||||
|
||||
Returns:
|
||||
Ein Dict mit 'antwort' und 'kommando'-Keys.
|
||||
"""
|
||||
kommando = erkenne_kommando(submission.text)
|
||||
|
||||
if kommando == KommandoTyp.HILFE:
|
||||
return {
|
||||
"kommando": kommando.value,
|
||||
"antwort": (
|
||||
"📋 Verfügbare Kommandos:\n"
|
||||
"• suche <spielname> — Spiel suchen\n"
|
||||
"• bewerte <spielname> — Spiel bewerten\n"
|
||||
"• hilfe — Diese Hilfe anzeigen"
|
||||
),
|
||||
}
|
||||
|
||||
if kommando == KommandoTyp.SPIEL_SUCHEN:
|
||||
suchbegriff = extrahiere_suchbegriff(submission.text)
|
||||
if suchbegriff:
|
||||
return {
|
||||
"kommando": kommando.value,
|
||||
"antwort": f"🔍 Suche nach '{suchbegriff}'...",
|
||||
"suchbegriff": suchbegriff,
|
||||
}
|
||||
return {
|
||||
"kommando": kommando.value,
|
||||
"antwort": "❌ Bitte gib einen Suchbegriff an: suche <spielname>",
|
||||
}
|
||||
|
||||
if kommando == KommandoTyp.SPIEL_BEWERTEN:
|
||||
return {
|
||||
"kommando": kommando.value,
|
||||
"antwort": "⭐ Bewertungsmodus aktiviert. Welches Spiel möchtest du bewerten?",
|
||||
}
|
||||
|
||||
return {
|
||||
"kommando": KommandoTyp.UNBEKANNT.value,
|
||||
"antwort": (
|
||||
"🤔 Das habe ich nicht verstanden. Schreib 'hilfe' "
|
||||
"für eine Liste der Kommandos."
|
||||
),
|
||||
}
|
||||
Reference in New Issue
Block a user