feat(lark-msg): 优化event和action的判断方式
All checks were successful
/ release (push) Successful in 53s

This commit is contained in:
zhaoyingbo 2024-10-29 06:58:26 +00:00
parent ca750a3bf8
commit 2bfa241511

View File

@ -2,8 +2,10 @@ import { LarkAction, LarkEvent } from "../types"
class LarkBody {
protected body: LarkEvent.Data | LarkAction.Data
public isEventMsg: boolean = false
public isActionMsg: boolean = false
public isEvent: boolean = false
public isAction: boolean = false
public eventType?: LarkEvent.Data["header"]["event_type"]
public isMessageEvent?: boolean
public msgType?: LarkEvent.Message["message_type"]
public userId?: LarkEvent.UserIdInfo["user_id"]
public msgText: string = ""
@ -26,11 +28,13 @@ class LarkBody {
*/
constructor(body: LarkEvent.Data | LarkAction.Data) {
this.body = body
this.isEventMsg = this.getIsEventMsg(body)
this.isActionMsg = this.getIsActionMsg(body)
this.isEvent = this.getIsEvent(body)
this.isAction = this.getIsAction(body)
if (this.isEventMsg) {
if (this.isEvent) {
const eventBody = body as LarkEvent.Data
this.eventType = this.getEventType(eventBody)
this.isMessageEvent = this.getIsMessageEvent(eventBody)
this.msgType = this.getMsgType(eventBody)
this.userId = this.getUserId(eventBody)
this.msgText = this.getMsgText(eventBody)
@ -39,7 +43,7 @@ class LarkBody {
this.mentions = this.getMentions(eventBody)
}
if (this.isActionMsg) {
if (this.isAction) {
const actionBody = body as LarkAction.Data
this.actionType = this.getActionType(actionBody)
this.actionValue = this.getActionValue(actionBody)
@ -50,13 +54,31 @@ class LarkBody {
this.messageId = this.getMessageId(body)
}
/**
*
* @param body
* @returns
*/
private getEventType(body: LarkEvent.Data) {
return body?.header?.event_type
}
/**
*
* @param body
* @returns
*/
private getIsMessageEvent(body: LarkEvent.Data) {
return body?.header?.event_type === "im.message.receive_v1"
}
/**
*
* @param body
* @returns
*/
private getIsEventMsg(body: any): body is LarkEvent.Data {
return body?.header?.event_type === "im.message.receive_v1"
private getIsEvent(body: any): body is LarkEvent.Data {
return !!body?.event
}
/**
@ -64,7 +86,7 @@ class LarkBody {
* @param body Action消息体
* @returns Action消息
*/
private getIsActionMsg(body: any): body is LarkAction.Data {
private getIsAction(body: any): body is LarkAction.Data {
return !!body?.action
}
@ -171,8 +193,8 @@ class LarkBody {
* @returns Id
*/
private getChatId(body: LarkEvent.Data | LarkAction.Data) {
if (this.getIsEventMsg(body)) return body?.event?.message?.chat_id
if (this.getIsActionMsg(body)) return body?.open_chat_id
if (this.getIsEvent(body)) return body?.event?.message?.chat_id
if (this.getIsAction(body)) return body?.open_chat_id
return ""
}
@ -182,8 +204,8 @@ class LarkBody {
* @returns Id
*/
private getMessageId(body: LarkEvent.Data | LarkAction.Data) {
if (this.getIsEventMsg(body)) return body?.event?.message?.message_id
if (this.getIsActionMsg(body)) return body?.open_message_id
if (this.getIsEvent(body)) return body?.event?.message?.message_id
if (this.getIsAction(body)) return body?.open_message_id
return ""
}
}