Skip to content

Commit

Permalink
Dev (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
danni-cool authored Mar 27, 2024
2 parents e6ea08e + c30ebcf commit fd88f43
Show file tree
Hide file tree
Showing 17 changed files with 294 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/publishNPM.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ jobs:
# 安装 pnpm
- name: Install pnpm
run: npm install -g pnpm

# 构建包必要文件
- name: Build package files
run: pnpm run build:cli

# 安装依赖
- name: Install Dependencies
Expand Down
60 changes: 51 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[🚢 Docker 镜像](https://hub.docker.com/repository/docker/dannicool/docker-wechatbot-webhook/general) | [📦 NPM包](https://www.npmjs.com/package/wechatbot-webhook)[🔍 FAQ](https://github.com/danni-cool/wechatbot-webhook/issues/72)
</div>

开箱即用的微信webhook机器人,通过 http 接口调用即可实现微信消息的发送和接收,作为基于 wechaty 的消息机器人服务在稳定性上做了较多优化。
一个小小的微信机器人webhook,帮你抹平了很多自己开发的障碍,基于 http 请求

## ✨ Features

Expand All @@ -34,7 +34,8 @@
| 接收文件 || |
| 接收公众号推文链接 || |
| 接收系统通知 | ✅ 上线通知 / 掉线通知 / 异常通知 | |
| [快捷回复](https://github.com/danni-cool/wechatbot-webhook?tab=readme-ov-file#2-%E6%94%B6%E6%B6%88%E6%81%AF-api) |||
| [头像获取](#33-获取静态资源接口) || |
| [快捷回复](#2-%E6%94%B6%E6%B6%88%E6%81%AF-api) |||
| **<群管理>** | | |
| **<好友管理>** | | |
| 接收好友申请 || |
Expand Down Expand Up @@ -410,12 +411,11 @@ curl --location 'https://your.recvdapi.com' \
#### token 配置说明
> 除了在 docker 启动时配置token,在默认缺省 token 的情况,会默认生成一个写入 `.env` 文件中
#### `/login?token=[YOUR_PERSONAL_TOKEN]`

- **描述**:获取登录二维码接口。
#### 3.1 获取登录二维码接口
- **地址**`/login`
- **methods**: `GET`
- **query**: token

- **example**: http://localhost:3001/login?token=[YOUR_PERSONAL_TOKEN]
**status**: `200`

##### 登录成功
Expand All @@ -430,23 +430,65 @@ curl --location 'https://your.recvdapi.com' \

展示微信登录扫码页面

#### `/healthz?token=[YOUR_PERSONAL_TOKEN]`
#### 3.2 健康检测接口

可以主动轮询该接口,检查服务是否正常运行

- **描述**健康检测接口。
- **地址**`/healthz`
- **methods**: `GET`
- **query**: token
- **status**: `200`
- **example**: http://localhost:3001/healthz?token=[YOUR_PERSONAL_TOKEN]

微信已登录, 返回纯文本 `healthy`,否则返回 `unHealthy`

#### 3.3 获取静态资源接口

从 2.8.0 版本开始,可以通过本接口访问到头像等静态资源,具体见 [recvd_api 数据结构示例的 avatar 字段](/docs/recvdApi.example.md#2-formdatasource-string)

注意所有上报 recvd_api 的静态资源地址不会默认带上 token, 需要自己拼接,否则会返回 401 错误, 请确保自己微信已登录,需要通过登录态去获取资源

- **地址**`/resouces`
- **methods**: `GET`
- **query**:
- token: 登录token
- media: encode过的相对路径,比如 `/avatar/1234567890.jpg` encode为 `avatar%2F1234567890.jpg`
- **status**: `200` `404` `401`

- **example**http://localhost:3001/resouces?media=%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxgetheadimg%3Fseq%3D83460%26username%3D%40%4086815a%26skey%3D&token=[YOUR_PERSONAL_TOKEN]

##### status: `200`

成功获取资源, 返回静态资源文件

##### status: `404`

获取资源失败

##### status: `401` 未携带登录token

```json
{"success":false, "message":"Unauthorized: Access is denied due to invalid credentials."}
```

##### status: `401` 微信登录态已过期

```json
{
"success": false, "message": "you must login first"
}
```


## 🌟 Star History

[![Star History Chart](https://api.star-history.com/svg?repos=danni-cool/wechatbot-webhook&type=Date)](https://star-history.com/#danni-cool/wechatbot-webhook&Date)

## Contributors

![](https://contrib.rocks/image?repo=danni-cool/wechatbot-webhook)
Thanks to all our contributors!

<a href="https://github.com/danni-cool/wechatbot-webhook/graphs/contributors">![](https://contrib.rocks/image?repo=danni-cool/wechatbot-webhook)</a>

## ⏫ 更新日志

Expand Down
16 changes: 10 additions & 6 deletions docs/recvdApi.example.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"id": "@xxxasdfsf",
"payload": {
"alias": "",
"avatar": "",
"avatar": "http://localhost:3001/resouces?media=%2Fcgi-bin%2Fmmwebwx-bixxx", //请配合 token=[YOUR_PERSONAL_TOKEN] 解密
"friend": false,
"gender": 1,
"id": "@xxx",
Expand All @@ -98,7 +98,7 @@
"id": "@xxxasdfsf",
"payload": {
"alias": "",
"avatar": "",
"avatar": "http://localhost:3001/resouces?media=%2Fcgi-bin%2Fmmwebwx-bixxx", //请配合 token=[YOUR_PERSONAL_TOKEN] 解密
"friend": false,
"gender": 1,
"id": "@xxx",
Expand All @@ -125,7 +125,7 @@
"id": "@xxxasdfsf",
"payload": {
"alias": "",
"avatar": "",
"avatar": "http://localhost:3001/resouces?media=%2Fcgi-bin%2Fmmwebwx-bixxx", //请配合 token=[YOUR_PERSONAL_TOKEN] 解密
"friend": false,
"gender": 1,
"id": "@xxx",
Expand Down Expand Up @@ -166,7 +166,11 @@
"adminIdList": [],
"avatar": "xxxx", // 相对路径,应该要配合解密
"memberList": [
{id: '@xxxx', name:'昵称', alias: '备注名'/** 个人备注名,非群备注名 */ }
{
id: '@xxxx',
avatar: "http://localhost:3001/resouces?media=%2Fcgi-bin%2Fmmwebwx-bixxx", //请配合 token=[YOUR_PERSONAL_TOKEN] 解密
name:'昵称',
alias: '备注名'/** 个人备注名,非群备注名 */ }
]
},
//以下暂不清楚什么用途,如有兴趣,请查阅 wechaty 官网文档
Expand All @@ -181,7 +185,7 @@

"payload": {
"alias": "", //备注名
"avatar": "xxx",
"avatar": "http://localhost:3001/resouces?media=%2Fcgi-bin%2Fmmwebwx-bixxx", //请配合 token=[YOUR_PERSONAL_TOKEN] 解密
"friend": false,
"gender": 1,
"id": "@xxx",
Expand All @@ -202,7 +206,7 @@

"payload": {
"alias": "",
"avatar": "xxx",
"avatar": "http://localhost:3001/resouces?media=%2Fcgi-bin%2Fmmwebwx-bixxx", //请配合 token=[YOUR_PERSONAL_TOKEN] 解密
"city": "北京",
"friend": true,
"gender": 1,
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"form-data": "^4.0.0",
"gerror": "^1.0.16",
"hono": "^3.11.11",
"lodash.clonedeep": "^4.5.0",
"log4js": "^6.9.1",
"mime": "^3.0.0",
"node-fetch-commonjs": "^3.3.2",
Expand All @@ -64,6 +65,8 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.2",
"husky": "^8.0.3",
"i": "^0.3.7",
"npm": "^10.5.0",
"prettier": "^3.1.1",
"tsc-files": "^1.1.4",
"typescript": "^5.3.3",
Expand Down
95 changes: 95 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/config/const.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const path = require('path')
const { PORT } = process.env

const config = {
/**
* 上报消息的api群成员缓存多久(单位:ms)
* @type {number}
*/
roomCachedTime: 1000 * 60 * 5
roomCachedTime: 1000 * 60 * 5,
/** 服务启动地址 */
localUrl: `http://localhost:${PORT}`
}

const { homeEnvCfg, homeMemoryCardPath } = process.env
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/loginCheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports.loginCheck = async (c, next) => {
c.status(401)
return c.json({
success: false,
message: 'you must login first before sending messages'
message: 'you must login first'
})
}

Expand Down
1 change: 1 addition & 0 deletions src/route/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ module.exports = function registerRoute({ app, bot }) {

require('./msg')({ app, bot })
require('./login')({ app, bot })
require('./resouces')({ app, bot })
}
39 changes: 39 additions & 0 deletions src/route/resouces.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const { downloadFile } = require('../utils/index')
const middleware = require('../middleware')
/**
* 通过该接口代理获取微信静态资源
* @param {Object} param
* @param {import('hono').Hono} param.app
* @param {import('wechaty').Wechaty} param.bot
*/
module.exports = function registerResourceAgentRoute({ app, bot }) {
app.get(
'/resouces',
middleware.loginCheck,
/** @param {import('hono').Context} c */
async (c) => {
// 暂时不考虑其他puppet的情况
const cookie =
// @ts-ignore 私有变量
bot.__puppet._memory.payload['\rpuppet\nPUPPET-WECHAT4U'].COOKIE
const mediaUrl = c.req.query('media')
const fullResouceUrl = `https://wx2.qq.com${decodeURIComponent(
mediaUrl || ''
)}`

const { buffer, contentType } = await downloadFile(fullResouceUrl, {
Cookie: Object.entries(cookie).reduce(
(pre, next) => (pre += `${next[0]}=${next[1]};`),
''
)
})
if (buffer) {
contentType && c.header('Content-Type', contentType)
return c.body(buffer)
} else {
c.status(404)
return c.json({ success: false, message: '获取资源失败' })
}
}
)
}
Loading

0 comments on commit fd88f43

Please sign in to comment.