feat(lark-msg-tool): 接入飞书消息工具包 #28
Some checks failed
Egg Server MIflow / build-image (push) Failing after 27s

This commit is contained in:
zhaoyingbo 2024-08-21 01:21:18 +00:00
parent 73be9b8b35
commit a11a23593f
6 changed files with 9 additions and 94 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -34,6 +34,7 @@
},
"dependencies": {
"@egg/hooks": "^1.1.0",
"@egg/lark-msg-tool": "^1.1.0",
"@egg/logger": "^1.2.1",
"@egg/net-tool": "^1.2.1",
"@egg/path-tool": "^1.2.1",

View File

@ -1,7 +1,7 @@
import { getActionType, getIsActionMsg } from "@egg/lark-msg-tool"
import { sleep } from "bun"
import { Context, LarkAction } from "../../types"
import { getActionType, getIsActionMsg } from "../../utils/msgTools"
/**
* ChatId卡片

View File

@ -1,5 +1,3 @@
import { LarkService } from "../../services"
import { Context, LarkEvent } from "../../types"
import {
getChatId,
getChatType,
@ -7,7 +5,10 @@ import {
getMentions,
getMsgText,
getMsgType,
} from "../../utils/msgTools"
} from "@egg/lark-msg-tool"
import { LarkService } from "../../services"
import { Context, LarkEvent } from "../../types"
/**
* P2P或者群聊并且艾特了小煎蛋
@ -105,6 +106,7 @@ const manageCMDMsg = ({
const text = getMsgText(body)
logger.debug(`bot req text: ${text}`)
const chatId = getChatId(body)
if (!chatId) return false
// 处理命令消息
if (text.trim() === "/id") {
logger.info(`bot command is /id, chatId: ${chatId}`)
@ -140,6 +142,7 @@ const manageCMDMsg = ({
const replyGuideMsg = ({ body, larkService, logger }: Context.Data): void => {
const chatId = getChatId(body)
logger.info(`reply guide message, chatId: ${chatId}`)
if (!chatId) return
const content = JSON.stringify({
type: "template",
data: {

View File

@ -1,89 +0,0 @@
import { LarkAction, LarkEvent } from "../types"
/**
*
* @param {LarkEvent.Data} body
*/
export const getIsEventMsg = (body: LarkEvent.Data) => {
return body?.header?.event_type === "im.message.receive_v1"
}
/**
*
* @param {LarkEvent.Data} body
* @returns
*/
export const getMsgType = (body: LarkEvent.Data) => {
return body?.event?.message?.message_type
}
/**
* Id
* @param {LarkEvent.Data} body
* @returns
*/
export const getChatId = (body: LarkEvent.Data) => {
return body?.event?.message?.chat_id
}
/**
* Id
* @param {LarkEvent.Data} body
* @returns
*/
export const getUserId = (body: LarkEvent.Data) => {
return body?.event?.sender?.sender_id?.user_id
}
/**
* Action消息
* @param {LarkAction.Data} body
*/
export const getIsActionMsg = (body: LarkAction.Data) => {
return body?.action
}
/**
* Action类型
* @param {LarkAction.Data} body
* @returns {string} Action类型
*/
export const getActionType = (body: LarkAction.Data) => {
return body?.action?.tag
}
/**
*
* @param {LarkEvent.Data} body
* @returns {string}
*/
export const getMsgText = (body: LarkEvent.Data) => {
try {
const { text }: { text: string } = JSON.parse(body?.event?.message?.content)
// 去掉@_user_1相关的内容例如 '@_user_1 测试' -> '测试'
const textWithoutAt = text.replace(/@_user_\d+/g, "")
// // 去除空格和换行
// const textWithoutSpace = textWithoutAt.replace(/[\s\n]/g, "");
return textWithoutAt
} catch (e) {
return ""
}
}
/**
*
* @param {LarkEvent.Data} body
* @returns {string}
*/
export const getChatType = (body: LarkEvent.Data) => {
return body?.event?.message?.chat_type
}
/**
*
* @param {LarkEvent.Data} body
* @returns {Array}
*/
export const getMentions = (body: LarkEvent.Data) => {
return body?.event?.message?.mentions
}

View File

@ -15,7 +15,7 @@ export const managePbError = async <T>(
): Promise<T | null> => {
try {
return await dbFunc()
} catch (err: any) {
} catch {
return null
}
}