all repos — telegram-bot-api @ 17d3e395d52d40e76b22f043424937a4e8d2f58c

Golang bindings for the Telegram Bot API

docs/internals/uploading-files.md (view raw)

 1# Uploading Files
 2
 3To make files work as expected, there's a lot going on behind the scenes. Make
 4sure to read through the [Files](../getting-started/files.md) section in
 5Getting Started first as we'll be building on that information.
 6
 7This section only talks about file uploading. For non-uploaded files such as
 8URLs and file IDs, you just need to pass a string.
 9
10## Fields
11
12Let's start by talking about how the library represents files as part of a
13Config.
14
15### Static Fields
16
17Most endpoints use static file fields. For example, `sendPhoto` expects a single
18file named `photo`. All we have to do is set that single field with the correct
19value (either a string or multipart file). Methods like `sendDocument` take two
20file uploads, a `document` and a `thumb`. These are pretty straightforward.
21
22Remembering that the `Fileable` interface only requires one method, let's
23implement it for `DocumentConfig`.
24
25```go
26func (config DocumentConfig) files() []RequestFile {
27    // We can have multiple files, so we'll create an array. We also know that
28    // there always is a document file, so initialize the array with that.
29	files := []RequestFile{{
30		Name: "document",
31		Data: config.File,
32	}}
33
34    // We'll only add a file if we have one.
35	if config.Thumb != nil {
36		files = append(files, RequestFile{
37			Name: "thumb",
38			Data: config.Thumb,
39		})
40	}
41
42	return files
43}
44```
45
46Telegram also supports the `attach://` syntax (discussed more later) for
47thumbnails, but there's no reason to make things more complicated.
48
49### Dynamic Fields
50
51Of course, not everything can be so simple. Methods like `sendMediaGroup`
52can accept many files, and each file can have custom markup. Using a static
53field isn't possible because we need to specify which field is attached to each
54item. Telegram introduced the `attach://` syntax for this.
55
56Let's follow through creating a new media group with string and file uploads.
57
58First, we start by creating some `InputMediaPhoto`.
59
60```go
61photo := tgbotapi.NewInputMediaPhoto(tgbotapi.FilePath("tests/image.jpg"))
62url := tgbotapi.NewInputMediaPhoto(tgbotapi.FileURL("https://i.imgur.com/unQLJIb.jpg"))
63```
64
65This created a new `InputMediaPhoto` struct, with a type of `photo` and the
66media interface that we specified.
67
68We'll now create our media group with the photo and URL.
69
70```go
71mediaGroup := NewMediaGroup(ChatID, []interface{}{
72    photo,
73    url,
74})
75```
76
77A `MediaGroupConfig` stores all the media in an array of interfaces. We now
78have all the data we need to upload, but how do we figure out field names for
79uploads? We didn't specify `attach://unique-file` anywhere.
80
81When the library goes to upload the files, it looks at the `params` and `files`
82for the Config. The params are generated by transforming the file into a value
83more suitable for uploading, file IDs and URLs are untouched but uploaded types
84are all changed into `attach://file-%d`. When collecting a list of files to
85upload, it names them the same way. This creates a nearly transparent way of
86handling multiple files in the background without the user having to consider
87what's going on.