245 lines
6.3 KiB
TypeScript
245 lines
6.3 KiB
TypeScript
import tempMap from "../../constant/template"
|
||
import gitlabEvent from "../../controller/gitlabEvent"
|
||
import groupAgent from "../../controller/groupAgent"
|
||
import createKVTemp from "../../controller/sheet/createKVTemp"
|
||
import { Context } from "../../types"
|
||
|
||
/**
|
||
* 过滤出非法消息,如果发表情包就直接发回去
|
||
* @param {Context} ctx - 上下文数据,包含body, logger和larkService
|
||
* @returns {boolean} 是否为非法消息
|
||
*/
|
||
const filterIllegalMsg = async ({
|
||
body,
|
||
logger,
|
||
larkService,
|
||
larkBody,
|
||
appInfo,
|
||
}: Context): Promise<boolean> => {
|
||
const { chatId, msgType, msgText } = larkBody
|
||
// 没有chatId的消息不处理
|
||
logger.info(`bot req chatId: ${chatId}`)
|
||
if (!chatId) return true
|
||
|
||
// 非私聊和群聊中艾特机器人的消息不处理
|
||
if (!larkBody.isP2P && !larkBody.isAtBot(appInfo.appName)) {
|
||
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}`)
|
||
larkService.message.sendText2Chat(
|
||
chatId,
|
||
"哇!这是什么东东?我只懂普通文本啦![可爱]"
|
||
)
|
||
}
|
||
|
||
// 非纯文本,全不放行
|
||
return true
|
||
}
|
||
|
||
/**
|
||
* 发送ID消息
|
||
* @param {Context} ctx - 上下文数据
|
||
*/
|
||
const manageIdMsg = ({
|
||
larkBody: { chatId },
|
||
larkCard,
|
||
larkService,
|
||
}: Context) =>
|
||
larkService.message.sendCard2Chat(
|
||
chatId,
|
||
larkCard.genTempCard("chatId", { chat_id: chatId })
|
||
)
|
||
|
||
/**
|
||
* 回复引导消息
|
||
* @param {Context} ctx - 上下文数据
|
||
*/
|
||
const manageHelpMsg = (
|
||
{ larkBody: { chatId }, larkCard, larkService }: Context,
|
||
tempKey: keyof typeof tempMap
|
||
) =>
|
||
larkService.message.sendCard2Chat(
|
||
chatId,
|
||
larkCard.genTempCard(tempKey, { chat_id: chatId }) as string
|
||
)
|
||
|
||
/**
|
||
* 处理命令消息
|
||
* @param {Context} ctx - 上下文数据
|
||
*/
|
||
const manageCMDMsg = async (ctx: Context) => {
|
||
const {
|
||
body,
|
||
logger,
|
||
larkService,
|
||
attachService,
|
||
larkBody: { msgText, chatId, isInGroup, isP2P },
|
||
} = ctx
|
||
logger.info(`bot req text: ${msgText}`)
|
||
|
||
// 处理命令消息
|
||
if (msgText === "/id") {
|
||
logger.info(`bot command is /id, chatId: ${chatId}`)
|
||
await manageIdMsg(ctx)
|
||
return
|
||
}
|
||
|
||
// CI监控
|
||
if (msgText === "/ci monitor") {
|
||
logger.info(`bot command is /ci, chatId: ${chatId}`)
|
||
await attachService.ciMonitor(chatId)
|
||
return
|
||
}
|
||
// 简报
|
||
if (msgText.includes("share") && msgText.includes("简报")) {
|
||
logger.info(`bot command is share report, chatId: ${chatId}`)
|
||
// 这个用时比较久,先发一条提醒用户收到了请求
|
||
// TODO: 迁移到简报服务中
|
||
await larkService.message.sendText2Chat(
|
||
chatId,
|
||
"正在为您收集简报,请稍等片刻~"
|
||
)
|
||
await attachService.reportCollector(body)
|
||
return
|
||
}
|
||
// 创建Sheet DB
|
||
if (msgText === "/gen db") {
|
||
logger.info(`bot command is /gen db, chatId: ${chatId}`)
|
||
await createKVTemp.createFromEvent(ctx)
|
||
return
|
||
}
|
||
|
||
// 关闭CICD成功提醒
|
||
if (msgText.startsWith("/ci off")) {
|
||
logger.info(`bot command is /notify ci off, chatId: ${chatId}`)
|
||
await gitlabEvent.register.closeCICDNotify(
|
||
ctx,
|
||
Number(msgText.replace("/ci off ", ""))
|
||
)
|
||
return
|
||
}
|
||
|
||
// 开启CICD成功提醒
|
||
if (msgText.startsWith("/ci")) {
|
||
logger.info(`bot command is /ci, chatId: ${chatId}`)
|
||
await gitlabEvent.register.openCICDNotify(
|
||
ctx,
|
||
Number(msgText.replace("/ci ", ""))
|
||
)
|
||
return
|
||
}
|
||
|
||
// 关闭MR自动总结
|
||
if (msgText.startsWith("/mr off")) {
|
||
logger.info(`bot command is /mr off, chatId: ${chatId}`)
|
||
await gitlabEvent.register.closeMRSummary(
|
||
ctx,
|
||
Number(msgText.replace("/mr off ", ""))
|
||
)
|
||
return
|
||
}
|
||
|
||
// 开启MR自动总结
|
||
if (msgText.startsWith("/mr")) {
|
||
logger.info(`bot command is /mr, chatId: ${chatId}`)
|
||
await gitlabEvent.register.openMRSummary(
|
||
ctx,
|
||
Number(msgText.replace("/mr ", ""))
|
||
)
|
||
return
|
||
}
|
||
|
||
// 私聊场景下或者/help命令
|
||
if (msgText === "/help" || isP2P) {
|
||
logger.info(`bot command is /help, chatId: ${chatId}`)
|
||
await manageHelpMsg(ctx, "eggGuide")
|
||
return
|
||
}
|
||
|
||
// 仅限群组功能
|
||
if (isInGroup) {
|
||
// 注册群组日报
|
||
if (msgText === "开启日报") {
|
||
logger.info(
|
||
`bot command is register, chatId: ${chatId}, timeScope: daily`
|
||
)
|
||
groupAgent.report.setSubscription(ctx, "daily", true)
|
||
return
|
||
}
|
||
// 注册群组周报
|
||
if (msgText === "开启周报") {
|
||
logger.info(
|
||
`bot command is register, chatId: ${chatId}, timeScope: weekly`
|
||
)
|
||
groupAgent.report.setSubscription(ctx, "weekly", true)
|
||
return
|
||
}
|
||
|
||
// 注销群组日报
|
||
if (msgText === "关闭日报") {
|
||
logger.info(
|
||
`bot command is unregister, chatId: ${chatId}, timeScope: daily`
|
||
)
|
||
groupAgent.report.setSubscription(ctx, "daily", false)
|
||
return
|
||
}
|
||
// 注销群组周报
|
||
if (msgText === "关闭周报") {
|
||
logger.info(
|
||
`bot command is unregister, chatId: ${chatId}, timeScope: weekly`
|
||
)
|
||
groupAgent.report.setSubscription(ctx, "weekly", false)
|
||
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
|
||
}
|
||
// 使用GroupAgent兜底
|
||
groupAgent.agent(ctx)
|
||
return
|
||
}
|
||
return
|
||
}
|
||
|
||
/**
|
||
* 处理Event消息
|
||
* @param {Context} ctx - 上下文数据
|
||
*/
|
||
export const manageEventMsg = async (ctx: Context) => {
|
||
// 过滤非法消息
|
||
if (await filterIllegalMsg(ctx)) return
|
||
// 处理命令消息
|
||
await manageCMDMsg(ctx)
|
||
}
|