167 lines
4.9 KiB
TypeScript
167 lines
4.9 KiB
TypeScript
import groupAgent from "../../controller/groupAgent"
|
||
import intentAgent from "../../controller/intentAgent"
|
||
import reportAgent from "../../controller/reportAgent"
|
||
import searchAgent from "../../controller/searchAgent"
|
||
import soupAgent from "../../controller/soupAgent"
|
||
import { Context } from "../../types"
|
||
import { isNotP2POrAtBot } from "../../utils/message"
|
||
|
||
/**
|
||
* 过滤出非法消息,如果发表情包就直接发回去
|
||
* @param {Context} ctx - 上下文数据,包含body, logger和larkService
|
||
* @returns {boolean} 是否为非法消息
|
||
*/
|
||
const filterIllegalMsg = async (ctx: Context): Promise<boolean> => {
|
||
const { body, logger, larkService, larkBody } = ctx
|
||
const { chatId, msgType, msgText } = larkBody
|
||
// 没有chatId的消息不处理
|
||
logger.info("接收到机器人请求", { chatId })
|
||
if (!chatId) return true
|
||
|
||
// 获取msgType
|
||
logger.info("处理机器人消息类型", { msgType })
|
||
// 放行纯文本消息
|
||
if (msgType === "text") {
|
||
// 过滤艾特全体成员的消息
|
||
if (msgText.includes("@_all")) {
|
||
return true
|
||
}
|
||
// 放行
|
||
return false
|
||
}
|
||
|
||
// 发表情包就直接发回去
|
||
if (msgType === "sticker") {
|
||
logger.info("收到表情包消息", { chatId })
|
||
const content = body?.event?.message?.content
|
||
larkService.message.send("chat_id", chatId, "sticker", content)
|
||
}
|
||
|
||
// 非表情包其他的信息都不处理
|
||
else {
|
||
logger.info("收到不支持的消息类型", { chatId, msgType })
|
||
larkService.message.sendText2Chat(
|
||
chatId,
|
||
"哇!这是什么东东?我只懂普通文本啦![可爱]"
|
||
)
|
||
}
|
||
|
||
// 非纯文本,全不放行
|
||
return true
|
||
}
|
||
|
||
/**
|
||
* 处理意图消息
|
||
* @param {Context} ctx - 上下文数据
|
||
*/
|
||
const manageIntent = async (ctx: Context) => {
|
||
const {
|
||
logger,
|
||
larkService: { message },
|
||
attachService,
|
||
larkBody: { msgText, chatId },
|
||
larkCard,
|
||
} = ctx
|
||
logger.info("收到文本消息内容", { msgText })
|
||
await message.updateOrReply(
|
||
larkCard.genPendingCard("正在理解您的意图,请稍等...")
|
||
)
|
||
try {
|
||
const intentRes = await intentAgent.agent(ctx)
|
||
logger.info("意图分析结果", { intentRes })
|
||
// 链接总结
|
||
if (intentAgent.isBriefing(intentRes)) {
|
||
reportAgent
|
||
.agent(ctx, intentRes.link, intentRes.userDescription)
|
||
.catch(() => null)
|
||
return
|
||
}
|
||
// 设置链接总结的结果存储表格
|
||
if (intentAgent.isBriefingLink(intentRes)) {
|
||
reportAgent.setSheet(ctx, intentRes.link).catch(() => null)
|
||
return
|
||
}
|
||
// 网络检索
|
||
if (intentAgent.isNetSearch(intentRes)) {
|
||
searchAgent.agent(ctx, intentRes.query).catch(() => null)
|
||
return
|
||
}
|
||
// 通用回复
|
||
if (intentAgent.isCommonResponse(intentRes)) {
|
||
await message.updateOrReply(larkCard.genSuccessCard(intentRes.message))
|
||
return
|
||
}
|
||
switch (intentRes.intent) {
|
||
// 获取聊天ID
|
||
case 3:
|
||
await message.updateOrReply(
|
||
larkCard.genTempCard("chatId", { chat_id: chatId }) as string
|
||
)
|
||
break
|
||
// CI监控
|
||
case 4:
|
||
await attachService.ciMonitor(chatId)
|
||
break
|
||
// 获取帮助
|
||
case 6:
|
||
await message.updateOrReply(
|
||
larkCard.genTempCard("eggGuide", { chat_id: chatId }) as string
|
||
)
|
||
break
|
||
// 开启日报订阅
|
||
case 7:
|
||
groupAgent.report.setSubscription(ctx, "daily", true).catch(() => null)
|
||
break
|
||
// 开启周报订阅
|
||
case 8:
|
||
groupAgent.report.setSubscription(ctx, "weekly", true).catch(() => null)
|
||
break
|
||
// 关闭日报订阅
|
||
case 9:
|
||
groupAgent.report.setSubscription(ctx, "daily", false).catch(() => null)
|
||
break
|
||
// 关闭周报订阅
|
||
case 10:
|
||
groupAgent.report
|
||
.setSubscription(ctx, "weekly", false)
|
||
.catch(() => null)
|
||
break
|
||
// 生成日报
|
||
case 11:
|
||
groupAgent.report.gen4Test(ctx, "daily").catch(() => null)
|
||
break
|
||
// 生成周报
|
||
case 12:
|
||
groupAgent.report.gen4Test(ctx, "weekly").catch(() => null)
|
||
break
|
||
// 开始海龟汤游戏
|
||
case 13:
|
||
soupAgent.startOrStopGame(ctx, true, "manual").catch(() => null)
|
||
break
|
||
// 默认为使用群聊上下文的意图
|
||
case 1:
|
||
default:
|
||
groupAgent.agent(ctx).catch(() => null)
|
||
break
|
||
}
|
||
} catch (error) {
|
||
logger.error("意图分析处理失败", { error })
|
||
await message.updateOrReply(larkCard.genErrorCard(`意图分析失败`))
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 处理Event消息
|
||
* @param {Context} ctx - 上下文数据
|
||
*/
|
||
export const manageEventMsg = async (ctx: Context) => {
|
||
// 非群聊和非艾特机器人的消息不处理
|
||
if (isNotP2POrAtBot(ctx)) return
|
||
// 过滤非法消息
|
||
if (await filterIllegalMsg(ctx)) return
|
||
// 海龟汤
|
||
if (await soupAgent.agent(ctx)) return
|
||
// 处理命令消息
|
||
await manageIntent(ctx)
|
||
}
|