Skip to content

Commit

Permalink
Merge pull request #8 from danni-cool/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
danni-cool authored Sep 29, 2023
2 parents d32ee7c + 5537a95 commit d388299
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 88 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
PORT=3001
# 如果想自己处理收到消息的逻辑,在下面填上你的webhook地址, 默认为空
RECVD_MSG_WEBHOOK=
# 如果想自己处理收到消息的逻辑,在下面填上你的API地址, 默认为空
LOCAL_RECVD_MSG_API=
140 changes: 109 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@

基于 [wechaty](https://github.com/wechaty/wechaty#readme)[Express](https://github.com/expressjs/express) 开发

在微信和webhook机器人之间架一座桥梁,从此微信里也可以有自己的webhook机器人了,快用它集成到自己的自动化工作流中( 推荐 [n8n](https://github.com/n8n-io/n8n))吧
在微信和webhook机器人之间架一座桥梁,从此微信里也可以有自己的webhook机器人了,快用它集成到自己的自动化工作流中吧,推荐 [n8n](https://github.com/n8n-io/n8n)

![Docker Image Version (latest semver)](https://img.shields.io/docker/v/dannicool/docker-wechatbot-webhook) ![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/danni-cool/docker-wechatbot-webhook/docker-build.yml) ![Docker Pulls](https://img.shields.io/docker/pulls/dannicool/docker-wechatbot-webhook)

[view this project on docker hub :)](https://hub.docker.com/repository/docker/dannicool/docker-wechatbot-webhook/general)

## 一、开始
## 一、启动

### 1. 本地调试

```
npm start
```

### 2. docker 启动
其他配置可以在 .env 文件中设置

```
# 如果想换端口
PORT=3001
# 如果想自己处理收到消息的逻辑,比如根据消息联动,在下面填上你的 API 地址, 默认为空
LOCAL_RECVD_MSG_API=https://example.com/your/url
```

### 2. Docker 部署

#### 拉取镜像

Expand All @@ -27,27 +36,30 @@ docker pull dannicool/docker-wechatbot-webhook
#### 启动容器(后台常驻)

```bash
docker run -d \
--name wcRoomBot \
docker run -d \
--name wxBotWebhook \
-p 3001:3001 \
-e RECVD_MSG_API="https://example.com/your/url" \
dannicool/docker-wechatbot-webhook
```
收消息钩子
> -e RECVD_MSG_API 如果想自己处理收到消息的逻辑,比如根据消息联动,填上你的处理逻辑url,该行可以省略
## 二、登录wx

#### 登录wx
以下只展示 docker 启动,本地调试可以直接在控制台找到链接

```bash
docker logs -f wcRoomBot
docker logs -f wxBotWebhook
```

找到二维码登录地址,图下 url 部分,浏览器访问,扫码登录wx

![](https://cdn.jsdelivr.net/gh/danni-cool/danni-cool@cdn/image/docker-login-wechat.png)

## 二、给机器人推送消息
![](https://cdn.jsdelivr.net/gh/danni-cool/danni-cool@cdn/image/docker-wechat-login-demo.png)

目前只支持 **文字****图片**,消息不支持图文自动拆分,请手动调多次
## 三、API

### webhook格式
### 1. 推消息

- Url:<http://localhost:3001/webhook/msg>
- Methods: `POST`
Expand All @@ -58,32 +70,98 @@ docker logs -f wcRoomBot

| 参数 | 说明 | 数据类型 | 默认值 | 可否为空 | 可选值 | 备注 |
|--|--|--|--|--|--|--|
| to | 会话名 | String | | N | | 发群消息填群名,发给个人填昵称 |
| isRoom | 是否发的群消息 | Boolean | false | Y | <ul><li>true</li><li>false</li></ul> | |
| type | 发送消息类型 | String || N | <ul><li>text</li><li>img</li></ul> | |
| content | 发送的消息 | String | | N | | 如果希望发多张图,type 指定为 img 同时,content 里填 url 以英文逗号分隔 |
| to | 会话名 | `String` | | N | | 发群消息填群名,发给个人填昵称 |
| isRoom | 是否发的群消息 | `Boolean` | `false` | Y | `true` / `false` | 这个参数决定了找人的时候找的是群还是人,因为昵称其实和群名相同在技术处理上 |
| type | 发送消息类型 | `String` | | N | `text` / `img` | 目前只支持 **文字****图片**,消息不支持图文自动拆分,请手动调多次 |
| content | 发送的消息 | `String` | | N | | 如果希望发多张图,type 指定为 img 同时,content 里填 url 以英文逗号分隔 |

<!-- ## 三、机器人收到消息
### 2. 收消息

> 目前收到消息也是使用webhook,逻辑单独放到外部流程去处理,如果觉得麻烦,想自己定制,欢迎 folk
> 收消息接口使用 `form` 传递参数,因为要兼容有文件的情况,文件目前也只兼容了**图片**
### 1. 配置收消息 webhook
入参:

#### 本地调试
找到项目里的 .env 文件
```
PORT=3001
# 如果想自己处理收到消息的逻辑,在下面填上你的webhook地址, 默认为空
RECVD_MSG_WEBHOOK=https://xxxx.com/web-hook/roomBot-msg-received
- Methods: `POST`
- ContentType: `multipart/form-data`
- Form格式如下

| formData | 说明 | 数据类型 | 可选值 |
|--|--|--|--|
| type | 表单类型 | `String` | `text` / `img` |
| content | 传输的内容,文件也放在这个字段,如果是图片收到的就是二进制buffer | `String` / `Binary` | |
| source | 消息的相关发送方数据, JSON String | `String` | |

source 字段示例

```json
{
// 消息来自群,会有以下对象,否则为空字符串
"room": {
"id": "@@xxx",
"topic": "abc" // 群名
"payload": {
"id": "@@xxxx",
"adminIdList": [],
"avatar": "xxxx", // 相对路径,应该要配合解密
"memberIdList": [ //群里人的id
"@xxxx",
"@xxxx"
],
},
//以下暂不清楚什么用途,如有兴趣,请查阅 wechaty 官网文档
"_events": {},
"_eventsCount": 0,
},


// 消息来自个人,会有以下对象,否则为空字符串
"to": {
"id": "@xxx",

"payload": {
"alias": "", //备注名
"avatar": "xxx",
"friend": false,
"gender": 1,
"id": "@xxx",
"name": "xxx",
"phone": [],
"signature": "hard mode",
"star": false,
"type": 1
},

"_events": {},
"_eventsCount": 0,
},

// 消息发送方
"from": {
"id": "@xxx",

"payload": {
"alias": "",
"avatar": "xxx",
"city": "北京",
"friend": true,
"gender": 1,
"id": "@xxxx",
"name": "abc", //昵称
"phone": [],
"province": "北京",
"star": false,
"type": 1
},

"_events": {},
"_eventsCount": 0,
}

}
```

### 2. 在群里 `@微信名` + 要说的话

## 四、更新日志
-->

## 、更新日志
## 、更新日志

更新内容参见 [CHANGELOG](https://github.com/danni-cool/docker-wechat-roomBot/blob/main/CHANGELOG.md)
2 changes: 1 addition & 1 deletion dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ RUN npm install
COPY . .

# 如果收消息想接入webhook
# ENV RECVD_MSG_WEBHOOK=
ENV RECVD_MSG_API=

# 暴露端口(你的 Express 应用程序监听的端口)
EXPOSE 3001
Expand Down
3 changes: 2 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ const wechatBotInit = require('./src/wechaty/init')
const registerRoute = require('./src/route')
const app = express();
const bot = wechatBotInit()
const chalk = require('chalk')

app.use(express.json());

// 注册webhook
registerRoute({app, bot})

app.listen(PORT, () => {
console.log(`service is running on http://localhost:${PORT}`);
console.log(`service is running on ${chalk.cyan('http://localhost:'+ PORT)}\n`);
});
36 changes: 20 additions & 16 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
},
"license": "MIT",
"dependencies": {
"chalk": "^4.1.2",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"form-data": "^4.0.0",
"node-fetch-commonjs": "^3.3.2",
"wechaty": "^1.20.2"
},
Expand Down
22 changes: 10 additions & 12 deletions src/route/msg.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { formatAndSendMsg } = require('../service/msg')
const { getUnvalidParamsList } = require('../utils/index')

module.exports = function registerPushHook({ app, bot }) {

Expand All @@ -8,18 +9,15 @@ module.exports = function registerPushHook({ app, bot }) {
const { to, isRoom = false, type, content } = req.body;

//校验必填参数
const checkList = [
{ key: 'to', val: to },
{ key: 'type', val: type },
{ key: 'content', val: content }
]

if (checkList.some(({ val }) => !val)) {
const unValidParamsStr = checkList
.filter(({ val }) => !val)
.map(({ key }) => key)
.join(',')

const unValidParamsStr = getUnvalidParamsList([
{ key: 'to', val: to, required: true, type: 'string', unValidReason: '' },
{ key: 'type', val: type, required: true, type: 'string', enum: ['text', 'img'], unValidReason: '' },
{ key: 'content', val: content, required: true, type: 'string', unValidReason: '' },
{ key: 'isRoom', val: isRoom, required: false, type: 'boolean', unValidReason: '' }
])
.map(({ unValidReason }) => unValidReason).join(',')

if (unValidParamsStr) {
return res.status(200).json({ success: false, message: `[${unValidParamsStr}] params is not valid, please checkout the api reference (https://github.com/danni-cool/docker-wechat-roomBot#body-%E5%8F%82%E6%95%B0%E8%AF%B4%E6%98%8E)` });
}

Expand Down
Loading

0 comments on commit d388299

Please sign in to comment.