64 lines
1.4 KiB
TypeScript
64 lines
1.4 KiB
TypeScript
import { sleep } from "bun"
|
|
|
|
import service from "../../services"
|
|
import { LarkAction } from "../../types"
|
|
import { getActionType, getIsActionMsg } from "../../utils/msgTools"
|
|
|
|
/**
|
|
* 返回ChatId卡片
|
|
* @param {LarkAction.Data} body
|
|
*/
|
|
const makeChatIdCard = async (body: LarkAction.Data) => {
|
|
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 {LarkAction.Data} body
|
|
*/
|
|
const manageBtnClick = async (body: LarkAction.Data) => {
|
|
const { action } = body?.action?.value as {
|
|
action: keyof typeof ACTION_MAP
|
|
}
|
|
if (!action) return
|
|
const func = ACTION_MAP[action]
|
|
if (!func) return
|
|
const card = await func(body)
|
|
if (!card) return
|
|
// 更新飞书的卡片
|
|
await service.lark.message.update()(body.open_message_id, card)
|
|
}
|
|
|
|
/**
|
|
* 处理Action消息
|
|
* @param {LarkAction.Data} body
|
|
* @returns {boolean} 是否在本函数中处理了消息
|
|
*/
|
|
export const manageActionMsg = (body: LarkAction.Data) => {
|
|
// 过滤非Action消息
|
|
if (!getIsActionMsg(body)) {
|
|
return false
|
|
}
|
|
const actionType = getActionType(body)
|
|
if (actionType === "button") {
|
|
manageBtnClick(body)
|
|
}
|
|
return true
|
|
}
|