192 lines
4.8 KiB
TypeScript
192 lines
4.8 KiB
TypeScript
import { type LarkBody } from "@egg/lark-msg-tool"
|
||
|
||
import tempMap from "../../constant/template"
|
||
import db from "../../db"
|
||
import { Context } from "../../types"
|
||
import createKVTemp from "../sheet/createKVTemp"
|
||
import groupAgent from "./groupAgent"
|
||
|
||
/**
|
||
* 是否为P2P或者群聊并且艾特了机器人
|
||
* @param larkBody
|
||
*/
|
||
const getIsP2pOrGroupAtBot = async (larkBody: LarkBody): Promise<boolean> => {
|
||
const appList = (await db.appInfo.getFullList())
|
||
.map((v) => v.app_name)
|
||
.filter((v) => v)
|
||
|
||
const isP2p = larkBody.chatType === "p2p"
|
||
const isAtBot = larkBody.mentions?.some?.((mention) =>
|
||
appList.includes(mention.name)
|
||
)
|
||
return Boolean(isP2p || isAtBot)
|
||
}
|
||
|
||
/**
|
||
* 过滤出非法消息,如果发表情包就直接发回去
|
||
* @param {Context.Data} ctx - 上下文数据,包含body, logger和larkService
|
||
* @returns {boolean} 是否为非法消息
|
||
*/
|
||
const filterIllegalMsg = async ({
|
||
body,
|
||
logger,
|
||
larkService,
|
||
larkBody,
|
||
}: Context.Data): Promise<boolean> => {
|
||
const { chatId, msgType, msgText } = larkBody
|
||
// 没有chatId的消息不处理
|
||
logger.info(`bot req chatId: ${chatId}`)
|
||
if (!chatId) return true
|
||
|
||
// 非私聊和群聊中艾特机器人的消息不处理
|
||
if (!(await getIsP2pOrGroupAtBot(larkBody))) {
|
||
return true
|
||
}
|
||
|
||
// 获取msgType
|
||
logger.info(`bot req msgType: ${msgType}`)
|
||
// 放行纯文本消息
|
||
if (msgType === "text") {
|
||
// 过滤艾特全体成员的消息
|
||
if (msgText.includes("@_all")) {
|
||
return true
|
||
}
|
||
// 放行
|
||
return false
|
||
}
|
||
|
||
// 发表情包就直接发回去
|
||
if (msgType === "sticker") {
|
||
logger.info(`got a sticker message, chatId: ${chatId}`)
|
||
const content = body?.event?.message?.content
|
||
larkService.message.send("chat_id", chatId, "sticker", content)
|
||
}
|
||
|
||
// 非表情包只在私聊或者群聊中艾特机器人时才回复
|
||
else if (await getIsP2pOrGroupAtBot(body)) {
|
||
logger.info(`got a illegal message, chatId: ${chatId}`)
|
||
const content = JSON.stringify({
|
||
text: "哇!这是什么东东?我只懂普通文本啦![可爱]",
|
||
})
|
||
larkService.message.send("chat_id", chatId, "text", content)
|
||
}
|
||
|
||
// 非纯文本,全不放行
|
||
return true
|
||
}
|
||
|
||
/**
|
||
* 发送ID消息
|
||
* @param {Context.Data} ctx - 上下文数据
|
||
*/
|
||
const manageIdMsg = ({
|
||
larkBody: { chatId },
|
||
larkCard,
|
||
larkService,
|
||
}: Context.Data): void => {
|
||
larkService.message.sendCard2Chat(
|
||
chatId,
|
||
larkCard.genTempCard("chatId", { chat_id: chatId })
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 回复引导消息
|
||
* @param {Context.Data} ctx - 上下文数据
|
||
*/
|
||
const manageHelpMsg = (
|
||
{ larkBody: { chatId }, larkCard, larkService }: Context.Data,
|
||
tempKey: keyof typeof tempMap
|
||
): void => {
|
||
larkService.message.sendCard2Chat(
|
||
chatId,
|
||
larkCard.genTempCard(tempKey, { chat_id: chatId }) as string
|
||
)
|
||
}
|
||
|
||
/**
|
||
* 处理命令消息
|
||
* @param {Context.Data} ctx - 上下文数据
|
||
*/
|
||
const manageCMDMsg = (ctx: Context.Data) => {
|
||
const {
|
||
app,
|
||
body,
|
||
logger,
|
||
larkService,
|
||
attachService,
|
||
larkBody: { msgText, chatId },
|
||
} = ctx
|
||
logger.info(`bot req text: ${msgText}`)
|
||
// 处理命令消息
|
||
if (msgText.trim() === "/id") {
|
||
logger.info(`bot command is /id, chatId: ${chatId}`)
|
||
manageIdMsg(ctx)
|
||
return
|
||
}
|
||
|
||
// 选择群组信息
|
||
if (msgText.trim().startsWith("/g")) {
|
||
logger.info(`bot command is /groupchat, chatId: ${chatId}`)
|
||
groupAgent(ctx)
|
||
return
|
||
}
|
||
|
||
// michat专属功能
|
||
if (app === "michat") {
|
||
// 帮助
|
||
logger.info(`bot command is /help, chatId: ${chatId}`)
|
||
manageHelpMsg(ctx, "miChatGuide")
|
||
}
|
||
|
||
// 小煎蛋专属功能
|
||
if (app === "egg") {
|
||
// CI监控
|
||
if (msgText.trim() === "/ci") {
|
||
logger.info(`bot command is /ci, chatId: ${chatId}`)
|
||
attachService.ciMonitor(chatId)
|
||
return
|
||
}
|
||
// 简报
|
||
if (msgText.includes("share") && msgText.includes("简报")) {
|
||
logger.info(`bot command is share report, chatId: ${chatId}`)
|
||
// 这个用时比较久,先发一条提醒用户收到了请求
|
||
// TODO: 迁移到简报服务中
|
||
larkService.message.send(
|
||
"chat_id",
|
||
chatId,
|
||
"text",
|
||
"正在为您收集简报,请稍等片刻~"
|
||
)
|
||
attachService.reportCollector(body)
|
||
return
|
||
}
|
||
// 创建Sheet DB
|
||
if (msgText.trim() === "/gen db") {
|
||
logger.info(`bot command is /gen db, chatId: ${chatId}`)
|
||
createKVTemp.createFromEvent(ctx)
|
||
return
|
||
}
|
||
|
||
// 帮助
|
||
if (msgText.trim() === "/help") {
|
||
logger.info(`bot command is /help, chatId: ${chatId}`)
|
||
manageHelpMsg(ctx, "eggGuide")
|
||
return
|
||
}
|
||
}
|
||
|
||
return
|
||
}
|
||
|
||
/**
|
||
* 处理Event消息
|
||
* @param {Context.Data} ctx - 上下文数据
|
||
*/
|
||
export const manageEventMsg = async (ctx: Context.Data) => {
|
||
// 过滤非法消息
|
||
if (await filterIllegalMsg(ctx)) return
|
||
// 处理命令消息
|
||
manageCMDMsg(ctx)
|
||
}
|