npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

jo-wechat4u

v0.0.9

Published

web wechat lib for user

Downloads

15

Readme

wechat4u.js

[email protected]更新了大量API,增强了稳定性

测试服务器wechat4u.duapp.com

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

安装使用

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(Stream | File)

上传媒体文件,返回:

{
  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.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)

获取小视频或视频

实例事件

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 的时间戳信息

相关项目

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

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

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