egg_server/services/lark/message.ts
zhaoyingbo b992ee0b21
Some checks failed
Egg Server MIflow / build-image (push) Failing after 5m7s
feat(group-agent): 新增支持群组问答
2024-09-25 09:14:10 +00:00

115 lines
3.5 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 { genTempMsg } from "../../utils/genMsg"
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
) {
const path = `/im/v1/messages?receive_id_type=${receiveIdType}`
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 receiveIdType 消息接收者id类型 open_id/user_id/union_id/email/chat_id
* @param receiveId 消息接收者的IDID类型应与查询参数receiveIdType 对应
* @param templateId 模板ID
* @param variable 模板变量
*/
async sendTemp(
receiveIdType: LarkServer.ReceiveIDType,
receiveId: string,
templateId: string,
variable: any
) {
return this.send(
receiveIdType,
receiveId,
"interactive",
genTempMsg(templateId, variable)
)
}
/**
* 发送富文本信息
* @param receiveId 消息接收者的IDID类型应与查询参数receiveIdType 对应
* @param content 消息内容
*/
async sendInteractive2Chat(receiveId: string, content: string) {
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) {
const path = `/im/v1/messages/${messageId}`
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