egg_server/utils/msgTools.ts
zhaoyingbo b7437f47e4
All checks were successful
Egg CI/CD / build-image (push) Successful in 49s
Egg CI/CD / deploy (push) Successful in 23s
feat: 优化请求处理 & 拆分Type
2024-06-08 09:15:14 +00:00

92 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { LarkAction, LarkEvent } from "../types";
/**
* 是否为事件消息
* @param {LarkEvent.Data} body
*/
export const getIsEventMsg = (body: LarkEvent.Data) => {
return body?.header?.event_type === "im.message.receive_v1";
};
/**
* 获取事件文本类型
* @param {LarkEvent.Data} body
* @returns
*/
export const getMsgType = (body: LarkEvent.Data) => {
return body?.event?.message?.message_type;
};
/**
* 获取对话流Id
* @param {LarkEvent.Data} body
* @returns
*/
export const getChatId = (body: LarkEvent.Data) => {
return body?.event?.message?.chat_id;
};
/**
* 获取用户Id
* @param {LarkEvent.Data} body
* @returns
*/
export const getUserId = (body: LarkEvent.Data) => {
return body?.event?.sender?.sender_id?.user_id;
};
/**
* 是否为Action消息
* @param {LarkAction.Data} body
*/
export const getIsActionMsg = (body: LarkAction.Data) => {
return body?.action;
};
/**
* 获取Action类型
* @param {LarkAction.Data} body
* @returns {string} Action类型
*/
export const getActionType = (body: LarkAction.Data) => {
return body?.action?.tag;
};
/**
* 获取文本内容并剔除艾特信息
* @param {LarkEvent.Data} body
* @returns {string} 文本内容
*/
export const getMsgText = (body: LarkEvent.Data) => {
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 {LarkEvent.Data} body
* @returns {string} 聊天类型
*/
export const getChatType = (body: LarkEvent.Data) => {
return body?.event?.message?.chat_type;
};
/**
* 获取艾特信息
* @param {LarkEvent.Data} body
* @returns {Array} 艾特信息
*/
export const getMentions = (body: LarkEvent.Data) => {
return body?.event?.message?.mentions;
};