All checks were successful
Egg Server MIflow / build-image (push) Successful in 1m5s
68 lines
1.6 KiB
TypeScript
68 lines
1.6 KiB
TypeScript
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
|
||
}
|