au_wechat4u

0.6.10 • Public • Published

wechat4u.js

npm version wechat group

Have A Try ?

  • 测试服务器

wechat4u.duapp.com,具有自动回复(文本,表情),监控和群发等功能

  • 测试微信机器人

微信号:abotofwechat4u

扫描二维码,开启激情果撩,验证消息:我爱wechat4u

安装使用

wechat4u@0.6.x更新了大量API,增强了稳定性

npm install --save wechat4u@latest
const Wechat = require('wechat4u')
let bot = new Wechat()
bot.start()
// 或使用核心API
// const WechatCore = require('wechat4u/lib/core')

开发测试

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

使用范例

node run-core.js

逻辑见代码,简明完整,一定要看

实例属性

所有属性均只读

bot.PROP

保持登录状态的必要信息

bot.CONF

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

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

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(Buffer | Stream | File, filename, toUserName)

上传媒体文件

返回

{
  name: name,
  size: size,
  ext: ext,
  mediatype: mediatype,
  mediaId: mediaId
}
bot.sendPic(mediaId, toUserName)

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

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'}时,发送对应媒体文件

bot.forwardMsg(msg, toUserName)

转发消息,msgmessage事件传递的msg对象

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.getHeadImg(HeadImgUrl)

获取联系人头像

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

获取图片或表情

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.verifyUser(UserName, Ticket)

通过好友添加请求

bot.createChatroom(Topic, MemberList)

创建群

Topic 群聊名称

MemberList 数组, 除自己外至少两人的UserName,格式为 [ {"UserName":"@250d8d156ad9f8b068c2e3df3464ecf2"}, {"UserName":"@42d725733741de6ac53cbe3738d8dd2e"} ]

bot.updateChatroom(ChatRoomName, MemberList, fun)

更新群成员

ChatRoomName '@@'开头的群UserName

MemberList 数组,联系人UserNa

fun 可选'addmember','delmember','invitemember'

bot.opLog(UserName, OP)

置顶或取消置顶联系人,可通过直接取消置顶群来获取群ChatRoomOwner

OP == 0 取消置顶

OP == 1 置顶

bot.updateRemarkName(UserName, RemarkName)

设置联系人备注或标签

实例事件

uuid

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

bot.on('uuid', uuid => {
  qrcode.generate('https://login.weixin.qq.com/l/' + uuid, {
    small: true
  })
  console.log('二维码链接:', 'https://login.weixin.qq.com/qrcode/' + uuid)
})
user-avatar

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

login

手机确认登录

logout

成功登出

contacts-updated

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

message

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

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 外,扩展以下属性:

contact.AvatarUrl // 处理过的头像地址
contact.isSelf    // 是否是登录用户本人

contact.getDisplayName()
contact.canSearch(keyword)

此外,wechat4u 在实例上提供 Contact 作为联系人的通用接口,扩展以下属性:

wechat.Contact.isRoomContact()
wechat.Contact.isSpContact()
wechat.Contact.isPublicContact()

wechat.Contact.getUserByUserName()
wechat.Contact.getSearchUser(keyword)

每个msg 对象继承自 interface/message,出原本 json 外,具有以下属性:

message.isSendBySelf // 是否是本人发送

message.isSendBy(contact)
message.getPeerUserName() // 获取所属对话的联系人 UserName
message.getDisplayTime() // 获取形如 12:00 的时间戳信息

相关项目

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

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

好了,差不多就这些资料了。如果想要开发个自己的,那就开工吧!

Dependencies (9)

Dev Dependencies (33)

Package Sidebar

Install

npm i au_wechat4u

Weekly Downloads

1

Version

0.6.10

License

MIT

Last publish

Collaborators

  • auwaiwing