Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Telegram client refactor for bot info availability #308

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/client-telegram/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const TelegramClientInterface: Client = {
const botToken = runtime.getSetting("TELEGRAM_BOT_TOKEN");
const tg = new TelegramClient(runtime, botToken);
await tg.start();

elizaLogger.success(
`βœ… Telegram client successfully started for character ${runtime.character.name}`
);
Expand All @@ -17,4 +18,4 @@ export const TelegramClientInterface: Client = {
},
};

export default TelegramClientInterface;
export default TelegramClientInterface;
4 changes: 2 additions & 2 deletions packages/client-telegram/src/messageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ Note that {{agentName}} is capable of reading/seeing/hearing various forms of me
` + messageCompletionFooter;

export class MessageManager {
private bot: Telegraf<Context>;
public bot: Telegraf<Context>;
private runtime: IAgentRuntime;
private imageService: IImageDescriptionService;

Expand Down Expand Up @@ -482,4 +482,4 @@ export class MessageManager {
console.error("Error sending message:", error);
}
}
}
}
81 changes: 46 additions & 35 deletions packages/client-telegram/src/telegramClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,6 @@ export class TelegramClient {
this.bot = new Telegraf(botToken);
this.messageManager = new MessageManager(this.bot, this.runtime);

elizaLogger.log("Setting up message handler...");
this.bot.on("message", async (ctx) => {
try {
elizaLogger.log("πŸ“₯ Received message:", ctx.message);
await this.messageManager.handleMessage(ctx);
} catch (error) {
elizaLogger.error("❌ Error handling message:", error);
await ctx.reply(
"An error occurred while processing your message."
);
}
});

// Handle specific message types for better logging
this.bot.on("photo", (ctx) => {
elizaLogger.log(
"πŸ“Έ Received photo message with caption:",
ctx.message.caption
);
});

this.bot.on("document", (ctx) => {
elizaLogger.log(
"πŸ“Ž Received document message:",
ctx.message.document.file_name
);
});

this.bot.catch((err, ctx) => {
elizaLogger.error(`❌ Telegram Error for ${ctx.updateType}:`, err);
ctx.reply("An unexpected error occurred. Please try again later.");
});

elizaLogger.log("βœ… TelegramClient constructor completed");
}

Expand All @@ -60,7 +27,51 @@ export class TelegramClient {
elizaLogger.log(
"✨ Telegram bot successfully launched and is running!"
);
elizaLogger.log(`Bot username: @${this.bot.botInfo?.username}`);

await this.bot.telegram.getMe().then((botInfo) => {
this.bot.botInfo = botInfo;
});

console.log(`Bot username: @${this.bot.botInfo?.username}`);

this.messageManager.bot = this.bot;

// Include if you want to view message maanger bot info
// console.log(`Message Manager bot info: @${this.messageManager.bot}`);

elizaLogger.log("Setting up message handler...");

this.bot.on("message", async (ctx) => {
try {
console.log("πŸ“₯ Received message:", ctx.message);
await this.messageManager.handleMessage(ctx);
} catch (error) {
elizaLogger.error("❌ Error handling message:", error);
await ctx.reply(
"An error occurred while processing your message."
);
}
});

// Handle specific message types for better logging
this.bot.on("photo", (ctx) => {
elizaLogger.log(
"πŸ“Έ Received photo message with caption:",
ctx.message.caption
);
});

this.bot.on("document", (ctx) => {
elizaLogger.log(
"πŸ“Ž Received document message:",
ctx.message.document.file_name
);
});

this.bot.catch((err, ctx) => {
elizaLogger.error(`❌ Telegram Error for ${ctx.updateType}:`, err);
ctx.reply("An unexpected error occurred. Please try again later.");
});

// Graceful shutdown handlers
const shutdownHandler = async (signal: string) => {
Expand Down Expand Up @@ -93,4 +104,4 @@ export class TelegramClient {
await this.bot.stop();
elizaLogger.log("Telegram bot stopped");
}
}
}
Loading