feat: 修改请求report的方式为完整的Body
All checks were successful
Egg CI/CD / build-image (push) Successful in 38s
Egg CI/CD / deploy (push) Successful in 1m19s

This commit is contained in:
zhaoyingbo 2024-05-24 12:01:51 +00:00
parent 592b431878
commit dab5b18276
4 changed files with 53 additions and 50 deletions

View File

@ -2,19 +2,25 @@ import { manageBotReq } from "./routes/bot";
import { manageMessageReq } from "./routes/message";
import { initSchedule } from "./schedule";
initSchedule()
initSchedule();
Bun.serve({
async fetch(req) {
const url = new URL(req.url);
// 根路由
if (url.pathname === "/") return new Response("hello, glade to see you!");
// 机器人
if (url.pathname === '/bot') return await manageBotReq(req);
// 消息发送
if (url.pathname === '/message') return await manageMessageReq(req);
// 其他
return new Response('OK')
try {
const url = new URL(req.url);
// 根路由
if (url.pathname === "/") return new Response("hello, glade to see you!");
// 机器人
if (url.pathname === "/bot") return await manageBotReq(req);
// 消息代理发送
if (url.pathname === "/message") return await manageMessageReq(req);
// 其他
return new Response("hello, glade to see you!");
} catch (error: any) {
// 错误处理
console.error("🚀 ~ serve ~ error", error);
return new Response(error.message || "server error", { status: 500 });
}
},
port: 3000
});
port: 3000,
});

View File

@ -4,31 +4,10 @@ import { LarkMessageEvent } from "../../types";
import {
getChatId,
getIsEventMsg,
getMsgText,
getMsgType,
getUserId,
} from "../../utils/msgTools";
/**
*
* @param {LarkMessageEvent} body
* @returns {string}
*/
export const getMsgText = (body: LarkMessageEvent) => {
// TODO: 如果之后想支持单独提醒,这里需要做模板解析
try {
const { text }: { text: string } = JSON.parse(
body?.event?.message?.content
);
// 去掉@_user_1相关的内容例如 '@_user_1 测试' -> '测试'
const textWithoutAt = text.replace(/@_user_\d+/g, "");
// // 去除空格和换行
// const textWithoutSpace = textWithoutAt.replace(/[\s\n]/g, "");
return textWithoutAt;
} catch (e) {
return "";
}
};
/**
*
* @param {LarkMessageEvent} body
@ -104,7 +83,7 @@ const manageCMDMsg = (body: LarkMessageEvent) => {
return true;
}
if (text.includes("share") && text.includes("简报")) {
fetchReportCollector(text, getUserId(body), chatId);
fetchReportCollector(body);
return true;
}
return false;

View File

@ -1,3 +1,5 @@
import { LarkMessageEvent } from "../types";
/**
* CI
*/
@ -14,24 +16,20 @@ export const fetchCIMonitor = async (chat_id: string) => {
/**
*
* @param msg
* @param user
* @param chat_id
* @param body
* @returns
*/
export const fetchReportCollector = async (
msg: string,
user: string,
chat_id: string
) => {
const params = new URLSearchParams();
params.append("msg", msg);
params.append("user", user);
params.append("chat_id", chat_id);
const url = `https://report.imoaix.cn/report?${params.toString()}`;
export const fetchReportCollector = async (body: LarkMessageEvent) => {
const url = "https://report.imoaix.cn/report";
// 将body作为请求体post到url
try {
const res = await fetch(url);
const res = await fetch(url, {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json",
},
});
return ((await res.json()) as string) || "";
} catch {
return "";

View File

@ -51,3 +51,23 @@ export const getIsActionMsg = (body: LarkUserAction) => {
export const getActionType = (body: LarkUserAction) => {
return body?.action?.tag;
};
/**
*
* @param {LarkMessageEvent} body
* @returns {string}
*/
export const getMsgText = (body: LarkMessageEvent) => {
try {
const { text }: { text: string } = JSON.parse(
body?.event?.message?.content
);
// 去掉@_user_1相关的内容例如 '@_user_1 测试' -> '测试'
const textWithoutAt = text.replace(/@_user_\d+/g, "");
// // 去除空格和换行
// const textWithoutSpace = textWithoutAt.replace(/[\s\n]/g, "");
return textWithoutAt;
} catch (e) {
return "";
}
};