Skip to content

Commit

Permalink
合并core,新版本 (#75)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
spacelan authored and stonexer committed Oct 17, 2016
1 parent 040f1b9 commit 5d0275e
Show file tree
Hide file tree
Showing 18 changed files with 1,689 additions and 1,021 deletions.
18 changes: 15 additions & 3 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
{
"env": {
"development": {
"presets": ["es2015", "stage-2"]
"presets": ["es2015", "es2017"],
"plugins": [
["transform-runtime", {
"polyfill": false,
"regenerator": true
}]
]
},
"production": {
"presets": ["es2015-rollup"]
"presets": ["es2015", "es2017"],
"plugins": [
["transform-runtime", {
"polyfill": false,
"regenerator": true
}]
]
}
}
}
}
296 changes: 209 additions & 87 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,68 +1,240 @@
#wechat4u.js
# wechat4u.js

![](http://7xr8pm.com1.z0.glb.clouddn.com/nodeWechat.png)

尝试功能的话,可在demo服务器(简单功能)中测试,支持多用户实例。
* [测试服务器](http://wechat4u.duapp.com/#!/login)
wechat4u@0.5.x更新了大量API,增强了稳定性

测试服务器[wechat4u.duapp.com](http://wechat4u.duapp.com)

<small>具有文本表情自动回复,监控,群发功能</small>

##安装使用
## 安装使用

```
npm install --save wechat4u@latest
```
npm install wechat4u

```javascript
const Wechat = require('wechat4u')
let bot = new Wechat()
bot.start()
// 或使用核心API
// const WechatCore = require('wechat4u/lib/core')
```

##使用Example测试
## 开发测试

```
npm run example
git clone https://github.com/nodeWechat/wechat4u.git
cd wechat4u
npm install
npm run example // web服务器模式
npm run core // 命令行模式
npm run compile // babel编译
```

## API 说明
## 使用范例

`node run-core.js`

逻辑见代码,简明完整

## 实例属性

所有属性均只读

####引入 wechat4u
##### bot.PROP

保持登录状态的必要信息

##### bot.CONF

配置信息,包括当前服务器地址,API路径和一些常量

程序中需要使用CONF中的常量来判断当前状态的新消息类型

```javascript
const wechat4u = require('wechat4u')
bot.state == bot.CONF.STATE.init // 初始化状态
bot.state == bot.CONF.STATE.uuid // 已获取 UUID
bot.state == bot.CONF.STATE.login // 已登录
bot.state == bot.CONF.STATE.logout // 已退出登录
msg.MsgType == bot.CONF.MSGTYPE_TEXT // 文本消息
msg.MsgType == bot.CONF.MSGTYPE_IMAGE // 图片消息
msg.MsgType == bot.CONF.MSGTYPE_VOICE // 语音消息
msg.MsgType == bot.CONF.MSGTYPE_EMOTICON // 自定义表情消息
msg.MsgType == bot.CONF.MSGTYPE_MICROVIDEO // 小视频消息
msg.MsgType == bot.CONF.MSGTYPE_VIDEO // 视频消息
```

####生成实例
##### bot.state

当前状态

##### bot.user

当前登录用户信息

##### bot.contacts

所有联系人,包括通讯录联系人,近期联系群,公众号

key为联系人UserName,UserName是本次登录时每个联系人的UUID,不过下次登录会改变

value为`Contact`对象,具体属性方法见`src/interface/contact.js`

##### msg

登录后接受到的所有消息

msg为`Message`对象,具体属性方法见`src/interface/message.js`

## 实例API

##### bot.start()

启动实例,登录和保持同步

调用该方法后,通过监听事件来处理消息

##### bot.stop()

停止实例,退出登录

调用该方法后,通过监听`logout`事件来登出

#### 以下方法均返回Promise

##### bot.sendText(msgString, toUserName)

发送文本消息,可以包含emoji(😒)和QQ表情([坏笑])

##### bot.uploadMedia(Stream | File)

上传媒体文件,返回:

```javascript
let wechat = new wechat4u()
{
name: name,
size: size,
ext: ext,
mediatype: mediatype,
mediaId: mediaId
}
```

####启动(分两种方式)
##### bot.sendPic(mediaId, toUserName)

发送图片,mediaId为uploadMedia返回的mediaId

```javascript
// 1. 分布启动
wechat.getUUID().then(uuid => {/*处理uuid*/})
wechat.start() // 返回一个 Promise 对象
bot.uploadMedia(fs.createReadStream('test.png'))
.then(res => {
return bot.sendPic(res.mediaId, ToUserName)
})
.catch(err => {
console.log(err)
})
```

##### bot.sendEmoticon(md5 | mediaId, toUserName)

发送表情,可是是表情的MD5或者uploadMedia返回的mediaId

表情的MD5,可以自己计算但是可能不存在在微信服务器中,也可以从微信返回的表情消息中获得

##### bot.sendVideo(mediaId, toUserName)

发送视频

##### bot.sendDoc(mediaId, name, size, ext, toUserName)

以应用卡片的形式发送文件,可以通过这个API发送语音

##### bot.getHeadImg(HeadImgUrl)

// 2. 直接启动
wechat.start() // 通过事件获得uuid等信息
获取联系人头像

```javascript
bot.getHeadImg(bot.contacts[UserName].HeadImgUrl).then(res => {
fs.writeFileSync(`${UserName}.jpg`, res.data)
}).catch(err => {
console.log(err)
})
```

####实例状态判断
##### bot.getMsgImg(MsgId)

获取图片或表情

```javascript
wechat.state === wechat4u.STATE.init === 'init' // 初始化状态
wechat.state === wechat4u.STATE.uuid === 'uuid' // 已获取 UUID
wechat.state === wechat4u.STATE.login === 'login' // 已登录
wechat.state === wechat4u.STATE.logout === 'logout' // 已退出登录
bot.getMsgImg(msg.MsgId).then(res => {
fs.writeFileSync(`${msg.MsgId}.jpg`, res.data)
}).catch(err => {
console.log(err)
})
```

####联系人接口
##### bot.getVoice(MsgId)

获取语音

##### bot.getVideo(MsgId)

获取小视频或视频

## 实例事件

##### uuid

得到uuid,之后可以构造二维码或从微信服务器取得二维码

```javascript
wechat.friendList // (* 不建议使用)通讯录(个人联系人,群聊)

wechat.user // 登陆账号
wechat.memberList // 所有联系人 [...]
wechat.contactList // 个人联系人 [...]
wechat.groupList // 已保存群聊 [...]
wechat.groupMemberList // 所有群聊内联系人 [...]
wechat.publicList // 公众账号 [...]
wechat.specialList // 特殊账号 [...]
bot.on('uuid', uuid => {
qrcode.generate('https://login.weixin.qq.com/l/' + uuid, {
small: true
})
console.log('二维码链接:', 'https://login.weixin.qq.com/qrcode/' + uuid)
})
```

数组中的每个 contact,继承自 interface/contact,除原本 json 外,扩展以下属性:
##### user-avatar

手机扫描后可以得到登录用户头像的Data URL

##### login

手机确认登录

##### logout

成功登出

##### contacts-updated

联系人更新,可得到已更新的联系人列表

##### message

所有通过同步得到的消息,通过`msg.MsgType`判断消息类型

```javascript
bot.on('message', msg => {
switch (msg.MsgType) {
case bot.CONF.MSGTYPE_STATUSNOTIFY:
break
case bot.CONF.MSGTYPE_TEXT:
break
case bot.CONF.MSGTYPE_RECALLED:
break
}
})
```

##### error

## Contact对象和Message对象

每个contact,继承自 interface/contact,除原本 json 外,扩展以下属性:

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

####消息发送接口
```javascript
wechat.sendMsg(msg, to) // 发送文字消息
wechat.sendImage(to, fileStream, type, size) // 发送图片消息
// 使用 fs 的 createdReadStream 的样例:
// let imgPath = __dirname + '/../public/images/nodeWechat.png'
// let imgStats = fs.statSync(imgPath)
// wechat.sendImage(user['UserName'], fs.createReadStream(imgPath))
```
####Events
```javascript
wechat.on('uuid', uuid => {})
wechat.on('scan', () => {})
wechat.on('confirm', () => {})
wechat.on('login', memberList => {})
wechat.on('logout', msg => {})
wechat.on('error', err => debug(err))

wechat.on('init-message', () => {})
wechat.on('text-message', msg => {})
wechat.on('picture-message', msg => {})
wechat.on('voice-message', msg => {})
wechat.on('emoticon-message', msg => {})
wechat.on('verify-message', msg => {})
```
####消息收取接口
```javascript
wechat.on('text-message', msg => {
msg.Content // '你好!'
})
wechat.on('picture-message', msg => {
msg.Content // {type:'image/jpeg',data:...buf...}
})
wechat.on('voice-message', msg => {
msg.Content // {type:'audio/mp3'',data:...buf...}
})
```
msg 对象继承自 interface/message,出原本 json 外,具有以下属性:
每个msg 对象继承自 interface/message,出原本 json 外,具有以下属性:

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

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

##相关项目
## 相关项目

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

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

关于微信网页端的接口说明,也有好几篇分析的很厉害的文章。
Expand Down
9 changes: 7 additions & 2 deletions example/.babelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"presets": ["es2015", "stage-2"],
"plugins": ["transform-runtime"],
"presets": ["es2015", "es2017"],
"plugins": [
["transform-runtime", {
"polyfill": false,
"regenerator": true
}]
],
"comments": false
}
Loading

0 comments on commit 5d0275e

Please sign in to comment.