fix: monica API limits, robust contact name matching, gemini model fallbacks
All checks were successful
Build & Push Friends Image to Gitea Registry / build-and-push (push) Successful in 10s
All checks were successful
Build & Push Friends Image to Gitea Registry / build-and-push (push) Successful in 10s
This commit is contained in:
86
telegram.js
86
telegram.js
@@ -6,7 +6,9 @@ import {
|
||||
cryptoRandomId,
|
||||
} from "./server.js";
|
||||
|
||||
const monicaBaseUrl = (process.env.MONICA_BASE_URL || "https://crm.mischlabs.de").replace(/\/$/, "");
|
||||
function getMonicaBaseUrl() {
|
||||
return (process.env.MONICA_BASE_URL || "https://crm.mischlabs.de").replace(/\/$/, "");
|
||||
}
|
||||
|
||||
// Helper to escape HTML characters for Telegram HTML parse_mode
|
||||
function escapeHtml(text) {
|
||||
@@ -85,40 +87,64 @@ async function transcribeVoice(audioBuffer) {
|
||||
}
|
||||
|
||||
const base64Audio = Buffer.from(audioBuffer).toString("base64");
|
||||
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=${geminiKey}`;
|
||||
|
||||
// List of models to try in order of preference
|
||||
const modelsToTry = [
|
||||
"gemini-2.5-flash",
|
||||
"gemini-2.0-flash",
|
||||
"gemini-1.5-flash-latest",
|
||||
"gemini-1.5-flash"
|
||||
];
|
||||
|
||||
const res = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
contents: [
|
||||
{
|
||||
parts: [
|
||||
let lastError = null;
|
||||
for (const model of modelsToTry) {
|
||||
const url = `https://generativelanguage.googleapis.com/v1beta/models/${model}:generateContent?key=${geminiKey}`;
|
||||
try {
|
||||
console.log(`[Telegram] Trying voice transcription with model: ${model}`);
|
||||
const res = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
contents: [
|
||||
{
|
||||
inlineData: {
|
||||
mimeType: "audio/ogg",
|
||||
data: base64Audio,
|
||||
},
|
||||
},
|
||||
{
|
||||
text: "Transkribiere diese Sprachnachricht wortgetreu in deutschen Text. Gib NUR die Transkription zurück, ohne Einleitung, Kommentare oder sonstige Zusätze. Falls nichts verständlich gesprochen wurde, antworte mit einem leeren Text.",
|
||||
parts: [
|
||||
{
|
||||
inlineData: {
|
||||
mimeType: "audio/ogg",
|
||||
data: base64Audio,
|
||||
},
|
||||
},
|
||||
{
|
||||
text: "Transkribiere diese Sprachnachricht wortgetreu in deutschen Text. Gib NUR die Transkription zurück, ohne Einleitung, Kommentare oder sonstige Zusätze. Falls nichts verständlich gesprochen wurde, antworte mit einem leeren Text.",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}),
|
||||
});
|
||||
}),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text();
|
||||
throw new Error(`Gemini API failed (${res.status}): ${errorText}`);
|
||||
if (res.status === 404) {
|
||||
console.warn(`[Telegram] Model ${model} returned 404. Trying next model...`);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
const errorText = await res.text();
|
||||
throw new Error(`Gemini API failed (${res.status}): ${errorText}`);
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
const text = data.candidates?.[0]?.content?.parts?.[0]?.text || "";
|
||||
return text.trim();
|
||||
} catch (err) {
|
||||
console.error(`[Telegram] Error with model ${model}:`, err.message);
|
||||
lastError = err;
|
||||
}
|
||||
}
|
||||
|
||||
const data = await res.json();
|
||||
const text = data.candidates?.[0]?.content?.parts?.[0]?.text || "";
|
||||
return text.trim();
|
||||
throw new Error(`All Gemini models failed. Last error: ${lastError ? lastError.message : "unknown"}`);
|
||||
}
|
||||
|
||||
// Fetch available contacts from Monica to perform NLP name matching
|
||||
@@ -133,7 +159,7 @@ async function fetchMonicaContacts() {
|
||||
}
|
||||
|
||||
try {
|
||||
const payload = await monicaFetch("/api/contacts?limit=150");
|
||||
const payload = await monicaFetch("/api/contacts?limit=100");
|
||||
const contacts = Array.isArray(payload.data) ? payload.data : [];
|
||||
return contacts.map((c) => ({
|
||||
id: c.id,
|
||||
@@ -280,7 +306,7 @@ async function handleUpdate(update) {
|
||||
}
|
||||
|
||||
if (analysis.contact) {
|
||||
const contactUrl = `${monicaBaseUrl}/people/${analysis.contact.id}`;
|
||||
const contactUrl = `${getMonicaBaseUrl()}/people/${analysis.contact.id}`;
|
||||
responseText += `👤 <b>Kontakt:</b> <a href="${contactUrl}">${escapeHtml(analysis.contact.name)}</a>\n`;
|
||||
} else {
|
||||
responseText += `👤 <b>Kontakt:</b> <i>Keine Zuordnung</i>\n`;
|
||||
|
||||
Reference in New Issue
Block a user