Repository navigation
Expand file tree
/
Copy pathtelegram-with-chatAgent-example.ts
More file actions
62 lines (52 loc) · 2 KB
/
Copy pathtelegram-with-chatAgent-example.ts
File metadata and controls
62 lines (52 loc) · 2 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
import TelegramPlugin from "../../plugins/telegramPlugin/src/telegramPlugin";
import { ChatAgent } from "../../src/chatAgent";
const telegramBot = new TelegramPlugin({
credentials: {
botToken: process.env.TELEGRAM_BOT_TOKEN || ""
}
})
const chatAgent = new ChatAgent(process.env.GAME_API_KEY || "", "You are a helpful assistant")
const main = async () => {
// Track active chats using a Map
const activeChats = new Map();
// Set up Telegram message handler
telegramBot.onMessage(async (message) => {
// Get or create chat for this user
let chat = activeChats.get(message.from.id);
if (!chat) {
chat = await chatAgent.createChat({
partnerId: message.from.id.toString(),
partnerName: message.from.first_name,
actionSpace: [
telegramBot.sendMessageFunction,
telegramBot.sendMediaFunction,
telegramBot.createPollFunction,
telegramBot.pinnedMessageFunction,
telegramBot.unPinnedMessageFunction,
telegramBot.deleteMessageFunction
],
});
activeChats.set(message.from.id, chat);
}
const userMessage = message.text;
const response = await chat.next(userMessage);
if (response.functionCall) {
console.log(`Function call: ${response.functionCall.fn_name}`);
// The function will be automatically executed by the agent
}
if (response.message) {
await telegramBot.sendMessageFunction.executable({
chat_id: message.chat.id,
text: response.message
}, console.log);
}
if (response.isFinished) {
await telegramBot.sendMessageFunction.executable({
chat_id: message.chat.id,
text: "Chat ended"
}, console.log);
}
});
// Start the bot
};
main().catch(console.error);