92 lines
2.1 KiB
TypeScript
92 lines
2.1 KiB
TypeScript
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;
|
||
};
|