From 6f9717f53086da5033eb6fce6f4a69a5341d22df Mon Sep 17 00:00:00 2001 From: Max Richter Date: Fri, 9 May 2025 19:32:00 +0200 Subject: [PATCH] feat: add some error messages --- lib/telegram.ts | 39 +++++++++++++++++++++++++++------------ main.ts | 1 - 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/telegram.ts b/lib/telegram.ts index 2572e59..c8b3875 100644 --- a/lib/telegram.ts +++ b/lib/telegram.ts @@ -10,7 +10,9 @@ const activeTasks: Record< string, { noteName: string; - entries: Array<{ type: string; content: string | Uint8Array }>; + entries: Array< + { type: string; content: string | Uint8Array; fileName?: string } + >; } > = {}; @@ -50,7 +52,8 @@ bot.command("end", async (ctx) => { const mp3Data = await convertOggToMp3(entry.content as Uint8Array); const transcript = await transcribe(mp3Data); finalNote += `**Voice Transcript:**\n${transcript}\n\n`; - } catch (_) { + } catch (error) { + console.log(error); finalNote += "**[Voice message could not be transcribed]**\n\n"; } } else if (entry.type === "photo") { @@ -68,20 +71,27 @@ bot.command("end", async (ctx) => { } } - for (const entry of photoTasks) { - await createDocument(entry.path, entry.content, "image/jpeg"); + try { + for (const entry of photoTasks) { + await createDocument(entry.path, entry.content, "image/jpeg"); + } + await createDocument(task.noteName, finalNote, "text/markdown"); + delete activeTasks[ctx.chat.id.toString()]; + await ctx.reply("Note complete. Here is your markdown:"); + await ctx.reply(finalNote); + } catch (error) { + console.error("Error creating document:", error); + await ctx.reply("Error creating document:"); + await ctx.reply(JSON.stringify(error)); } - await createDocument(task.noteName, finalNote, "text/markdown"); - - delete activeTasks[ctx.chat.id.toString()]; - await ctx.reply("Note complete. Here is your markdown:"); - await ctx.reply(finalNote); }); bot.on("message:text", (ctx) => { const task = activeTasks[ctx.chat.id.toString()]; if (!task) return; - task.entries.push({ type: "text", content: ctx.message.text }); + const entry = { type: "text", content: ctx.message.text }; + console.log("New Entry", entry); + task.entries.push(entry); }); bot.on("message:voice", async (ctx) => { @@ -89,7 +99,9 @@ bot.on("message:voice", async (ctx) => { if (!task) return; const file = await ctx.getFile(); const buffer = await downloadFile(file.file_path!); - task.entries.push({ type: "voice", content: buffer }); + const entry = { type: "voice", content: buffer }; + console.log("New Entry", entry); + task.entries.push(entry); }); bot.on("message:photo", async (ctx) => { @@ -97,7 +109,10 @@ bot.on("message:photo", async (ctx) => { if (!task) return; const file = await ctx.getFile(); const buffer = await downloadFile(file.file_path!); - task.entries.push({ type: "photo", content: buffer }); + const fileName = file.file_path!.split("/").pop()!; + const entry = { type: "photo", content: buffer, fileName }; + console.log("New Entry", entry); + task.entries.push(entry); }); export async function convertOggToMp3( diff --git a/main.ts b/main.ts index 5ffbe34..768122e 100644 --- a/main.ts +++ b/main.ts @@ -8,6 +8,5 @@ import { start } from "$fresh/server.ts"; import manifest from "./fresh.gen.ts"; import config from "./fresh.config.ts"; import "@lib/telegram.ts"; -console.log("hello"); await start(manifest, config);