From 4bdf40027fd68eb2faaeb13b1d462e74b650865f Mon Sep 17 00:00:00 2001 From: Dhravya Shah Date: Thu, 29 Aug 2024 14:31:46 -0700 Subject: [PATCH] fix: telegram bot --- apps/cf-ai-backend/src/index.ts | 2 ++ apps/web/app/api/telegram/route.ts | 22 ++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/apps/cf-ai-backend/src/index.ts b/apps/cf-ai-backend/src/index.ts index 0b8009b8..1493d90c 100644 --- a/apps/cf-ai-backend/src/index.ts +++ b/apps/cf-ai-backend/src/index.ts @@ -340,6 +340,8 @@ app.post( }, }); + console.log(task); + if ((task as string) === "add") { // addString is the plaintext string that the user wants to add to the database //chunk the note diff --git a/apps/web/app/api/telegram/route.ts b/apps/web/app/api/telegram/route.ts index 20241611..e44901a1 100644 --- a/apps/web/app/api/telegram/route.ts +++ b/apps/web/app/api/telegram/route.ts @@ -16,12 +16,18 @@ const token = process.env.TELEGRAM_BOT_TOKEN; const bot = new Bot(token); +function escapeMarkdown(text: string): string { + return text.replace(/[_*[\]()~`>#+=|{}.!-]/g, "\\$&"); +} + bot.command("start", async (ctx) => { const user: User = (await ctx.getAuthor()).user; const cipherd = cipher(user.id.toString()); await ctx.reply( - `Welcome to Supermemory bot. I am here to help you remember things better. [Click here to create and link your account](https://supermemory.ai/signin?telegramUser=${cipherd})`, + escapeMarkdown( + `Welcome to Supermemory bot. I am here to help you remember things better. [Click here to create and link your account](https://supermemory.ai/signin?telegramUser=${cipherd})`, + ), { parse_mode: "MarkdownV2", }, @@ -41,7 +47,9 @@ bot.on("message", async (ctx) => { if (!dbUser) { await ctx.reply( - `Welcome to Supermemory bot. I am here to help you remember things better. [Click here to create and link your account](https://supermemory.ai/signin?telegramUser=${cipherd})`, + escapeMarkdown( + `Welcome to Supermemory bot. I am here to help you remember things better. [Click here to create and link your account](https://supermemory.ai/signin?telegramUser=${cipherd})`, + ), { parse_mode: "MarkdownV2", }, @@ -53,7 +61,7 @@ bot.on("message", async (ctx) => { const message = await ctx.reply("I'm thinking..."); const response = await fetch( - `${process.env.BACKEND_BASE_URL}/api/autoChatOrAdd?query=${ctx.message.text}&user=${dbUser.id}`, + `${process.env.BACKEND_BASE_URL}/api/autoChatOrAdd?query=${encodeURIComponent(ctx.message.text!)}&user=${dbUser.id}`, { method: "POST", headers: { @@ -75,8 +83,10 @@ bot.on("message", async (ctx) => { console.log("Failed to get response from backend"); console.log(response.status); await ctx.reply( - "Sorry, I am not able to process your request at the moment." + - (await response.text()), + escapeMarkdown( + "Sorry, I am not able to process your request at the moment." + + (await response.text()), + ), ); return; } @@ -112,7 +122,7 @@ bot.on("message", async (ctx) => { await ctx.api.editMessageText( ctx.chat.id, message.message_id, - data.response, + escapeMarkdown(data.response), { parse_mode: "MarkdownV2", },