-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles_func.go
58 lines (43 loc) · 965 Bytes
/
files_func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package telebot
import (
"encoding/json"
"errors"
"io"
"net/http"
)
func (b *bot) GetFile(fileID string) (*File, error) {
var resp struct {
Result File
}
params := getFileRequest{
FileID: fileID,
}
req, err := b.sendMethodRequest(methodGetFile, params)
if err != nil {
return nil, err
}
if err = json.Unmarshal(req, &resp); err != nil {
return nil, err
}
return &resp.Result, nil
}
// URL currently only supports TELEGRAM's OFFICIAL API URL
// todo: implement SelfHosted API Support
func (f *File) URL() string {
return "https://api.telegram.org/file/bot" + telegramSecretToken + "/" + f.FilePath
}
func (f *File) Download() ([]byte, error) {
resp, err := http.Get(f.URL())
if err != nil {
return nil, err
}
defer panicIf(wrapError(resp.Body.Close()))
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if len(body) > 0 {
return body, nil
}
return nil, errors.New("failed to download file")
}