32 lines
937 B
TypeScript
32 lines
937 B
TypeScript
import { Context } from "../../types"
|
|
import { manageActionMsg } from "./actionMsg"
|
|
import { manageEventMsg } from "./eventMsg"
|
|
|
|
/**
|
|
* 处理机器人请求
|
|
* @param {Context} ctx - 上下文数据,包含请求体、日志记录器和响应生成器
|
|
* @returns {Promise<Response>} 返回响应对象
|
|
*/
|
|
export const manageBotReq = async (ctx: Context): Promise<Response> => {
|
|
const { body, larkBody, logger } = ctx
|
|
|
|
// 检查请求体是否为空
|
|
if (!body) {
|
|
return ctx.genResp.badRequest("bot req body is empty")
|
|
}
|
|
|
|
// 验证机器人
|
|
if (body.type === "url_verification") {
|
|
logger.info("机器人验证", { challenge: body.challenge })
|
|
return Response.json({ challenge: body.challenge })
|
|
}
|
|
|
|
// 处理消息事件
|
|
if (larkBody.isMessageEvent) manageEventMsg(ctx)
|
|
// 处理Action消息
|
|
if (larkBody.isAction) return await manageActionMsg(ctx)
|
|
|
|
// 返回成功响应
|
|
return ctx.genResp.ok()
|
|
}
|