egg_server/routes/bot/eventMsg.ts
zhaoyingbo 09e352a9c1
All checks were successful
Egg Server MIflow / build-image (push) Successful in 1m5s
feat: 抽象网络请求类 & 内容转为ctx向内传递
2024-08-16 09:12:11 +00:00

181 lines
4.7 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 { LarkService } from "../../services"
import { Context, LarkEvent } from "../../types"
import {
getChatId,
getChatType,
getIsEventMsg,
getMentions,
getMsgText,
getMsgType,
} from "../../utils/msgTools"
/**
* 是否为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.debug(`bot req chatId: ${chatId}`)
if (!chatId) return true
// 获取msgType
const msgType = getMsgType(body)
logger.debug(`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 => {
const content = JSON.stringify({
type: "template",
data: {
config: {
update_multi: true,
},
template_id: "ctp_AAi3NnHb6zgK",
template_variable: {
chat_id: chatId,
},
},
})
service.message.send("chat_id", chatId, "interactive", content)
}
/**
* 处理命令消息
* @param {Context.Data} ctx - 上下文数据包含body, logger, larkService和attachService
* @returns {boolean} 是否处理了命令消息
*/
const manageCMDMsg = ({
body,
logger,
larkService,
attachService,
}: Context.Data): boolean => {
const text = getMsgText(body)
logger.debug(`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
}
// 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
}
return false
}
/**
* 回复引导消息
* @param {Context.Data} ctx - 上下文数据包含body, larkService和logger
*/
const replyGuideMsg = ({ body, larkService, logger }: Context.Data): void => {
const chatId = getChatId(body)
logger.info(`reply guide message, chatId: ${chatId}`)
const content = JSON.stringify({
type: "template",
data: {
config: {
enable_forward: false,
update_multi: true,
},
template_id: "ctp_AAyVx5R39xU9",
template_variable: {
chat_id: chatId,
},
},
})
larkService.message.send("chat_id", chatId, "interactive", content)
}
/**
* 处理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
}
// 返回引导消息
replyGuideMsg(ctx)
return true
}