diff --git a/docker-compose.yml b/docker-compose.yml index 678fdc4..e239236 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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" \ No newline at end of file diff --git a/sample.env b/sample.env index aaf873f..d2ea606 100644 --- a/sample.env +++ b/sample.env @@ -5,4 +5,5 @@ COMPOSE_FILE=docker-compose.yml COMPOSE_PROJECT_NAME=cskefu.sync NODE_ENV=development -NOTIFY_FEISHU_GROUPS= \ No newline at end of file +FEISHU_GROUP_GITHUB_BOTS= +FEISHU_GROUP_W3_BOTS= \ No newline at end of file diff --git a/sync/app/controllers/w3.ctrl.js b/sync/app/controllers/w3.ctrl.js new file mode 100644 index 0000000..eff5e12 --- /dev/null +++ b/sync/app/controllers/w3.ctrl.js @@ -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(); \ No newline at end of file diff --git a/sync/app/routers/index.js b/sync/app/routers/index.js index 9d2cf50..3d7c7d0 100644 --- a/sync/app/routers/index.js +++ b/sync/app/routers/index.js @@ -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; \ No newline at end of file diff --git a/sync/app/routers/w3.router.js b/sync/app/routers/w3.router.js new file mode 100644 index 0000000..568c78c --- /dev/null +++ b/sync/app/routers/w3.router.js @@ -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; \ No newline at end of file diff --git a/sync/app/sample.env b/sync/app/sample.env index 92b19a9..a186bac 100644 --- a/sync/app/sample.env +++ b/sync/app/sample.env @@ -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 diff --git a/sync/app/services/feishu.service.js b/sync/app/services/feishu.service.js index 8446f83..fb6d012 100644 --- a/sync/app/services/feishu.service.js +++ b/sync/app/services/feishu.service.js @@ -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 @@ -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, { @@ -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 @@ -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, { @@ -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"); } } @@ -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, { @@ -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"); } } @@ -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, { @@ -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"); } } @@ -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, { @@ -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"); } } @@ -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, { @@ -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 = { @@ -501,5 +575,6 @@ exports = module.exports = { sendIssueCommentEventNotification, sendForkEventNotification, sendPullRequestEventNotification, - sendMilestoneEventNotification + sendMilestoneEventNotification, + sendW3BroadcastNotification }