forked from kontorol/claude-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
56 lines (48 loc) · 1.6 KB
/
index.js
File metadata and controls
56 lines (48 loc) · 1.6 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
import { Telegraf, session } from "telegraf";
import Claude from "claude-ai";
import dotenv from "dotenv";
dotenv.config();
const bot = new Telegraf(process.env.BOT_TOKEN);
bot.use(session());
bot.launch();
const claude = new Claude({
sessionKey: process.env.SESSION,
});
const sessions = new Map();
const getSession = async (ctx) => {
await claude.init();
let sessionId = sessions.get(ctx.chat.id);
if (!sessionId) {
const conversation = await claude.startConversation("Hello Claude!");
await conversation.sendMessage("How are you today?");
sessionId = conversation.conversationId;
sessions.set(ctx.chat.id, sessionId);
}
return sessionId;
};
bot.command("start", async (ctx) => {
await ctx.reply("Claude 2 AI Bot\n\n developed by @sabber_dev");
});
bot.on("message", async (ctx) => {
await ctx.sendChatAction("typing");
const sessionId = await getSession(ctx);
await claude.init();
// get conversation with existing session
const conversation = await claude.getConversation(sessionId);
const reply = await conversation.sendMessage(ctx.message.text);
await ctx.reply(reply.completion);
});
bot.command("delete", async (ctx) => {
await ctx.sendChatAction("typing");
const sessionId = await getSession(ctx);
await claude.init();
// delete conversation with existing session
const conversation = await claude.getConversation(sessionId);
await conversation.delete();
await ctx.reply("گفت و گو پاک شد");
});
bot.catch((err) => {
console.log("Ooops", err);
});
process.once("SIGINT", () => bot.stop("SIGINT"));
process.once("SIGTERM", () => bot.stop("SIGTERM"));