-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathtelegram-agent.ts
More file actions
89 lines (76 loc) · 2.35 KB
/
telegram-agent.ts
File metadata and controls
89 lines (76 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* Telegram bot using elizaOS with full message pipeline.
*
* Required env vars: TELEGRAM_BOT_TOKEN, OPENAI_API_KEY
* Optional: POSTGRES_URL (defaults to PGLite)
*/
import { AgentRuntime, createCharacter } from "@elizaos/core";
export function readRequiredEnv(key: string): string {
const value = process.env[key];
if (typeof value !== "string" || value.trim().length === 0) {
throw new Error(`Missing required environment variable: ${key}`);
}
return value;
}
export function createTelegramCharacter(params: {
telegramBotToken: string;
openaiApiKey: string;
}) {
return createCharacter({
name: "TelegramEliza",
bio: "A helpful AI assistant on Telegram.",
system: `You are TelegramEliza, a helpful AI assistant on Telegram.
Be friendly, concise, and genuinely helpful.
Keep responses short - suitable for mobile chat.`,
settings: {
// Match how the chat example configures model selection via runtime settings
// (read by @elizaos/plugin-openai).
OPENAI_SMALL_MODEL: "gpt-5-mini",
OPENAI_LARGE_MODEL: "gpt-5-mini",
},
// Optional: pass through secrets so plugins can read via runtime.getSetting()
secrets: {
TELEGRAM_BOT_TOKEN: params.telegramBotToken,
OPENAI_API_KEY: params.openaiApiKey,
},
});
}
async function main() {
const [
{ openaiPlugin },
{ default: sqlPlugin },
{ default: telegramPlugin },
] = await Promise.all([
import("@elizaos/plugin-openai"),
import("@elizaos/plugin-sql"),
import("@elizaos/plugin-telegram"),
]);
let telegramBotToken: string;
let openaiApiKey: string;
try {
telegramBotToken = readRequiredEnv("TELEGRAM_BOT_TOKEN");
openaiApiKey = readRequiredEnv("OPENAI_API_KEY");
} catch (error) {
console.error(error instanceof Error ? error.message : String(error));
process.exit(1);
}
const character = createTelegramCharacter({
telegramBotToken,
openaiApiKey,
});
console.log("Starting TelegramEliza...");
const runtime = new AgentRuntime({
character,
plugins: [sqlPlugin, openaiPlugin, telegramPlugin],
});
await runtime.initialize();
console.log(`${character.name} is running. Press Ctrl+C to stop.`);
process.on("SIGINT", async () => {
await runtime.stop();
process.exit(0);
});
await new Promise(() => {});
}
if (import.meta.main) {
main().catch(console.error);
}