all repos — telegram-bot-api @ 68663614af0037ee08d7175763002ee9b07a22e5

Golang bindings for the Telegram Bot API

docs/getting-started/files.md (view raw)

 1# Files
 2
 3Telegram supports specifying files in many different formats. In order to
 4accommodate them all, there are multiple structs and type aliases required.
 5
 6All of these types implement the `RequestFileData` interface.
 7
 8| Type         | Description                                                               |
 9| ------------ | ------------------------------------------------------------------------- |
10| `FilePath`   | A local path to a file                                                    |
11| `FileID`     | Existing file ID on Telegram's servers                                    |
12| `FileURL`    | URL to file, must be served with expected MIME type                       |
13| `FileReader` | Use an `io.Reader` to provide a file. Lazily read to save memory.         |
14| `FileBytes`  | `[]byte` containing file data. Prefer to use `FileReader` to save memory. |
15
16## `FilePath`
17
18A path to a local file.
19
20```go
21file := tgbotapi.FilePath("tests/image.jpg")
22```
23
24## `FileID`
25
26An ID previously uploaded to Telegram. IDs may only be reused by the same bot
27that received them. Additionally, thumbnail IDs cannot be reused.
28
29```go
30file := tgbotapi.FileID("AgACAgIAAxkDAALesF8dCjAAAa_…")
31```
32
33## `FileURL`
34
35A URL to an existing resource. It must be served with a correct MIME type to
36work as expected.
37
38```go
39file := tgbotapi.FileURL("https://i.imgur.com/unQLJIb.jpg")
40```
41
42## `FileReader`
43
44Use an `io.Reader` to provide file contents as needed. Requires a filename for
45the virtual file.
46
47```go
48var reader io.Reader
49
50file := tgbotapi.FileReader{
51    Name: "image.jpg",
52    Reader: reader,
53}
54```
55
56## `FileBytes`
57
58Use a `[]byte` to provide file contents. Generally try to avoid this as it
59results in high memory usage. Also requires a filename for the virtual file.
60
61```go
62var data []byte
63
64file := tgbotapi.FileBytes{
65    Name: "image.jpg",
66    Bytes: data,
67}
68```