From 7d90bd368ed90c28fc6f7238afd670d4029129a8 Mon Sep 17 00:00:00 2001 From: zhaoyingbo Date: Tue, 21 May 2024 06:54:31 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=80=9A=E8=BF=87Cha?= =?UTF-8?q?tID=E5=8F=91=E9=80=81CI=E6=8A=A5=E5=91=8A=E7=9A=84=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/manageRobot/index.ts | 23 ++++++++++++++++++++--- index.ts | 15 ++++++++++++--- service/index.ts | 31 +++++++++++++++++++++++-------- 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/controllers/manageRobot/index.ts b/controllers/manageRobot/index.ts index 373f9e7..ad1155b 100644 --- a/controllers/manageRobot/index.ts +++ b/controllers/manageRobot/index.ts @@ -96,6 +96,10 @@ const getProjDiffInfo = async () => { return group.slice(0, 5); }; +/** + * 获取机器人消息 + * @returns + */ const getRobotMsg = async () => JSON.stringify({ type: "template", @@ -112,11 +116,24 @@ const getRobotMsg = async () => }, }); -const sendRobotMsg = async () => await service.sendMessage(await getRobotMsg()); +/** + * 通过ChatID发送CI报告 + * @param chat_id + */ +const sendCIReportByChatId = async (chat_id: string) => { + await service.sendMessage.byChatId(chat_id, await getRobotMsg()); +}; + +/** + * 通过定时任务发送CI报告 + * @returns + */ +const sendCIReportByCron = async () => + await service.sendMessage.byGroupId("52usf3w8l6z4vs1", await getRobotMsg()); const manageRobot = { - getRobotMsg, - sendRobotMsg, + sendCIReportByChatId, + sendCIReportByCron, }; export default manageRobot; diff --git a/index.ts b/index.ts index e9a42d5..59599b6 100644 --- a/index.ts +++ b/index.ts @@ -22,13 +22,22 @@ main(); scheduleJob("*/15 * * * *", main); -scheduleJob("0 10 * * 5", manageRobot.sendRobotMsg); +scheduleJob("0 10 * * 5", manageRobot.sendCIReportByCron); Bun.serve({ async fetch(req) { const url = new URL(req.url); - if (url.pathname === '/ci') { - return Response.json(await manageRobot.getRobotMsg()); + if (url.pathname === "/ci") { + const chat_id = url.searchParams.get("chat_id"); + console.log("🚀 ~ fetch ~ chat_id:", chat_id); + if (!chat_id) + return new Response("chat_id is required!", { status: 400 }); + manageRobot.sendCIReportByChatId(chat_id); + return Response.json({ + code: 0, + msg: "success", + data: "reporting...", + }); } return new Response("OK"); }, diff --git a/service/index.ts b/service/index.ts index b2d9785..d10087b 100644 --- a/service/index.ts +++ b/service/index.ts @@ -62,26 +62,41 @@ const fetchPipelineDetails = async ( } }; -const sendMessage = async (content: string) => { +const sendMessage = async (body: string) => { try { const response = await fetch("https://egg.imoaix.cn/message", { method: "POST", headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ - group_id: "52usf3w8l6z4vs1", - msg_type: "interactive", - content, - }), + body, }); - const body = await response.json(); - return body; + const res = await response.json(); + return res; } catch { return null; } }; +sendMessage.byGroupId = async (group_id: string, content: string) => { + const body = JSON.stringify({ + group_id, + msg_type: "interactive", + content, + }); + return sendMessage(body); +}; + +sendMessage.byChatId = async (chat_id: string, content: string) => { + const body = JSON.stringify({ + receive_id: chat_id, + receive_id_type: "chat_id", + msg_type: "interactive", + content, + }); + return sendMessage(body); +}; + const service = { fetchProjectDetails, fetchPipelines,