Skip to content

Commit 1ecad79

Browse files
committed
Add files
1 parent 5088f64 commit 1ecad79

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

src/files.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
title: Files
3+
parent: /#walkthrough
4+
prev: /keyboards-and-callback-queries
5+
---
6+
7+
Methods that send photos, videos, documents, and other media accept a {{ "FileSource" |> t }}. A file source can be a file path, URL, file ID, `Uint8Array`, iterable of byte arrays, or readable stream.
8+
9+
## Uploading Files
10+
11+
Pass a file source directly to the method for the type of media you want to send.
12+
13+
```ts
14+
// A local file path.
15+
await client.sendDocument(chatId, "./report.pdf");
16+
17+
// A remote file.
18+
await client.sendPhoto(
19+
chatId,
20+
new URL("https://example.com/photo.jpg"),
21+
);
22+
23+
// Bytes already in memory.
24+
await client.sendDocument(chatId, bytes, {
25+
fileName: "data.bin",
26+
});
27+
```
28+
29+
Local file paths are available only in server-side runtimes. In browsers, use a URL, byte array, iterable, or stream.
30+
31+
## Reusing Files
32+
33+
Received media includes a `fileId`. Pass it to a compatible send method to reuse the file without uploading it again.
34+
35+
```ts
36+
client.on("message:photo", async (ctx) => {
37+
await ctx.replyPhoto(ctx.msg.photo.fileId);
38+
});
39+
```
40+
41+
Media also includes a `fileUniqueId`. It remains stable and is useful for comparing files, but it cannot be used to download or resend them.
42+
43+
## Downloading Files
44+
45+
The `download` method returns an async generator that yields the file as chunks of bytes.
46+
47+
```ts
48+
for await (const chunk of client.download(fileId)) {
49+
// Write or otherwise process the chunk.
50+
}
51+
```
52+
53+
Processing chunks as they arrive avoids loading the entire file into memory. You can also set the chunk size or cancel a download with an abort signal.
54+
55+
```ts
56+
const controller = new AbortController();
57+
58+
for await (const chunk of client.download(fileId, {
59+
chunkSize: 256 * 1024,
60+
signal: controller.signal,
61+
})) {
62+
// ...
63+
}
64+
```
65+
66+
## Upload Progress
67+
68+
To receive progress updates for an upload, get a progress ID and pass it to the send method.
69+
70+
```ts
71+
const progressId = await client.getProgressId();
72+
73+
client.on("uploadProgress", (ctx) => {
74+
const progress = ctx.update.uploadProgress;
75+
if (progress.id === progressId) {
76+
console.log(progress.uploaded, "/", progress.total);
77+
}
78+
});
79+
80+
await client.sendDocument(chatId, "./report.pdf", { progressId });
81+
```
82+
83+
For sources whose size is not known in advance, `total` is `0` until the upload completes.

src/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Its key features include:
3232
7. [Commands](/commands) {{ "/commands" |> i }}
3333
8. [Reactions](/reactions) {{ "/reactions" |> i }}
3434
9. [Keyboards and Callback Queries](/keyboards-and-callback-queries) {{ "/keyboards-and-callback-queries" |> i }}
35+
10. [Files](/files) {{ "/files" |> i }}
3536

3637
### Guides
3738

src/keyboards-and-callback-queries.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
title: Keyboards and Callback Queries
33
parent: /#walkthrough
44
prev: /reactions
5+
next: /files
56
---
67

78
Bots can attach a keyboard to a message to give users a set of actions to choose from. An inline keyboard appears directly below the message and can contain buttons that send data back to the bot, open URLs, launch mini apps, and perform other actions.

0 commit comments

Comments
 (0)