egg_server/routes/bot/eventMsg.ts
zhaoyingbo 8b917546d7
All checks were successful
Egg CI/CD / build-image (push) Successful in 21s
Egg CI/CD / deploy (push) Successful in 21s
feat: 添加发送CI监控消息的函数
2024-05-17 13:29:09 +00:00

170 lines
4.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { fetchCIMonitor, fetchReportCollector } from "../../service";
import lark from "../../service/lark";
import { LarkMessageEvent } from "../../types";
import { getChatId, getIsEventMsg, getMsgType } from "../../utils/msgTools";
/**
* 获取文本内容并剔除艾特信息
* @param {LarkMessageEvent} body
* @returns {string} 文本内容
*/
export const getMsgText = (body: LarkMessageEvent) => {
// TODO: 如果之后想支持单独提醒,这里需要做模板解析
try {
const { text }: { text: string } = JSON.parse(
body?.event?.message?.content
);
// 去掉@_user_1相关的内容例如 '@_user_1 测试' -> '测试'
const textWithoutAt = text.replace(/@_user_\d+/g, "");
// // 去除空格和换行
// const textWithoutSpace = textWithoutAt.replace(/[\s\n]/g, "");
return textWithoutAt;
} catch (e) {
return "";
}
};
/**
* 过滤出非法消息,如果发表情包就直接发回去
* @param {LarkMessageEvent} body
* @returns {boolean} 是否为非法消息
*/
const filterIllegalMsg = (body: LarkMessageEvent) => {
const chatId = getChatId(body);
if (!chatId) return true;
// 获取msgType
const msgType = getMsgType(body);
// 发表情包就直接发回去
if (msgType === "sticker") {
const content = body?.event?.message?.content;
lark.sendMsg("chat_id", chatId, "sticker", content);
return true;
}
// 剩下的非文字消息暂时不处理
if (msgType !== "text") {
const textList = [
"仅支持普通文本内容[黑脸]",
"唔...我只能处理普通文本哦[泣不成声]",
"噢!这似乎是个非普通文本[看]",
"哇!这是什么东东?我只懂普通文本啦![可爱]",
"只能处理普通文本内容哦[捂脸]",
];
const content = JSON.stringify({
text: textList[Math.floor(Math.random() * textList.length)],
});
lark.sendMsg("chat_id", chatId, "text", content);
return true;
}
// 还得过滤下艾特全体成员的消息
if (getMsgText(body).includes("@_all")) {
return true;
}
return false;
};
/**
* 发送CI监控消息
* @param chatId - 发送消息的chatId
*/
const manageCIMsg = async (chatId: string) => {
lark.sendMsg("chat_id", chatId, "interactive", await fetchCIMonitor());
};
/**
* 发送简报消息
* @param chatId - 发送消息的chatId
* @param msg - 消息内容
*/
const manageReportMsg = async (chatId: string, msg: string) => {
lark.sendMsg(
"chat_id",
chatId,
"interactive",
await fetchReportCollector(msg)
);
};
/**
* 发送ID消息
* @param chatId - 发送消息的chatId
*/
const manageIdMsg = async (chatId: string) => {
const content = JSON.stringify({
type: "template",
data: {
config: {
update_multi: true,
},
template_id: "ctp_AAi3NnHb6zgK",
template_variable: {
chat_id: chatId,
},
},
});
lark.sendMsg("chat_id", chatId, "interactive", content);
};
/**
* 处理命令消息
* @param body - 消息体
* @returns
*/
const manageCMDMsg = (body: LarkMessageEvent) => {
const text = getMsgText(body);
const chatId = getChatId(body);
if (text === "/id") {
manageIdMsg(chatId);
return true;
}
if (text === "/ci") {
manageCIMsg(chatId);
return true;
}
if (text.includes("share") && text.includes("简报")) {
manageReportMsg(chatId, text);
return true;
}
return false;
};
/**
* 回复引导消息
* @param {LarkMessageEvent} body
*/
const replyGuideMsg = async (body: LarkMessageEvent) => {
const chatId = getChatId(body);
const content = JSON.stringify({
type: "template",
data: {
config: {
enable_forward: false,
update_multi: true,
},
template_id: "ctp_AAyVx5R39xU9",
},
});
await lark.sendMsg("chat_id", chatId, "interactive", content);
};
/**
* 处理Event消息
* @param {LarkUserAction} body
*/
export const manageEventMsg = (body: LarkMessageEvent) => {
// 过滤非Event消息
if (!getIsEventMsg(body)) {
return false;
}
// 过滤非法消息
if (filterIllegalMsg(body)) {
return true;
}
// 处理命令消息
if (manageCMDMsg(body)) {
return true;
}
// 返回引导消息
replyGuideMsg(body);
return true;
};