# wechat4u.js
![](http://7xr8pm.com1.z0.glb.clouddn.com/nodeWechat.png) [![npm version](https://img.shields.io/npm/v/wechat4u.svg)](https://www.npmjs.org/package/wechat4u) [![wechat group](https://img.shields.io/badge/wechat-group-brightgreen.svg)](http://www.qr-code-generator.com/phpqrcode/getCode.php?cht=qr&chl=http%3A%2F%2Fweixin.qq.com%2Fg%2FA1zJ47b19KtgMnAx&chs=180x180&choe=UTF-8&chld=L|0)
## Announcing wechat4u v0.7.14
### Features
- 导出和导入保持微信登录的必要数据 bot.botData ([#160](https://github.com/nodeWechat/wechat4u/pull/160))
- 修改联系人备注 bot.updateRemarkName(UserName, RemarkName) ([#121](https://github.com/nodeWechat/wechat4u/pull/121))
- 修改群名 bot.updateChatRoomName(ChatRoomUserName, NewName) ([#168](https://github.com/nodeWechat/wechat4u/pull/168))
- 转发消息 bot.forwardMsg(msg, toUserName)
- 撤回消息 bot.revokeMsg(MsgID, toUserName)
### Changes
- 修复大文件上传失败问题
- 支持uos协议,所有微信均可使用,请先微信实名认证后使用
- 发送消息的一类方法在成功时会返回完整响应数据
- bot.user 对象中不再存储用户头像的 base64 数据
- 移除 example 目录
- 修复 Contact 和 Message 中数据某些数据不可枚举
- 向上层代码传递完整的 Error 对象,并将原来的中文错误描述放在 err.tips
- bot.getContact(Seq) 方法增加 Seq 参数,支持增量获取完整联系人
## 安装使用
```
npm install --save wechat4u@latest
```
```javascript
const Wechat = require('wechat4u')
let bot = new Wechat()
bot.start()
// 或使用核心API
// const WechatCore = require('wechat4u/lib/core')
```
## 开发测试
```shell
git clone https://github.com/nodeWechat/wechat4u.git
cd wechat4u
npm install
npm run core // 命令行模式
npm run compile // babel编译
```
## 使用范例
`node run-core.js`
逻辑见[代码](https://github.com/nodeWechat/wechat4u/blob/master/run-core.js),简明完整,一定要看
## 实例化Wechat类
```javascript
let bot = new Wechat([botData])
```
若传入`botData`,则使用此机器人信息,重新开始之前的同步
## 实例属性
所有属性均只读
### bot.botData
可导出的实例基本信息,在下次new新bot时,可以填入此信息,重新同步
### bot.PROP
保持登录状态的必要信息
### bot.CONF
配置信息,包括当前服务器地址,API路径和一些常量
程序中需要使用CONF中的常量来判断当前状态的新消息类型
```javascript
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.restart()
利用实例已获取的必要信息,重新登录和进行同步
### bot.stop()
停止实例,退出登录
调用该方法后,通过监听`logout`事件来登出
### bot.setPollingMessageGetter(getter)
自定义心跳消息内容
`getter` 函数返回心跳消息内容
`typeof(getter())` 应为 `"string"`
```javascript
bot.setRollingMessageGetter(function () {
//
return (new Date()).toJSON();
});
```
### bot.setPollingIntervalGetter(getter)
自定义心跳间隔
`getter` 函数返回心跳间隔(以毫秒为单位)
`typeof(getter())` 应为 `"number"`
```javascript
bot.setRollingIntervalGetter(function () {
return 2 * 60 * 1000;
});
```
### bot.setPollingTargetGetter(getter)
自定义心跳目标用户
`getter` 函数返回目标用户的 `UserName` (形如 `@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx` )
`typeof(getter())` 应为 `"string"`
注: 如要使用 `bot.user.UserName` ,需在 `login` 事件后定义目标用户
```javascript
bot.setRollingmeGetter(function () {
return bot.user.UserName;
});
```
> 以下方法均返回Promise
### bot.sendText(msgString, toUserName)
发送文本消息,可以包含emoji(😒)和QQ表情([坏笑])
### bot.uploadMedia(Buffer | Stream | File, filename, toUserName)
上传媒体文件
返回
```javascript
{
name: name,
size: size,
ext: ext,
mediatype: mediatype,
mediaId: mediaId
}
```
### bot.sendPic(mediaId, toUserName)
发送图片,mediaId为uploadMedia返回的mediaId
```javascript
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.sendMsg(msg, toUserName)
对以上发送消息的方法的封装,是发送消息的通用方法
当`msg`为string时,发送文本消息
当`msg`为`{file:xxx,filename:'xxx.ext'}`时,发送对应媒体文件
当`msg`为`{emoticonMd5:xxx}`时,发送表情
```javascript
bot.sendMsg({
file: request('https://raw.githubusercontent.com/nodeWechat/wechat4u/master/bot-qrcode.jpg'),
filename: 'bot-qrcode.jpg'
}, ToUserName)
.catch(err => {
console.log(err)
})
```
### bot.forwardMsg(msg, toUserName)
转发消息,`msg`为`message`事件传递的`msg`对象
### bot.revokeMsg(MsgID, toUserName)
撤回消息
`MsgID`为发送消息后返回的代表消息的ID
```javascript
bot.sendMsg('测试撤回', toUserName)
.then(res => {
return bot.revokeMsg(res.MsgID, toUserName)
})
.catch(err => {
console.log(err)
})
```
### bot.getContact(Seq)
获取通讯录中的联系人
`Seq` 上一次调用 bot.getContact 后返回的 seq,第一次调用可不传
### bot.batchGetContact(contacts)
批量获取指定联系人数据
`contacts` 数组,指定需要获取的数据
当`contacts`为`[{UserName: xxx}]`时,可获取指定联系人或群信息
当`contacts`为`[{UserName: xxx, EncryChatRoomId: xxx}]`时,可获取指定群内成员详细信息,EncryChatRoomId 可从群信息中获得
### bot.getHeadImg(HeadImgUrl)
获取联系人头像
```javascript
bot.getHeadImg(bot.contacts[UserName].HeadImgUrl).then(res => {
fs.writeFileSync(`${UserName}.jpg`, res.data)
}).catch(err => {
console.log(err)
})
```
### bot.getMsgImg(MsgId)
获取图片或表情
```javascript
bot.getMsgImg(msg.MsgId).then(res => {
fs.writeFileSync(`${msg.MsgId}.jpg`, res.data)
}).catch(err => {
console.log(err)
})
```
### bot.getVoice(MsgId)
获取语音
### bot.getVideo(MsgId)
获取小视频或视频
### bot.getDoc(UserName, MediaId, FileName)
获取文件,消息的`MsgType`为49且`AppMsgType`为6时即为文件。
### bot.addFriend(UserName, Content)
添加好友
`UserName` 一般可从群信息中获得
`Content` 验证信息
### bot.verifyUser(UserName, Ticket)
通过好友添加请求
### bot.createChatroom(Topic, MemberList)
创建群
`
没有合适的资源?快使用搜索试试~ 我知道了~
微信 wechat web 网页版接口的 JavaScript 实现,兼容Node和浏览器,微-wechat4u.zip
共29个文件
js:12个
json:2个
webwxsync:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 122 浏览量
2024-01-30
21:26:31
上传
评论
收藏 579KB ZIP 举报
温馨提示
微信 wechat web 网页版接口的 JavaScript 实现,兼容Node和浏览器,微-wechat4u
资源推荐
资源详情
资源评论
收起资源包目录
微信 wechat web 网页版接口的 JavaScript 实现,兼容Node和浏览器,微-wechat4u.zip (29个子文件)
wechat4u-master
.eslintrc 133B
.editorconfig 63B
yarn.lock 126KB
media
test.mp4 345KB
test.txt 7KB
test.gif 52KB
src
wechat.js 11KB
interface
message.js 2KB
contact.js 3KB
core.js 36KB
util
conf.js 8KB
request.js 2KB
index.js 74B
global.js 2KB
run-core.js 7KB
.babelrc 25B
package.json 1KB
package-lock.json 426KB
.npmignore 503B
test
unit.js 4KB
response
webwxinit 11KB
webwxsync 13KB
webwxgetcontact 7KB
webwxbatchgetcontact 2KB
nock.js 4KB
index.js 132B
.gitignore 594B
README.md 12KB
bot-qrcode.jpg 41KB
共 29 条
- 1
资源评论
武昌库里写JAVA
- 粉丝: 3136
- 资源: 1872
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功