66 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			66 lines
		
	
	
		
			2.1 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { Bot } from "https://deno.land/x/grammy@v1.36.1/mod.ts";
 | |
| import { TELEGRAM_API_KEY } from "@lib/env.ts";
 | |
| import { createLogger } from "./log/index.ts";
 | |
| 
 | |
| const bot = new Bot(TELEGRAM_API_KEY);
 | |
| const log = createLogger("telegram");
 | |
| 
 | |
| import * as manager from "./taskManager.ts";
 | |
| 
 | |
| async function downloadFile(filePath: string): Promise<Uint8Array> {
 | |
|   log.info(`Downloading file from path: ${filePath}`);
 | |
|   const url = `https://api.telegram.org/file/bot${bot.token}/${filePath}`;
 | |
|   const response = await fetch(url);
 | |
|   if (!response.ok) {
 | |
|     throw new Error("Failed to download file: " + response.statusText);
 | |
|   }
 | |
|   log.info("File downloaded successfully");
 | |
|   const buffer = await response.arrayBuffer();
 | |
|   return new Uint8Array(buffer);
 | |
| }
 | |
| 
 | |
| bot.command("start", async (ctx) => {
 | |
|   log.info("Received /start command");
 | |
|   const [_, noteName] = ctx.message?.text?.split(" ") || [];
 | |
|   if (!noteName) {
 | |
|     return ctx.reply("Please provide a note name. Usage: /start NoteName");
 | |
|   }
 | |
|   manager.startTask(ctx.chat.id.toString(), noteName);
 | |
|   await ctx.reply(`Started note: ${noteName}`);
 | |
| });
 | |
| 
 | |
| bot.command("end", async (ctx) => {
 | |
|   log.info("Received /end command");
 | |
|   const finalNote = await manager.endTask(ctx.chat.id.toString());
 | |
|   if (!finalNote) return ctx.reply("No active note found.");
 | |
| 
 | |
|   try {
 | |
|     await ctx.reply("Note complete. Here is your markdown:");
 | |
|     await ctx.reply(finalNote, { parse_mode: "MarkdownV2" });
 | |
|   } catch (err) {
 | |
|     console.error("Error sending final note:", err);
 | |
|   }
 | |
| });
 | |
| 
 | |
| bot.on("message:text", (ctx) => {
 | |
|   log.info("Received text message");
 | |
|   manager.addTextEntry(ctx.chat.id.toString(), ctx.message.text);
 | |
| });
 | |
| 
 | |
| bot.on("message:voice", async (ctx) => {
 | |
|   log.info("Received photo message");
 | |
|   log.info("Received voice message");
 | |
|   const file = await ctx.getFile();
 | |
|   const buffer = await downloadFile(file.file_path!);
 | |
|   manager.addVoiceEntry(ctx.chat.id.toString(), buffer.buffer);
 | |
| });
 | |
| 
 | |
| bot.on("message:photo", async (ctx) => {
 | |
|   const file = await ctx.getFile();
 | |
|   const buffer = await downloadFile(file.file_path!);
 | |
|   const fileName = file.file_path!.split("/").pop()!;
 | |
|   manager.addPhotoEntry(ctx.chat.id.toString(), buffer.buffer, fileName);
 | |
| });
 | |
| 
 | |
| bot.start();
 |