diff --git a/package-lock.json b/package-lock.json index 66ff4b1..e3d7585 100644 --- a/package-lock.json +++ b/package-lock.json @@ -394,6 +394,10 @@ "resolved": "packages/hooks", "link": true }, + "node_modules/@egg/lark-msg-tool": { + "resolved": "packages/lark-msg-tool", + "link": true + }, "node_modules/@egg/logger": { "resolved": "packages/logger", "link": true @@ -12252,6 +12256,14 @@ "lodash": "*" } }, + "packages/lark-msg-tool": { + "name": "@egg/lark-msg-tool", + "version": "1.0.0", + "license": "ISC", + "dependencies": { + "@egg/hooks": "*" + } + }, "packages/logger": { "name": "@egg/logger", "version": "1.2.1", diff --git a/packages/lark-msg-tool/.npmignore b/packages/lark-msg-tool/.npmignore new file mode 100644 index 0000000..9626c66 --- /dev/null +++ b/packages/lark-msg-tool/.npmignore @@ -0,0 +1,8 @@ +src +node_modules +.git +.vscode +.devcontainer +.npmrc +tsconfig.json +tsconfig.tsbuildinfo \ No newline at end of file diff --git a/packages/lark-msg-tool/CHANGELOG.md b/packages/lark-msg-tool/CHANGELOG.md new file mode 100644 index 0000000..60eb6cc --- /dev/null +++ b/packages/lark-msg-tool/CHANGELOG.md @@ -0,0 +1,20 @@ +# Change Log + +All notable changes to this project will be documented in this file. +See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. + +## [1.2.1](http://yingbo.im:3000/zhaoyingbo/egg_tools/compare/@egg/path-tool@1.2.0...@egg/path-tool@1.2.1) (2024-08-20) + +**Note:** Version bump only for package @egg/path-tool + +# [1.2.0](http://yingbo.im:3000/zhaoyingbo/egg_tools/compare/@egg/path-tool@1.1.0...@egg/path-tool@1.2.0) (2024-08-20) + +### Features + +- 为每个包添加npmignore ([3539dd3](http://yingbo.im:3000/zhaoyingbo/egg_tools/commits/3539dd3514ce6512b90f6561cda40f9a40e18292)) + +# 1.1.0 (2024-08-19) + +### Features + +- 完成所有包及其自动发布部署 [#3](http://yingbo.im:3000/zhaoyingbo/egg_tools/issues/3) ([ca2f556](http://yingbo.im:3000/zhaoyingbo/egg_tools/commits/ca2f556b3429daac8f765a0c3a9a81aa6db827bc)) diff --git a/packages/lark-msg-tool/package.json b/packages/lark-msg-tool/package.json new file mode 100644 index 0000000..891fd17 --- /dev/null +++ b/packages/lark-msg-tool/package.json @@ -0,0 +1,20 @@ +{ + "name": "@egg/lark-msg-tool", + "version": "1.0.0", + "description": "Lark Msg Tools for Egg projects", + "type": "module", + "main": "dist/index.js", + "types": "dist/index.d.ts", + "scripts": { + "build": "tsc", + "prepublishOnly": "npm run build" + }, + "keywords": [ + "egg", + "tools", + "lark", + "msg" + ], + "author": "RainSun ", + "license": "ISC" +} \ No newline at end of file diff --git a/packages/lark-msg-tool/src/index.ts b/packages/lark-msg-tool/src/index.ts new file mode 100644 index 0000000..8b61409 --- /dev/null +++ b/packages/lark-msg-tool/src/index.ts @@ -0,0 +1,91 @@ +import { LarkAction, LarkEvent } from "./types" + +/** + * 是否为事件消息 + * @param {LarkEvent.Data} body - 事件消息体 + * @returns {boolean} 是否为事件消息 + */ +export const getIsEventMsg = (body: LarkEvent.Data): boolean => { + return body?.header?.event_type === "im.message.receive_v1" +} + +/** + * 获取事件文本类型 + * @param {LarkEvent.Data} body - 事件消息体 + * @returns {string | undefined} 事件文本类型 + */ +export const getMsgType = (body: LarkEvent.Data): string | undefined => { + return body?.event?.message?.message_type +} + +/** + * 获取对话流Id + * @param {LarkEvent.Data} body - 事件消息体 + * @returns {string | undefined} 对话流Id + */ +export const getChatId = (body: LarkEvent.Data): string | undefined => { + return body?.event?.message?.chat_id +} + +/** + * 获取用户Id + * @param {LarkEvent.Data} body - 事件消息体 + * @returns {string | undefined} 用户Id + */ +export const getUserId = (body: LarkEvent.Data): string | undefined => { + return body?.event?.sender?.sender_id?.user_id +} + +/** + * 是否为Action消息 + * @param {LarkAction.Data} body - Action消息体 + * @returns {boolean} 是否为Action消息 + */ +export const getIsActionMsg = (body: LarkAction.Data): boolean => { + return !!body?.action +} + +/** + * 获取Action类型 + * @param {LarkAction.Data} body - Action消息体 + * @returns {string | undefined} Action类型 + */ +export const getActionType = (body: LarkAction.Data): string | undefined => { + return body?.action?.tag +} + +/** + * 获取文本内容并剔除艾特信息 + * @param {LarkEvent.Data} body - 事件消息体 + * @returns {string} 文本内容 + */ +export const getMsgText = (body: LarkEvent.Data): string => { + try { + const { text }: { text: string } = JSON.parse(body?.event?.message?.content) + // 去掉@_user_1相关的内容,例如 '@_user_1 测试' -> '测试' + const textWithoutAt = text.replace(/@_user_\d+/g, "") + return textWithoutAt + } catch { + return "" + } +} + +/** + * 获取聊天类型 + * @param {LarkEvent.Data} body - 事件消息体 + * @returns {string | undefined} 聊天类型 + */ +export const getChatType = (body: LarkEvent.Data): string | undefined => { + return body?.event?.message?.chat_type +} + +/** + * 获取艾特信息 + * @param {LarkEvent.Data} body - 事件消息体 + * @returns {Array | undefined} 艾特信息 + */ +export const getMentions = (body: LarkEvent.Data): Array | undefined => { + return body?.event?.message?.mentions +} + +export { LarkAction, LarkEvent } diff --git a/packages/lark-msg-tool/src/types/index.ts b/packages/lark-msg-tool/src/types/index.ts new file mode 100644 index 0000000..9334017 --- /dev/null +++ b/packages/lark-msg-tool/src/types/index.ts @@ -0,0 +1,4 @@ +import type { LarkAction } from "./larkAction" +import type { LarkEvent } from "./larkEvent" + +export { LarkAction, LarkEvent } \ No newline at end of file diff --git a/packages/lark-msg-tool/src/types/larkAction.ts b/packages/lark-msg-tool/src/types/larkAction.ts new file mode 100644 index 0000000..22e57c6 --- /dev/null +++ b/packages/lark-msg-tool/src/types/larkAction.ts @@ -0,0 +1,56 @@ +export namespace LarkAction { + export interface Data { + /** + * open_id + */ + open_id: string + /** + * 用户名 + * @example zhaoyingbo + */ + user_id: string + /** + * 当前消息的ID + * @example om_038fc0eceed6224a1abc1cdaa4266405 + */ + open_message_id: string + /** + * 对话流ID + * @example oc_433b1cb7a9dbb7ebe70a4e1a59cb8bb1 + */ + open_chat_id: string + /** + * 应用ID + * @example 2ee61fe50f4f1657 + */ + tenant_key: string + /** + * token + * @example tV9djUKSjzVnekV7xTg2Od06NFTcsBnj + */ + token: string + /** + * 事件结果 + */ + action: { + /** + * 传的参数 + */ + value: any + /** + * 标签名 + * @example picker_datetime + */ + tag: string + /** + * 选择的事件 + * @example 2023-09-03 10:35 +0800 + */ + option: string + /** + * 时区 + */ + timezone: string + } + } +} diff --git a/packages/lark-msg-tool/src/types/larkEvent.ts b/packages/lark-msg-tool/src/types/larkEvent.ts new file mode 100644 index 0000000..aad0ea4 --- /dev/null +++ b/packages/lark-msg-tool/src/types/larkEvent.ts @@ -0,0 +1,163 @@ +export namespace LarkEvent { + /** + * 消息事件头 + */ + export interface Header { + /** + * 事件ID + * @example 0f8ab23b60993cf8dd15c8cde4d7b0f5 + */ + event_id: string + /** + * token + * @example tV9djUKSjzVnekV7xTg2Od06NFTcsBnj + */ + token: string + /** + * 创建时间戳 + * @example 1693565712117 + */ + create_time: string + /** + * 事件类型 + * @example im.message.receive_v1 + */ + event_type: string + /** + * tenant_key + * @example 2ee61fe50f4f1657 + */ + tenant_key: string + /** + * app_id + * @example cli_a1eff35b43b89063 + */ + app_id: string + } + /** + * 被AT的人的信息 + */ + export interface Mention { + /** + * 被艾特的人的ID信息 + */ + id: UserIdInfo + /** + * 对应到文本内的内容 + * @example "@_user_1" + */ + key: string + /** + * 用户名 + * @example 小煎蛋 + */ + name: string + /** + * 应用ID + * @example 2ee61fe50f4f1657 + */ + tenant_key: string + } + /** + * 消息内容信息 + */ + export interface Message { + /** + * 对话流ID + * @example oc_433b1cb7a9dbb7ebe70a4e1a59cb8bb1 + */ + chat_id: string + /** + * 消息类型 + * @example group | p2p + */ + chat_type: "group" | "p2p" + /** + * JSON字符串文本内容 + * @example "{\"text\":\"@_user_1 测试\"}" + */ + content: string + /** + * 消息发送时间戳 + * @example 1693565711996 + */ + create_time: string + /** + * 被艾特的人信息 + */ + mentions?: Mention[] + /** + * 当前消息的ID + * @example om_038fc0eceed6224a1abc1cdaa4266405 + */ + message_id: string + /** + * 消息类型 + * @example text、post、image、file、audio、media、sticker、interactive、share_chat、share_user + */ + message_type: string + } + + /** + * 用户ID信息 + */ + export interface UserIdInfo { + /** + * 用户标记 + * @example ou_032f507d08f9a7f28b042fcd086daef5 + */ + open_id: string + /** + * 用户标记 + * @example on_7111660fddd8302ce47bf1999147c011 + */ + union_id: string + /** + * 用户名 + * @example zhaoyingbo + */ + user_id: string + } + /** + * 消息发送者信息 + */ + export interface Sender { + /** + * id 相关信息 + */ + sender_id: UserIdInfo + /** + * 发送者类型 + * @example user + */ + sender_type: string + /** + * 应用ID + * @example 2ee61fe50f4f1657 + */ + tenant_key: string + } + /** + * 事件详情 + */ + export interface Event { + message: Message + sender: Sender + } + + export interface Data { + /** + * 协议版本 + * @example 2.0 + */ + schema: string + /** + * 事件头 + */ + header: Header + /** + * 事件详情 + */ + event: Event + } +} diff --git a/packages/lark-msg-tool/tsconfig.json b/packages/lark-msg-tool/tsconfig.json new file mode 100644 index 0000000..87bccba --- /dev/null +++ b/packages/lark-msg-tool/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "outDir": "./dist", + "rootDir": "./src" + }, + "include": ["./src"] +}