-
Notifications
You must be signed in to change notification settings - Fork 984
/
index.ts
118 lines (107 loc) · 3.08 KB
/
index.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { WechatyBuilder } from 'wechaty';
import qrcodeTerminal from 'qrcode-terminal';
import config from './config.js';
import { replyMessage, initChatGPT } from './chatgpt.js';
let bot: any = {};
const startTime = new Date();
initProject();
async function onMessage(msg) {
// 避免重复发送
if (msg.date() < startTime) {
return;
}
const contact = msg.talker();
const receiver = msg.to();
const content = msg.text().trim();
const room = msg.room();
const alias = (await contact.alias()) || (await contact.name());
const isText = msg.type() === bot.Message.Type.Text;
if (msg.self()) {
return;
}
if (room && isText) {
const topic = await room.topic();
console.log(
`Group name: ${topic} talker: ${await contact.name()} content: ${content}`
);
const pattern = RegExp(`^@${receiver.name()}\\s+${config.groupKey}[\\s]*`);
if (await msg.mentionSelf()) {
if (pattern.test(content)) {
const groupContent = content.replace(pattern, '');
replyMessage(room, groupContent);
return;
} else {
console.log(
'Content is not within the scope of the customizition format'
);
}
}
} else if (isText) {
console.log(`talker: ${alias} content: ${content}`);
if (config.autoReply) {
if (content.startsWith(config.privateKey) || config.privateKey === '') {
let privateContent = content;
if (config.privateKey === '') {
privateContent = content.substring(config.privateKey.length).trim();
}
replyMessage(contact, privateContent);
} else {
console.log(
'Content is not within the scope of the customizition format'
);
}
}
}
}
function onScan(qrcode) {
qrcodeTerminal.generate(qrcode); // 在console端显示二维码
const qrcodeImageUrl = [
'https://api.qrserver.com/v1/create-qr-code/?data=',
encodeURIComponent(qrcode),
].join('');
console.log(qrcodeImageUrl);
}
async function onLogin(user) {
console.log(`${user} has logged in`);
const date = new Date();
console.log(`Current time:${date}`);
if (config.autoReply) {
console.log(`Automatic robot chat mode has been activated`);
}
}
function onLogout(user) {
console.log(`${user} has logged out`);
}
async function onFriendShip(friendship) {
if (friendship.type() === 2) {
if (config.friendShipRule.test(friendship.hello())) {
await friendship.accept();
}
}
}
async function initProject() {
try {
await initChatGPT();
bot = WechatyBuilder.build({
name: 'WechatEveryDay',
puppet: 'wechaty-puppet-wechat', // 如果有token,记得更换对应的puppet
puppetOptions: {
uos: true,
},
});
bot
.on('scan', onScan)
.on('login', onLogin)
.on('logout', onLogout)
.on('message', onMessage);
if (config.friendShipRule) {
bot.on('friendship', onFriendShip);
}
bot
.start()
.then(() => console.log('Start to log in wechat...'))
.catch((e) => console.error(e));
} catch (error) {
console.log('init error: ', error);
}
}