{"id":15035306,"url":"https://github.com/nodewechat/wechat4u","last_synced_at":"2025-05-14T12:10:11.185Z","repository":{"id":43642707,"uuid":"52418159","full_name":"nodeWechat/wechat4u","owner":"nodeWechat","description":"微信 wechat web 网页版接口的 JavaScript 实现，兼容Node和浏览器，微信机器人","archived":false,"fork":false,"pushed_at":"2024-07-26T07:48:06.000Z","size":1156,"stargazers_count":1832,"open_issues_count":55,"forks_count":310,"subscribers_count":90,"default_branch":"master","last_synced_at":"2025-05-04T08:04:33.415Z","etag":null,"topics":["bot","wechat","wechat4u"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nodeWechat.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-02-24T05:55:22.000Z","updated_at":"2025-04-28T19:29:20.000Z","dependencies_parsed_at":"2024-09-25T00:29:17.683Z","dependency_job_id":"f813b9df-3256-4b9d-899f-99ead285c818","html_url":"https://github.com/nodeWechat/wechat4u","commit_stats":{"total_commits":222,"total_committers":21,"mean_commits":"10.571428571428571","dds":0.5180180180180181,"last_synced_commit":"f66fb69a352b4775210edd87d1101d7a165de797"},"previous_names":["nodewechat/wechat4u.js"],"tags_count":20,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodeWechat%2Fwechat4u","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodeWechat%2Fwechat4u/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodeWechat%2Fwechat4u/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nodeWechat%2Fwechat4u/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nodeWechat","download_url":"https://codeload.github.com/nodeWechat/wechat4u/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254140760,"owners_count":22021219,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bot","wechat","wechat4u"],"created_at":"2024-09-24T20:28:07.880Z","updated_at":"2025-05-14T12:10:06.168Z","avatar_url":"https://github.com/nodeWechat.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# wechat4u.js\n\n![](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\u0026chl=http%3A%2F%2Fweixin.qq.com%2Fg%2FA1zJ47b19KtgMnAx\u0026chs=180x180\u0026choe=UTF-8\u0026chld=L|0)\n\n## Announcing wechat4u v0.7.14\n\n### Features\n\n- 导出和导入保持微信登录的必要数据 bot.botData ([#160](https://github.com/nodeWechat/wechat4u/pull/160))\n- 修改联系人备注 bot.updateRemarkName(UserName, RemarkName) ([#121](https://github.com/nodeWechat/wechat4u/pull/121))\n- 修改群名 bot.updateChatRoomName(ChatRoomUserName, NewName) ([#168](https://github.com/nodeWechat/wechat4u/pull/168))\n- 转发消息 bot.forwardMsg(msg, toUserName)\n- 撤回消息 bot.revokeMsg(MsgID, toUserName)\n\n### Changes\n\n- 修复大文件上传失败问题\n- 支持uos协议，所有微信均可使用，请先微信实名认证后使用\n- 发送消息的一类方法在成功时会返回完整响应数据\n- bot.user 对象中不再存储用户头像的 base64 数据\n- 移除 example 目录\n- 修复 Contact 和 Message 中数据某些数据不可枚举\n- 向上层代码传递完整的 Error 对象，并将原来的中文错误描述放在 err.tips\n- bot.getContact(Seq) 方法增加 Seq 参数，支持增量获取完整联系人\n\n## 安装使用\n\n```\nnpm install --save wechat4u@latest\n```\n\n```javascript\nconst Wechat = require('wechat4u')\nlet bot = new Wechat()\nbot.start()\n// 或使用核心API\n// const WechatCore = require('wechat4u/lib/core')\n```\n\n## 开发测试\n\n```shell\ngit clone https://github.com/nodeWechat/wechat4u.git\ncd wechat4u\nnpm install\nnpm run core // 命令行模式\nnpm run compile // babel编译\n```\n\n## 使用范例\n\n`node run-core.js`\n\n逻辑见[代码](https://github.com/nodeWechat/wechat4u/blob/master/run-core.js)，简明完整，一定要看\n\n## 实例化Wechat类\n\n```javascript\nlet bot = new Wechat([botData])\n```\n\n若传入`botData`，则使用此机器人信息，重新开始之前的同步\n\n## 实例属性\n\n所有属性均只读\n\n### bot.botData\n\n可导出的实例基本信息，在下次new新bot时，可以填入此信息，重新同步\n\n### bot.PROP\n\n保持登录状态的必要信息\n\n### bot.CONF\n\n配置信息，包括当前服务器地址，API路径和一些常量\n\n程序中需要使用CONF中的常量来判断当前状态的新消息类型\n\n```javascript\nbot.state == bot.CONF.STATE.init // 初始化状态\nbot.state == bot.CONF.STATE.uuid // 已获取 UUID\nbot.state == bot.CONF.STATE.login // 已登录\nbot.state == bot.CONF.STATE.logout // 已退出登录\nmsg.MsgType == bot.CONF.MSGTYPE_TEXT // 文本消息\nmsg.MsgType == bot.CONF.MSGTYPE_IMAGE // 图片消息\nmsg.MsgType == bot.CONF.MSGTYPE_VOICE // 语音消息\nmsg.MsgType == bot.CONF.MSGTYPE_EMOTICON // 自定义表情消息\nmsg.MsgType == bot.CONF.MSGTYPE_MICROVIDEO // 小视频消息\nmsg.MsgType == bot.CONF.MSGTYPE_VIDEO // 视频消息\n```\n\n### bot.state\n\n当前状态\n\n### bot.user\n\n当前登录用户信息\n\n### bot.contacts\n\n所有联系人，包括通讯录联系人，近期联系群，公众号\n\nkey为联系人UserName，UserName是本次登录时每个联系人的UUID，不过下次登录会改变\n\nvalue为`Contact`对象，具体属性方法见`src/interface/contact.js`\n\n### msg\n\n登录后接受到的所有消息\n\nmsg为`Message`对象，具体属性方法见`src/interface/message.js`\n\n## 实例API\n\n### bot.start()\n\n启动实例，登录和保持同步\n\n调用该方法后，通过监听事件来处理消息\n\n### bot.restart()\n\n利用实例已获取的必要信息，重新登录和进行同步\n\n### bot.stop()\n\n停止实例，退出登录\n\n调用该方法后，通过监听`logout`事件来登出\n\n### bot.setPollingMessageGetter(getter)\n\n自定义心跳消息内容\n\n`getter` 函数返回心跳消息内容\n\n`typeof(getter())` 应为 `\"string\"`\n\n```javascript\nbot.setRollingMessageGetter(function () {\n  //\n  return (new Date()).toJSON();\n});\n```\n\n### bot.setPollingIntervalGetter(getter)\n\n自定义心跳间隔\n\n`getter` 函数返回心跳间隔（以毫秒为单位）\n\n`typeof(getter())` 应为 `\"number\"`\n\n```javascript\nbot.setRollingIntervalGetter(function () {\n  return 2 * 60 * 1000;\n});\n```\n\n### bot.setPollingTargetGetter(getter)\n\n自定义心跳目标用户\n\n`getter` 函数返回目标用户的 `UserName` （形如 `@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx` ）\n\n`typeof(getter())` 应为 `\"string\"`\n\n注： 如要使用 `bot.user.UserName` ，需在 `login` 事件后定义目标用户\n\n```javascript\nbot.setRollingmeGetter(function () {\n  return bot.user.UserName;\n});\n```\n\n\u003e 以下方法均返回Promise\n\n### bot.sendText(msgString, toUserName)\n\n发送文本消息，可以包含emoji(😒)和QQ表情([坏笑])\n\n### bot.uploadMedia(Buffer | Stream | File, filename, toUserName)\n\n上传媒体文件\n\n返回\n\n```javascript\n{\n  name: name,\n  size: size,\n  ext: ext,\n  mediatype: mediatype,\n  mediaId: mediaId\n}\n```\n\n### bot.sendPic(mediaId, toUserName)\n\n发送图片，mediaId为uploadMedia返回的mediaId\n\n```javascript\nbot.uploadMedia(fs.createReadStream('test.png'))\n  .then(res =\u003e {\n    return bot.sendPic(res.mediaId, ToUserName)\n  })\n  .catch(err =\u003e {\n    console.log(err)\n  })\n```\n\n### bot.sendEmoticon(md5 | mediaId, toUserName)\n\n发送表情，可是是表情的MD5或者uploadMedia返回的mediaId\n\n表情的MD5，可以自己计算但是可能不存在在微信服务器中，也可以从微信返回的表情消息中获得\n\n### bot.sendVideo(mediaId, toUserName)\n\n发送视频\n\n### bot.sendDoc(mediaId, name, size, ext, toUserName)\n\n以应用卡片的形式发送文件，可以通过这个API发送语音\n\n### bot.sendMsg(msg, toUserName)\n\n对以上发送消息的方法的封装，是发送消息的通用方法\n\n当`msg`为string时，发送文本消息\n\n当`msg`为`{file:xxx,filename:'xxx.ext'}`时，发送对应媒体文件\n\n当`msg`为`{emoticonMd5:xxx}`时，发送表情\n\n```javascript\nbot.sendMsg({\n    file: request('https://raw.githubusercontent.com/nodeWechat/wechat4u/master/bot-qrcode.jpg'),\n    filename: 'bot-qrcode.jpg'\n  }, ToUserName)\n  .catch(err =\u003e {\n    console.log(err)\n  })\n```\n\n### bot.forwardMsg(msg, toUserName)\n\n转发消息，`msg`为`message`事件传递的`msg`对象\n\n### bot.revokeMsg(MsgID, toUserName)\n\n撤回消息\n\n`MsgID`为发送消息后返回的代表消息的ID\n\n```javascript\nbot.sendMsg('测试撤回', toUserName)\n  .then(res =\u003e {\n    return bot.revokeMsg(res.MsgID, toUserName)\n  })\n  .catch(err =\u003e {\n    console.log(err)\n  })\n```\n\n### bot.getContact(Seq)\n\n获取通讯录中的联系人\n\n`Seq` 上一次调用 bot.getContact 后返回的 seq，第一次调用可不传\n\n### bot.batchGetContact(contacts)\n\n批量获取指定联系人数据\n\n`contacts` 数组，指定需要获取的数据\n\n当`contacts`为`[{UserName: xxx}]`时，可获取指定联系人或群信息\n\n当`contacts`为`[{UserName: xxx, EncryChatRoomId: xxx}]`时，可获取指定群内成员详细信息，EncryChatRoomId 可从群信息中获得\n\n### bot.getHeadImg(HeadImgUrl)\n\n获取联系人头像\n\n```javascript\nbot.getHeadImg(bot.contacts[UserName].HeadImgUrl).then(res =\u003e {\n  fs.writeFileSync(`${UserName}.jpg`, res.data)\n}).catch(err =\u003e {\n  console.log(err)\n})\n```\n\n### bot.getMsgImg(MsgId)\n\n获取图片或表情\n\n```javascript\nbot.getMsgImg(msg.MsgId).then(res =\u003e {\n  fs.writeFileSync(`${msg.MsgId}.jpg`, res.data)\n}).catch(err =\u003e {\n  console.log(err)\n})\n```\n\n### bot.getVoice(MsgId)\n\n获取语音\n\n### bot.getVideo(MsgId)\n\n获取小视频或视频\n\n### bot.getDoc(UserName, MediaId, FileName)\n\n获取文件，消息的`MsgType`为49且`AppMsgType`为6时即为文件。\n\n\n### bot.addFriend(UserName, Content)\n\n添加好友\n\n`UserName` 一般可从群信息中获得\n\n`Content` 验证信息\n\n### bot.verifyUser(UserName, Ticket)\n\n通过好友添加请求\n\n### bot.createChatroom(Topic, MemberList)\n\n创建群\n\n`Topic` 群聊名称\n\n`MemberList` 数组, 除自己外至少两人的UserName，格式为 [ {\"UserName\":\"@250d8d156ad9f8b068c2e3df3464ecf2\"}, {\"UserName\":\"@42d725733741de6ac53cbe3738d8dd2e\"} ]\n\n### bot.updateChatroom(ChatRoomUserName, MemberList, fun)\n\n更新群成员\n\n`ChatRoomUserName` '@@'开头的群UserName\n\n`MemberList` 数组，联系人UserNa\n\n`fun` 可选'addmember'，'delmember'，'invitemember'\n\n### bot.updateChatRoomName(ChatRoomUserName, NewName)\n\n更新群名称\n\n`ChatRoomUserName` '@@'开头的群UserName\n\n`NewName` 字符串，新的群名称\n\n### bot.opLog(UserName, OP)\n\n置顶或取消置顶联系人，可通过直接取消置顶群来获取群ChatRoomOwner\n\nOP == 0 取消置顶\n\nOP == 1 置顶\n\n### bot.updateRemarkName(UserName, RemarkName)\n\n设置联系人备注或标签\n\n## 实例事件\n\n### uuid\n\n得到uuid，之后可以构造二维码或从微信服务器取得二维码\n\n```javascript\nbot.on('uuid', uuid =\u003e {\n  qrcode.generate('https://login.weixin.qq.com/l/' + uuid, {\n    small: true\n  })\n  console.log('二维码链接：', 'https://login.weixin.qq.com/qrcode/' + uuid)\n})\n```\n\n### user-avatar\n\n手机扫描后可以得到登录用户头像的Data URL\n\n### login\n\n手机确认登录\n\n### logout\n\n成功登出\n\n### contacts-updated\n\n联系人更新，可得到已更新的联系人列表\n\n### message\n\n所有通过同步得到的消息，通过`msg.MsgType`判断消息类型\n\n```javascript\nbot.on('message', msg =\u003e {\n  switch (msg.MsgType) {\n    case bot.CONF.MSGTYPE_STATUSNOTIFY:\n      break\n    case bot.CONF.MSGTYPE_TEXT:\n      break\n    case bot.CONF.MSGTYPE_RECALLED:\n      break\n  }\n})\n```\n\n### error\n\n## Contact对象和Message对象\n\n每个contact，继承自 interface/contact，除原本 json 外，扩展以下属性：\n\n```javascript\ncontact.AvatarUrl // 处理过的头像地址\ncontact.isSelf    // 是否是登录用户本人\n\ncontact.getDisplayName()\ncontact.canSearch(keyword)\n```\n\n此外，wechat4u 在实例上提供 Contact 作为联系人的通用接口，扩展以下属性：\n\n```javascript\nwechat.Contact.isRoomContact()\nwechat.Contact.isSpContact()\nwechat.Contact.isPublicContact()\n\nwechat.Contact.getUserByUserName()\nwechat.Contact.getSearchUser(keyword)\n```\n\n每个msg 对象继承自 interface/message，出原本 json 外，具有以下属性：\n\n```javascript\nmessage.isSendBySelf // 是否是本人发送\n\nmessage.isSendBy(contact)\nmessage.getPeerUserName() // 获取所属对话的联系人 UserName\nmessage.getDisplayTime() // 获取形如 12:00 的时间戳信息\n```\n\n## 相关项目\n\n关于微信网页端机器人的实现，已经有大量的轮子了。感谢各位大神！（排名不分先后。。收录的肯定也不齐。。）\n\n- [Python2 的 WeixinBot](https://github.com/Urinx/WeixinBot)\n- [QT 的 QWX](https://github.com/xiangzhai/qwx)\n- [Node，可能会写成uProxy插件的 uProxy_wechat](https://github.com/LeMasque/uProxy_wechat)\n- [Node，可在shell中直接运行的 wechat-user-bot](https://github.com/HalfdogStudio/wechat-user-bot)\n- [Python3 的 wechat_robot](https://github.com/lyyyuna/wechat_robot)\n- [开放协议 支持 QQ\u0026微信 的 wxagent](https://github.com/kitech/wxagent)\n- [在微信网页版和 IRC 间搭建通道支持 IRC 操作的 wechatircd](https://github.com/MaskRay/wechatircd)\n- [Chrome 插件版的微信机器人](https://github.com/spacelan/weixin-bot-chrome-extension)\n\n关于微信网页端的接口说明，也有好几篇分析的很厉害的文章。\n\n- [Reverland 大神的web 微信与基于node的微信机器人实现](http://reverland.org/javascript/2016/01/15/webchat-user-bot/)\n- [Urinx 大神的 API Map](https://github.com/Urinx/WeixinBot/blob/master/README.md)\n- [聂永 大神的 微信协议简单调研笔记](http://www.blogjava.net/yongboy/archive/2014/03/05/410636.html)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodewechat%2Fwechat4u","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnodewechat%2Fwechat4u","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnodewechat%2Fwechat4u/lists"}