feat(multi-app): 支持多机器人功能
All checks were successful
Egg Server MIflow / build-image (push) Successful in 50s

This commit is contained in:
zhaoyingbo 2024-09-26 08:24:40 +00:00
parent cbe9e77336
commit d5e8755886
4 changed files with 33 additions and 26 deletions

View File

@ -16,7 +16,8 @@ const get = async (appName: string) =>
*
* @returns
*/
const getFullList = () => pbClient.collection("app_info").getFullList()
const getFullList = () =>
pbClient.collection("app_info").getFullList<DB.AppInfo>()
/**
*

View File

@ -7,20 +7,24 @@ import {
getMsgType,
} from "@egg/lark-msg-tool"
import db from "../../db"
import { LarkService } from "../../services"
import { Context } from "../../types"
import createKVTemp from "../sheet/createKVTemp"
import groupAgent from "./groupAgent"
/**
* P2P或者群聊并且艾特了小煎蛋
* P2P或者群聊并且艾特了机器人
* @param {LarkEvent.Data} body
* @returns {boolean} P2P或者群聊并且艾特了小煎蛋
*/
const getIsP2pOrGroupAtBot = (body: LarkEvent.Data): boolean => {
const getIsP2pOrGroupAtBot = async (body: LarkEvent.Data): Promise<boolean> => {
const appList = (await db.appInfo.getFullList())
.map((v) => v.app_name)
.filter((v) => v)
const isP2p = getChatType(body) === "p2p"
const isAtBot = getMentions(body)?.some?.(
(mention) => mention.name === "小煎蛋"
const isAtBot = getMentions(body)?.some?.((mention) =>
appList.includes(mention.name)
)
return Boolean(isP2p || isAtBot)
}
@ -30,18 +34,18 @@ const getIsP2pOrGroupAtBot = (body: LarkEvent.Data): boolean => {
* @param {Context.Data} ctx - body, logger和larkService
* @returns {boolean}
*/
const filterIllegalMsg = ({
const filterIllegalMsg = async ({
body,
logger,
larkService,
}: Context.Data): boolean => {
}: Context.Data): Promise<boolean> => {
// 没有chatId的消息不处理
const chatId = getChatId(body)
logger.info(`bot req chatId: ${chatId}`)
if (!chatId) return true
// 非私聊和群聊中艾特小煎蛋的消息不处理
if (!getIsP2pOrGroupAtBot(body)) {
// 非私聊和群聊中艾特机器人的消息不处理
if (!(await getIsP2pOrGroupAtBot(body))) {
return true
}
@ -65,8 +69,8 @@ const filterIllegalMsg = ({
larkService.message.send("chat_id", chatId, "sticker", content)
}
// 非表情包只在私聊或者群聊中艾特小煎蛋时才回复
else if (getIsP2pOrGroupAtBot(body)) {
// 非表情包只在私聊或者群聊中艾特机器人时才回复
else if (await getIsP2pOrGroupAtBot(body)) {
logger.info(`got a illegal message, chatId: ${chatId}`)
const content = JSON.stringify({
text: "哇!这是什么东东?我只懂普通文本啦![可爱]",
@ -102,9 +106,8 @@ const manageHelpMsg = (chatId: string, service: LarkService): void => {
/**
*
* @param {Context.Data} ctx - body, logger, larkService和attachService
* @returns {boolean}
*/
const manageCMDMsg = (ctx: Context.Data): boolean => {
const manageCMDMsg = (ctx: Context.Data) => {
const { body, logger, larkService, attachService } = ctx
const text = getMsgText(body)
logger.info(`bot req text: ${text}`)
@ -113,19 +116,19 @@ const manageCMDMsg = (ctx: Context.Data): boolean => {
if (text.trim() === "/id") {
logger.info(`bot command is /id, chatId: ${chatId}`)
manageIdMsg(chatId, larkService)
return true
return
}
// 帮助
if (text.trim() === "/help") {
logger.info(`bot command is /help, chatId: ${chatId}`)
manageHelpMsg(chatId, larkService)
return true
return
}
// CI监控
if (text.trim() === "/ci") {
logger.info(`bot command is /ci, chatId: ${chatId}`)
attachService.ciMonitor(chatId)
return true
return
}
// 简报
if (text.includes("share") && text.includes("简报")) {
@ -139,31 +142,30 @@ const manageCMDMsg = (ctx: Context.Data): boolean => {
"正在为您收集简报,请稍等片刻~"
)
attachService.reportCollector(body)
return true
return
}
// 创建Sheet DB
if (text.trim() === "/gen db") {
logger.info(`bot command is /gen db, chatId: ${chatId}`)
createKVTemp.createFromEvent(ctx)
return true
return
}
// 选择群组信息
if (text.trim() === "@群聊助手") {
logger.info(`bot command is @群聊助手, chatId: ${chatId}`)
groupAgent.sendGroupSelector(ctx)
return true
return
}
return false
return
}
/**
* Event消息
* @param {Context.Data} ctx -
*/
export const manageEventMsg = (ctx: Context.Data) => {
export const manageEventMsg = async (ctx: Context.Data) => {
// 过滤非法消息
if (filterIllegalMsg(ctx)) return
if (await filterIllegalMsg(ctx)) return
// 处理命令消息
manageCMDMsg(ctx)
return true
}

View File

@ -16,5 +16,6 @@ export namespace Context {
attachService: AttachService
path: PathCheckTool
searchParams: URLSearchParams
app: string
}
}

View File

@ -35,10 +35,12 @@ const genContext = async (req: Request) => {
} catch {
/* empty */
}
const searchParams = new URL(req.url).searchParams
const app = searchParams.get("app") || "egg"
const requestId = getPreRequestId(body) || uuid()
const logger = loggerIns.child({ requestId })
const genResp = new NetTool({ requestId })
const larkService = new LarkService("egg", requestId)
const larkService = new LarkService(app, requestId)
const attachService = new AttachService({ requestId })
const path = new PathCheckTool(req.url)
@ -52,7 +54,8 @@ const genContext = async (req: Request) => {
text,
larkService,
attachService,
searchParams: new URL(req.url).searchParams,
searchParams,
app,
} as Context.Data
}