This commit is contained in:
parent
ca750a3bf8
commit
2bfa241511
@ -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 ""
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user