egg_server/services/lark/message.ts
zhaoyingbo d64d6211c0
All checks were successful
Egg Server CI/CD / build-image (push) Successful in 50s
Egg Server CI/CD / refresh-image (push) Successful in 12s
Egg Server CI/CD / fast-deploy (push) Successful in 3s
feat(group-agent): 支持大模型语义化理解用户输入
2024-10-16 12:20:25 +00:00

102 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { LarkServer } from "../../types/larkServer"
import LarkBaseService from "./base"
class LarkMessageService extends LarkBaseService {
/**
* 发送卡片
* @param receiveIdType 消息接收者id类型 open_id/user_id/union_id/email/chat_id
* @param receiveId 消息接收者的IDID类型应与查询参数receiveIdType 对应
* @param msgType 消息类型 包括text、post、image、file、audio、media、sticker、interactive、share_chat、share_user
* @param content 消息内容JSON结构序列化后的字符串。不同msgType对应不同内容
*/
async send(
receiveIdType: LarkServer.ReceiveIDType,
receiveId: string,
msgType: LarkServer.MsgType,
content: string | Record<string, any>
) {
const path = `/im/v1/messages?receive_id_type=${receiveIdType}`
if (typeof content === "object") {
content = JSON.stringify(content)
}
if (msgType === "text" && !content.includes('"text"')) {
content = JSON.stringify({ text: content })
}
return this.post<LarkServer.BaseRes<{ message_id: string }>>(path, {
receive_id: receiveId,
msg_type: msgType,
content,
})
}
/**
* 发送卡片信息
* @param receiveId 消息接收者的IDID类型应与查询参数receiveIdType 对应
* @param content 消息内容
*/
async sendCard2Chat(
receiveId: string,
content: string | Record<string, any>
) {
return this.send("chat_id", receiveId, "interactive", content)
}
/**
* 发送文本信息
* @param receiveId 消息接收者的IDID类型应与查询参数receiveIdType 对应
* @param content 消息内容
*/
async sendText2Chat(receiveId: string, content: string) {
return this.send("chat_id", receiveId, "text", content)
}
/**
* 更新卡片
* @param messageId 消息id
* @param content 消息内容JSON结构序列化后的字符串。不同msgType对应不同内容
*/
async update(messageId: string, content: string | Record<string, any>) {
const path = `/im/v1/messages/${messageId}`
if (typeof content === "object") {
content = JSON.stringify(content)
}
return this.patch<LarkServer.BaseRes>(path, { content })
}
/**
* 获取消息历史记录
* @param chatId 会话ID
* @param startTime 开始时间 秒级时间戳
* @param endTime 结束时间 秒级时间戳
*/
async getHistory(chatId: string, startTime: string, endTime: string) {
const path = `/im/v1/messages`
const messageList = [] as LarkServer.MessageData[]
let hasMore = true
let pageToken = ""
while (hasMore) {
const { code, data } = await this.get<
LarkServer.BaseListRes<LarkServer.MessageData>
>(path, {
container_id_type: "chat",
container_id: chatId,
start_time: startTime,
end_time: endTime,
page_size: 50,
page_token: pageToken,
})
if (code !== 0) break
messageList.push(...data.items)
hasMore = data.has_more
pageToken = data.page_token
}
return {
code: 0,
data: messageList,
message: "ok",
}
}
}
export default LarkMessageService