-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
34 lines (26 loc) · 798 Bytes
/
index.js
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
const { Client, LocalAuth, MessageMedia } = require("whatsapp-web.js");
const qrcode = require("qrcode-terminal");
const dotenv = require("dotenv");
const ChatGPT = require("./chatgpt");
dotenv.config();
const bot = new ChatGPT(process.env.OPENAI_API_KEY);
const client = new Client({
authStrategy: new LocalAuth(),
});
client.on("qr", (qr) => {
qrcode.generate(qr, { small: true });
});
client.on("authenticated", (session) => {
console.log("User authenticated successfully");
});
client.on("ready", async () => {
console.log("Client is ready!");
});
client.on("message", async (message) => {
let question = message.body;
if (question.startsWith("gpt, ")) {
let answer = await bot.ask(question.replace("gpt, ", ""));
message.reply(answer);
}
});
client.initialize();