feat: 添加通过ChatID发送CI报告的功能
All checks were successful
CI Monitor CI/CD / build-image (push) Successful in 23s
CI Monitor CI/CD / deploy (push) Successful in 30s

This commit is contained in:
zhaoyingbo 2024-05-21 06:54:31 +00:00
parent 6cb7a26ad5
commit 7d90bd368e
3 changed files with 55 additions and 14 deletions

View File

@ -96,6 +96,10 @@ const getProjDiffInfo = async () => {
return group.slice(0, 5); return group.slice(0, 5);
}; };
/**
*
* @returns
*/
const getRobotMsg = async () => const getRobotMsg = async () =>
JSON.stringify({ JSON.stringify({
type: "template", 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 = { const manageRobot = {
getRobotMsg, sendCIReportByChatId,
sendRobotMsg, sendCIReportByCron,
}; };
export default manageRobot; export default manageRobot;

View File

@ -22,13 +22,22 @@ main();
scheduleJob("*/15 * * * *", main); scheduleJob("*/15 * * * *", main);
scheduleJob("0 10 * * 5", manageRobot.sendRobotMsg); scheduleJob("0 10 * * 5", manageRobot.sendCIReportByCron);
Bun.serve({ Bun.serve({
async fetch(req) { async fetch(req) {
const url = new URL(req.url); const url = new URL(req.url);
if (url.pathname === '/ci') { if (url.pathname === "/ci") {
return Response.json(await manageRobot.getRobotMsg()); 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"); return new Response("OK");
}, },

View File

@ -62,26 +62,41 @@ const fetchPipelineDetails = async (
} }
}; };
const sendMessage = async (content: string) => { const sendMessage = async (body: string) => {
try { try {
const response = await fetch("https://egg.imoaix.cn/message", { const response = await fetch("https://egg.imoaix.cn/message", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body,
group_id: "52usf3w8l6z4vs1",
msg_type: "interactive",
content,
}),
}); });
const body = await response.json(); const res = await response.json();
return body; return res;
} catch { } catch {
return null; 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 = { const service = {
fetchProjectDetails, fetchProjectDetails,
fetchPipelines, fetchPipelines,