feat(lark-msg): 支持生成卡片信息
Some checks failed
/ release (push) Failing after 19s

This commit is contained in:
zhaoyingbo 2024-09-30 09:27:46 +00:00
parent 19cfb2c9c3
commit 566188f405
4 changed files with 67 additions and 6 deletions

4
package-lock.json generated
View File

@ -12,7 +12,7 @@
"packages/*"
],
"dependencies": {
"lodash": "4.17.21",
"lodash": "^4.17.21",
"winston": "3.14.2",
"winston-daily-rotate-file": "5.0.0"
},
@ -21,7 +21,7 @@
"@commitlint/config-conventional": "19.2.2",
"@eslint/js": "9.9.0",
"@lerna/conventional-commits": "6.4.1",
"@types/lodash": "4.17.7",
"@types/lodash": "^4.17.7",
"@types/node": "22.4.0",
"eslint": "9.9.0",
"eslint-plugin-simple-import-sort": "12.1.1",

View File

@ -26,7 +26,7 @@
"@commitlint/config-conventional": "19.2.2",
"@eslint/js": "9.9.0",
"@lerna/conventional-commits": "6.4.1",
"@types/lodash": "4.17.7",
"@types/lodash": "^4.17.7",
"@types/node": "22.4.0",
"eslint": "9.9.0",
"eslint-plugin-simple-import-sort": "12.1.1",
@ -39,8 +39,8 @@
"typescript-eslint": "8.1.0"
},
"dependencies": {
"lodash": "4.17.21",
"lodash": "^4.17.21",
"winston": "3.14.2",
"winston-daily-rotate-file": "5.0.0"
}
}
}

View File

@ -16,5 +16,11 @@
"msg"
],
"author": "RainSun <zhaoyingbo@live.cn>",
"license": "ISC"
"license": "ISC",
"devDependencies": {
"@types/lodash": "*"
},
"dependencies": {
"lodash": "*"
}
}

View File

@ -1,3 +1,5 @@
import _ from "lodash"
import { LarkAction, LarkEvent } from "./types"
/**
@ -119,4 +121,57 @@ export const getMessageId = (body: LarkEvent.Data | LarkAction.Data) => {
return ""
}
/**
*
* @param {any} card -
* @param {object} variables -
* @param {boolean} [stringify=true] - JSON
*/
export const genCardMsg = (
card: any,
variables: { [key: string]: any },
stringify: boolean = true
): any => {
const replaceVariables = (str: string): string => {
const variablePattern = /\$\{(\w+)\}/g
const matches = str.match(variablePattern)
if (matches && matches.length === 1 && matches[0] === str) {
// 如果字符串中只有一个变量,且整个字符串就是这个变量
const variableName = matches[0].slice(2, -1) // 去掉 ${ 和 }
return variables[variableName] || ""
} else {
// 否则只替换字符串中的变量部分
return str.replace(variablePattern, (_, v) => variables[v] || "")
}
}
const traverseAndReplace = (obj: any): any => {
if (_.isString(obj)) {
return replaceVariables(obj)
} else if (_.isArray(obj)) {
return obj.map(traverseAndReplace)
} else if (_.isObject(obj)) {
return _.mapValues(obj, traverseAndReplace)
}
return obj
}
const updatedCard = traverseAndReplace(card)
return stringify ? JSON.stringify(updatedCard) : updatedCard
}
/**
* Options
*/
export const genCardOptions = (options: Record<string, string>) => {
return Object.entries(options).map(([text, value]) => ({
text: {
tag: "plain_text",
content: text,
},
value,
}))
}
export { LarkAction, LarkEvent }