import service from "../../services"; import { LarkEvent } from "../../types"; import { getChatId, getChatType, getIsEventMsg, getMentions, getMsgText, getMsgType, } from "../../utils/msgTools"; /** * 是否为P2P或者群聊并且艾特了小煎蛋 * @param {LarkEvent.Data} body * @returns {boolean} 是否为P2P或者群聊并且艾特了小煎蛋 */ const getIsP2pOrGroupAtBot = (body: LarkEvent.Data) => { const isP2p = getChatType(body) === "p2p"; const isAtBot = getMentions(body)?.some?.( (mention) => mention.name === "小煎蛋" ); return isP2p || isAtBot; }; /** * 过滤出非法消息,如果发表情包就直接发回去 * @param {LarkEvent.Data} body * @returns {boolean} 是否为非法消息 */ const filterIllegalMsg = (body: LarkEvent.Data) => { // 没有chatId的消息不处理 const chatId = getChatId(body); if (!chatId) return true; // 获取msgType const msgType = getMsgType(body); // 放行纯文本消息 if (msgType === "text") { // 过滤艾特全体成员的消息 if (getMsgText(body).includes("@_all")) { return true; } // 放行 return false; } // 发表情包就直接发回去 if (msgType === "sticker") { const content = body?.event?.message?.content; service.lark.message.send("chat_id", chatId, "sticker", content); } // 非表情包只在私聊或者群聊中艾特小煎蛋时才回复 else if (getIsP2pOrGroupAtBot(body)) { const content = JSON.stringify({ text: "哇!这是什么东东?我只懂普通文本啦![可爱]", }); service.lark.message.send("chat_id", chatId, "text", content); } // 非纯文本,全不放行 return true; }; /** * 发送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, }, }, }); service.lark.message.send("chat_id", chatId, "interactive", content); }; /** * 处理命令消息 * @param body - 消息体 * @returns */ const manageCMDMsg = (body: LarkEvent.Data) => { const text = getMsgText(body); const chatId = getChatId(body); if (text === "/id") { manageIdMsg(chatId); return true; } if (text === "/ci") { service.attach.ciMonitor(chatId); return true; } if (text.includes("share") && text.includes("简报")) { service.attach.reportCollector(body); // 这个用时比较久,先发一条提醒用户收到了请求 const content = JSON.stringify({ text: "正在为您收集简报,请稍等片刻~", }); service.lark.message.send("chat_id", chatId, "text", content); return true; } return false; }; /** * 回复引导消息 * @param {LarkEvent.Data} body */ const replyGuideMsg = async (body: LarkEvent.Data) => { const chatId = getChatId(body); const content = JSON.stringify({ type: "template", data: { config: { enable_forward: false, update_multi: true, }, template_id: "ctp_AAyVx5R39xU9", template_variable: { chat_id: chatId, }, }, }); await service.lark.message.send("chat_id", chatId, "interactive", content); }; /** * 处理Event消息 * @param {LarkUserAction} body * @returns {boolean} 是否在本函数中处理了消息 */ export const manageEventMsg = (body: LarkEvent.Data) => { // 过滤非Event消息 if (!getIsEventMsg(body)) { return false; } // 过滤非法消息 if (filterIllegalMsg(body)) { return true; } // 处理命令消息 if (manageCMDMsg(body)) { return true; } // 返回引导消息 replyGuideMsg(body); return true; };