This commit is contained in:
parent
5f859131aa
commit
850cf6af51
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -2,6 +2,9 @@
|
|||||||
"cSpell.words": [
|
"cSpell.words": [
|
||||||
"commitlint",
|
"commitlint",
|
||||||
"tseslint",
|
"tseslint",
|
||||||
|
"Xauthor",
|
||||||
|
"Xicon",
|
||||||
|
"Xname",
|
||||||
"yingbo",
|
"yingbo",
|
||||||
"zhaoyingbo"
|
"zhaoyingbo"
|
||||||
]
|
]
|
||||||
|
@ -22,7 +22,6 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@egg/logger": "^1.4.3",
|
"@egg/logger": "^1.4.3",
|
||||||
"lodash": "*",
|
|
||||||
"winston": "*"
|
"winston": "*"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { LarkEvent, LarkAction } from "../types"
|
import { LarkAction, LarkEvent } from "../types"
|
||||||
|
|
||||||
class LarkBody {
|
class LarkBody {
|
||||||
protected body: LarkEvent.Data | LarkAction.Data
|
protected body: LarkEvent.Data | LarkAction.Data
|
||||||
@ -87,7 +87,9 @@ class LarkBody {
|
|||||||
*/
|
*/
|
||||||
private getMsgText(body: LarkEvent.Data): string {
|
private getMsgText(body: LarkEvent.Data): string {
|
||||||
try {
|
try {
|
||||||
const { text }: { text: string } = JSON.parse(body?.event?.message?.content)
|
const { text }: { text: string } = JSON.parse(
|
||||||
|
body?.event?.message?.content
|
||||||
|
)
|
||||||
// 去掉@_user_1相关的内容,例如 '@_user_1 测试' -> '测试'
|
// 去掉@_user_1相关的内容,例如 '@_user_1 测试' -> '测试'
|
||||||
const textWithoutAt = text.replace(/@_user_\d+/g, "")
|
const textWithoutAt = text.replace(/@_user_\d+/g, "")
|
||||||
return textWithoutAt
|
return textWithoutAt
|
||||||
@ -164,4 +166,4 @@ class LarkBody {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default LarkBody
|
export default LarkBody
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
import logger from "@egg/logger"
|
import logger from "@egg/logger"
|
||||||
import _ from "lodash"
|
import _ from "lodash"
|
||||||
import { Logger } from "winston"
|
import { Logger } from "winston"
|
||||||
import { baseErrorCard, basePendingCard, baseSuccessCard } from "./component";
|
|
||||||
|
|
||||||
type FunctionMap = Record<string, { Xname: string; Xauthor: string, Xicon: string }>
|
import { baseErrorCard, basePendingCard, baseSuccessCard } from "./component"
|
||||||
|
|
||||||
|
type FunctionMap = Record<
|
||||||
|
string,
|
||||||
|
{ Xname: string; Xauthor: string; Xicon: string }
|
||||||
|
>
|
||||||
|
|
||||||
type CardMap = Record<string, any>
|
type CardMap = Record<string, any>
|
||||||
|
|
||||||
@ -33,7 +37,7 @@ class LarkCard {
|
|||||||
requestId: string,
|
requestId: string,
|
||||||
cardMap: CardMap,
|
cardMap: CardMap,
|
||||||
tempMap: TempMap,
|
tempMap: TempMap,
|
||||||
functionMap: FunctionMap,
|
functionMap: FunctionMap
|
||||||
) {
|
) {
|
||||||
this.stringify = stringify
|
this.stringify = stringify
|
||||||
this.requestId = requestId
|
this.requestId = requestId
|
||||||
@ -72,7 +76,14 @@ class LarkCard {
|
|||||||
* @returns 子卡片实例
|
* @returns 子卡片实例
|
||||||
*/
|
*/
|
||||||
child(func: string, stringify: boolean = true) {
|
child(func: string, stringify: boolean = true) {
|
||||||
return new LarkCard(func, stringify, this.requestId, this.cardMap, this.tempMap, this.functionMap)
|
return new LarkCard(
|
||||||
|
func,
|
||||||
|
stringify,
|
||||||
|
this.requestId,
|
||||||
|
this.cardMap,
|
||||||
|
this.tempMap,
|
||||||
|
this.functionMap
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -83,19 +94,21 @@ class LarkCard {
|
|||||||
*/
|
*/
|
||||||
genCard(
|
genCard(
|
||||||
cardKey: keyof typeof this.cardMap,
|
cardKey: keyof typeof this.cardMap,
|
||||||
variables: { [key: string]: any },
|
variables: { [key: string]: any }
|
||||||
) {
|
) {
|
||||||
const card = this.cardMap[cardKey]
|
const card = this.cardMap[cardKey]
|
||||||
if (!card) {
|
if (!card) {
|
||||||
this.logger.error(`Card ${cardKey} not found`)
|
this.logger.error(`Card ${cardKey} not found`)
|
||||||
throw new Error(`Card ${cardKey} not found`);
|
throw new Error(`Card ${cardKey} not found`)
|
||||||
}
|
}
|
||||||
const finalVariables: Record<string, any> = {
|
const finalVariables: Record<string, any> = {
|
||||||
...variables,
|
...variables,
|
||||||
requestId: this.requestId,
|
requestId: this.requestId,
|
||||||
...this.functionMap[this.funcName],
|
...this.functionMap[this.funcName],
|
||||||
}
|
}
|
||||||
this.logger.debug(`Card ${cardKey} final variables: ${JSON.stringify(finalVariables)}`)
|
this.logger.debug(
|
||||||
|
`Card ${cardKey} final variables: ${JSON.stringify(finalVariables)}`
|
||||||
|
)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 替换字符串中的变量
|
* 替换字符串中的变量
|
||||||
@ -133,7 +146,9 @@ class LarkCard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const content = traverseAndReplace(card)
|
const content = traverseAndReplace(card)
|
||||||
this.logger.debug(`Card ${cardKey} final content: ${JSON.stringify(content)}`)
|
this.logger.debug(
|
||||||
|
`Card ${cardKey} final content: ${JSON.stringify(content)}`
|
||||||
|
)
|
||||||
return this.stringify ? JSON.stringify(content) : content
|
return this.stringify ? JSON.stringify(content) : content
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,14 +189,16 @@ class LarkCard {
|
|||||||
const tempId = this.tempMap[tempKey]
|
const tempId = this.tempMap[tempKey]
|
||||||
if (!tempId) {
|
if (!tempId) {
|
||||||
this.logger.error(`Temp ${tempKey} not found`)
|
this.logger.error(`Temp ${tempKey} not found`)
|
||||||
throw new Error(`Temp ${tempKey} not found`);
|
throw new Error(`Temp ${tempKey} not found`)
|
||||||
}
|
}
|
||||||
const finalVariables: Record<string, any> = {
|
const finalVariables: Record<string, any> = {
|
||||||
...variables,
|
...variables,
|
||||||
requestId: this.requestId,
|
requestId: this.requestId,
|
||||||
...this.functionMap[this.funcName],
|
...this.functionMap[this.funcName],
|
||||||
}
|
}
|
||||||
this.logger.debug(`Temp ${tempKey} final variables: ${JSON.stringify(finalVariables)}`)
|
this.logger.debug(
|
||||||
|
`Temp ${tempKey} final variables: ${JSON.stringify(finalVariables)}`
|
||||||
|
)
|
||||||
const content = {
|
const content = {
|
||||||
type: "template",
|
type: "template",
|
||||||
data: {
|
data: {
|
||||||
@ -193,9 +210,11 @@ class LarkCard {
|
|||||||
template_variable: finalVariables,
|
template_variable: finalVariables,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
this.logger.debug(`Temp ${tempKey} final content: ${JSON.stringify(content)}`)
|
this.logger.debug(
|
||||||
|
`Temp ${tempKey} final content: ${JSON.stringify(content)}`
|
||||||
|
)
|
||||||
return this.stringify ? JSON.stringify(content) : content
|
return this.stringify ? JSON.stringify(content) : content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default LarkCard
|
export default LarkCard
|
||||||
|
@ -1,12 +1,7 @@
|
|||||||
import _ from "lodash"
|
|
||||||
|
|
||||||
import { LarkAction, LarkEvent } from "./types"
|
|
||||||
|
|
||||||
import * as CardComponent from "./Card/component"
|
|
||||||
|
|
||||||
import LarkCard from "./Card"
|
|
||||||
|
|
||||||
import LarkBody from "./Body"
|
import LarkBody from "./Body"
|
||||||
|
import LarkCard from "./Card"
|
||||||
|
import * as CardComponent from "./Card/component"
|
||||||
|
import { LarkAction, LarkEvent } from "./types"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成消息卡片的Options
|
* 生成消息卡片的Options
|
||||||
@ -21,4 +16,4 @@ export const genCardOptions = (options: Record<string, string>) => {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
export { LarkAction, LarkEvent, CardComponent, LarkCard, LarkBody }
|
export { CardComponent, LarkAction, LarkBody, LarkCard, LarkEvent }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user