48 lines
1.5 KiB
TypeScript
48 lines
1.5 KiB
TypeScript
import db from "../db";
|
||
|
||
/**
|
||
* 发送卡片
|
||
* @param {ReceiveIDType} receive_id_type 消息接收者id类型 open_id/user_id/union_id/email/chat_id
|
||
* @param {string} receive_id 消息接收者的ID,ID类型应与查询参数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对应不同内容
|
||
* @returns {string} 消息id
|
||
*/
|
||
export const sendMsg = async (
|
||
receive_id_type: ReceiveIDType,
|
||
receive_id: string,
|
||
msg_type: MsgType,
|
||
content: string
|
||
) => {
|
||
const URL = `https://open.f.mioffice.cn/open-apis/im/v1/messages?receive_id_type=${receive_id_type}`;
|
||
const tenant_access_token = await db.tenantAccessToken.get();
|
||
const header = {
|
||
"Content-Type": "application/json",
|
||
Authorization: `Bearer ${tenant_access_token}`,
|
||
};
|
||
const body = { receive_id, msg_type, content };
|
||
const res = await fetch(URL, {
|
||
method: "POST",
|
||
headers: header,
|
||
body: JSON.stringify(body),
|
||
});
|
||
const data = (await res.json()) as any;
|
||
if (data.code !== 0) {
|
||
console.log("sendMsg error", data);
|
||
return {
|
||
code: data.code,
|
||
msg: data.msg,
|
||
};
|
||
}
|
||
console.log("sendMsg success", data);
|
||
return {
|
||
code: 0,
|
||
msg: "success",
|
||
data: {
|
||
message_id: data.data.message_id,
|
||
},
|
||
};
|
||
};
|
||
|
||
// sendMsg('user_id', 'liuke9', 'text', JSON.stringify({text: '这是测试消息,不要回复'}))
|