From e1dcf775f21570834979f8d15942e4c36c5a8e6e Mon Sep 17 00:00:00 2001 From: Kravets <57632712+kravetsone@users.noreply.github.com> Date: Wed, 7 Feb 2024 21:25:44 +0300 Subject: [PATCH] chore: add files Media Upload --- docs/.vitepress/config.mts | 1 + docs/files/media-upload.md | 71 ++++++++++++++++++++++++++++++++++++++ docs/files/overview.md | 18 +++++----- 3 files changed, 80 insertions(+), 10 deletions(-) create mode 100644 docs/files/media-upload.md diff --git a/docs/.vitepress/config.mts b/docs/.vitepress/config.mts index 3d09680..7c3f3ff 100644 --- a/docs/.vitepress/config.mts +++ b/docs/.vitepress/config.mts @@ -67,6 +67,7 @@ export default defineConfig({ text: "Overview", link: "/files/overview", }, + { text: "Media Upload", link: "/files/media-upload" }, { text: "Media Input", link: "/files/media-input" }, ], }, diff --git a/docs/files/media-upload.md b/docs/files/media-upload.md new file mode 100644 index 0000000..117ee99 --- /dev/null +++ b/docs/files/media-upload.md @@ -0,0 +1,71 @@ +# Media Upload + +Class-helper with static methods for file uploading. + +## path + +Method for uploading Media File by local path. + +```ts +ctx.sendDocument(MediaUpload.path("./package.json")); +// or with filename +ctx.sendDocument(MediaUpload.path("./package.json", "some-other.json")); +``` + +If filename not specified, the filename set to filename :) + +## url + +Method for uploading Media File by URL (also with fetch options). + +```ts +ctx.sendPhoto(MediaUpload.url("https://example.com/cat.png")); +// or with filename +ctx.sendPhoto(MediaUpload.url("https://example.com/cat.png", "cute-cat.png")); +// or with filename and fetch options (for example headers) +ctx.sendPhoto( + MediaUpload.url("https://example.com/cat.png", "cute-cat.png", { + headers: { + Authorization: "Bearer gramio", + }, + }) +); +``` + +If filename not specified, the filename set to last part after `/`. + +## buffer + +Method for uploading Media File by Buffer or ArrayBuffer. + +```ts +const res = await fetch("https://..."); +ctx.sendDocument(await res.arrayBuffer(), "from-buffer.json"); +``` + +By default filename is `file.buffer`. + +## stream + +Method for uploading Media File by Readable stream. + +```ts +ctx.sendDocument( + MediaUpload.stream( + fs.createReadStream("./cute-cat.png"), + "the-same-cute-cat.png" + ) +); +``` + +By default filename is `file.stream`. + +## text + +Method for uploading Media File by text content. + +```ts +ctx.sendDocument(MediaUpload.text("GramIO is the best!", "truth.txt")); +``` + +By default filename is `text.txt`. diff --git a/docs/files/overview.md b/docs/files/overview.md index 0a15184..4566a7f 100644 --- a/docs/files/overview.md +++ b/docs/files/overview.md @@ -10,16 +10,14 @@ import { Bot, MediaInput, MediaUpload, InlineKeyboard } from "gramio"; const bot = new Bot(process.env.BOT_TOKEN); bot.updates.on("message", async (ctx) => { - if (ctx.text === "test") { - ctx.sendMediaGroup([ - MediaInput.document( - MediaUpload.url( - "https://raw.githubusercontent.com/gramiojs/types/main/README.md" - ) - ), - MediaInput.document(MediaUpload.path("./package.json")), - ]); - } + ctx.sendMediaGroup([ + MediaInput.document( + MediaUpload.url( + "https://raw.githubusercontent.com/gramiojs/types/main/README.md" + ) + ), + MediaInput.document(MediaUpload.path("./package.json")), + ]); }); bot.updates.startPolling();