Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

Commit

Permalink
feat: 🎸 增加了参数校验,docker tag 改为latest,更新部分注释
Browse files Browse the repository at this point in the history
  • Loading branch information
chentianyu committed Sep 20, 2023
1 parent 3878634 commit 61ddd8a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ jobs:

- name: Build and Push Docker Image
run: |
docker build . -t dannicool/docker-wechat-roombot:v$(node -p "require('./package.json').version")
docker push dannicool/docker-wechat-roombot:v$(node -p "require('./package.json').version")
docker build . -t dannicool/docker-wechat-roombot:latest
docker push dannicool/docker-wechat-roombot:latest
- name: commit and tag release
run: |
Expand Down
4 changes: 0 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@
## 1.0.1 (2023-09-19)



# 1.1.0 (2023-09-19)


### Features

* 🎸 增加推送支持多图推送 ([9c659ad](https://github.com/danni-cool/docker-wechat-roomBot/commit/9c659ad15e1365194df1a02560ef4307ed2ecae5))
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
## 1. 拉取镜像

```bash
docker pull dannicool/docker-wechat-roombot:v1.0.1
docker pull dannicool/docker-wechat-roombot
```

## 2. 启动容器(后台常驻)
Expand All @@ -22,7 +22,7 @@ docker pull dannicool/docker-wechat-roombot:v1.0.1
docker run -d \
--name wcRoomBot \
-p 3001:3001 \
dannicool/docker-wechat-roombot:v1.0.1
dannicool/docker-wechat-roombot
```

## 3. 登录wx
Expand All @@ -49,13 +49,13 @@ docker logs -f wcRoomBot

### Body 参数说明

> 如果希望发多张图,type指定为img,content里的url以英文逗号分隔
| 参数 | 说明 | 数据类型 | 可选值 | 可否为空 | 例子 |
|--|--|--|--|--|--|
| to | 群名 | String | | N | 这是群名 |
| to | 群名 | String | any | N | Test Group |
| type | 发送消息类型 | String | <ul><li>text</li><li>img</li></ul>| N | text |
| content | 发送的消息 | String | | N | 这是一条群消息 |
| content | 发送的消息 | String | any | N | 这是一条群消息 |

> 如果希望发多张图,type 指定为 img,content 里填 url 以英文逗号分隔
# Changelog

Expand Down
2 changes: 1 addition & 1 deletion dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ COPY . .
EXPOSE 3001

# 启动应用程序
CMD ["node", "main.js"]
CMD ["npm", "start"]
25 changes: 12 additions & 13 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { FileBox } = require('file-box') // bugfix: dependency of wechaty but can't not require from wechaty
const { FileBox } = require('file-box') // bugfix: wechaty can not export FileBox, but wechaty is rely on file-box package

const formatAndSendMsg = async function ({ type, content, msgInstance }) {

switch (type) {
// 纯文本
case 'text':
Expand All @@ -9,19 +10,17 @@ const formatAndSendMsg = async function ({ type, content, msgInstance }) {

// 图片
case 'img':
const imgArr = content.split(',')
// 逗号分割的多张图的情况
if (imgArr.length > 0) {
// 只有一张图
imgArr.length === 1 ?
await msgInstance.say(FileBox.fromUrl(imgArr[0])) :
// 多张图的情况
await (async () => {
for(let i=0; i< imgArr.length; i++){
await msgInstance.say(FileBox.fromUrl(imgArr[i]))
}
})()
}
const imgArr = content.split(',')
// 只有一张图
imgArr.length === 1 ?
await msgInstance.say(FileBox.fromUrl(imgArr[0])) :
// 多张图的情况
await (async () => {
for (let i = 0; i < imgArr.length; i++) {
await msgInstance.say(FileBox.fromUrl(imgArr[i]))
}
})()

return true
}
Expand Down
22 changes: 19 additions & 3 deletions src/webhook/RoomMsg.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,31 @@ module.exports = function registerRoomHook({ app, bot }) {
try {
const { to, 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(',')

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)` });
}

// 查找指定的 Room
const targetRoom = await bot.Room.find({ topic: to });

if (targetRoom) {
await Utils.formatAndSendMsg({type, content, msgInstance: targetRoom})
const sendStatus = await Utils.formatAndSendMsg({ type, content, msgInstance: targetRoom, res })
res.status(200).json({ success: sendStatus, message: `Message sent ${sendStatus ? 'successfully' : 'failed' }.` });

res.status(200).json({ success: true, message: 'Message sent successfully.' });
} else {
res.status(404).json({ success: false, message: 'Room not found.' });
res.status(200).json({ success: false, message: 'Room is not found' });
}
} catch (error) {
console.error('Error handling POST request:', error);
Expand Down

0 comments on commit 61ddd8a

Please sign in to comment.