feat: 支持插件化功能
All checks were successful
Egg CI/CD / build-image (push) Successful in 26s
Egg CI/CD / deploy (push) Successful in 20s

This commit is contained in:
zhaoyingbo 2024-03-08 11:47:55 +00:00
parent 3b97b1def5
commit 3c36f59a1d
4 changed files with 22 additions and 17 deletions

View File

@ -1,20 +1,24 @@
import { sleep } from "bun";
import { fetchCIMonitor } from "../../service";
import { getActionType, getIsActionMsg } from "../../utils/msgTools";
import { updateCard } from "../../utils/sendMsg";
const makeChatIdCard = (body: LarkUserAction) => {
return JSON.stringify({ text: `chatId: ${body.open_chat_id}` });
};
const makeCICard = async () => {
const card = await fetchCIMonitor();
if (!card) return "";
return JSON.stringify(card);
const makeChatIdCard = async (body: LarkUserAction) => {
await sleep(500);
return JSON.stringify({
type: "template",
data: {
template_id: "ctp_AAi3NnHb6zgK",
template_variable: {
chat_id: body.open_chat_id,
},
},
});
};
const ACTION_MAP = {
chat_id: makeChatIdCard,
ci: makeCICard,
ci: fetchCIMonitor,
};
/**
@ -26,7 +30,9 @@ const manageBtnClick = async (body: LarkUserAction) => {
action: keyof typeof ACTION_MAP;
};
if (!action) return;
const card = await ACTION_MAP[action](body);
const func = ACTION_MAP[action];
if (!func) return;
const card = await func(body);
if (!card) return;
// 更新飞书的卡片
await updateCard(body.open_message_id, card);
@ -36,7 +42,7 @@ const manageBtnClick = async (body: LarkUserAction) => {
* Action消息
* @param {LarkUserAction} body
*/
export const manageActionMsg = async (body: LarkUserAction) => {
export const manageActionMsg = (body: LarkUserAction) => {
// 过滤非Action消息
if (!getIsActionMsg(body)) {
return false;

View File

@ -70,7 +70,7 @@ const replyNomalMsg = async (body: LarkMessageEvent) => {
* Event消息
* @param {LarkUserAction} body
*/
export const manageEventMsg = async (body: LarkMessageEvent) => {
export const manageEventMsg = (body: LarkMessageEvent) => {
// 过滤非Event消息
if (!getIsEventMsg(body)) {
return false;

View File

@ -5,12 +5,11 @@ export const manageBotReq = async (req: Request) => {
const body = (await req.json()) as any;
// 验证机器人
if (body?.type === "url_verification") {
console.log("🚀 ~ manageBotReq ~ url_verification:");
return Response.json({ challenge: body?.challenge });
}
// 处理Event消息
if (await manageEventMsg(body)) return new Response("success");
if (manageEventMsg(body)) return new Response("success");
// 处理Action消息
if (await manageActionMsg(body)) return new Response("success");
if (manageActionMsg(body)) return new Response("success");
return new Response("hello, glade to see you!");
};

View File

@ -1,8 +1,8 @@
export const fetchCIMonitor = async () => {
try {
const res = await fetch("https://ci-monitor.xiaomiwh.cn/ci");
return (await res.json()) as any;
return ((await res.json()) as string) || "";
} catch {
return null;
return "";
}
};