-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
58 lines (54 loc) · 2.14 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
var Wechat = require('./src/index')
// 全局配置
Wechat.config({
autoLogin: true, // 是否保存cookie 以便自动登录
openBrowser: false, // 是否在浏览器中打开二维码链接 (默认在terminal中显示)
})
// 初始化程序
Wechat.run(async () => {
// 获取联系人列表
let memberList = await Wechat.getContact()
// 获取账户信息
let ownerInfo = await Wechat.getOwnerInfo()
// 给指定用户发送消息(这里测试自己给自己发)
var ownUserName = ownerInfo.User.UserName;
let sendStatus = await Wechat.sendMsg(ownUserName, ownUserName, `hello word! 现在的时间是:${new Date()}`)
if (sendStatus) {
console.log('消息发送成功!')
}
// 自动回复消息
Wechat.listener.on('message', async data => {
// 群聊消息
if (data.type === 'Group') {
var groupName = data.fromUser.NickName;
var masterUserName = data.toUser.NickName;
if (data.msg.indexOf('exit') > -1) {
console.log('退出群聊')
await (Wechat.deleteMemberFromChatroom(data.fromUser.UserName, data.toUser.UserName))
}
if (data.msg.indexOf('rename') > -1) {
console.log('修改群聊名称')
console.log(await (Wechat.renameChatroom(data.fromUser.UserName, new Date().getTime())))
}
console.log(`群聊消息: ${masterUserName}(${groupName}): ${data.msg}`)
}
// 普通消息
else {
if (data.msg === '帮助') {
Wechat.sendMsg(ownUserName, data.fromUser.UserName,
`命令:
1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
2.xxxxxxxxxxxxxxxxxxxxxxxx
3.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
4.xxxxxxxxxxxxxxxxxxxxxxxxxxx
5.更多`)
}
console.log(`收到来自: ${data.fromUser.NickName} 的消息: ${data.msg}`)
}
// 自动回复
let sendStatus = await Wechat.sendMsg(ownUserName, data.fromUser.UserName, `自动回复: ${data.msg}`)
if (sendStatus) {
console.log('消息发送成功!')
}
})
})