Skip to content

Commit

Permalink
#5 send notify into feishu for w3 post events
Browse files Browse the repository at this point in the history
  • Loading branch information
Hai Liang Wang committed Oct 8, 2022
1 parent 470be2e commit 7171645
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 26 deletions.
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
environment:
- DEBUG=sync*
- NODE_ENV=${NODE_ENV:-development}
- NOTIFY_FEISHU_GROUPS=${NOTIFY_FEISHU_GROUPS:-placeholder}
- FEISHU_GROUP_GITHUB_BOTS=${FEISHU_GROUP_GITHUB_BOTS:-placeholder}
- FEISHU_GROUP_W3_BOTS=${FEISHU_GROUP_W3_BOTS:-placeholder}
ports:
- "${SYNC_PORT:-8201}:8201"
3 changes: 2 additions & 1 deletion sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ COMPOSE_FILE=docker-compose.yml
COMPOSE_PROJECT_NAME=cskefu.sync

NODE_ENV=development
NOTIFY_FEISHU_GROUPS=
FEISHU_GROUP_GITHUB_BOTS=
FEISHU_GROUP_W3_BOTS=
86 changes: 86 additions & 0 deletions sync/app/controllers/w3.ctrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/**
* W3 Ctrl
*/

const debug = require("debug")("sync:ctrl:w3");
const utils = require("../utils/index");
const feishuService = require("../services/feishu.service");



function Controller() {

}

/**
* Handle W3 Events
* @param {*} headers
* @param {*} params
* @param {*} body
*/
Controller.prototype.handleW3broadcast = async function (headers, params, body) {
let ret = { "msg": "done" };
/**
* sync:routes:w3 /broadcast ctx.params {} , body
{
"link": "https://www.cskefu.com/2022/10/08/test-c/",
"post_title": "Test C",
"author_id": "99",
"display_name": "Hai",
"user_email": "h@cskefu.com",
"user_profile": "https://www.cskefu.com/user/99/",
"categories": [
{
"term_id": 118,
"name": "业务观点",
"slug": "business",
"term_group": 0,
"term_taxonomy_id": 118,
"taxonomy": "category",
"description": "业务、产品及服务的应用和解决方案:呼叫中心、联络中心、智能客服、客服机器人、CTI、云计算等。有价值的干货:应用场景、业务价
值、创新方案。",
"parent": 0,
"count": 12,
"filter": "raw",
"cat_ID": 118,
"category_count": 12,
"category_description": "业务、产品及服务的应用和解决方案:呼叫中心、联络中心、智能客服、客服机器人、CTI、云计算等。有价值的干货:应用场
景、业务价值、创新方案。",
"cat_name": "业务观点",
"category_nicename": "business",
"category_parent": 0
},
{
"term_id": 145,
"name": "产品专栏",
"slug": "product",
"term_group": 0,
"term_taxonomy_id": 145,
"taxonomy": "category",
"description": "产品理念、产品设计、产品构想等;主要由春松客服产品经理或产品办公室发布。",
"parent": 0,
"count": 2,
"filter": "raw",
"cat_ID": 145,
"category_count": 2,
"category_description": "产品理念、产品设计、产品构想等;主要由春松客服产品经理或产品办公室发布。",
"cat_name": "产品专栏",
"category_nicename": "product",
"category_parent": 0
}
]
}
*/
utils.writeTmpOutputFileOnDevelopment(body);

// 发送 W3 文章通知到飞书群
await feishuService.sendW3BroadcastNotification(body);


debug("[handleW3broadcast] ret", JSON.stringify(ret))
return ret;
}



exports = module.exports = new Controller();
5 changes: 5 additions & 0 deletions sync/app/routers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
const debug = require("debug")("sync:routes:index");
const router = require("koa-router")();
const githubRouter = require("./github.router");
const w3Router = require("./w3.router");

router.use("/api/github",
githubRouter.routes(),
githubRouter.allowedMethods());

router.use("/api/w3",
w3Router.routes(),
w3Router.allowedMethods());

exports = module.exports = router;
26 changes: 26 additions & 0 deletions sync/app/routers/w3.router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Connect with W3
*/

const debug = require("debug")("sync:routes:w3");
const _ = require("lodash");
const axios = require("axios");
const Router = require("koa-router");
const router = new Router();
const w3Ctrl = require("../controllers/w3.ctrl");

/**
* Receive GitHub Webhook Events
*/
router.post('/broadcast', async (ctx, next) => {
let body = ctx.request.body;
debug("/broadcast headers", JSON.stringify(ctx.request.headers, null, 2))
debug("/broadcast", "ctx.params", JSON.stringify(ctx.params), ", body\n", JSON.stringify(body, null, 2));

ctx.body = await w3Ctrl.handleW3broadcast(ctx.request.headers, ctx.params, body);

await next();
})


exports = module.exports = router;
3 changes: 2 additions & 1 deletion sync/app/sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
# for debugging purpose, generate tmp files, etc

NODE_ENV=development
NOTIFY_FEISHU_GROUPS=xxx,yyy
FEISHU_GROUP_GITHUB_BOTS=xxx,yyy
FEISHU_GROUP_W3_BOTS=xxx,yyy
121 changes: 98 additions & 23 deletions sync/app/services/feishu.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ const utils = require("../utils/index");
const moment = require('moment-timezone');

const TZ = 'Asia/Shanghai'

const NOTIFY_FEISHU_GROUPS = process.env["NOTIFY_FEISHU_GROUPS"] ? process.env["NOTIFY_FEISHU_GROUPS"].split(",") : null;

const FEISHU_GROUP_GITHUB_BOTS = process.env["FEISHU_GROUP_GITHUB_BOTS"] ? process.env["FEISHU_GROUP_GITHUB_BOTS"].split(",") : null;
const FEISHU_GROUP_W3_BOTS = process.env["FEISHU_GROUP_W3_BOTS"] ? process.env["FEISHU_GROUP_W3_BOTS"].split(",") : null;

/**
* Compose Push Event Content
Expand Down Expand Up @@ -58,8 +57,8 @@ async function sendPushEventNotification(payload) {
})


if (NOTIFY_FEISHU_GROUPS && NOTIFY_FEISHU_GROUPS.length > 0) {
for (let notify_feishu_group of NOTIFY_FEISHU_GROUPS) {
if (FEISHU_GROUP_GITHUB_BOTS && FEISHU_GROUP_GITHUB_BOTS.length > 0) {
for (let notify_feishu_group of FEISHU_GROUP_GITHUB_BOTS) {
// Check out payload body for message conent with
// https://github.com/cskefu/cskefu.sync/issues/2
let response = await axios.post(notify_feishu_group, {
Expand All @@ -81,11 +80,10 @@ async function sendPushEventNotification(payload) {
debug("[sendPushEventNotification] resp %j", response.data)
}
} else {
debug("[sendPushEventNotification] No notify group defined with ENV NOTIFY_FEISHU_GROUPS");
debug("[sendPushEventNotification] No notify group defined with ENV FEISHU_GROUP_GITHUB_BOTS");
}
}


/**
* Get Issue lable string
* @param {*} labels
Expand Down Expand Up @@ -170,8 +168,8 @@ async function sendIssuesEventNotification(payload) {
})


if (NOTIFY_FEISHU_GROUPS && NOTIFY_FEISHU_GROUPS.length > 0) {
for (let notify_feishu_group of NOTIFY_FEISHU_GROUPS) {
if (FEISHU_GROUP_GITHUB_BOTS && FEISHU_GROUP_GITHUB_BOTS.length > 0) {
for (let notify_feishu_group of FEISHU_GROUP_GITHUB_BOTS) {
// Check out payload body for message conent with
// https://github.com/cskefu/cskefu.sync/issues/2
let response = await axios.post(notify_feishu_group, {
Expand All @@ -193,7 +191,7 @@ async function sendIssuesEventNotification(payload) {
debug("[sendIssuesEventNotification] resp %j", response.data)
}
} else {
debug("[sendIssuesEventNotification] No notify group defined with ENV NOTIFY_FEISHU_GROUPS");
debug("[sendIssuesEventNotification] No notify group defined with ENV FEISHU_GROUP_GITHUB_BOTS");
}
}

Expand Down Expand Up @@ -242,8 +240,8 @@ async function sendIssueCommentEventNotification(payload) {
})


if (NOTIFY_FEISHU_GROUPS && NOTIFY_FEISHU_GROUPS.length > 0) {
for (let notify_feishu_group of NOTIFY_FEISHU_GROUPS) {
if (FEISHU_GROUP_GITHUB_BOTS && FEISHU_GROUP_GITHUB_BOTS.length > 0) {
for (let notify_feishu_group of FEISHU_GROUP_GITHUB_BOTS) {
// Check out payload body for message conent with
// https://github.com/cskefu/cskefu.sync/issues/2
let response = await axios.post(notify_feishu_group, {
Expand All @@ -265,7 +263,7 @@ async function sendIssueCommentEventNotification(payload) {
debug("[sendIssueCommentEventNotification] resp %j", response.data)
}
} else {
debug("[sendIssueCommentEventNotification] No notify group defined with ENV NOTIFY_FEISHU_GROUPS");
debug("[sendIssueCommentEventNotification] No notify group defined with ENV FEISHU_GROUP_GITHUB_BOTS");
}
}

Expand Down Expand Up @@ -300,8 +298,8 @@ async function sendForkEventNotification(payload) {
})


if (NOTIFY_FEISHU_GROUPS && NOTIFY_FEISHU_GROUPS.length > 0) {
for (let notify_feishu_group of NOTIFY_FEISHU_GROUPS) {
if (FEISHU_GROUP_GITHUB_BOTS && FEISHU_GROUP_GITHUB_BOTS.length > 0) {
for (let notify_feishu_group of FEISHU_GROUP_GITHUB_BOTS) {
// Check out payload body for message conent with
// https://github.com/cskefu/cskefu.sync/issues/2
let response = await axios.post(notify_feishu_group, {
Expand All @@ -323,7 +321,7 @@ async function sendForkEventNotification(payload) {
debug("[sendForkEventNotification] resp %j", response.data)
}
} else {
debug("[sendForkEventNotification] No notify group defined with ENV NOTIFY_FEISHU_GROUPS");
debug("[sendForkEventNotification] No notify group defined with ENV FEISHU_GROUP_GITHUB_BOTS");
}
}

Expand Down Expand Up @@ -394,8 +392,8 @@ async function sendPullRequestEventNotification(payload) {
})


if (NOTIFY_FEISHU_GROUPS && NOTIFY_FEISHU_GROUPS.length > 0) {
for (let notify_feishu_group of NOTIFY_FEISHU_GROUPS) {
if (FEISHU_GROUP_GITHUB_BOTS && FEISHU_GROUP_GITHUB_BOTS.length > 0) {
for (let notify_feishu_group of FEISHU_GROUP_GITHUB_BOTS) {
// Check out payload body for message conent with
// https://github.com/cskefu/cskefu.sync/issues/2
let response = await axios.post(notify_feishu_group, {
Expand All @@ -417,7 +415,7 @@ async function sendPullRequestEventNotification(payload) {
debug("[sendPullRequestEventNotification] resp %j", response.data)
}
} else {
debug("[sendPullRequestEventNotification] No notify group defined with ENV NOTIFY_FEISHU_GROUPS");
debug("[sendPullRequestEventNotification] No notify group defined with ENV FEISHU_GROUP_GITHUB_BOTS");
}
}

Expand Down Expand Up @@ -466,8 +464,8 @@ async function sendMilestoneEventNotification(payload) {
})


if (NOTIFY_FEISHU_GROUPS && NOTIFY_FEISHU_GROUPS.length > 0) {
for (let notify_feishu_group of NOTIFY_FEISHU_GROUPS) {
if (FEISHU_GROUP_GITHUB_BOTS && FEISHU_GROUP_GITHUB_BOTS.length > 0) {
for (let notify_feishu_group of FEISHU_GROUP_GITHUB_BOTS) {
// Check out payload body for message conent with
// https://github.com/cskefu/cskefu.sync/issues/2
let response = await axios.post(notify_feishu_group, {
Expand All @@ -489,10 +487,86 @@ async function sendMilestoneEventNotification(payload) {
debug("[sendMilestoneEventNotification] resp %j", response.data)
}
} else {
debug("[sendMilestoneEventNotification] No notify group defined with ENV NOTIFY_FEISHU_GROUPS");
debug("[sendMilestoneEventNotification] No notify group defined with ENV FEISHU_GROUP_GITHUB_BOTS");
}
}

/**
* 获得 W3 文章标签分类信息
* @param {*} payload
*/
function getW3PostCategories(payload){
if(payload.categories && payload.categories.length > 0){
let ret = [];

for(let x of payload.categories){
ret.push(x.name);
}

return ret.join("_")
} else {
return "未分类"
}
}


/**
* Send W3 Post Events into Feishu Groups
* @param {*} payload
*/
async function sendW3BroadcastNotification(payload) {
debug("[sendW3BroadcastNotification]", JSON.stringify(payload));
let elements = [];

elements.push({
"tag": "div",
"text": {
"content": `**标题** ${payload.post_title}\n**作者** [${payload.display_name}(${payload.user_email})](${payload.user_profile})`,
"tag": "lark_md"
}
})

elements.push({
"actions": [{
"tag": "button",
"text": {
"content": "✅ 阅读原文",
"tag": "lark_md"
},
"url": `${payload.link}`,
"type": "default",
"value": {}
}],
"tag": "action"
})


if (FEISHU_GROUP_W3_BOTS && FEISHU_GROUP_W3_BOTS.length > 0) {
for (let notify_feishu_group of FEISHU_GROUP_W3_BOTS) {
// Check out payload body for message conent with
// https://github.com/cskefu/cskefu.sync/issues/2
let response = await axios.post(notify_feishu_group, {
"msg_type": "interactive",
"card": {
"config": {
"wide_screen_mode": true,
"enable_forward": true
},
"elements": elements,
"header": {
"title": {
"content": utils.capitalizeFirstLetter(getW3PostCategories(payload) + " - ") + payload.post_title + " | 春松客服 W3",
"tag": "plain_text"
}
}
}
});
debug("[sendW3BroadcastNotification] resp %j", response.data)
}
} else {
debug("[sendW3BroadcastNotification] No notify group defined with ENV FEISHU_GROUP_W3_BOTS");
}
}


exports = module.exports = {
Expand All @@ -501,5 +575,6 @@ exports = module.exports = {
sendIssueCommentEventNotification,
sendForkEventNotification,
sendPullRequestEventNotification,
sendMilestoneEventNotification
sendMilestoneEventNotification,
sendW3BroadcastNotification
}

0 comments on commit 7171645

Please sign in to comment.