fix: telegram bot

This commit is contained in:
Dhravya Shah 2024-08-29 14:31:46 -07:00
parent ba3a7b6a80
commit 4bdf40027f
2 changed files with 18 additions and 6 deletions

View file

@ -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

View file

@ -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",
},