Skip to content

Commit

Permalink
refactor: clean up TelegramAdapter message handling and improve sende…
Browse files Browse the repository at this point in the history
…r ID extraction
  • Loading branch information
Salasifaoui committed Nov 21, 2024
1 parent ea12898 commit bf9f514
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 37 deletions.
65 changes: 30 additions & 35 deletions src/adapters/telegram.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bot, InlineKeyboard } from "grammy";
import { Bot } from "grammy";

import { IMessage, ISender } from "../types";
import { BaseAdapter } from "./base";
Expand All @@ -14,6 +14,7 @@ export class TelegramAdapter extends BaseAdapter {

constructor(token: TelegramAdapterOptions) {
super(token);

if (!token) {
throw new Error(
"Telegram bot token is undefined, please set TELEGRAM_BOT_TOKEN",
Expand All @@ -26,7 +27,7 @@ export class TelegramAdapter extends BaseAdapter {
"No access token provided through environment. Please PERMANENTLY store the access token in an the environment variable.",
);
}

this.token = token;
}

Expand All @@ -41,52 +42,46 @@ export class TelegramAdapter extends BaseAdapter {
});
});
this.client.api.setMyCommands(commands);

return this;
}

async init({ autoJoin }: { autoJoin?: boolean } = { autoJoin: true }) {
this.client.command("start", async (ctx) => {
await ctx.reply("Welcome!");
});

this.client.on("message", (ctx) => {
this.client.on("message", async (ctx) => {
console.log("first chatId", ctx.message.chat.id);
const sender: ISender = {
id: ctx.message.from.id.toString(),
id: ctx.message.chat.id.toString(),
name: ctx.message.from.username || ctx.message.from.first_name,
isDirect: true,
};
const message: IMessage = {
chatId: ctx.message.from.id.toString(), // TODO
text: ctx.message.text,
chatId: ctx.message.chat.id.toString(), // TODO
text: ctx.message.text || "",
};
console.log("========");
console.log("ctx", ctx);
console.log("========");
// const message = ctx.message;
console.log("message", message);
console.log("========");

if (!this.bot) {
return;
}
this.bot.onMessage(
sender,
message,
(reply) => {
console.log("========== BOT REPLY ==========");
console.log(reply);
ctx.reply(reply, {
parse_mode: "Markdown",
});

return reply;
},
(isTyping) => {
if (isTyping) {
ctx.replyWithChatAction("typing");
}
},
);

if (message.text) {
this.bot.onMessage(
sender,
message,
(reply) => {
ctx.reply(reply, {
parse_mode: "Markdown",
});

return reply;
},
(isTyping) => {
if (isTyping) {
ctx.replyWithChatAction("typing");
}
},
);
}
});

this.client.start();
Expand All @@ -95,7 +90,7 @@ export class TelegramAdapter extends BaseAdapter {
return this.client;
}

sendMessage(text: string, chatId?: string): Promise<void> {
async sendMessage(text: string, chatId?: string): Promise<void> {
throw new Error("Method not implemented.");
}
}
4 changes: 2 additions & 2 deletions src/bots/@base/OpenAIAssistantBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export abstract class OpenAIAssistantBot extends BaseBot {
sender.id.toString(),
);
const threadId = await this.getRoomThreadId(message.chatId);

const assistant = await this.getAssistant(
senderWalletAddress,
);
Expand Down Expand Up @@ -118,9 +119,8 @@ export abstract class OpenAIAssistantBot extends BaseBot {
limit: 1,
}
);

for (const reply of messages.data.reverse()) {
console.log('AI REPLU::', JSON.stringify(reply, null, 2))
console.log(`${reply.role} > ${reply.content[0].text.value}`);
onReply(
reply.content[0].text.value
);
Expand Down

0 comments on commit bf9f514

Please sign in to comment.