egg_server/routes/bot/actionMsg.ts
zhaoyingbo b992ee0b21
Some checks failed
Egg Server MIflow / build-image (push) Failing after 5m7s
feat(group-agent): 新增支持群组问答
2024-09-25 09:14:10 +00:00

68 lines
1.8 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 { getActionType, getIsActionMsg } from "@egg/lark-msg-tool"
import { sleep } from "bun"
import { Context } from "../../types"
import groupAgent from "./groupAgent"
/**
* 返回ChatId卡片
* @param {LarkAction.Data} body
* @returns {Promise<string>} 返回包含ChatId卡片的JSON字符串
*/
const makeChatIdCard = async ({ body }: Context.Data): Promise<string> => {
await sleep(500)
return JSON.stringify({
type: "template",
data: {
config: {
update_multi: true,
},
template_id: "ctp_AAi3NnHb6zgK",
template_variable: {
chat_id: body.open_chat_id,
},
},
})
}
const ACTION_MAP = {
chat_id: makeChatIdCard,
group_selector: groupAgent.setChatGroupContext,
}
/**
* 处理按钮点击事件
* @param {Context.Data} ctx - 上下文数据包含body, larkService和logger
* @returns {Promise<void>} 无返回值
*/
const manageBtnClick = async (ctx: Context.Data): Promise<void> => {
const { body, larkService, logger } = ctx
const { action } = body?.action?.value as {
action: keyof typeof ACTION_MAP
}
logger.info(`got button click action: ${action}`)
if (!action) return
const func = ACTION_MAP[action]
if (!func) return
const card = await func(ctx)
if (!card) return
// 更新飞书的卡片
await larkService.message.update(body.open_message_id, card)
}
/**
* 处理Action消息
* @param {Context.Data} ctx - 上下文数据
* @returns {boolean} 是否在本函数中处理了消息
*/
export const manageActionMsg = (ctx: Context.Data): boolean => {
// 过滤非Action消息
if (!getIsActionMsg(ctx.body)) {
return false
}
const actionType = getActionType(ctx.body)
if (actionType === "button") manageBtnClick(ctx)
if (actionType === "select_static") manageBtnClick(ctx)
return true
}