feat: 封装发送卡片请求
All checks were successful
Egg CI/CD / build-image (push) Successful in 2m5s
Egg CI/CD / deploy (push) Successful in 21s

This commit is contained in:
zhaoyingbo 2024-03-09 08:25:05 +00:00
parent 3c36f59a1d
commit b5522acd20
3 changed files with 57 additions and 70 deletions

View File

@ -8,6 +8,9 @@ const makeChatIdCard = async (body: LarkUserAction) => {
return JSON.stringify({
type: "template",
data: {
config: {
update_multi: true,
},
template_id: "ctp_AAi3NnHb6zgK",
template_variable: {
chat_id: body.open_chat_id,

8
typings.d.ts vendored
View File

@ -442,4 +442,10 @@ interface LarkUserAction {
type ReceiveIDType = "open_id" | "user_id" | "union_id" | "email" | "chat_id";
type MsgType = "text" | "post" | "image" | "file" | "audio" | "media" | "sticker" | "interactive" | "share_chat" | "share_user";
type MsgType = "text" | "post" | "image" | "file" | "audio" | "media" | "sticker" | "interactive" | "share_chat" | "share_user";
interface ServerResponse {
code: number;
data: any;
msg: string;
}

View File

@ -1,5 +1,38 @@
import db from "../db";
/**
*
* @param func fetch
* @returns
*/
const manageFetch = async (func: Function) => {
try {
const res = await func();
const data = (await res.json()) as ServerResponse;
console.log("🚀 ~ manageFetch ~ data:", data);
return data;
} catch (error) {
console.log("🚀 ~ manageFetch ~ error:", error);
return {
code: 1,
data: null,
msg: "sendMsg fetch error",
};
}
};
/**
* header
* @returns header
*/
const getHeaders = async () => {
const tenant_access_token = await db.tenantAccessToken.get();
return {
"Content-Type": "application/json",
Authorization: `Bearer ${tenant_access_token}`,
};
};
/**
*
* @param {ReceiveIDType} receive_id_type id类型 open_id/user_id/union_id/email/chat_id
@ -15,41 +48,14 @@ export const sendMsg = async (
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 };
try {
const res = await fetch(URL, {
const headers = await getHeaders();
return await manageFetch(() =>
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,
},
};
} catch (error) {
console.log("sendMsg error", error);
return {
code: 1,
msg: "sendMsg fetch error",
};
}
headers,
body: JSON.stringify({ receive_id, msg_type, content }),
})
);
};
/**
@ -59,40 +65,12 @@ export const sendMsg = async (
*/
export const updateCard = async (message_id: string, content: string) => {
const URL = `https://open.f.mioffice.cn/open-apis/im/v1/messages/${message_id}`;
const tenant_access_token = await db.tenantAccessToken.get();
const header = {
"Content-Type": "application/json; charset=utf-8",
Authorization: `Bearer ${tenant_access_token}`,
};
const body = { content };
try {
const res = await fetch(URL, {
const headers = await getHeaders();
return await manageFetch(() =>
fetch(URL, {
method: "PATCH",
headers: header,
body: JSON.stringify(body),
});
const data = (await res.json()) as any;
if (data.code !== 0) {
console.log("updateCard error", data);
return {
code: data.code,
msg: data.msg,
};
}
console.log("updateCard success", data);
return {
code: 0,
msg: "success",
data: {
message_id: data.data.message_id,
},
};
} catch (error) {
console.log("updateCard error", error);
return {
code: 1,
msg: "updateCard fetch error",
};
}
headers,
body: JSON.stringify({ content }),
})
);
};