Skip to content

Commit 7171645

Browse files
author
Hai Liang Wang
committed
#5 send notify into feishu for w3 post events
1 parent 470be2e commit 7171645

File tree

7 files changed

+221
-26
lines changed

7 files changed

+221
-26
lines changed

docker-compose.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ services:
77
environment:
88
- DEBUG=sync*
99
- NODE_ENV=${NODE_ENV:-development}
10-
- NOTIFY_FEISHU_GROUPS=${NOTIFY_FEISHU_GROUPS:-placeholder}
10+
- FEISHU_GROUP_GITHUB_BOTS=${FEISHU_GROUP_GITHUB_BOTS:-placeholder}
11+
- FEISHU_GROUP_W3_BOTS=${FEISHU_GROUP_W3_BOTS:-placeholder}
1112
ports:
1213
- "${SYNC_PORT:-8201}:8201"

sample.env

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ COMPOSE_FILE=docker-compose.yml
55
COMPOSE_PROJECT_NAME=cskefu.sync
66

77
NODE_ENV=development
8-
NOTIFY_FEISHU_GROUPS=
8+
FEISHU_GROUP_GITHUB_BOTS=
9+
FEISHU_GROUP_W3_BOTS=

sync/app/controllers/w3.ctrl.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* W3 Ctrl
3+
*/
4+
5+
const debug = require("debug")("sync:ctrl:w3");
6+
const utils = require("../utils/index");
7+
const feishuService = require("../services/feishu.service");
8+
9+
10+
11+
function Controller() {
12+
13+
}
14+
15+
/**
16+
* Handle W3 Events
17+
* @param {*} headers
18+
* @param {*} params
19+
* @param {*} body
20+
*/
21+
Controller.prototype.handleW3broadcast = async function (headers, params, body) {
22+
let ret = { "msg": "done" };
23+
/**
24+
* sync:routes:w3 /broadcast ctx.params {} , body
25+
{
26+
"link": "https://www.cskefu.com/2022/10/08/test-c/",
27+
"post_title": "Test C",
28+
"author_id": "99",
29+
"display_name": "Hai",
30+
"user_email": "h@cskefu.com",
31+
"user_profile": "https://www.cskefu.com/user/99/",
32+
"categories": [
33+
{
34+
"term_id": 118,
35+
"name": "业务观点",
36+
"slug": "business",
37+
"term_group": 0,
38+
"term_taxonomy_id": 118,
39+
"taxonomy": "category",
40+
"description": "业务、产品及服务的应用和解决方案:呼叫中心、联络中心、智能客服、客服机器人、CTI、云计算等。有价值的干货:应用场景、业务价
41+
值、创新方案。",
42+
"parent": 0,
43+
"count": 12,
44+
"filter": "raw",
45+
"cat_ID": 118,
46+
"category_count": 12,
47+
"category_description": "业务、产品及服务的应用和解决方案:呼叫中心、联络中心、智能客服、客服机器人、CTI、云计算等。有价值的干货:应用场
48+
景、业务价值、创新方案。",
49+
"cat_name": "业务观点",
50+
"category_nicename": "business",
51+
"category_parent": 0
52+
},
53+
{
54+
"term_id": 145,
55+
"name": "产品专栏",
56+
"slug": "product",
57+
"term_group": 0,
58+
"term_taxonomy_id": 145,
59+
"taxonomy": "category",
60+
"description": "产品理念、产品设计、产品构想等;主要由春松客服产品经理或产品办公室发布。",
61+
"parent": 0,
62+
"count": 2,
63+
"filter": "raw",
64+
"cat_ID": 145,
65+
"category_count": 2,
66+
"category_description": "产品理念、产品设计、产品构想等;主要由春松客服产品经理或产品办公室发布。",
67+
"cat_name": "产品专栏",
68+
"category_nicename": "product",
69+
"category_parent": 0
70+
}
71+
]
72+
}
73+
*/
74+
utils.writeTmpOutputFileOnDevelopment(body);
75+
76+
// 发送 W3 文章通知到飞书群
77+
await feishuService.sendW3BroadcastNotification(body);
78+
79+
80+
debug("[handleW3broadcast] ret", JSON.stringify(ret))
81+
return ret;
82+
}
83+
84+
85+
86+
exports = module.exports = new Controller();

sync/app/routers/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@
44
const debug = require("debug")("sync:routes:index");
55
const router = require("koa-router")();
66
const githubRouter = require("./github.router");
7+
const w3Router = require("./w3.router");
78

89
router.use("/api/github",
910
githubRouter.routes(),
1011
githubRouter.allowedMethods());
1112

13+
router.use("/api/w3",
14+
w3Router.routes(),
15+
w3Router.allowedMethods());
16+
1217
exports = module.exports = router;

sync/app/routers/w3.router.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Connect with W3
3+
*/
4+
5+
const debug = require("debug")("sync:routes:w3");
6+
const _ = require("lodash");
7+
const axios = require("axios");
8+
const Router = require("koa-router");
9+
const router = new Router();
10+
const w3Ctrl = require("../controllers/w3.ctrl");
11+
12+
/**
13+
* Receive GitHub Webhook Events
14+
*/
15+
router.post('/broadcast', async (ctx, next) => {
16+
let body = ctx.request.body;
17+
debug("/broadcast headers", JSON.stringify(ctx.request.headers, null, 2))
18+
debug("/broadcast", "ctx.params", JSON.stringify(ctx.params), ", body\n", JSON.stringify(body, null, 2));
19+
20+
ctx.body = await w3Ctrl.handleW3broadcast(ctx.request.headers, ctx.params, body);
21+
22+
await next();
23+
})
24+
25+
26+
exports = module.exports = router;

sync/app/sample.env

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
# for debugging purpose, generate tmp files, etc
33

44
NODE_ENV=development
5-
NOTIFY_FEISHU_GROUPS=xxx,yyy
5+
FEISHU_GROUP_GITHUB_BOTS=xxx,yyy
6+
FEISHU_GROUP_W3_BOTS=xxx,yyy

sync/app/services/feishu.service.js

+98-23
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ const utils = require("../utils/index");
99
const moment = require('moment-timezone');
1010

1111
const TZ = 'Asia/Shanghai'
12-
13-
const NOTIFY_FEISHU_GROUPS = process.env["NOTIFY_FEISHU_GROUPS"] ? process.env["NOTIFY_FEISHU_GROUPS"].split(",") : null;
14-
12+
const FEISHU_GROUP_GITHUB_BOTS = process.env["FEISHU_GROUP_GITHUB_BOTS"] ? process.env["FEISHU_GROUP_GITHUB_BOTS"].split(",") : null;
13+
const FEISHU_GROUP_W3_BOTS = process.env["FEISHU_GROUP_W3_BOTS"] ? process.env["FEISHU_GROUP_W3_BOTS"].split(",") : null;
1514

1615
/**
1716
* Compose Push Event Content
@@ -58,8 +57,8 @@ async function sendPushEventNotification(payload) {
5857
})
5958

6059

61-
if (NOTIFY_FEISHU_GROUPS && NOTIFY_FEISHU_GROUPS.length > 0) {
62-
for (let notify_feishu_group of NOTIFY_FEISHU_GROUPS) {
60+
if (FEISHU_GROUP_GITHUB_BOTS && FEISHU_GROUP_GITHUB_BOTS.length > 0) {
61+
for (let notify_feishu_group of FEISHU_GROUP_GITHUB_BOTS) {
6362
// Check out payload body for message conent with
6463
// https://github.com/cskefu/cskefu.sync/issues/2
6564
let response = await axios.post(notify_feishu_group, {
@@ -81,11 +80,10 @@ async function sendPushEventNotification(payload) {
8180
debug("[sendPushEventNotification] resp %j", response.data)
8281
}
8382
} else {
84-
debug("[sendPushEventNotification] No notify group defined with ENV NOTIFY_FEISHU_GROUPS");
83+
debug("[sendPushEventNotification] No notify group defined with ENV FEISHU_GROUP_GITHUB_BOTS");
8584
}
8685
}
8786

88-
8987
/**
9088
* Get Issue lable string
9189
* @param {*} labels
@@ -170,8 +168,8 @@ async function sendIssuesEventNotification(payload) {
170168
})
171169

172170

173-
if (NOTIFY_FEISHU_GROUPS && NOTIFY_FEISHU_GROUPS.length > 0) {
174-
for (let notify_feishu_group of NOTIFY_FEISHU_GROUPS) {
171+
if (FEISHU_GROUP_GITHUB_BOTS && FEISHU_GROUP_GITHUB_BOTS.length > 0) {
172+
for (let notify_feishu_group of FEISHU_GROUP_GITHUB_BOTS) {
175173
// Check out payload body for message conent with
176174
// https://github.com/cskefu/cskefu.sync/issues/2
177175
let response = await axios.post(notify_feishu_group, {
@@ -193,7 +191,7 @@ async function sendIssuesEventNotification(payload) {
193191
debug("[sendIssuesEventNotification] resp %j", response.data)
194192
}
195193
} else {
196-
debug("[sendIssuesEventNotification] No notify group defined with ENV NOTIFY_FEISHU_GROUPS");
194+
debug("[sendIssuesEventNotification] No notify group defined with ENV FEISHU_GROUP_GITHUB_BOTS");
197195
}
198196
}
199197

@@ -242,8 +240,8 @@ async function sendIssueCommentEventNotification(payload) {
242240
})
243241

244242

245-
if (NOTIFY_FEISHU_GROUPS && NOTIFY_FEISHU_GROUPS.length > 0) {
246-
for (let notify_feishu_group of NOTIFY_FEISHU_GROUPS) {
243+
if (FEISHU_GROUP_GITHUB_BOTS && FEISHU_GROUP_GITHUB_BOTS.length > 0) {
244+
for (let notify_feishu_group of FEISHU_GROUP_GITHUB_BOTS) {
247245
// Check out payload body for message conent with
248246
// https://github.com/cskefu/cskefu.sync/issues/2
249247
let response = await axios.post(notify_feishu_group, {
@@ -265,7 +263,7 @@ async function sendIssueCommentEventNotification(payload) {
265263
debug("[sendIssueCommentEventNotification] resp %j", response.data)
266264
}
267265
} else {
268-
debug("[sendIssueCommentEventNotification] No notify group defined with ENV NOTIFY_FEISHU_GROUPS");
266+
debug("[sendIssueCommentEventNotification] No notify group defined with ENV FEISHU_GROUP_GITHUB_BOTS");
269267
}
270268
}
271269

@@ -300,8 +298,8 @@ async function sendForkEventNotification(payload) {
300298
})
301299

302300

303-
if (NOTIFY_FEISHU_GROUPS && NOTIFY_FEISHU_GROUPS.length > 0) {
304-
for (let notify_feishu_group of NOTIFY_FEISHU_GROUPS) {
301+
if (FEISHU_GROUP_GITHUB_BOTS && FEISHU_GROUP_GITHUB_BOTS.length > 0) {
302+
for (let notify_feishu_group of FEISHU_GROUP_GITHUB_BOTS) {
305303
// Check out payload body for message conent with
306304
// https://github.com/cskefu/cskefu.sync/issues/2
307305
let response = await axios.post(notify_feishu_group, {
@@ -323,7 +321,7 @@ async function sendForkEventNotification(payload) {
323321
debug("[sendForkEventNotification] resp %j", response.data)
324322
}
325323
} else {
326-
debug("[sendForkEventNotification] No notify group defined with ENV NOTIFY_FEISHU_GROUPS");
324+
debug("[sendForkEventNotification] No notify group defined with ENV FEISHU_GROUP_GITHUB_BOTS");
327325
}
328326
}
329327

@@ -394,8 +392,8 @@ async function sendPullRequestEventNotification(payload) {
394392
})
395393

396394

397-
if (NOTIFY_FEISHU_GROUPS && NOTIFY_FEISHU_GROUPS.length > 0) {
398-
for (let notify_feishu_group of NOTIFY_FEISHU_GROUPS) {
395+
if (FEISHU_GROUP_GITHUB_BOTS && FEISHU_GROUP_GITHUB_BOTS.length > 0) {
396+
for (let notify_feishu_group of FEISHU_GROUP_GITHUB_BOTS) {
399397
// Check out payload body for message conent with
400398
// https://github.com/cskefu/cskefu.sync/issues/2
401399
let response = await axios.post(notify_feishu_group, {
@@ -417,7 +415,7 @@ async function sendPullRequestEventNotification(payload) {
417415
debug("[sendPullRequestEventNotification] resp %j", response.data)
418416
}
419417
} else {
420-
debug("[sendPullRequestEventNotification] No notify group defined with ENV NOTIFY_FEISHU_GROUPS");
418+
debug("[sendPullRequestEventNotification] No notify group defined with ENV FEISHU_GROUP_GITHUB_BOTS");
421419
}
422420
}
423421

@@ -466,8 +464,8 @@ async function sendMilestoneEventNotification(payload) {
466464
})
467465

468466

469-
if (NOTIFY_FEISHU_GROUPS && NOTIFY_FEISHU_GROUPS.length > 0) {
470-
for (let notify_feishu_group of NOTIFY_FEISHU_GROUPS) {
467+
if (FEISHU_GROUP_GITHUB_BOTS && FEISHU_GROUP_GITHUB_BOTS.length > 0) {
468+
for (let notify_feishu_group of FEISHU_GROUP_GITHUB_BOTS) {
471469
// Check out payload body for message conent with
472470
// https://github.com/cskefu/cskefu.sync/issues/2
473471
let response = await axios.post(notify_feishu_group, {
@@ -489,10 +487,86 @@ async function sendMilestoneEventNotification(payload) {
489487
debug("[sendMilestoneEventNotification] resp %j", response.data)
490488
}
491489
} else {
492-
debug("[sendMilestoneEventNotification] No notify group defined with ENV NOTIFY_FEISHU_GROUPS");
490+
debug("[sendMilestoneEventNotification] No notify group defined with ENV FEISHU_GROUP_GITHUB_BOTS");
493491
}
494492
}
495493

494+
/**
495+
* 获得 W3 文章标签分类信息
496+
* @param {*} payload
497+
*/
498+
function getW3PostCategories(payload){
499+
if(payload.categories && payload.categories.length > 0){
500+
let ret = [];
501+
502+
for(let x of payload.categories){
503+
ret.push(x.name);
504+
}
505+
506+
return ret.join("_")
507+
} else {
508+
return "未分类"
509+
}
510+
}
511+
512+
513+
/**
514+
* Send W3 Post Events into Feishu Groups
515+
* @param {*} payload
516+
*/
517+
async function sendW3BroadcastNotification(payload) {
518+
debug("[sendW3BroadcastNotification]", JSON.stringify(payload));
519+
let elements = [];
520+
521+
elements.push({
522+
"tag": "div",
523+
"text": {
524+
"content": `**标题** ${payload.post_title}\n**作者** [${payload.display_name}(${payload.user_email})](${payload.user_profile})`,
525+
"tag": "lark_md"
526+
}
527+
})
528+
529+
elements.push({
530+
"actions": [{
531+
"tag": "button",
532+
"text": {
533+
"content": "✅ 阅读原文",
534+
"tag": "lark_md"
535+
},
536+
"url": `${payload.link}`,
537+
"type": "default",
538+
"value": {}
539+
}],
540+
"tag": "action"
541+
})
542+
543+
544+
if (FEISHU_GROUP_W3_BOTS && FEISHU_GROUP_W3_BOTS.length > 0) {
545+
for (let notify_feishu_group of FEISHU_GROUP_W3_BOTS) {
546+
// Check out payload body for message conent with
547+
// https://github.com/cskefu/cskefu.sync/issues/2
548+
let response = await axios.post(notify_feishu_group, {
549+
"msg_type": "interactive",
550+
"card": {
551+
"config": {
552+
"wide_screen_mode": true,
553+
"enable_forward": true
554+
},
555+
"elements": elements,
556+
"header": {
557+
"title": {
558+
"content": utils.capitalizeFirstLetter(getW3PostCategories(payload) + " - ") + payload.post_title + " | 春松客服 W3",
559+
"tag": "plain_text"
560+
}
561+
}
562+
}
563+
});
564+
debug("[sendW3BroadcastNotification] resp %j", response.data)
565+
}
566+
} else {
567+
debug("[sendW3BroadcastNotification] No notify group defined with ENV FEISHU_GROUP_W3_BOTS");
568+
}
569+
}
496570

497571

498572
exports = module.exports = {
@@ -501,5 +575,6 @@ exports = module.exports = {
501575
sendIssueCommentEventNotification,
502576
sendForkEventNotification,
503577
sendPullRequestEventNotification,
504-
sendMilestoneEventNotification
578+
sendMilestoneEventNotification,
579+
sendW3BroadcastNotification
505580
}

0 commit comments

Comments
 (0)