Skip to content

Commit 5d0275e

Browse files
spacelanstonexer
authored andcommitted
合并core,新版本 (#75)
* Wechat4u core版本 * Move getClientMsgId to util * Update sync * Add status report * Update too much Update too much * Fix cookie bug wx2.qq.com wx8.qq.com * Update README.md and version * Update babel runtime
1 parent 040f1b9 commit 5d0275e

18 files changed

+1689
-1021
lines changed

.babelrc

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
{
22
"env": {
33
"development": {
4-
"presets": ["es2015", "stage-2"]
4+
"presets": ["es2015", "es2017"],
5+
"plugins": [
6+
["transform-runtime", {
7+
"polyfill": false,
8+
"regenerator": true
9+
}]
10+
]
511
},
612
"production": {
7-
"presets": ["es2015-rollup"]
13+
"presets": ["es2015", "es2017"],
14+
"plugins": [
15+
["transform-runtime", {
16+
"polyfill": false,
17+
"regenerator": true
18+
}]
19+
]
820
}
921
}
10-
}
22+
}

README.md

+209-87
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,240 @@
1-
#wechat4u.js
1+
# wechat4u.js
2+
23
![](http://7xr8pm.com1.z0.glb.clouddn.com/nodeWechat.png)
34

4-
尝试功能的话,可在demo服务器(简单功能)中测试,支持多用户实例。
5-
* [测试服务器](http://wechat4u.duapp.com/#!/login)
5+
[email protected]更新了大量API,增强了稳定性
6+
7+
测试服务器[wechat4u.duapp.com](http://wechat4u.duapp.com)
8+
9+
<small>具有文本表情自动回复,监控,群发功能</small>
610

7-
##安装使用
11+
## 安装使用
12+
13+
```
14+
npm install --save wechat4u@latest
815
```
9-
npm install wechat4u
16+
17+
```javascript
18+
const Wechat = require('wechat4u')
19+
let bot = new Wechat()
20+
bot.start()
21+
// 或使用核心API
22+
// const WechatCore = require('wechat4u/lib/core')
1023
```
1124

12-
##使用Example测试
25+
## 开发测试
26+
1327
```
14-
npm run example
28+
git clone https://github.com/nodeWechat/wechat4u.git
29+
cd wechat4u
30+
npm install
31+
npm run example // web服务器模式
32+
npm run core // 命令行模式
33+
npm run compile // babel编译
1534
```
1635

17-
## API 说明
36+
## 使用范例
37+
38+
`node run-core.js`
39+
40+
逻辑见代码,简明完整
41+
42+
## 实例属性
43+
44+
所有属性均只读
1845

19-
####引入 wechat4u
46+
##### bot.PROP
47+
48+
保持登录状态的必要信息
49+
50+
##### bot.CONF
51+
52+
配置信息,包括当前服务器地址,API路径和一些常量
53+
54+
程序中需要使用CONF中的常量来判断当前状态的新消息类型
2055

2156
```javascript
22-
const wechat4u = require('wechat4u')
57+
bot.state == bot.CONF.STATE.init // 初始化状态
58+
bot.state == bot.CONF.STATE.uuid // 已获取 UUID
59+
bot.state == bot.CONF.STATE.login // 已登录
60+
bot.state == bot.CONF.STATE.logout // 已退出登录
61+
msg.MsgType == bot.CONF.MSGTYPE_TEXT // 文本消息
62+
msg.MsgType == bot.CONF.MSGTYPE_IMAGE // 图片消息
63+
msg.MsgType == bot.CONF.MSGTYPE_VOICE // 语音消息
64+
msg.MsgType == bot.CONF.MSGTYPE_EMOTICON // 自定义表情消息
65+
msg.MsgType == bot.CONF.MSGTYPE_MICROVIDEO // 小视频消息
66+
msg.MsgType == bot.CONF.MSGTYPE_VIDEO // 视频消息
2367
```
2468

25-
####生成实例
69+
##### bot.state
70+
71+
当前状态
72+
73+
##### bot.user
74+
75+
当前登录用户信息
76+
77+
##### bot.contacts
78+
79+
所有联系人,包括通讯录联系人,近期联系群,公众号
80+
81+
key为联系人UserName,UserName是本次登录时每个联系人的UUID,不过下次登录会改变
82+
83+
value为`Contact`对象,具体属性方法见`src/interface/contact.js`
84+
85+
##### msg
86+
87+
登录后接受到的所有消息
88+
89+
msg为`Message`对象,具体属性方法见`src/interface/message.js`
90+
91+
## 实例API
92+
93+
##### bot.start()
94+
95+
启动实例,登录和保持同步
96+
97+
调用该方法后,通过监听事件来处理消息
98+
99+
##### bot.stop()
100+
101+
停止实例,退出登录
102+
103+
调用该方法后,通过监听`logout`事件来登出
104+
105+
#### 以下方法均返回Promise
106+
107+
##### bot.sendText(msgString, toUserName)
108+
109+
发送文本消息,可以包含emoji(😒)和QQ表情([坏笑])
110+
111+
##### bot.uploadMedia(Stream | File)
112+
113+
上传媒体文件,返回:
26114

27115
```javascript
28-
let wechat = new wechat4u()
116+
{
117+
name: name,
118+
size: size,
119+
ext: ext,
120+
mediatype: mediatype,
121+
mediaId: mediaId
122+
}
29123
```
30124

31-
####启动(分两种方式)
125+
##### bot.sendPic(mediaId, toUserName)
126+
127+
发送图片,mediaId为uploadMedia返回的mediaId
32128

33129
```javascript
34-
// 1. 分布启动
35-
wechat.getUUID().then(uuid => {/*处理uuid*/})
36-
wechat.start() // 返回一个 Promise 对象
130+
bot.uploadMedia(fs.createReadStream('test.png'))
131+
.then(res => {
132+
return bot.sendPic(res.mediaId, ToUserName)
133+
})
134+
.catch(err => {
135+
console.log(err)
136+
})
137+
```
138+
139+
##### bot.sendEmoticon(md5 | mediaId, toUserName)
140+
141+
发送表情,可是是表情的MD5或者uploadMedia返回的mediaId
142+
143+
表情的MD5,可以自己计算但是可能不存在在微信服务器中,也可以从微信返回的表情消息中获得
144+
145+
##### bot.sendVideo(mediaId, toUserName)
146+
147+
发送视频
148+
149+
##### bot.sendDoc(mediaId, name, size, ext, toUserName)
150+
151+
以应用卡片的形式发送文件,可以通过这个API发送语音
152+
153+
##### bot.getHeadImg(HeadImgUrl)
37154

38-
// 2. 直接启动
39-
wechat.start() // 通过事件获得uuid等信息
155+
获取联系人头像
156+
157+
```javascript
158+
bot.getHeadImg(bot.contacts[UserName].HeadImgUrl).then(res => {
159+
fs.writeFileSync(`${UserName}.jpg`, res.data)
160+
}).catch(err => {
161+
console.log(err)
162+
})
40163
```
41164

42-
####实例状态判断
165+
##### bot.getMsgImg(MsgId)
166+
167+
获取图片或表情
43168

44169
```javascript
45-
wechat.state === wechat4u.STATE.init === 'init' // 初始化状态
46-
wechat.state === wechat4u.STATE.uuid === 'uuid' // 已获取 UUID
47-
wechat.state === wechat4u.STATE.login === 'login' // 已登录
48-
wechat.state === wechat4u.STATE.logout === 'logout' // 已退出登录
170+
bot.getMsgImg(msg.MsgId).then(res => {
171+
fs.writeFileSync(`${msg.MsgId}.jpg`, res.data)
172+
}).catch(err => {
173+
console.log(err)
174+
})
49175
```
50176

51-
####联系人接口
177+
##### bot.getVoice(MsgId)
178+
179+
获取语音
180+
181+
##### bot.getVideo(MsgId)
182+
183+
获取小视频或视频
184+
185+
## 实例事件
186+
187+
##### uuid
188+
189+
得到uuid,之后可以构造二维码或从微信服务器取得二维码
52190

53191
```javascript
54-
wechat.friendList // (* 不建议使用)通讯录(个人联系人,群聊)
55-
56-
wechat.user // 登陆账号
57-
wechat.memberList // 所有联系人 [...]
58-
wechat.contactList // 个人联系人 [...]
59-
wechat.groupList // 已保存群聊 [...]
60-
wechat.groupMemberList // 所有群聊内联系人 [...]
61-
wechat.publicList // 公众账号 [...]
62-
wechat.specialList // 特殊账号 [...]
192+
bot.on('uuid', uuid => {
193+
qrcode.generate('https://login.weixin.qq.com/l/' + uuid, {
194+
small: true
195+
})
196+
console.log('二维码链接:', 'https://login.weixin.qq.com/qrcode/' + uuid)
197+
})
63198
```
64199

65-
数组中的每个 contact,继承自 interface/contact,除原本 json 外,扩展以下属性:
200+
##### user-avatar
201+
202+
手机扫描后可以得到登录用户头像的Data URL
203+
204+
##### login
205+
206+
手机确认登录
207+
208+
##### logout
209+
210+
成功登出
211+
212+
##### contacts-updated
213+
214+
联系人更新,可得到已更新的联系人列表
215+
216+
##### message
217+
218+
所有通过同步得到的消息,通过`msg.MsgType`判断消息类型
219+
220+
```javascript
221+
bot.on('message', msg => {
222+
switch (msg.MsgType) {
223+
case bot.CONF.MSGTYPE_STATUSNOTIFY:
224+
break
225+
case bot.CONF.MSGTYPE_TEXT:
226+
break
227+
case bot.CONF.MSGTYPE_RECALLED:
228+
break
229+
}
230+
})
231+
```
232+
233+
##### error
234+
235+
## Contact对象和Message对象
236+
237+
每个contact,继承自 interface/contact,除原本 json 外,扩展以下属性:
66238

67239
```javascript
68240
contact.AvatarUrl // 处理过的头像地址
@@ -83,50 +255,7 @@ wechat.Contact.getUserByUserName()
83255
wechat.Contact.getSearchUser(keyword)
84256
```
85257

86-
####消息发送接口
87-
88-
```javascript
89-
wechat.sendMsg(msg, to) // 发送文字消息
90-
wechat.sendImage(to, fileStream, type, size) // 发送图片消息
91-
// 使用 fs 的 createdReadStream 的样例:
92-
// let imgPath = __dirname + '/../public/images/nodeWechat.png'
93-
// let imgStats = fs.statSync(imgPath)
94-
// wechat.sendImage(user['UserName'], fs.createReadStream(imgPath))
95-
```
96-
97-
####Events
98-
99-
```javascript
100-
wechat.on('uuid', uuid => {})
101-
wechat.on('scan', () => {})
102-
wechat.on('confirm', () => {})
103-
wechat.on('login', memberList => {})
104-
wechat.on('logout', msg => {})
105-
wechat.on('error', err => debug(err))
106-
107-
wechat.on('init-message', () => {})
108-
wechat.on('text-message', msg => {})
109-
wechat.on('picture-message', msg => {})
110-
wechat.on('voice-message', msg => {})
111-
wechat.on('emoticon-message', msg => {})
112-
wechat.on('verify-message', msg => {})
113-
```
114-
115-
####消息收取接口
116-
117-
```javascript
118-
wechat.on('text-message', msg => {
119-
msg.Content // '你好!'
120-
})
121-
wechat.on('picture-message', msg => {
122-
msg.Content // {type:'image/jpeg',data:...buf...}
123-
})
124-
wechat.on('voice-message', msg => {
125-
msg.Content // {type:'audio/mp3'',data:...buf...}
126-
})
127-
```
128-
129-
msg 对象继承自 interface/message,出原本 json 外,具有以下属性:
258+
每个msg 对象继承自 interface/message,出原本 json 外,具有以下属性:
130259

131260
```javascript
132261
message.isSendBySelf // 是否是本人发送
@@ -136,15 +265,8 @@ message.getPeerUserName() // 获取所属对话的联系人 UserName
136265
message.getDisplayTime() // 获取形如 12:00 的时间戳信息
137266
```
138267

139-
####请求接口
140-
141-
```javascript
142-
wechat.request() // 包含相关 cookie 的 request,目前使用 axios
143-
```
144-
145-
*如无特别强调,接口皆返回一个 promise 对象
146268

147-
##相关项目
269+
## 相关项目
148270

149271
关于微信网页端机器人的实现,已经有大量的轮子了。感谢各位大神!(排名不分先后。。收录的肯定也不齐。。)
150272

@@ -154,7 +276,7 @@ wechat.request() // 包含相关 cookie 的 request,目前使用 axios
154276
* [Node,可在shell中直接运行的 wechat-user-bot](https://github.com/HalfdogStudio/wechat-user-bot)
155277
* [Python3 的 wechat_robot](https://github.com/lyyyuna/wechat_robot)
156278
* [开放协议 支持 QQ&微信 的 wxagent](https://github.com/kitech/wxagent)
157-
* [在微信网页版和 IRC 间搭建通道支持 IRC 操作的 wechatircd](https://github.com/MaskRay/wechatircd)
279+
* [在微信网页版和 IRC 间搭建通道支持 IRC 操作的 wechatircd](https://github.com/MaskRay/wechatircd)
158280
* [Chrome 插件版的微信机器人](https://github.com/spacelan/weixin-bot-chrome-extension)
159281

160282
关于微信网页端的接口说明,也有好几篇分析的很厉害的文章。

example/.babelrc

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{
2-
"presets": ["es2015", "stage-2"],
3-
"plugins": ["transform-runtime"],
2+
"presets": ["es2015", "es2017"],
3+
"plugins": [
4+
["transform-runtime", {
5+
"polyfill": false,
6+
"regenerator": true
7+
}]
8+
],
49
"comments": false
510
}

0 commit comments

Comments
 (0)