Skip to content

Commit 51c31fc

Browse files
fix: fix
1 parent 986d02e commit 51c31fc

File tree

2 files changed

+73
-77
lines changed

2 files changed

+73
-77
lines changed

packages/adapter-postgres/src/index.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,9 @@ export class PostgresDatabaseAdapter
736736
);
737737

738738
if (existingParticipant.rows.length > 0) {
739-
console.log(`Participant with userId ${userId} already exists in room ${roomId}.`);
739+
console.log(
740+
`Participant with userId ${userId} already exists in room ${roomId}.`
741+
);
740742
return; // Exit early if the participant already exists
741743
}
742744

@@ -750,11 +752,13 @@ export class PostgresDatabaseAdapter
750752
} catch (error) {
751753
// This is to prevent duplicate participant error in case of a race condition
752754
// Handle unique constraint violation error (code 23505)
753-
if (error.code === '23505') {
754-
console.warn(`Participant with userId ${userId} already exists in room ${roomId}.`); // Optionally, you can log this or handle it differently
755+
if (error.code === "23505") {
756+
console.warn(
757+
`Participant with userId ${userId} already exists in room ${roomId}.`
758+
); // Optionally, you can log this or handle it differently
755759
} else {
756760
// Handle other errors
757-
console.error('Error adding participant:', error);
761+
console.error("Error adding participant:", error);
758762
return false;
759763
}
760764
} finally {

packages/client-telegram/src/telegramClient.ts

+65-73
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Context, Telegraf } from "telegraf";
2-
3-
import { IAgentRuntime } from "@ai16z/eliza";
2+
import { IAgentRuntime, elizaLogger } from "@ai16z/eliza";
43
import { MessageManager } from "./messageManager.ts";
5-
import { elizaLogger } from "@ai16z/eliza";
64

75
export class TelegramClient {
86
private bot: Telegraf<Context>;
@@ -14,94 +12,88 @@ export class TelegramClient {
1412
this.runtime = runtime;
1513
this.bot = new Telegraf(botToken);
1614
this.messageManager = new MessageManager(this.bot, this.runtime);
17-
1815
elizaLogger.log("✅ TelegramClient constructor completed");
1916
}
2017

2118
public async start(): Promise<void> {
2219
elizaLogger.log("🚀 Starting Telegram bot...");
2320
try {
24-
this.bot.launch({
25-
dropPendingUpdates: true,
26-
});
27-
elizaLogger.log(
28-
"✨ Telegram bot successfully launched and is running!"
29-
);
21+
await this.initializeBot();
22+
this.setupMessageHandlers();
23+
this.setupShutdownHandlers();
24+
} catch (error) {
25+
elizaLogger.error("❌ Failed to launch Telegram bot:", error);
26+
throw error;
27+
}
28+
}
3029

31-
await this.bot.telegram.getMe().then((botInfo) => {
32-
this.bot.botInfo = botInfo;
33-
});
30+
private async initializeBot(): Promise<void> {
31+
this.bot.launch({ dropPendingUpdates: true });
32+
elizaLogger.log(
33+
"✨ Telegram bot successfully launched and is running!"
34+
);
3435

35-
elizaLogger.success(`Bot username: @${this.bot.botInfo?.username}`);
36+
const botInfo = await this.bot.telegram.getMe();
37+
this.bot.botInfo = botInfo;
38+
elizaLogger.success(`Bot username: @${botInfo.username}`);
3639

37-
this.messageManager.bot = this.bot;
40+
this.messageManager.bot = this.bot;
41+
}
3842

39-
// Include if you want to view message maanger bot info
40-
// console.log(`Message Manager bot info: @${this.messageManager.bot}`);
43+
private setupMessageHandlers(): void {
44+
elizaLogger.log("Setting up message handler...");
4145

42-
elizaLogger.log("Setting up message handler...");
46+
this.bot.on("message", async (ctx) => {
47+
try {
48+
await this.messageManager.handleMessage(ctx);
49+
} catch (error) {
50+
elizaLogger.error("❌ Error handling message:", error);
51+
await ctx.reply(
52+
"An error occurred while processing your message."
53+
);
54+
}
55+
});
4356

44-
this.bot.on("message", async (ctx) => {
45-
try {
46-
// console.log("📥 Received message:", ctx.message);
47-
await this.messageManager.handleMessage(ctx);
48-
} catch (error) {
49-
elizaLogger.error("❌ Error handling message:", error);
50-
await ctx.reply(
51-
"An error occurred while processing your message."
52-
);
53-
}
54-
});
57+
this.bot.on("photo", (ctx) => {
58+
elizaLogger.log(
59+
"📸 Received photo message with caption:",
60+
ctx.message.caption
61+
);
62+
});
5563

56-
// Handle specific message types for better logging
57-
this.bot.on("photo", (ctx) => {
58-
elizaLogger.log(
59-
"📸 Received photo message with caption:",
60-
ctx.message.caption
61-
);
62-
});
64+
this.bot.on("document", (ctx) => {
65+
elizaLogger.log(
66+
"📎 Received document message:",
67+
ctx.message.document.file_name
68+
);
69+
});
6370

64-
this.bot.on("document", (ctx) => {
65-
elizaLogger.log(
66-
"📎 Received document message:",
67-
ctx.message.document.file_name
68-
);
69-
});
71+
this.bot.catch((err, ctx) => {
72+
elizaLogger.error(`❌ Telegram Error for ${ctx.updateType}:`, err);
73+
ctx.reply("An unexpected error occurred. Please try again later.");
74+
});
75+
}
7076

71-
this.bot.catch((err, ctx) => {
77+
private setupShutdownHandlers(): void {
78+
const shutdownHandler = async (signal: string) => {
79+
elizaLogger.log(
80+
`⚠️ Received ${signal}. Shutting down Telegram bot gracefully...`
81+
);
82+
try {
83+
await this.stop();
84+
elizaLogger.log("🛑 Telegram bot stopped gracefully");
85+
} catch (error) {
7286
elizaLogger.error(
73-
`❌ Telegram Error for ${ctx.updateType}:`,
74-
err
75-
);
76-
ctx.reply(
77-
"An unexpected error occurred. Please try again later."
87+
"❌ Error during Telegram bot shutdown:",
88+
error
7889
);
79-
});
90+
throw error;
91+
}
92+
};
8093

81-
// Graceful shutdown handlers
82-
const shutdownHandler = async (signal: string) => {
83-
elizaLogger.log(
84-
`⚠️ Received ${signal}. Shutting down Telegram bot gracefully...`
85-
);
86-
try {
87-
await this.stop();
88-
elizaLogger.log("🛑 Telegram bot stopped gracefully");
89-
} catch (error) {
90-
elizaLogger.error(
91-
"❌ Error during Telegram bot shutdown:",
92-
error
93-
);
94-
throw error;
95-
}
96-
};
97-
98-
process.once("SIGINT", () => shutdownHandler("SIGINT"));
99-
process.once("SIGTERM", () => shutdownHandler("SIGTERM"));
100-
process.once("SIGHUP", () => shutdownHandler("SIGHUP"));
101-
} catch (error) {
102-
elizaLogger.error("❌ Failed to launch Telegram bot:", error);
103-
throw error;
104-
}
94+
process.once("SIGINT", () => shutdownHandler("SIGINT"));
95+
process.once("SIGTERM", () => shutdownHandler("SIGTERM"));
96+
process.once("SIGHUP", () => shutdownHandler("SIGHUP"));
10597
}
10698

10799
public async stop(): Promise<void> {

0 commit comments

Comments
 (0)