Skip to content

Commit

Permalink
Merge pull request #308 from ai16z/telegram-client-fixes
Browse files Browse the repository at this point in the history
Telegram client refactor for bot info availability
  • Loading branch information
lalalune authored Nov 14, 2024
2 parents 7875865 + df17626 commit e235713
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 38 deletions.
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");
}
}
}

0 comments on commit e235713

Please sign in to comment.