Skip to content

Commit

Permalink
chore: add files Media Upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kravetsone committed Feb 7, 2024
1 parent d580e9e commit e1dcf77
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
],
},
Expand Down
71 changes: 71 additions & 0 deletions docs/files/media-upload.md
Original file line number Diff line number Diff line change
@@ -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`.
18 changes: 8 additions & 10 deletions docs/files/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit e1dcf77

Please sign in to comment.