egg_server/routes/bot/actionMsg.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

68 lines
1.6 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 { sleep } from "bun"
import { Context, LarkAction } from "../../types"
import { getActionType, getIsActionMsg } from "../../utils/msgTools"
/**
* 返回ChatId卡片
* @param {LarkAction.Data} body
* @returns {Promise<string>} 返回包含ChatId卡片的JSON字符串
*/
const makeChatIdCard = async (body: LarkAction.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,
}
/**
* 处理按钮点击事件
* @param {Context.Data} ctx - 上下文数据包含body, larkService和logger
* @returns {Promise<void>} 无返回值
*/
const manageBtnClick = async ({
body,
larkService,
logger,
}: Context.Data): Promise<void> => {
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(body)
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)
return true
}