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

33 lines
927 B
TypeScript

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