-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbot.js
More file actions
92 lines (74 loc) 路 2.37 KB
/
Copy pathbot.js
File metadata and controls
92 lines (74 loc) 路 2.37 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
90
91
92
import makeWASocket, { useMultiFileAuthState, DisconnectReason } from "@whiskeysockets/baileys";
import pino from "pino";
import qrcode from "qrcode-terminal";
import QRCode from "qrcode";
import { v4 as uuid } from "uuid";
import dotenv from "dotenv";
import { summarizeText } from "./summarizer.js";
import { addMessage } from "./store.js";
import { targets } from "./targets.js";
dotenv.config();
export async function startBot() {
const { state, saveCreds } = await useMultiFileAuthState("./auth_info");
const sock = makeWASocket({
auth: state,
browser: ["MVPBot", "Chrome", "1.0"],
logger: pino({ level: "silent" })
});
sock.ev.on("creds.update", saveCreds);
// -------------------------------------
// Connection updates
// -------------------------------------
sock.ev.on("connection.update", async ({ connection, lastDisconnect, qr }) => {
if (qr) {
console.log("Scan QR:");
qrcode.generate(qr, { small: true });
QRCode.toFile("qr.png", qr);
}
if (connection === "close") {
const reason = lastDisconnect?.error?.output?.statusCode;
if (reason !== DisconnectReason.loggedOut) {
console.log("Reconnecting...");
startBot();
}
} else if (connection === "open") {
console.log("Connected to WhatsApp");
}
});
// -------------------------------------
// Incoming messages
// -------------------------------------
sock.ev.on("messages.upsert", async ({ messages }) => {
for (const msg of messages) {
if (!msg.message) continue;
if (msg.key.fromMe) continue;
const sender = msg.key.remoteJid;
console.log("from" ,sender)
// Only accept allowed target numbers
const targetInfo = targets.get(sender);
if (!targetInfo) return;
const { name } = targetInfo;
// Extract message text
const text =
msg.message.conversation ||
msg.message.extendedTextMessage?.text ||
null;
if (!text) continue;
console.log(`From: ${name}`);
console.log("Message:", text);
// -------------------------------------
// Summarize text
// -------------------------------------
const { tone, summary } = await summarizeText(text);
addMessage({
id: uuid(),
sender,
name,
tone,
summary,
ts: Date.now(),
});
console.log("Saved & summarized.");
}
});
}