Skip to content

Commit 07b31aa

Browse files
committed
feat: support notifying multiple wecom group
1 parent 6ebbc6b commit 07b31aa

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ jobs:
2121
content: '## Test' # see https://developer.work.weixin.qq.com/document/path/91770
2222
key: ${{ secrets.WECOM_BOT_KEY }} # Your key of wecom bot hook
2323
```
24+
25+
> [!TIP]
26+
> If you want to send message to multiple wecom bot, you can use `,` to split the bot keys.

src/main.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,18 @@ function validateBotKey(botKey: string): boolean {
2424

2525
// 发送消息到企业微信机器人的函数,支持多种消息类型
2626
async function sendMessageToWeComBot(
27-
botKey: string,
27+
botKey: string[],
2828
type: MessageTypeValue,
2929
message: string
3030
): Promise<void> {
31-
const url = `${WeComBotHookBaseUrl}?key=${encodeURIComponent(botKey)}`
32-
let payload
31+
let payload: any
3332

3433
switch (type) {
3534
case MessageType.TEXT:
3635
payload = {msgtype: 'text', text: {content: message}}
3736
break
3837
case MessageType.MARKDOWN:
3938
payload = {msgtype: 'markdown', markdown: {content: message}}
40-
core.debug(`payload: ${payload}`)
4139
break
4240
case MessageType.IMAGE:
4341
// message should be base64 encoded image
@@ -57,11 +55,23 @@ async function sendMessageToWeComBot(
5755
}
5856

5957
try {
60-
const res = await axios.post(url, payload)
61-
// core.info('Message sent to WeCom Bot successfully.')
62-
core.info(
63-
`${MessageType.MARKDOWN} ${type} ${JSON.stringify(payload)} ${JSON.stringify(res.data)}`
64-
)
58+
// 发送消息到企业微信机器人
59+
core.info('Sending message to WeCom Bot...')
60+
const tasks = botKey.map(key => {
61+
return async () => {
62+
if (!validateBotKey(key)) {
63+
core.setFailed('Invalid or missing wecom bot hook key.')
64+
return
65+
}
66+
const url = `${WeComBotHookBaseUrl}?key=${encodeURIComponent(key)}`
67+
const res = await axios.post(url, payload)
68+
core.info('Message sent to WeCom Bot successfully.')
69+
core.info(
70+
`${MessageType.MARKDOWN} ${type} ${JSON.stringify(payload)} ${JSON.stringify(res.data)}`
71+
)
72+
}
73+
})
74+
await Promise.all(tasks)
6575
} catch (error: any) {
6676
core.error(`Failed to send message to WeCom Bot: ${error.message}`)
6777
core.setFailed(error)
@@ -70,11 +80,9 @@ async function sendMessageToWeComBot(
7080
}
7181

7282
async function run() {
73-
const wxWorkBotKey = core.getInput('key', {required: true})
74-
if (!validateBotKey(wxWorkBotKey)) {
75-
core.setFailed('Invalid or missing wecom bot hook key.')
76-
return
77-
}
83+
const keyStr = core.getInput('key', {required: true})
84+
85+
const keys = keyStr.split(',')
7886

7987
// 获取消息内容和消息类型
8088
const msgContent = core.getInput('content', {required: true})
@@ -102,7 +110,7 @@ async function run() {
102110
core.debug(`Message Type: ${msgType}`)
103111

104112
// 发送消息
105-
await sendMessageToWeComBot(wxWorkBotKey, msgType, msgContent)
113+
await sendMessageToWeComBot(keys, msgType, msgContent)
106114
}
107115

108116
run()

0 commit comments

Comments
 (0)