0.1.9 • Published 8 years ago

wemax v0.1.9

Weekly downloads
20
License
MIT
Repository
github
Last release
8 years ago

Wemax

A wechat client for nodejs

安装使用

npm install --save wemax@latest
const Wemax = require('wemax');
let wemax = new Wemax({creds: ...});
wemax.start();
// 或使用原生 Wechat 接口方法
// const Wechat = require('wemax').Wechat;

开发测试

git clone https://github.com/taoyuan/wemax.git
cd wemax
npm install
npm run example // Web服务器模式
npm run bot     // 命令行模式
npm run compile // babel编译

使用范例

node bot.js

示例代码!

实例属性

状态和类型
wemax.state === 'init';       // 初始化状态
wemax.state === 'uuid';       // 已获取 UUID
wemax.state === 'login';      // 已登录
wemax.state === 'logout';     // 已退出登录

msg.type === 'text';          // 文本消息
msg.type === 'image';         // 图片消息
msg.type === 'voice';         // 语音消息
msg.type === 'emoticon';       // 自定义表情消息
msg.type === 'microvideo';     // 小视频消息
msg.type === 'video';          // 视频消息
wemax.creds

登陆凭证信息,login 事件触发时更新,此时可以读取并保存,下次登录时作为初始化参数,实现免登功能。

const Wemax = require('wemax');
let creds;
try {
  creds = fs.readJSONSync('./.etc/creds.json');
} catch (e) {
  // no-op
}

const bot = new Wemax({creds});

// ...

bot.on('login', () => {
  fs.writeJSONSync('./.etc/creds.json', bot.creds);
});
wemax.secret

保持登录状态的必要信息

wemax.state

当前状态

wemax.user

当前登录用户信息

wemax.contacts

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

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

value为Contact对象

msg

登录后接受到的所有消息

msg为Message对象

实例API

wemax.start()

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

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

wemax.stop()

停止实例,退出登录

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

以下方法均返回Promise

wemax.sendText(msgString, toUserName)

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

wemax.uploadMedia(Buffer | Stream | File, filename, toUserName)

上传媒体文件

返回

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

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

wemax.uploadMedia(fs.createReadStream('test.png'))
  .then(res => {
    return wemax.sendPic(res.mediaId, ToUserName)
  })
  .catch(err => {
    console.log(err)
  })
wemax.sendEmoticon(md5 | mediaId, toUserName)

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

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

wemax.sendVideo(mediaId, toUserName)

发送视频

wemax.sendDoc(mediaId, name, size, ext, toUserName)

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

wemax.sendMsg(msg, toUserName)

对以上发送消息的方法的封装,是发送消息的通用方法

msg为string时,发送文本消息

msg{file:xxx,filename:'xxx.ext'}时,发送对应媒体文件

wemax.forwardMsg(msg, toUserName)

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

wemax.sendMsg({
    file: request('http://cn.bing.com/az/hprichbg/rb/PlungeDiving_ZH-CN11143756334_1920x1080.jpg'),
    filename: 'image.jpg'
  }, ToUserName)
  .catch(err => {
    console.log(err)
  })
wemax.getHeadImg(HeadImgUrl)

获取联系人头像

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

获取图片或表情

wemax.getMsgImg(msg.MsgId).then(res => {
  fs.writeFileSync(`${msg.MsgId}.jpg`, res.data)
}).catch(err => {
  console.log(err)
})
wemax.getVoice(MsgId)

获取语音

wemax.getVideo(MsgId)

获取小视频或视频

wemax.verifyUser(UserName, Ticket)

通过好友添加请求

wemax.createChatroom(Topic, MemberList)

创建群

Topic 群聊名称

MemberList 数组, 除自己外至少两人的UserName,格式为

[
  {"UserName":"@250d8d156ad9f8b068c2e3df3464ecf2"},
  {"UserName":"@42d725733741de6ac53cbe3738d8dd2e"}
]
wemax.updateChatroom(ChatRoomName, MemberList, fun)

更新群成员

ChatRoomName '@@'开头的群UserName

MemberList 数组,联系人UserNa

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

wemax.opLog(UserName, OP)

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

OP == 0 取消置顶

OP == 1 置顶

wemax.updateRemarkName(UserName, RemarkName)

设置联系人备注或标签

实例事件

uuid

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

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

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

login

手机确认登录

logout

成功登出

contacts

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

message

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

wemax.on('message', msg => {
  switch (msg.type) {
    case 'statusnotify':
      break
    case 'text':
      break
    case 'recalled':
      break
  }
})
error

Contact

contact.uin               // 用户识别码(似乎是唯一的身份码)
contact.avatar            // 处理过的头像地址
contact.isLoggedIn        // 是否是登录用户本人
contact.sex               // 联系人性别,可以是:'male', 'female', 'unknown'
contact.type              // 联系人类型,可以是:'brand', 'sp', 'group', 'contact'
contact.username          // 联系人 UserName, 微信临时 id
contact.displayName       // 联系人显示名 RemarkName || DisplayName || NickName
contact.isMatch(keyword)  // 是否匹配关键字,可用于关键字查询

Message

message.getPeerUserName()
message.getDisplayTime()

相关项目

0.1.9

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago

0.0.4

8 years ago

0.0.3

8 years ago

0.0.2

8 years ago

0.0.1

8 years ago