-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathhelper.ts
119 lines (87 loc) · 3.53 KB
/
helper.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
119
import {log, Message} from "wechaty";
import * as PUPPET from "wechaty-puppet";
import fs from "fs";
import path from "path";
import moment from 'moment';
export const LOGPRE = "[PadLocalDemo]"
export async function getMessagePayload(message: Message) {
switch (message.type()) {
case PUPPET.types.Message.Text:
log.silly(LOGPRE, `get message text: ${message.text()}`);
const room = message.room();
const roomName = await room?.topic();
const userName = message.talker().name();
const text = message.text();
const time = message.date();
// 写入到本地
const today = moment().format("YYYY-MM-DD");
if (!fs.existsSync(path.resolve(__dirname, `./data/${today}`))) {
fs.mkdirSync(path.resolve(__dirname, `./data/${today}`));
}
const filePath = path.resolve(
__dirname,
`./data/${today}/${roomName}.txt`
);
const data = `${moment(time).format('YYYY-MM-DD HH:mm:ss')}:\n${userName}:\n${text}\n\n`;
fs.appendFile(filePath, data, (err: any) => {
if (err) {
console.log(err);
} else {
console.log("写入成功");
}
});
break;
case PUPPET.types.Message.Attachment:
case PUPPET.types.Message.Audio: {
const attachFile = await message.toFileBox();
const dataBuffer = await attachFile.toBuffer();
log.info(LOGPRE, `get message audio or attach: ${dataBuffer.length}`);
break;
}
case PUPPET.types.Message.Video: {
const videoFile = await message.toFileBox();
const videoData = await videoFile.toBuffer();
log.info(LOGPRE, `get message video: ${videoData.length}`);
break;
}
case PUPPET.types.Message.Emoticon: {
const emotionFile = await message.toFileBox();
const emotionJSON = emotionFile.toJSON();
log.info(LOGPRE, `get message emotion json: ${JSON.stringify(emotionJSON)}`);
const emotionBuffer: Buffer = await emotionFile.toBuffer();
log.info(LOGPRE, `get message emotion: ${emotionBuffer.length}`);
break;
}
case PUPPET.types.Message.Image: {
const messageImage = await message.toImage();
const thumbImage = await messageImage.thumbnail();
const thumbImageData = await thumbImage.toBuffer();
log.info(LOGPRE, `get message image, thumb: ${thumbImageData.length}`);
const hdImage = await messageImage.hd();
const hdImageData = await hdImage.toBuffer();
log.info(LOGPRE, `get message image, hd: ${hdImageData.length}`);
const artworkImage = await messageImage.artwork();
const artworkImageData = await artworkImage.toBuffer();
log.info(LOGPRE, `get message image, artwork: ${artworkImageData.length}`);
break;
}
case PUPPET.types.Message.Url: {
const urlLink = await message.toUrlLink();
log.info(LOGPRE, `get message url: ${JSON.stringify(urlLink)}`);
const urlThumbImage = await message.toFileBox();
const urlThumbImageData = await urlThumbImage.toBuffer();
log.info(LOGPRE, `get message url thumb: ${urlThumbImageData.length}`);
break;
}
case PUPPET.types.Message.MiniProgram: {
const miniProgram = await message.toMiniProgram();
log.info(LOGPRE, `MiniProgramPayload: ${JSON.stringify(miniProgram)}`);
break;
}
}
}
export async function dingDongBot(message: Message) {
if (message.to()?.self() && message.text().indexOf("ding") !== -1) {
await message.talker().say(message.text().replace("ding", "dong"));
}
}