forked from atorber/puppet-xp-getting-started
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
340 lines (315 loc) · 10.3 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env -S node --no-warnings --loader ts-node/esm
/**
* wechaty-puppet-xp示例代码,可以作为模版编写自己的业务逻辑.
*
**/
import 'dotenv/config.js'
import {
type Contact,
type Message,
ScanStatus,
WechatyBuilder,
log,
types,
} from 'wechaty'
import qrcodeTerminal from 'qrcode-terminal'
import { FileBox } from 'file-box'
const onScan = (qrcode: string, status: ScanStatus) => {
if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) {
const qrcodeImageUrl = [
'https://wechaty.js.org/qrcode/',
encodeURIComponent(qrcode),
].join('')
log.info('onScan: %s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl)
qrcodeTerminal.generate(qrcode, { small: true }) // show qrcode on console
} else {
log.info('onScan: %s(%s)', ScanStatus[status], status)
}
}
const onLogin = async (user: Contact) => {
log.info('onLogin', '%s login', user)
// 登录成功后调用bot
await main()
}
const onLogout = (user: Contact) => {
log.info('onLogout', '%s logout', user)
}
const onMessage = async (msg: Message) => {
log.info('onMessage', JSON.stringify(msg))
// Message doc : https://wechaty.js.org/docs/api/message#messageage--number
const talker = msg.talker() // 发消息人
const listener = msg.listener() // 接收消息人
const room = msg.room() // 是否是群消息
const text = msg.text() // 消息内容
const type = msg.type() // 消息类型
const self = msg.self() // 是否自己发给自己的消息
log.info('talker', JSON.stringify(talker))
log.info('listener', listener || 'undefined')
log.info('room', room || 'undefined')
log.info('text', text)
log.info('type', type)
log.info('self', self ? 'true' : 'false')
try {
switch (text) {
case 'ding': // 接收到的消息是ding,回复dong
await msg.say('dong')
break
case 'send text': // 接收到的消息是send text,发送文本消息
await msg.say('this is a test text')
break
case 'send image': {
// 接收到的消息是send image,发送图片
const image = FileBox.fromFile('file/ledongmao.jpg')
await msg.say(image)
break
}
case 'send file': {
// 接收到的消息是send file,发送文件
const fileBox = FileBox.fromUrl('https://wechaty.js.org/assets/logo.png')
await msg.say(fileBox)
break
}
case 'send video': {
// 接收到的消息是send video,发送视频
const video = FileBox.fromUrl('http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4')
await msg.say(video)
break
}
case 'send audio': {
// 接收到的消息是send audio,发送音频
const audio = FileBox.fromUrl('http://www.zhongguoyinhang.com/upload/2018-11/201811161154314128.mp3')
await msg.say(audio)
break
}
case 'send emotion': {
// 接收到的消息是send emotion,发送表情
const emotion = FileBox.fromUrl('https://res.wx.qq.com/mpres/htmledition/images/icon/emotion/0.gif')
await msg.say(emotion)
break
}
default:
break
}
} catch (e) {
log.error('发送消息失败...', e)
}
try {
switch (type) {
case types.Message.Text: // 接收到的消息是文本
log.info('接收到的消息是文本')
break
case types.Message.Image: {
// 接收到的消息是图片
log.info('接收到的消息是图片')
const image = await msg.toImage().thumbnail() // Save the media message as a FileBox
const filePath = 'file/' + image.name
try {
await image.toFile(filePath, true)
log.info(`Saved file: ${filePath}`)
} catch (e) {
log.error('保存文件错误:', e)
}
break
}
case types.Message.Attachment: {
// 接收到的消息是附件
log.info('接收到的消息是附件')
const attachment = await msg.toFileBox() // Save the media message as a FileBox
const attachmentPath = 'file/' + attachment.name
try {
await attachment.toFile(attachmentPath, true)
log.info(`Saved file: ${attachmentPath}`)
} catch (e) {
log.error('保存文件错误:', e)
}
break
}
case types.Message.Video: {
// 接收到的消息是视频
log.info('接收到的消息是视频')
const video = await msg.toFileBox() // Save the media message as a FileBox
const videoPath = 'file/' + video.name
try {
await video.toFile(videoPath, true)
log.info(`Saved file: ${videoPath}`)
} catch (e) {
log.error('保存文件错误:', e)
}
break
}
case types.Message.Audio: {
// 接收到的消息是音频
log.info('接收到的消息是音频')
break
}
case types.Message.Emoticon: {
// 接收到的消息是表情
log.info('接收到的消息是表情')
const emoticon = await msg.toFileBox() // Save the media message as a FileBox
const emoticonPath = 'file/' + emoticon.name
try {
await emoticon.toFile(emoticonPath, true)
log.info(`Saved file: ${emoticonPath}`)
} catch (e) {
log.error('保存文件错误:', e)
}
break
}
case types.Message.Url:{
// 接收到的消息是链接
log.info('接收到的消息是链接')
try {
const urlLink = await msg.toUrlLink()
log.info('链接标题:', urlLink.title)
log.info('链接地址:', urlLink.url)
} catch (e) {
log.error('获取链接错误:', e)
}
break
}
case types.Message.MiniProgram: {
// 接收到的消息是小程序
log.info('接收到的消息是小程序')
try {
const miniProgram = await msg.toMiniProgram()
log.info('小程序标题:', miniProgram.title)
log.info('小程序描述:', miniProgram.description)
log.info('小程序页面地址:', miniProgram.pagePath)
log.info('小程序缩略图:', miniProgram.thumbUrl)
} catch (e) {
log.error('获取小程序错误:', e)
}
break
}
case types.Message.Transfer: // 接收到的消息是转账
log.info('接收到的消息是转账')
break
case types.Message.RedEnvelope: // 接收到的消息是红包
log.info('接收到的消息是红包')
break
case types.Message.Recalled: // 接收到的消息是撤回的消息
log.info('接收到的消息是撤回的消息')
break
case types.Message.Location: // 接收到的消息是位置
log.info('接收到的消息是位置')
break
case types.Message.GroupNote: // 接收到的消息是群公告
log.info('接收到的消息是群公告')
break
case types.Message.Contact: // 接收到的消息是联系人名片
log.info('接收到的消息是联系人名片')
break
case types.Message.ChatHistory: // 接收到的消息是聊天记录
log.info('接收到的消息是聊天记录')
break
case types.Message.Post: // 接收到的消息是公众号文章
log.info('接收到的消息是公众号文章')
break
case types.Message.Unknown: // 接收到的消息是未知类型
log.info('接收到的消息是未知类型')
break
default:
break
}
} catch (e) {
log.error('获取消息内容错误:', e)
}
// 关键词回复,同时也是发送消息的方法
}
const bot = WechatyBuilder.build({
name: 'ding-dong-bot',
puppet: 'wechaty-puppet-xp',
puppetOptions: {
version: '3.9.2.23',
},
})
bot.on('scan', onScan)
bot.on('login', onLogin)
bot.on('logout', onLogout)
bot.on('message', onMessage)
bot.start()
.then(() => {
log.info('StarterBot', 'Starter Bot Started.')
return null
})
.catch(e => log.error('StarterBot', e))
const main = async () => {
// 获取当前登录微信信息
try {
const self = bot.currentUser
log.info('当前登录账号信息:', self)
} catch (e) {
log.error('get user failed', e)
}
// 通过微信号搜索联系人
try {
const contactById = await bot.Contact.find({
id: 'filehelper',
})
log.info('微信号查找联系人:', contactById)
// 向联系人发送消息
await contactById?.say('向指定好友微信号发送消息')
} catch (e) {
log.error('contactByWeixin', e)
}
// 通过昵称搜索联系人
try {
const contactByName = await bot.Contact.find({
name: '文件传输助手',
})
log.info('昵称查找联系人:', contactByName)
// 向联系人发送消息
await contactByName?.say('向指定好友昵称发送消息')
} catch (e) {
log.error('contactByName', e)
}
// 通过备注搜索联系人
try {
const contactByAlias = await bot.Contact.find({
alias: '超哥',
})
log.info('备注名称查找联系人:', contactByAlias || '没有找到联系人')
// 向联系人发送消息
await contactByAlias?.say('向指定好友备注好友发送消息')
} catch (e) {
log.error('contactByAlias', e)
}
try {
// 通过群ID搜索群
const roomById = await bot.Room.find({
id: '21341182572@chatroom',
})
log.info('群ID查找群:', roomById)
// 向群里发送消息
await roomById?.say('向指定群ID发送消息')
} catch (e) {
log.error('roomById', e)
}
try {
// 通过群名称搜索群
const roomByName = await bot.Room.find({
topic: '大师是群主',
})
log.info('群名称查找群:', roomByName || '没有找到群')
// 向群里发送消息
await roomByName?.say('向指定群名称发送消息')
} catch (e) {
log.error('roomByName', e)
}
try {
// 获取所有联系人
const contactList = await bot.Contact.findAll()
// log.info('获取联系人列表:', contactList)
log.info('联系人数量:', contactList.length)
} catch (e) {
log.error('contactList', e)
}
try {
// 获取所有群
const roomList = await bot.Room.findAll()
// log.info('获取群列表:', roomList)
log.info('群数量:', roomList.length)
} catch (e) {
log.error('roomList', e)
}
}