-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
56 lines (54 loc) · 1.5 KB
/
main.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
import { WechatyBuilder } from "wechaty";
import QRCode from "qrcode";
import { ChatGPTBot } from "./bot.js";
const chatGPTBot = new ChatGPTBot();
const bot = WechatyBuilder.build({
name: "wechat-assistant", // generate xxxx.memory-card.json and save login data for the next login
puppetOptions: {
uos: true, // 开启uos协议
},
puppet: "wechaty-puppet-wechat",
});
// get a Wechaty instance
async function main() {
const initializedAt = Date.now()
await chatGPTBot.startGPTBot();
bot
.on("scan", async (qrcode, status) => {
const url = `https://wechaty.js.org/qrcode/${encodeURIComponent(qrcode)}`;
console.log(`Scan QR Code to login: ${status}\n${url}`);
console.log(
await QRCode.toString(qrcode, { type: "terminal", small: true })
);
})
.on("login", async (user) => {
console.log(`User ${user} logged in`);
chatGPTBot.setBotName(user.name());
})
.on("message", async (message) => {
if (
!chatGPTBot.ready ||
message.date().getTime() < initializedAt
) {
return;
}
if (message.text().startsWith("/ping")) {
await message.say("pong");
return;
}
try {
console.log(`Message: ${message}`);
await chatGPTBot.onMessage(message);
} catch (e) {
console.error(e);
}
});
try {
await bot.start();
} catch (e) {
console.error(
`⚠️ Bot start failed, can you log in through wechat on the web?: ${e}`
);
}
}
main();