-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
104 lines (79 loc) · 2.46 KB
/
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
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
93
94
95
96
97
98
99
100
101
102
103
104
/*
Do not forget to create a .env file with the following two variables set:
CHROME_PATH=
EMAIL=
PASSWORD=
See documentation for help
*/
import { ChatGPTAPIBrowser } from 'chatgpt'
import whatsappweb from "whatsapp-web.js";
const { Client, LocalAuth } = whatsappweb;
import qrcode from "qrcode-terminal";
import * as dotenv from "dotenv";
dotenv.config();
// Create whatsapp client instance
const whatsapp = new Client({
puppeteer: {
executablePath: process.env.CHROME_PATH,
},
authStrategy: new LocalAuth(),
});
console.log(process.env.CHROME_PATH);
// Initialize conversation storage
const conversations = {};
whatsapp.initialize();
// This will output a QR code to the console, scan this with the WhatsApp app on the account that will be dedicated to chatGPT
whatsapp.on("qr", (qr) => {
qrcode.generate(qr, { small: true });
});
whatsapp.on("authenticated", () => {
console.log("Authentication complete");
});
whatsapp.on("ready", () => {
console.log("Ready to accept messages");
});
async function main() {
const chatgpt = new ChatGPTAPIBrowser({
email: process.env.EMAIL,
password: process.env.PASSWORD,
})
await chatgpt.initSession()
whatsapp.on("message", (message) => {
(async () => {
console.log(
`From: ${message._data.id.remote} (${message._data.notifyName})`
);
console.log(`Message: ${message.body}`);
// If added to a chatgroup, only respond if tagged
const chat = await message.getChat();
if (
chat.isGroup &&
!message.mentionedIds.includes(whatsapp.info.wid._serialized)
)
return;
// Do we already have a conversation for this sender, or is the user resetting this conversation?
if (
conversations[message._data.id.remote] === undefined ||
message.body === "reset"
) {
console.log(`Creating new conversation for ${message._data.id.remote}`);
if (message.body === "reset") {
message.reply("Conversation reset");
return;
}
conversations[message._data.id.remote] = chatgpt;
}
const response = await conversations[message._data.id.remote].sendMessage(
message.body
);
console.log(`Response: ${response.response}`);
message.reply(response.response);
})();
});
}
main().catch((err) => {
console.error(err);
process.exit(1);
});
const sleep = (waitTimeInMs) =>
new Promise((resolve) => setTimeout(resolve, waitTimeInMs));