feat: 修改请求report的方式为完整的Body
This commit is contained in:
parent
592b431878
commit
dab5b18276
30
index.ts
30
index.ts
@ -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,
|
||||
});
|
||||
|
@ -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;
|
||||
|
@ -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 "";
|
||||
|
@ -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 "";
|
||||
}
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user