egg_server/routes/bot/eventMsg.ts
zhaoyingbo b992ee0b21
Some checks failed
Egg Server MIflow / build-image (push) Failing after 5m7s
feat(group-agent): 新增支持群组问答
2024-09-25 09:14:10 +00:00

187 lines
5.0 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 type { LarkEvent } from "@egg/lark-msg-tool"
import {
getChatId,
getChatType,
getIsEventMsg,
getMentions,
getMsgText,
getMsgType,
} from "@egg/lark-msg-tool"
import { LarkService } from "../../services"
import { Context } from "../../types"
import createKVTemp from "../sheet/createKVTemp"
import groupAgent from "./groupAgent"
/**
* 是否为P2P或者群聊并且艾特了小煎蛋
* @param {LarkEvent.Data} body
* @returns {boolean} 是否为P2P或者群聊并且艾特了小煎蛋
*/
const getIsP2pOrGroupAtBot = (body: LarkEvent.Data): boolean => {
const isP2p = getChatType(body) === "p2p"
const isAtBot = getMentions(body)?.some?.(
(mention) => mention.name === "小煎蛋"
)
return Boolean(isP2p || isAtBot)
}
/**
* 过滤出非法消息,如果发表情包就直接发回去
* @param {Context.Data} ctx - 上下文数据包含body, logger和larkService
* @returns {boolean} 是否为非法消息
*/
const filterIllegalMsg = ({
body,
logger,
larkService,
}: Context.Data): boolean => {
// 没有chatId的消息不处理
const chatId = getChatId(body)
logger.info(`bot req chatId: ${chatId}`)
if (!chatId) return true
// 非私聊和群聊中艾特小煎蛋的消息不处理
if (!getIsP2pOrGroupAtBot(body)) {
return true
}
// 获取msgType
const msgType = getMsgType(body)
logger.info(`bot req msgType: ${msgType}`)
// 放行纯文本消息
if (msgType === "text") {
// 过滤艾特全体成员的消息
if (getMsgText(body).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 (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 {string} chatId - 发送消息的chatId
* @param {LarkService} service - Lark服务实例
*/
const manageIdMsg = (chatId: string, service: LarkService): void => {
service.message.sendTemp("chat_id", chatId, "ctp_AAi3NnHb6zgK", {
chat_id: chatId,
})
}
/**
* 回复引导消息
* @param {Context.Data} ctx - 上下文数据包含body, larkService和logger
*/
const manageHelpMsg = (chatId: string, service: LarkService): void => {
service.message.sendTemp("chat_id", chatId, "ctp_AAyVx5R39xU9", {
chat_id: chatId,
})
}
/**
* 处理命令消息
* @param {Context.Data} ctx - 上下文数据包含body, logger, larkService和attachService
* @returns {boolean} 是否处理了命令消息
*/
const manageCMDMsg = (ctx: Context.Data): boolean => {
const { body, logger, larkService, attachService } = ctx
const text = getMsgText(body)
logger.info(`bot req text: ${text}`)
const chatId = getChatId(body)
// 处理命令消息
if (text.trim() === "/id") {
logger.info(`bot command is /id, chatId: ${chatId}`)
manageIdMsg(chatId, larkService)
return true
}
// 帮助
if (text.trim() === "/help") {
logger.info(`bot command is /help, chatId: ${chatId}`)
manageHelpMsg(chatId, larkService)
return true
}
// CI监控
if (text.trim() === "/ci") {
logger.info(`bot command is /ci, chatId: ${chatId}`)
attachService.ciMonitor(chatId)
return true
}
// 简报
if (text.includes("share") && text.includes("简报")) {
logger.info(`bot command is share report, chatId: ${chatId}`)
// 这个用时比较久,先发一条提醒用户收到了请求
larkService.message.send(
"chat_id",
chatId,
"text",
"正在为您收集简报,请稍等片刻~"
)
attachService.reportCollector(body)
return true
}
// 创建Sheet DB
if (text.trim() === "/gen db") {
logger.info(`bot command is /gen db, chatId: ${chatId}`)
createKVTemp.createFromEvent(ctx)
return true
}
// 选择群组信息
if (text.trim() === "/sg") {
logger.info(`bot command is /sg, chatId: ${chatId}`)
groupAgent.sendGroupSelector(ctx)
return true
}
// 获取当前群组信息
if (text.trim() === "/cg") {
logger.info(`bot command is /cg, chatId: ${chatId}`)
groupAgent.getCurrentGroup(ctx)
return true
}
return false
}
/**
* 处理Event消息
* @param {Context.Data} ctx - 上下文数据
* @returns {boolean} 是否在本函数中处理了消息
*/
export const manageEventMsg = (ctx: Context.Data): boolean => {
// 过滤非Event消息
if (!getIsEventMsg(ctx.body)) {
return false
}
// 过滤非法消息
if (filterIllegalMsg(ctx)) {
return true
}
// 处理命令消息
if (manageCMDMsg(ctx)) {
return true
}
// 群组消息处理
groupAgent.manageGroupMsg(ctx)
return true
}