feat: add some error messages

This commit is contained in:
max_richter 2025-05-09 19:32:00 +02:00
parent 6883780d57
commit 6f9717f530
Signed by: max
GPG Key ID: 51973802EF3F77C5
2 changed files with 27 additions and 13 deletions

View File

@ -10,7 +10,9 @@ const activeTasks: Record<
string, string,
{ {
noteName: 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 mp3Data = await convertOggToMp3(entry.content as Uint8Array);
const transcript = await transcribe(mp3Data); const transcript = await transcribe(mp3Data);
finalNote += `**Voice Transcript:**\n${transcript}\n\n`; finalNote += `**Voice Transcript:**\n${transcript}\n\n`;
} catch (_) { } catch (error) {
console.log(error);
finalNote += "**[Voice message could not be transcribed]**\n\n"; finalNote += "**[Voice message could not be transcribed]**\n\n";
} }
} else if (entry.type === "photo") { } else if (entry.type === "photo") {
@ -68,20 +71,27 @@ bot.command("end", async (ctx) => {
} }
} }
try {
for (const entry of photoTasks) { for (const entry of photoTasks) {
await createDocument(entry.path, entry.content, "image/jpeg"); await createDocument(entry.path, entry.content, "image/jpeg");
} }
await createDocument(task.noteName, finalNote, "text/markdown"); await createDocument(task.noteName, finalNote, "text/markdown");
delete activeTasks[ctx.chat.id.toString()]; delete activeTasks[ctx.chat.id.toString()];
await ctx.reply("Note complete. Here is your markdown:"); await ctx.reply("Note complete. Here is your markdown:");
await ctx.reply(finalNote); await ctx.reply(finalNote);
} catch (error) {
console.error("Error creating document:", error);
await ctx.reply("Error creating document:");
await ctx.reply(JSON.stringify(error));
}
}); });
bot.on("message:text", (ctx) => { bot.on("message:text", (ctx) => {
const task = activeTasks[ctx.chat.id.toString()]; const task = activeTasks[ctx.chat.id.toString()];
if (!task) return; 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) => { bot.on("message:voice", async (ctx) => {
@ -89,7 +99,9 @@ bot.on("message:voice", async (ctx) => {
if (!task) return; if (!task) return;
const file = await ctx.getFile(); const file = await ctx.getFile();
const buffer = await downloadFile(file.file_path!); 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) => { bot.on("message:photo", async (ctx) => {
@ -97,7 +109,10 @@ bot.on("message:photo", async (ctx) => {
if (!task) return; if (!task) return;
const file = await ctx.getFile(); const file = await ctx.getFile();
const buffer = await downloadFile(file.file_path!); 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( export async function convertOggToMp3(

View File

@ -8,6 +8,5 @@ import { start } from "$fresh/server.ts";
import manifest from "./fresh.gen.ts"; import manifest from "./fresh.gen.ts";
import config from "./fresh.config.ts"; import config from "./fresh.config.ts";
import "@lib/telegram.ts"; import "@lib/telegram.ts";
console.log("hello");
await start(manifest, config); await start(manifest, config);