feat(lark-msg): 修改注入变量的位置
All checks were successful
/ release (push) Successful in 29s

This commit is contained in:
zhaoyingbo 2024-10-16 12:14:05 +00:00
parent 47c23a7ad6
commit 0e0c0512ce
2 changed files with 72 additions and 9 deletions

View File

@ -100,23 +100,35 @@ class LarkCard<
} else if (_.isArray(obj)) {
return obj.map(traverseAndReplace)
} else if (_.isObject(obj)) {
// 给 value 字段注入公共变量
if ((obj as any).value) {
;(obj as any).value = {
...variables,
...((obj as any).value || {}),
}
}
return _.mapValues(obj, traverseAndReplace)
}
return obj
}
const content = traverseAndReplace(card)
// 递归遍历content遇到key是"value"的Object将variables注入
const injectVariables = (obj: { value: any }): any => {
if (_.isObject(obj) && !_.isArray(obj)) {
if (_.has(obj, "value") && _.isObject(obj.value)) {
return {
...obj,
value: { ...obj.value, ...finalVariables },
}
}
return _.mapValues(obj, injectVariables)
} else if (_.isArray(obj)) {
return obj.map(injectVariables)
}
return obj
}
const finalContent = injectVariables(content)
this.logger.debug(
`Card ${String(cardKey)} final content: ${JSON.stringify(content)}`
`Card ${String(cardKey)} final content: ${JSON.stringify(finalContent)}`
)
return this.stringify ? JSON.stringify(content) : content
return this.stringify ? JSON.stringify(finalContent) : finalContent
}
genSuccessCard(content: any) {

51
test/inject.ts Normal file
View File

@ -0,0 +1,51 @@
import { has, isArray, isObject, mapValues } from "lodash"
const variables = {
content: "分析中,请稍等...",
requestId: "1988fcc0-6ca8-4015-b256-b19945c33117",
xName: "Group Agent",
xAuthor: "AI创新应用组",
xIcon: "🔥",
}
const content = {
config: { update_multi: true },
elements: [
{ tag: "markdown", content: "分析中,请稍等..." },
{ tag: "hr" },
{
tag: "note",
elements: [
{
tag: "plain_text",
content:
"💡 功能由AI创新应用组提供支持Rid1988fcc0-6ca8-4015-b256-b19945c33117",
},
],
},
],
header: {
template: "purple",
title: { content: "🔥 感谢使用 Group Agent", tag: "plain_text" },
},
}
const injectVariables = (obj: any): any => {
type Obj = { value: any }
console.log("🚀 ~ obj:", obj)
if (isObject(obj) && !isArray((obj as Obj))) {
if (has(obj, "value") && isObject(obj.value)) {
return {
...obj,
value: { ...(obj as Obj).value, ...variables },
}
}
return mapValues(obj, injectVariables)
} else if (isArray(obj)) {
return obj.map(injectVariables)
}
return obj
}
const finalContent = injectVariables(content)
console.log("🚀 ~ finalContent:", finalContent)