egg_server/services/lark/message.ts
zhaoyingbo 09e352a9c1
All checks were successful
Egg Server MIflow / build-image (push) Successful in 1m5s
feat: 抽象网络请求类 & 内容转为ctx向内传递
2024-08-16 09:12:11 +00:00

41 lines
1.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 LarkBaseService from "./base"
class LarkMessageService extends LarkBaseService {
/**
* 发送卡片
* @param {LarkServer.ReceiveIDType} receive_id_type 消息接收者id类型 open_id/user_id/union_id/email/chat_id
* @param {string} receive_id 消息接收者的IDID类型应与查询参数receive_id_type 对应
* @param {MsgType} msg_type 消息类型 包括text、post、image、file、audio、media、sticker、interactive、share_chat、share_user
* @param {string} content 消息内容JSON结构序列化后的字符串。不同msg_type对应不同内容
*/
async send(
receive_id_type: LarkServer.ReceiveIDType,
receive_id: string,
msg_type: LarkServer.MsgType,
content: string
) {
const path = `/im/v1/messages?receive_id_type=${receive_id_type}`
if (msg_type === "text" && !content.includes('"text"')) {
content = JSON.stringify({ text: content })
}
return this.post<LarkServer.BaseRes>(path, {
receive_id,
msg_type,
content,
})
}
/**
* 更新卡片
* @param {string} message_id 消息id
* @param {string} content 消息内容JSON结构序列化后的字符串。不同msg_type对应不同内容
*/
async update(message_id: string, content: string) {
const path = `/im/v1/messages/${message_id}`
return this.patch<LarkServer.BaseRes>(path, { content })
}
}
export default LarkMessageService