feat: 优化非法消息过滤 & 优化错误返回
All checks were successful
Egg CI/CD / build-image (push) Successful in 36s
Egg CI/CD / deploy (push) Successful in 23s

This commit is contained in:
zhaoyingbo 2024-05-24 11:26:06 +00:00
parent 47540f4800
commit 592b431878
8 changed files with 64 additions and 52 deletions

View File

@ -1,20 +1,39 @@
import { RecordModel } from "pocketbase";
import pbClient from "../pbClient";
import { managePb404 } from "../../utils/pbTools";
const getAppId = async () => {
const { value } = await pbClient
.collection("config")
.getOne("iscqaiwe0bnp8ej");
return value as string;
export interface AppConfigRecordModel extends RecordModel {
value: string;
}
/**
*
* @param key
* @returns
*/
const get = async (key: string) => {
const config = await managePb404<AppConfigRecordModel>(
async () =>
await await pbClient.collection("config").getFirstListItem(`key='${key}'`)
);
if (!config) return "";
return config.value;
};
const getAppSecret = async () => {
const { value } = await pbClient
.collection("config")
.getOne("9kyiw5mv8unia49");
return value as string;
};
/**
* Appid
* @returns {string} Appid
*/
const getAppId = async () => get("app_id");
/**
* AppSecret
* @returns {string} AppSecret
*/
const getAppSecret = async () => get("app_secret");
const appConfig = {
get,
getAppId,
getAppSecret,
};

View File

@ -17,9 +17,9 @@ export interface PBMessageGroup {
}
const getOne = (groupId: string) =>
managePb404(
managePb404<PBMessageGroup>(
async () => await pbClient.collection("message_group").getOne(groupId)
) as Promise<PBMessageGroup>;
);
const messageGroup = {
getOne,

View File

@ -1,3 +1,4 @@
import appConfig from "../appConfig";
import pbClient from "../pbClient";
let token = "";
@ -18,10 +19,7 @@ const update = async (value: string) => {
*/
const get = async () => {
if (token) return token;
const { value } = await pbClient
.collection("config")
.getOne("ugel8f0cpk0rut6");
return value as string;
return await appConfig.get("tenant_access_token");
};
const tenantAccessToken = {

View File

@ -43,6 +43,7 @@ const manageBtnClick = async (body: LarkUserAction) => {
/**
* Action消息
* @param {LarkUserAction} body
* @returns {boolean}
*/
export const manageActionMsg = (body: LarkUserAction) => {
// 过滤非Action消息

View File

@ -37,6 +37,10 @@ export const getMsgText = (body: LarkMessageEvent) => {
const filterIllegalMsg = (body: LarkMessageEvent) => {
const chatId = getChatId(body);
if (!chatId) return true;
// 过滤下艾特全体成员的消息
if (getMsgText(body).includes("@_all")) {
return true;
}
// 获取msgType
const msgType = getMsgType(body);
// 发表情包就直接发回去
@ -60,10 +64,6 @@ const filterIllegalMsg = (body: LarkMessageEvent) => {
lark.sendMsg("chat_id", chatId, "text", content);
return true;
}
// 还得过滤下艾特全体成员的消息
if (getMsgText(body).includes("@_all")) {
return true;
}
return false;
};
@ -135,6 +135,7 @@ const replyGuideMsg = async (body: LarkMessageEvent) => {
/**
* Event消息
* @param {LarkUserAction} body
* @returns {boolean}
*/
export const manageEventMsg = (body: LarkMessageEvent) => {
// 过滤非Event消息

View File

@ -20,16 +20,16 @@ type MessageReqJson = GroupMsg & NormalMsg;
const validateMessageReq = (body: MessageReqJson) => {
if (!body.group_id && !body.receive_id) {
return new Response("group_id or receive_id is required");
return new Response("group_id or receive_id is required", { status: 400 });
}
if (body.receive_id && !body.receive_id_type) {
return new Response("receive_id_type is required");
return new Response("receive_id_type is required", { status: 400 });
}
if (!body.msg_type) {
return new Response("msg_type is required");
return new Response("msg_type is required", { status: 400 });
}
if (!body.content) {
return new Response("content is required");
return new Response("content is required", { status: 400 });
}
return false;
};
@ -62,7 +62,7 @@ export const manageMessageReq = async (req: Request) => {
// 获取所有接收者
const group = await db.messageGroup.getOne(body.group_id!);
if (!group) {
return new Response("group not found");
return new Response("group not found", { status: 404 });
}
const { chat_id, open_id, union_id, user_id, email } = group;
@ -71,14 +71,11 @@ export const manageMessageReq = async (req: Request) => {
const makeSendFunc = (receive_id_type: ReceiveIDType) => {
return (receive_id: string) => {
sendList.push(
lark.sendMsg(
receive_id_type,
receive_id,
body.msg_type,
finalContent
).then((res) => {
sendRes[receive_id_type][receive_id] = res;
})
lark
.sendMsg(receive_id_type, receive_id, body.msg_type, finalContent)
.then((res) => {
sendRes[receive_id_type][receive_id] = res;
})
);
};
};
@ -93,14 +90,16 @@ export const manageMessageReq = async (req: Request) => {
if (body.receive_id && body.receive_id_type) {
sendList.push(
lark.sendMsg(
body.receive_id_type,
body.receive_id,
body.msg_type,
finalContent
).then((res) => {
sendRes[body.receive_id_type][body.receive_id] = res;
})
lark
.sendMsg(
body.receive_id_type,
body.receive_id,
body.msg_type,
finalContent
)
.then((res) => {
sendRes[body.receive_id_type][body.receive_id] = res;
})
);
}

View File

@ -24,9 +24,6 @@ export const fetchReportCollector = async (
user: string,
chat_id: string
) => {
// const url = `https://report.imoaix.cn/report?msg=${msg}&user=${user}&chat_id=${chat_id}`;
// 使用URLSearchParams拼接参数
const params = new URLSearchParams();
params.append("msg", msg);
params.append("user", user);
@ -34,7 +31,6 @@ export const fetchReportCollector = async (
const url = `https://report.imoaix.cn/report?${params.toString()}`;
try {
console.log("🚀 ~ url:", url);
const res = await fetch(url);
return ((await res.json()) as string) || "";
} catch {

View File

@ -1,11 +1,9 @@
export const managePb404 = async (dbFunc: Function) => {
export const managePb404 = async <T>(dbFunc: Function): Promise<T | null> => {
try {
return await dbFunc()
return await dbFunc();
} catch (err: any) {
console.log("🚀 ~ manage404 ~ err:", err)
// 没有这个提醒就返回空
if (err?.message === "The requested resource wasn't found.") {
return null
return null;
} else throw err;
}
}
};