146 lines
3.6 KiB
TypeScript
146 lines
3.6 KiB
TypeScript
import { fetchCIMonitor } from "../../service";
|
||
import { sendMsg } from "../../utils/sendMsg";
|
||
|
||
/**
|
||
* 是否为事件消息
|
||
* @param {LarkMessageEvent} body
|
||
*/
|
||
const isEventMsg = (body: LarkMessageEvent) => {
|
||
return body?.header?.event_type === "im.message.receive_v1";
|
||
};
|
||
|
||
/**
|
||
* 获取事件文本类型
|
||
* @param {LarkMessageEvent} body
|
||
* @returns
|
||
*/
|
||
const getMsgType = (body: LarkMessageEvent) => {
|
||
return body?.event?.message?.message_type;
|
||
};
|
||
|
||
/**
|
||
* 获取对话流Id
|
||
* @param {LarkMessageEvent} body
|
||
* @returns
|
||
*/
|
||
const getChatId = (body: LarkMessageEvent) => {
|
||
return body?.event?.message?.chat_id;
|
||
};
|
||
|
||
/**
|
||
* 获取文本内容并剔除艾特信息
|
||
* @param {LarkMessageEvent} body
|
||
* @returns {string} 文本内容
|
||
*/
|
||
const getMsgText = (body: LarkMessageEvent) => {
|
||
// TODO: 如果之后想支持单独提醒,这里需要做模板解析
|
||
try {
|
||
const { text } = 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 textWithoutSpace;
|
||
} catch (e) {
|
||
return "";
|
||
}
|
||
};
|
||
|
||
/**
|
||
* 过滤出非法消息,如果发表情包就直接发回去
|
||
* @param {LarkMessageEvent} body
|
||
* @returns {boolean} 是否为非法消息
|
||
*/
|
||
const filterIllegalMsg = (body: LarkMessageEvent) => {
|
||
const chatId = getChatId(body);
|
||
if (!chatId) return true;
|
||
const msgType = getMsgType(body);
|
||
if (msgType === "sticker") {
|
||
const content = body?.event?.message?.content;
|
||
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)],
|
||
});
|
||
sendMsg("chat_id", chatId, "text", content);
|
||
return true;
|
||
}
|
||
return false;
|
||
};
|
||
|
||
/**
|
||
* 处理info指令
|
||
* @param chatId 聊天流Id
|
||
*/
|
||
const manageInfoCommand = (chatId: string) => {
|
||
const content = JSON.stringify({ text: `chatId: ${chatId}` });
|
||
sendMsg("chat_id", chatId, "text", content);
|
||
};
|
||
|
||
/**
|
||
* 过滤出指令
|
||
* @param {LarkMessageEvent} body
|
||
* @returns {boolean} 是否为info指令
|
||
*/
|
||
const filterCommand = (body: LarkMessageEvent) => {
|
||
const chatId = getChatId(body);
|
||
const text = getMsgText(body);
|
||
if (text === "info") {
|
||
manageInfoCommand(chatId);
|
||
return true;
|
||
}
|
||
if (text === "ci") {
|
||
fetchCIMonitor();
|
||
return true;
|
||
}
|
||
|
||
return false;
|
||
};
|
||
|
||
const replyNomalMsg = async (body: LarkMessageEvent) => {
|
||
const chatId = getChatId(body);
|
||
const content = JSON.stringify({
|
||
elements: [
|
||
{
|
||
tag: "markdown",
|
||
content: "**目前支持的指令**\n1. info\n2. ci",
|
||
},
|
||
],
|
||
header: {
|
||
template: "blue",
|
||
title: {
|
||
content: "欢迎使用小煎蛋 ₍ᐢ..ᐢ₎♡",
|
||
tag: "plain_text",
|
||
},
|
||
},
|
||
});
|
||
sendMsg("chat_id", chatId, "interactive", content);
|
||
};
|
||
|
||
export const manageEventMsg = async (body: LarkMessageEvent) => {
|
||
// 过滤非Event消息
|
||
if (!isEventMsg(body)) {
|
||
return false;
|
||
}
|
||
// 过滤非法消息
|
||
if (filterIllegalMsg(body)) {
|
||
return true;
|
||
}
|
||
// 过滤指令
|
||
if (filterCommand(body)) {
|
||
return true;
|
||
}
|
||
// 临时返回消息
|
||
replyNomalMsg(body);
|
||
return true;
|
||
};
|