195 lines
4.9 KiB
TypeScript
195 lines
4.9 KiB
TypeScript
import tempMap from "../../constant/template"
|
||
import groupAgent from "../../controller/groupAgent"
|
||
import createKVTemp from "../../controller/sheet/createKVTemp"
|
||
import { Context } from "../../types"
|
||
|
||
/**
|
||
* 过滤出非法消息,如果发表情包就直接发回去
|
||
* @param {Context.Data} ctx - 上下文数据,包含body, logger和larkService
|
||
* @returns {boolean} 是否为非法消息
|
||
*/
|
||
const filterIllegalMsg = async ({
|
||
body,
|
||
logger,
|
||
larkService,
|
||
larkBody,
|
||
appInfo,
|
||
}: Context.Data): Promise<boolean> => {
|
||
const { chatId, msgType, msgText } = larkBody
|
||
// 没有chatId的消息不处理
|
||
logger.info(`bot req chatId: ${chatId}`)
|
||
if (!chatId) return true
|
||
|
||
// 非私聊和群聊中艾特机器人的消息不处理
|
||
if (!larkBody.isP2P && !larkBody.isAtBot(appInfo.app_name)) {
|
||
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 {
|
||
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, isInGroup },
|
||
} = ctx
|
||
logger.info(`bot req text: ${msgText}`)
|
||
|
||
// 处理命令消息
|
||
if (msgText === "/id") {
|
||
logger.info(`bot command is /id, chatId: ${chatId}`)
|
||
manageIdMsg(ctx)
|
||
return
|
||
}
|
||
|
||
// 仅限群组功能
|
||
if (isInGroup) {
|
||
// 注册群组
|
||
if (msgText === "开启日报、周报") {
|
||
logger.info(`bot command is register, chatId: ${chatId}`)
|
||
groupAgent.report.subscribe(ctx)
|
||
return
|
||
}
|
||
// 注销群组
|
||
if (msgText === "关闭日报、周报") {
|
||
logger.info(`bot command is unregister, chatId: ${chatId}`)
|
||
groupAgent.report.unsubscribe(ctx)
|
||
return
|
||
}
|
||
// 立即发送日简报
|
||
if (msgText === "总结日报") {
|
||
logger.info(`bot command is summary, chatId: ${chatId}`)
|
||
groupAgent.report.gen4Test(ctx, "daily")
|
||
return
|
||
}
|
||
// 立即发送周简报
|
||
if (msgText === "总结周报") {
|
||
logger.info(`bot command is summary, chatId: ${chatId}`)
|
||
groupAgent.report.gen4Test(ctx, "weekly")
|
||
return
|
||
}
|
||
}
|
||
|
||
// 小煎蛋专属功能
|
||
if (app === "egg") {
|
||
// CI监控
|
||
if (msgText === "/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 === "/gen db") {
|
||
logger.info(`bot command is /gen db, chatId: ${chatId}`)
|
||
createKVTemp.createFromEvent(ctx)
|
||
return
|
||
}
|
||
|
||
// 帮助
|
||
if (msgText === "/help") {
|
||
logger.info(`bot command is /help, chatId: ${chatId}`)
|
||
manageHelpMsg(ctx, "eggGuide")
|
||
return
|
||
}
|
||
}
|
||
|
||
// 如果是群聊,使用groupAgent兜底
|
||
if (isInGroup) {
|
||
groupAgent.agent(ctx)
|
||
return
|
||
}
|
||
|
||
return
|
||
}
|
||
|
||
/**
|
||
* 处理Event消息
|
||
* @param {Context.Data} ctx - 上下文数据
|
||
*/
|
||
export const manageEventMsg = async (ctx: Context.Data) => {
|
||
// 过滤非法消息
|
||
if (await filterIllegalMsg(ctx)) return
|
||
// 处理命令消息
|
||
manageCMDMsg(ctx)
|
||
}
|