Skip to content

Commit e78522e

Browse files
Surface voice/photo processing errors instead of failing silently
The voice transcription and vision (photo) blocks in handleMessage sat outside any try/catch. A thrown error (bad model id, Groq API error, etc.) propagated past the code that replies to the user and was only caught by the outer webhook handler, which returns JSON to Supabase's gateway - never to Telegram. Users saw total silence with no way to tell what went wrong. Now both paths catch their own errors, reply to the user, and log the real cause. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 4a02aac commit e78522e

1 file changed

Lines changed: 32 additions & 22 deletions

File tree

supabase/functions/telegram-bot/index.ts

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -459,29 +459,39 @@ async function handleMessage(message: any) {
459459
if (message.text) {
460460
userMessage = message.text;
461461
} else if (message.voice) {
462-
const fileUrl = await getTelegramFileUrl(message.voice.file_id);
463-
const response = await fetch(fileUrl);
464-
const buffer = await response.arrayBuffer();
465-
const file = new File([buffer], "voice.ogg", { type: "audio/ogg" });
466-
const transcription = await groq.audio.transcriptions.create({ file, model: "whisper-large-v3-turbo" });
467-
userMessage = transcription.text;
462+
try {
463+
const fileUrl = await getTelegramFileUrl(message.voice.file_id);
464+
const response = await fetch(fileUrl);
465+
const buffer = await response.arrayBuffer();
466+
const file = new File([buffer], "voice.ogg", { type: "audio/ogg" });
467+
const transcription = await groq.audio.transcriptions.create({ file, model: "whisper-large-v3-turbo" });
468+
userMessage = transcription.text;
469+
} catch (err: any) {
470+
console.error("Voice transcription error:", err);
471+
return await sendTelegramMessage(chatId, "Sorry, I couldn't process that voice note: " + err.message);
472+
}
468473
} else if (message.photo) {
469-
const fileId = message.photo[message.photo.length - 1].file_id;
470-
const fileUrl = await getTelegramFileUrl(fileId);
471-
const response = await fetch(fileUrl);
472-
const buffer = await response.arrayBuffer();
473-
const base64 = btoa(String.fromCharCode(...new Uint8Array(buffer)));
474-
475-
const visionCompletion = await groq.chat.completions.create({
476-
messages: [
477-
{ role: "user", content: [
478-
{ type: "text", text: `Extract any event details from this image. ${message.caption || ""}` },
479-
{ type: "image_url", image_url: { url: `data:image/jpeg;base64,${base64}` } },
480-
]},
481-
],
482-
model: "qwen/qwen3.6-27b",
483-
});
484-
userMessage = visionCompletion.choices[0]?.message?.content || message.caption || "";
474+
try {
475+
const fileId = message.photo[message.photo.length - 1].file_id;
476+
const fileUrl = await getTelegramFileUrl(fileId);
477+
const response = await fetch(fileUrl);
478+
const buffer = await response.arrayBuffer();
479+
const base64 = btoa(String.fromCharCode(...new Uint8Array(buffer)));
480+
481+
const visionCompletion = await groq.chat.completions.create({
482+
messages: [
483+
{ role: "user", content: [
484+
{ type: "text", text: `Extract any event details from this image. ${message.caption || ""}` },
485+
{ type: "image_url", image_url: { url: `data:image/jpeg;base64,${base64}` } },
486+
]},
487+
],
488+
model: "qwen/qwen3.6-27b",
489+
});
490+
userMessage = visionCompletion.choices[0]?.message?.content || message.caption || "";
491+
} catch (err: any) {
492+
console.error("Vision processing error:", err);
493+
return await sendTelegramMessage(chatId, "Sorry, I couldn't process that image: " + err.message);
494+
}
485495
}
486496

487497
if (!userMessage.trim()) return;

0 commit comments

Comments
 (0)