fix(talkbot): improve text translation language handling

This commit is contained in:
2026-09-05 15:56:32 +02:00
vanhempi 6062c76cf8
commit 93bb6ffb5a
+57 -1
Näytä tiedosto
@@ -62,6 +62,18 @@ QUEUE_DB_PATH = os.environ.get(
MAX_ATTEMPTS = 3
RETRY_BACKOFF_SECONDS = 20
AUDIO_EXTENSIONS = {
".aac",
".flac",
".m4a",
".mp3",
".oga",
".ogg",
".opus",
".wav",
".webm",
}
MANAGER_USER = NC_USER
app = FastAPI(title="Nextcloud Talk voice translator")
@@ -314,6 +326,20 @@ def extract_file_param(parameters) -> Optional[dict]:
return None
def is_audio_file(file_param: dict) -> bool:
mimetype = str(file_param.get("mimetype", "") or "").lower()
if mimetype.startswith("audio/"):
return True
filename = str(
file_param.get("name")
or file_param.get("path")
or ""
).lower()
return any(filename.endswith(extension) for extension in AUDIO_EXTENSIONS)
def actor_user_id(actor: dict) -> str:
actor_id = str(actor.get("id", "") or "")
@@ -1223,6 +1249,17 @@ def process_event(job: dict) -> None:
sender_user_id,
)
sender_language = normalize_language_code(
get_user_language(sender_user_id),
)
log.info(
"Text sender %s language: %s -> %s",
sender_user_id,
sender_language,
language_name(sender_language),
)
translations = []
for recipient in recipients:
@@ -1232,10 +1269,17 @@ def process_event(job: dict) -> None:
translated = translate(
message_text,
"auto",
sender_language,
target_language,
)
log.info(
"Translated text for %s (%s): %s",
recipient,
language_name(target_language),
translated,
)
if translated.strip().lower() == message_text.lower():
continue
@@ -1464,6 +1508,18 @@ async def webhook(
parameters,
)
if file_param and not is_audio_file(file_param):
log.info(
"Ignoring unsupported attachment in message %s: name=%s mimetype=%s",
message_id,
file_param.get("name"),
file_param.get("mimetype"),
)
# If the message also contains text, process the text and ignore the
# unrelated attachment. Otherwise there is nothing for this bot to do.
file_param = None
if not file_param and not message_text:
log.info(
"Message %s has no text or attachment",