mirror of
https://github.com/zhigang1992/telert.git
synced 2026-01-12 08:24:41 +08:00
feat: add ability to upload file
This commit is contained in:
50
src/index.ts
50
src/index.ts
@@ -44,6 +44,20 @@ router.post("/t/:webhookId/raw", async (context) => {
|
||||
context.res.body = { ok: true };
|
||||
});
|
||||
|
||||
router.post("/t/:webhookId/file", async (context) => {
|
||||
const chat = await TG_GROUPS.get(
|
||||
`webhook-chat:${context.req.params.webhookId}`
|
||||
);
|
||||
if (chat == null) {
|
||||
context.res.body = { ok: false, error: "chatId not found" };
|
||||
context.res.status = 404;
|
||||
return;
|
||||
}
|
||||
const result = await context.req.body.json();
|
||||
await uploadFileToChat(JSON.parse(chat), result.fileName, result.content);
|
||||
context.res.body = { ok: true };
|
||||
});
|
||||
|
||||
router.post("/t/:webhookId", async (context) => {
|
||||
const chat = await TG_GROUPS.get(
|
||||
`webhook-chat:${context.req.params.webhookId}`
|
||||
@@ -139,3 +153,39 @@ async function sendToChat(
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
export async function uploadFileToChat(
|
||||
chat: {
|
||||
chatId: number;
|
||||
messageThreadId?: number;
|
||||
},
|
||||
fileName: string,
|
||||
content: string
|
||||
) {
|
||||
await fetch(`https://api.telegram.org/bot${BOT_TOKEN}/sendDocument`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data; boundary=WebAppBoundary",
|
||||
},
|
||||
body: `--WebAppBoundary
|
||||
Content-Disposition: form-data; name="chat_id"
|
||||
Content-Type: text/plain
|
||||
|
||||
${chat.chatId}
|
||||
${
|
||||
chat.messageThreadId != null
|
||||
? `--WebAppBoundary
|
||||
Content-Disposition: form-data; name="message_thread_id"
|
||||
Content-Type: text/plain
|
||||
|
||||
${chat.messageThreadId}
|
||||
`
|
||||
: ""
|
||||
}--WebAppBoundary
|
||||
Content-Disposition: form-data; name="document"; filename="${fileName}"
|
||||
Content-Type: application/octet-stream
|
||||
|
||||
${content}
|
||||
--WebAppBoundary--`.replace(/\n/g, "\r\n"),
|
||||
}).then((a) => a.json());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user