egg_server/utils/sendMsg.ts
zhaoyingbo cabc23ae77
Some checks are pending
Egg CI/CD / build-image (push) Waiting to run
Egg CI/CD / deploy (push) Blocked by required conditions
feat: 支持根据用户组转发消息
2024-03-04 12:01:14 +00:00

48 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 db from "../db";
/**
* 发送卡片
* @param {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对应不同内容
* @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: '这是测试消息,不要回复'}))