-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.ts
72 lines (66 loc) · 2.08 KB
/
bot.ts
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
import TelegramBot from "node-telegram-bot-api";
import dotenv from "dotenv";
import { ClientMux, Session } from "@bazed-ai/bazed-af";
import CupidAgent, { CupidAgentOptions } from "./agents/cupid";
dotenv.config();
const token: string = process.env.TELEGRAM_BOT_TOKEN || "";
const openAIKey: string = process.env.OPENAI_API_KEY || "";
const bot = new TelegramBot(token, { polling: true });
const clientMux = new ClientMux(openAIKey);
interface CupidContext {
chatId: number;
agent: CupidAgent;
session: Session;
userMessages: number;
botMessages: number;
startTime: Date;
lastMessageTime?: Date;
ended?: boolean;
}
const sessions: Record<number, CupidContext> = {};
bot.on("message", async (msg) => {
if (msg.chat.type !== "private") return;
const chatId = msg.chat.id;
bot.sendChatAction(chatId, "typing");
if (!sessions[chatId]) {
const session = new Session(clientMux, {});
const agent = session.spawnAgent(CupidAgent, {
bot: bot,
userName: msg.from?.first_name || "stranger",
chatID: chatId,
sendCallback: async (message) => {
sessions[chatId].botMessages++;
sessions[chatId].lastMessageTime = new Date();
await bot.sendMessage(chatId, message, { parse_mode: "Markdown" });
return;
},
initialMessage: msg,
} as CupidAgentOptions);
sessions[chatId] = {
startTime: new Date(msg.date * 1000),
userMessages: 1,
botMessages: 0,
chatId,
session,
agent,
};
agent.run().catch((e) => {
console.error("agent loop ended", sessions[chatId], e);
sessions[chatId].ended = true;
bot.sendMessage(chatId, "Sorry, I'm not available right now.");
});
} else if (!sessions[chatId].ended) {
sessions[chatId].lastMessageTime = new Date(msg.date * 1000);
sessions[chatId].userMessages++;
sessions[chatId].agent.receive(msg);
}
console.log(
chatId,
sessions[chatId].lastMessageTime,
sessions[chatId].startTime,
msg.from?.username,
sessions[chatId].userMessages,
sessions[chatId].botMessages,
sessions[chatId].ended
);
});