From b44f66f6be95cff599549f712ec861840aed57df Mon Sep 17 00:00:00 2001 From: zhaoyingbo Date: Fri, 11 Oct 2024 10:59:32 +0000 Subject: [PATCH] =?UTF-8?q?feat(mi-chat):=20=E4=BC=98=E5=8C=96=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E8=BF=94=E5=9B=9E=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 3 +- bun.lockb | Bin 148017 -> 148049 bytes package.json | 2 +- routes/bot/actionMsg.ts | 13 +- routes/bot/groupAgent/cards.ts | 232 +++++++++++++++++++++++++++++++++ routes/bot/groupAgent/index.ts | 118 ++++++++++------- routes/bot/index.ts | 2 +- routes/sheet/createKVTemp.ts | 4 +- services/lark/message.ts | 2 +- utils/Card/common.ts | 61 +++++++++ utils/Card/groupAgentCard.ts | 134 +++++++++++++++++++ utils/Card/index.ts | 32 +++++ utils/genMsg.ts | 96 -------------- utils/larkMsg.ts | 129 ++++++++++++++++++ 14 files changed, 676 insertions(+), 152 deletions(-) create mode 100644 routes/bot/groupAgent/cards.ts create mode 100644 utils/Card/common.ts create mode 100644 utils/Card/groupAgentCard.ts create mode 100644 utils/Card/index.ts delete mode 100644 utils/genMsg.ts create mode 100644 utils/larkMsg.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 2e7c8dd..d5e7f79 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -27,6 +27,7 @@ "tseslint", "userid", "wlpbbgiky", - "Yoav" + "Yoav", + "zhaoyingbo" ] } diff --git a/bun.lockb b/bun.lockb index 168b2e32463b1f9f79e571fbcd2b9e249e4da14c..1317ad525cabb37deec1888565e7d49e1b4bebc8 100755 GIT binary patch delta 5779 zcmeI0dvI0N6^GBhNoXQX350kJAteEl7$AX!#1Q2|!uw58T2PP@sTJBVvBv74kOT~x zKvR<*8E#?VuJPL9qBhh#f1GV!z*Icf4g%rhj*`C*S$4 zz1B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{m2yt}?gBhBk6k=h)ByY@fG{Rt2BfRzJ0&SVFg0pY|g% zuY$Kg7ckHI{MZM)egV4Bl3(RE8ybZDck8d7SwyyV=)|ZOC!}^w>e4I4c!>o zM^n{x=(b)9S~g#UuC(c6u(P~1HFTKcHPo1mp)var(bMp6(AYc!XW>0K0*63vDn5hH z;S10}{mQHVmASv?bA&f|(JM?`$`QOr;XT*^|Ad{e3wFb+u+1x5VcdKzv402?K;yDM z41lY^4+CK^XfqQ5JwZFEbKam@6Bp2?>oClQQqWkP4nGF%(QkxFPy`d;1}K07TtO%3 zRYD%*LoQqcnJ^qiz%bD2aR|i2P)Go6t>cU{)X~myAG`r?!U5=nx1h=Es5SmkjhIiv zGtdAU-N)g5_z(`kJMb>FdV?M`cLqxNmSv!g#f>ltu7xp>2l=2qRT5~grL9~fM8SVJ z#aD0~-Uq#e_!C`gI@0uW6HJ8hFb?!OOYd#6L4OzC5e;_I6o58phHaoX2M>9Jer-ks zv_F{+H^XF@0#jicjDteZ(?t8d6i9_Mh=vd7W+rIbD+IkO+6sGM7d!@QVLjLN1Uv~Y ztc47YM@HjP^a)7OKD5p?YMS0PSuQNX}6~U!-rqyQ_2aA7WDtkqpSaUIauh+81qy^8c zG3!i3Zbp{lB{rBiZ-Z<6CM&qzHAnjew>Fs0^r&Wi1xlg!WV_Md+&kQEs-qU`TTvbh zE_mH+PW019=ECxO?jE(M^3GBBR#Yq;m7Rgl`^7mk&s!Sri}U`L?d#>uJ7;2p9p_AS G$$tUCYlB$; diff --git a/package.json b/package.json index a6f7714..44e7ef1 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "dependencies": { "@dotenvx/dotenvx": "^1.14.2", "@egg/hooks": "^1.2.0", - "@egg/lark-msg-tool": "^1.4.0", + "@egg/lark-msg-tool": "^1.5.0", "@egg/logger": "^1.4.3", "@egg/net-tool": "^1.9.1", "@egg/path-tool": "^1.4.1", diff --git a/routes/bot/actionMsg.ts b/routes/bot/actionMsg.ts index 3755d55..d26db18 100644 --- a/routes/bot/actionMsg.ts +++ b/routes/bot/actionMsg.ts @@ -12,9 +12,8 @@ const ACTION_MAP = { /** * 处理点击事件 * @param {Context.Data} ctx - 上下文数据,包含body, larkService和logger - * @returns {Promise} 无返回值 */ -const manageClick = async (ctx: Context.Data): Promise => { +const manageAction = async (ctx: Context.Data) => { const { body, logger } = ctx const { action } = body?.action?.value as { action: keyof typeof ACTION_MAP @@ -23,15 +22,17 @@ const manageClick = async (ctx: Context.Data): Promise => { if (!action) return const func = ACTION_MAP[action] if (!func) return - func(ctx) + return func(ctx) } /** * 处理Action消息 * @param {Context.Data} ctx - 上下文数据 */ -export const manageActionMsg = (ctx: Context.Data) => { +export const manageActionMsg = async (ctx: Context.Data) => { const actionType = getActionType(ctx.body) - if (actionType === "button") manageClick(ctx) - if (actionType === "select_static") manageClick(ctx) + if (["button", "select_static"].includes(actionType)) return ctx.genResp.ok() + const card = await manageAction(ctx) + if (card) return ctx.genResp.custom(card) + return ctx.genResp.ok() } diff --git a/routes/bot/groupAgent/cards.ts b/routes/bot/groupAgent/cards.ts new file mode 100644 index 0000000..682be31 --- /dev/null +++ b/routes/bot/groupAgent/cards.ts @@ -0,0 +1,232 @@ +const functionSelectorCard = { + elements: [ + { + tag: "markdown", + content: "已经选中的群聊:**${chatName}**\n", + }, + { + tag: "action", + actions: [ + { + tag: "select_static", + placeholder: { + tag: "plain_text", + content: "请选择功能", + }, + value: { + chatId: "${chatId}", + chatName: "${chatName}", + action: "sendTimeScopeSelector", + }, + options: "${functions}", + }, + ], + }, + { + tag: "hr", + }, + { + tag: "note", + elements: [ + { + tag: "plain_text", + content: "🍳 功能由AI创新应用组提供支持,Rid:${requestId}", + }, + ], + }, + ], + header: { + template: "purple", + title: { + content: "🧑‍💻 感谢使用 Group Agent", + tag: "plain_text", + }, + }, +} + +const timeScopeSelectorCard = { + elements: [ + { + tag: "markdown", + content: + "已经选中群聊:**${chatName}**\n\n已经选择功能:**${functionName}**\n\n请选择时间范围", + }, + { + tag: "action", + actions: [ + { + tag: "button", + text: { + tag: "plain_text", + content: "过去一天", + }, + type: "default", + value: { + chatId: "${chatId}", + chatName: "${chatName}", + functionId: "${functionId}", + functionName: "${functionName}", + timeScope: "1", + requestId: "${requestId}", + action: "manageGroupMsg", + }, + }, + { + tag: "button", + text: { + tag: "plain_text", + content: "过去三天", + }, + type: "default", + value: { + chatId: "${chatId}", + chatName: "${chatName}", + functionId: "${functionId}", + functionName: "${functionName}", + timeScope: "3", + requestId: "${requestId}", + action: "manageGroupMsg", + }, + }, + { + tag: "button", + text: { + tag: "plain_text", + content: "过去七天", + }, + type: "default", + value: { + chatId: "${chatId}", + chatName: "${chatName}", + functionId: "${functionId}", + functionName: "${functionName}", + timeScope: "7", + requestId: "${requestId}", + action: "manageGroupMsg", + }, + }, + ], + }, + { + tag: "hr", + }, + { + tag: "note", + elements: [ + { + tag: "plain_text", + content: "🍳 功能由AI创新应用组提供支持,Rid:${requestId}", + }, + ], + }, + ], + header: { + template: "purple", + title: { + content: "🧑‍💻 感谢使用 Group Agent", + tag: "plain_text", + }, + }, +} + +const resultReportCard = { + elements: [ + { + tag: "markdown", + content: + "群聊:**${chatName}**,功能:**${functionName}**,总结天数:**${timeScope}**\n\n以下内容由AI模型生成,耗时:**${processingTime}s**", + }, + { + tag: "hr", + }, + { + tag: "markdown", + content: "${llmRes}", + }, + { + tag: "hr", + }, + { + tag: "note", + elements: [ + { + tag: "plain_text", + content: "🍳 功能由AI创新应用组提供支持,Rid:${requestId}", + }, + ], + }, + ], + header: { + template: "green", + title: { + content: "🧑‍💻 感谢使用 Group Agent", + tag: "plain_text", + }, + }, +} + +const successMsgCard = { + elements: [ + { + tag: "markdown", + content: "${content}", + }, + { + tag: "hr", + }, + { + tag: "note", + elements: [ + { + tag: "plain_text", + content: "🍳 功能由${author}提供支持,Rid:${requestId}", + }, + ], + }, + ], + header: { + template: "green", + title: { + content: "🔥 感谢使用 ${app}", + tag: "plain_text", + }, + }, +} + +const errorMsgCard = { + elements: [ + { + tag: "markdown", + content: "${content}", + }, + { + tag: "hr", + }, + { + tag: "note", + elements: [ + { + tag: "plain_text", + content: "🍳 功能由${author}提供支持,Rid:${requestId}", + }, + ], + }, + ], + header: { + template: "red", + title: { + content: "⛔ ${app} 错误提示", + tag: "plain_text", + }, + }, +} + +const cardTemplateMap = { + functionSelector: functionSelectorCard, + timeScopeSelector: timeScopeSelectorCard, + resultReport: resultReportCard, + successMsg: successMsgCard, + errorMsg: errorMsgCard, +} + +export default cardTemplateMap diff --git a/routes/bot/groupAgent/index.ts b/routes/bot/groupAgent/index.ts index af9e15a..fa3a8a6 100644 --- a/routes/bot/groupAgent/index.ts +++ b/routes/bot/groupAgent/index.ts @@ -1,4 +1,5 @@ import { + genCardMsg, getActionOption, getActionValue, getChatId, @@ -8,6 +9,7 @@ import { import { Context } from "../../../types" import llm from "../../../utils/llm" +import cardTemplateMap from "./cards" enum CardTemplate { groupSelector = "ctp_AA0LgXOkJpIn", @@ -48,10 +50,10 @@ const sendGroupSelector = async ({ /** * 发送功能选择器 - * @param ctx - 上下文数据,包含body和larkService + * @param ctx - 上下文数据 */ const sendFunctionSelector = async (ctx: Context.Data) => { - const { larkService, logger, requestId } = ctx + const { logger, requestId } = ctx const body = ctx.body as LarkAction.Data const option = getActionOption(body) logger.debug(`Action option: ${JSON.stringify(option)}`) @@ -60,7 +62,14 @@ const sendFunctionSelector = async (ctx: Context.Data) => { logger.error( `Invalid targetChatId or targetChatName: ${JSON.stringify(option)}` ) - return + return genCardMsg( + cardTemplateMap.errorMsg, + { + content: "Invalid targetChatId or targetChatName", + requestId, + }, + false + ) } // 组织功能数据 const functions = [ @@ -68,25 +77,16 @@ const sendFunctionSelector = async (ctx: Context.Data) => { text: "总结消息", value: "summary-gpt-4o|总结消息", }, - // { - // text: "总结消息 (deepseek-chat)", - // value: "summary-deepseek-chat|总结消息 (deepseek-chat)", - // }, - // { - // text: "总结消息 (qwen2-72b-instruct-int4)", - // value: - // "summary-qwen2-72b-instruct-int4|总结消息 (qwen2-72b-instruct-int4)", - // }, ] - larkService.message.updateTemp( - getMessageId(body), - CardTemplate.functionSelector, + return genCardMsg( + cardTemplateMap.functionSelector, { functions, chatId, chatName, requestId, - } + }, + false ) } @@ -95,58 +95,54 @@ const sendFunctionSelector = async (ctx: Context.Data) => { * @param ctx - 上下文数据,包含body和larkService */ const sendTimeScopeSelector = async (ctx: Context.Data) => { - const { larkService, logger, requestId } = ctx + const { logger, requestId } = ctx const body = ctx.body as LarkAction.Data const option = getActionOption(body) logger.debug(`Action option: ${JSON.stringify(option)}`) const [functionId, functionName] = option.split("|") if (!functionId || !functionName) { logger.error(`Invalid functionId or functionName: ${option}`) - return + return genCardMsg( + cardTemplateMap.errorMsg, + { + content: "Invalid functionId or functionName", + requestId, + }, + false + ) } const value = getActionValue(body) logger.debug(`Action value: ${JSON.stringify(value)}`) const { chatId, chatName } = value if (!chatName || !chatId) { logger.error(`Invalid chatName or chatId: ${JSON.stringify(value)}`) - return + return genCardMsg( + cardTemplateMap.errorMsg, + { + content: "Invalid chatName or chatId", + requestId, + }, + false + ) } - larkService.message.updateTemp( - getMessageId(body), - CardTemplate.timeScopeSelector, + return genCardMsg( + cardTemplateMap.timeScopeSelector, { chatId, chatName, functionId, functionName, requestId, - } + }, + false ) } -/** - * 发送结果报告 - * @param ctx - 上下文数据,包含body和larkService - */ -const manageGroupMsg = async (ctx: Context.Data) => { +const sendGroupReport = async (ctx: Context.Data) => { const { larkService, logger, requestId } = ctx const body = ctx.body as LarkAction.Data - const value = getActionValue(body) - logger.debug(`Action value: ${JSON.stringify(value)}`) - const { chatId, chatName, functionId, functionName, timeScope } = value - if (!chatId || !chatName || !functionId || !functionName || !timeScope) { - logger.error(`Invalid value: ${JSON.stringify(value)}`) - return - } - // 发送一个loading的消息 - await larkService.message.updateTemp( - getMessageId(body), - CardTemplate.successMsg, - { - content: "Group Agent 正在爬楼中,请稍等...", - requestId, - } - ) + const { chatId, chatName, functionId, functionName, timeScope } = + getActionValue(body) // 记录发送loading消息后的时间戳 const startTime = Date.now() @@ -216,6 +212,40 @@ const manageGroupMsg = async (ctx: Context.Data) => { } } +/** + * 发送结果报告 + * @param ctx - 上下文数据,包含body和larkService + */ +const manageGroupMsg = async (ctx: Context.Data) => { + const { logger, requestId } = ctx + const body = ctx.body as LarkAction.Data + const value = getActionValue(body) + logger.debug(`Action value: ${JSON.stringify(value)}`) + const { chatId, chatName, functionId, functionName, timeScope } = value + if (!chatId || !chatName || !functionId || !functionName || !timeScope) { + logger.error(`Invalid value: ${JSON.stringify(value)}`) + return genCardMsg( + cardTemplateMap.errorMsg, + { + content: "Invalid value", + requestId, + }, + false + ) + } + // 处理群组消息 + sendGroupReport(ctx) + // 发送一个loading的消息 + return genCardMsg( + cardTemplateMap.successMsg, + { + content: "Group Agent 正在爬楼中,请稍等...", + requestId, + }, + false + ) +} + const groupAgent = { sendGroupSelector, sendFunctionSelector, diff --git a/routes/bot/index.ts b/routes/bot/index.ts index 9f10922..2339e89 100644 --- a/routes/bot/index.ts +++ b/routes/bot/index.ts @@ -26,7 +26,7 @@ export const manageBotReq = async (ctx: Context.Data): Promise => { // 处理Event消息 if (getIsEventMsg(body)) manageEventMsg(ctx) // 处理Action消息 - if (getIsActionMsg(body)) manageActionMsg(ctx) + if (getIsActionMsg(body)) return await manageActionMsg(ctx) // 返回成功响应 return ctx.genResp.custom("{}") diff --git a/routes/sheet/createKVTemp.ts b/routes/sheet/createKVTemp.ts index 5a3f107..e70f692 100644 --- a/routes/sheet/createKVTemp.ts +++ b/routes/sheet/createKVTemp.ts @@ -1,7 +1,7 @@ import { getChatId, getChatType, getUserId } from "@egg/lark-msg-tool" import { Context, LarkServer } from "../../types" -import { genSheetDbErrorMsg } from "../../utils/genMsg" +import { genSheetDbErrorMsg } from "../../utils/larkMsg" /** * 创建键值多维表格 @@ -101,7 +101,7 @@ const createFromEvent = async (ctx: Context.Data) => { ) } catch (e: any) { ctx.logger.error(`create KV bitable failed: ${e.message}`) - const errorMsg = genSheetDbErrorMsg(e.message) + const errorMsg = genSheetDbErrorMsg(e.message) as string ctx.larkService.message.send("chat_id", chatId, "interactive", errorMsg) } } diff --git a/services/lark/message.ts b/services/lark/message.ts index 810836b..d08affb 100644 --- a/services/lark/message.ts +++ b/services/lark/message.ts @@ -1,5 +1,5 @@ import { LarkServer } from "../../types/larkServer" -import { genTempMsg } from "../../utils/genMsg" +import { genTempMsg } from "../../utils/larkMsg" import LarkBaseService from "./base" class LarkMessageService extends LarkBaseService { diff --git a/utils/Card/common.ts b/utils/Card/common.ts new file mode 100644 index 0000000..5fcbb89 --- /dev/null +++ b/utils/Card/common.ts @@ -0,0 +1,61 @@ +export const commonNote = { + tag: "note", + elements: [ + { + tag: "plain_text", + content: "🍳 功能由${xAuthor}提供支持,Rid:${requestId}", + }, + ], +} + +export const successHeader = { + template: "green", + title: { + content: "🔥 感谢使用 ${xName}", + tag: "plain_text", + }, +} + +export const errorHeader = { + template: "red", + title: { + content: "⛔ ${xName} 错误提示", + tag: "plain_text", + }, +} + +export const pendingHeader = { + template: "purple", + title: { + content: "🔥 感谢使用 ${xName}", + tag: "plain_text", + }, +} + +export const baseSuccessCard = { + elements: [ + { + tag: "markdown", + content: "${content}", + }, + { + tag: "hr", + }, + commonNote, + ], + successHeader, +} + +export const baseErrorCard = { + elements: [ + { + tag: "markdown", + content: "${content}", + }, + { + tag: "hr", + }, + commonNote, + ], + errorHeader, +} diff --git a/utils/Card/groupAgentCard.ts b/utils/Card/groupAgentCard.ts new file mode 100644 index 0000000..3a65b12 --- /dev/null +++ b/utils/Card/groupAgentCard.ts @@ -0,0 +1,134 @@ +import { commonNote, pendingHeader, successHeader } from "./common" + +const functionSelector = { + elements: [ + { + tag: "markdown", + content: "已经选中的群聊:**${chatName}**\n", + }, + { + tag: "action", + actions: [ + { + tag: "select_static", + placeholder: { + tag: "plain_text", + content: "请选择功能", + }, + value: { + chatId: "${chatId}", + chatName: "${chatName}", + action: "sendTimeScopeSelector", + }, + options: "${functions}", + }, + ], + }, + { + tag: "hr", + }, + commonNote, + ], + pendingHeader, +} + +const timeScopeSelector = { + elements: [ + { + tag: "markdown", + content: + "已经选中群聊:**${chatName}**\n\n已经选择功能:**${functionName}**\n\n请选择时间范围", + }, + { + tag: "action", + actions: [ + { + tag: "button", + text: { + tag: "plain_text", + content: "过去一天", + }, + type: "default", + value: { + chatId: "${chatId}", + chatName: "${chatName}", + functionId: "${functionId}", + functionName: "${functionName}", + timeScope: "1", + requestId: "${requestId}", + action: "manageGroupMsg", + }, + }, + { + tag: "button", + text: { + tag: "plain_text", + content: "过去三天", + }, + type: "default", + value: { + chatId: "${chatId}", + chatName: "${chatName}", + functionId: "${functionId}", + functionName: "${functionName}", + timeScope: "3", + requestId: "${requestId}", + action: "manageGroupMsg", + }, + }, + { + tag: "button", + text: { + tag: "plain_text", + content: "过去七天", + }, + type: "default", + value: { + chatId: "${chatId}", + chatName: "${chatName}", + functionId: "${functionId}", + functionName: "${functionName}", + timeScope: "7", + requestId: "${requestId}", + action: "manageGroupMsg", + }, + }, + ], + }, + { + tag: "hr", + }, + commonNote, + ], + pendingHeader, +} + +const resultReport = { + elements: [ + { + tag: "markdown", + content: + "群聊:**${chatName}**,功能:**${functionName}**,总结天数:**${timeScope}**\n\n以下内容由AI模型生成,耗时:**${processingTime}s**", + }, + { + tag: "hr", + }, + { + tag: "markdown", + content: "${llmRes}", + }, + { + tag: "hr", + }, + commonNote, + ], + successHeader, +} + +const groupAgentCard = { + functionSelector, + timeScopeSelector, + resultReport, +} + +export default groupAgentCard diff --git a/utils/Card/index.ts b/utils/Card/index.ts new file mode 100644 index 0000000..659e8a0 --- /dev/null +++ b/utils/Card/index.ts @@ -0,0 +1,32 @@ +class LarkCard { + private requestId: string + private xName: string + private xAuthor: string + + private functionMap = { + egg: { + name: "小煎蛋", + author: "zhaoyingbo", + }, + groupAgent: { + name: "Group Agent", + author: "AI创新应用组", + }, + sheetDB: { + name: "小煎蛋 Sheet DB", + author: "zhaoyingbo", + }, + } + + constructor(func: keyof typeof this.functionMap, requestId: string) { + this.requestId = requestId + this.xName = this.functionMap[func].name + this.xAuthor = this.functionMap[func].author + } + + child(func: keyof typeof this.functionMap) { + return new LarkCard(func, this.requestId) + } +} + +export default LarkCard diff --git a/utils/genMsg.ts b/utils/genMsg.ts deleted file mode 100644 index 8695df3..0000000 --- a/utils/genMsg.ts +++ /dev/null @@ -1,96 +0,0 @@ -/** - * 生成错误消息的 JSON 字符串 - * @param {string} title - 消息标题 - * @param {string} content - 消息内容 - * @returns {string} JSON 字符串 - */ -const genErrorMsg = (title: string, content: string) => - JSON.stringify({ - elements: [ - { - tag: "markdown", - content, - }, - ], - header: { - title: { - content: title, - tag: "plain_text", - }, - template: "red", - }, - }) - -/** - * 生成成功消息的 JSON 字符串 - * @param {string} title - 消息标题 - * @param {string} content - 消息内容 - * @returns {string} JSON 字符串 - */ -const genSuccessMsg = (title: string, content: string) => - JSON.stringify({ - elements: [ - { - tag: "markdown", - content, - }, - ], - header: { - title: { - content: title, - tag: "plain_text", - }, - template: "green", - }, - }) - -/** - * 生成模板消息的 JSON 字符串 - * @param {string} id - 模板 ID - * @param {any} variable - 模板变量 - * @returns {string} JSON 字符串 - */ -export const genTempMsg = (id: string, variable: any) => - JSON.stringify({ - type: "template", - data: { - config: { - update_multi: true, - enable_forward: false, - }, - template_id: id, - template_variable: variable, - }, - }) - -/** - * 生成 Sheet DB 错误消息的 JSON 字符串 - * @param {string} content - 消息内容 - * @returns {string} JSON 字符串 - */ -export const genSheetDbErrorMsg = (content: string) => - genErrorMsg("🍪 小煎蛋 Sheet DB 错误提醒", content) - -/** - * 生成 Sheet DB 成功消息的 JSON 字符串 - * @param {string} content - 消息内容 - * @returns {string} JSON 字符串 - */ -export const genSheetDbSuccessMsg = (content: string) => - genSuccessMsg("🍪 感谢使用小煎蛋 Sheet DB", content) - -/** - * 生成 Group Agent 错误消息的 JSON 字符串 - * @param {string} content - 消息内容 - * @returns {string} JSON 字符串 - */ -export const genGroupAgentErrorMsg = (content: string) => - genErrorMsg("🧑‍💻 Group Agent 错误提醒", content) - -/** - * 生成 Group Agent 成功消息的 JSON 字符串 - * @param {string} content - 消息内容 - * @returns {string} JSON 字符串 - */ -export const genGroupAgentSuccessMsg = (content: string) => - genSuccessMsg("🧑‍💻 感谢使用 Group Agent", content) diff --git a/utils/larkMsg.ts b/utils/larkMsg.ts new file mode 100644 index 0000000..dbf4566 --- /dev/null +++ b/utils/larkMsg.ts @@ -0,0 +1,129 @@ +/** + * 生成错误消息的 JSON 字符串 + * @param {string} title - 消息标题 + * @param {string} content - 消息内容 + * @param {boolean} [stringify=true] - 是否返回 JSON 字符串 + * @returns {string | object} JSON 字符串或对象 + */ +const genErrorMsg = ( + title: string, + content: string, + stringify: boolean = true +) => { + const msg = { + elements: [ + { + tag: "markdown", + content, + }, + ], + header: { + title: { + content: title, + tag: "plain_text", + }, + template: "red", + }, + } + return stringify ? JSON.stringify(msg) : msg +} + +/** + * 生成成功消息的 JSON 字符串 + * @param {string} title - 消息标题 + * @param {string} content - 消息内容 + * @param {boolean} [stringify=true] - 是否返回 JSON 字符串 + * @returns {string | object} JSON 字符串或对象 + */ +const genSuccessMsg = ( + title: string, + content: string, + stringify: boolean = true +) => { + const msg = { + elements: [ + { + tag: "markdown", + content, + }, + ], + header: { + title: { + content: title, + tag: "plain_text", + }, + template: "green", + }, + } + return stringify ? JSON.stringify(msg) : msg +} + +/** + * 生成模板消息的 JSON 字符串 + * @param {string} id - 模板 ID + * @param {any} variable - 模板变量 + * @param {boolean} [stringify=true] - 是否返回 JSON 字符串 + * @returns {string | object} JSON 字符串或对象 + */ +export const genTempMsg = ( + id: string, + variable: any, + stringify: boolean = true +) => { + const msg = { + type: "template", + data: { + config: { + update_multi: true, + enable_forward: false, + }, + template_id: id, + template_variable: variable, + }, + } + return stringify ? JSON.stringify(msg) : msg +} + +/** + * 生成 Sheet DB 错误消息的 JSON 字符串 + * @param {string} content - 消息内容 + * @param {boolean} [stringify=true] - 是否返回 JSON 字符串 + * @returns {string | object} JSON 字符串或对象 + */ +export const genSheetDbErrorMsg = ( + content: string, + stringify: boolean = true +) => genErrorMsg("🍪 小煎蛋 Sheet DB 错误提醒", content, stringify) + +/** + * 生成 Sheet DB 成功消息的 JSON 字符串 + * @param {string} content - 消息内容 + * @param {boolean} [stringify=true] - 是否返回 JSON 字符串 + * @returns {string | object} JSON 字符串或对象 + */ +export const genSheetDbSuccessMsg = ( + content: string, + stringify: boolean = true +) => genSuccessMsg("🍪 感谢使用小煎蛋 Sheet DB", content, stringify) + +/** + * 生成 Group Agent 错误消息的 JSON 字符串 + * @param {string} content - 消息内容 + * @param {boolean} [stringify=true] - 是否返回 JSON 字符串 + * @returns {string | object} JSON 字符串或对象 + */ +export const genGroupAgentErrorMsg = ( + content: string, + stringify: boolean = true +) => genErrorMsg("🧑‍💻 Group Agent 错误提醒", content, stringify) + +/** + * 生成 Group Agent 成功消息的 JSON 字符串 + * @param {string} content - 消息内容 + * @param {boolean} [stringify=true] - 是否返回 JSON 字符串 + * @returns {string | object} JSON 字符串或对象 + */ +export const genGroupAgentSuccessMsg = ( + content: string, + stringify: boolean = true +) => genSuccessMsg("🧑‍💻 感谢使用 Group Agent", content, stringify)