feat(lark-msg-tool): 接入飞书消息工具包 #28
Some checks failed
Egg Server MIflow / build-image (push) Failing after 27s
Some checks failed
Egg Server MIflow / build-image (push) Failing after 27s
This commit is contained in:
parent
73be9b8b35
commit
a11a23593f
@ -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",
|
||||
|
@ -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卡片
|
||||
|
@ -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: {
|
||||
|
@ -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
|
||||
}
|
@ -15,7 +15,7 @@ export const managePbError = async <T>(
|
||||
): Promise<T | null> => {
|
||||
try {
|
||||
return await dbFunc()
|
||||
} catch (err: any) {
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user