/
filerecipient.go
52 lines (43 loc) · 1.29 KB
/
filerecipient.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
package reporter
import (
"io"
"net/http"
"time"
tbapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/pkg/errors"
)
// FileRecipient knows how to get file by fileID
// Needed mainly for tests to mock
type FileRecipient interface {
GetFile(fileID string) (io.ReadCloser, error)
}
// TelegramFileRecipient implements FileRecipient for Telegram files
type TelegramFileRecipient struct {
botAPI *tbapi.BotAPI
httpClient *http.Client
}
// NewTelegramFileRecipient creates TelegramFileRecipient
func NewTelegramFileRecipient(botAPI *tbapi.BotAPI, timeout time.Duration) FileRecipient {
return &TelegramFileRecipient{
botAPI: botAPI,
httpClient: &http.Client{
Timeout: timeout,
},
}
}
// GetFile gets file from Telegram
func (tfd TelegramFileRecipient) GetFile(fileID string) (io.ReadCloser, error) {
url, err := tfd.botAPI.GetFileDirectURL(fileID)
if err != nil {
return nil, errors.Wrapf(err, "get file direct URL (fileID: %s)", fileID)
}
resp, err := tfd.httpClient.Get(url)
if err != nil {
return nil, errors.Wrapf(err, "file by direct URL (fileID: %s)", fileID)
// don't expose `url` – it contains Bot API Token
}
if resp.StatusCode != 200 {
return nil, errors.Wrapf(err, "non-200 response from file direct URL (fileID: %s)", fileID)
}
return resp.Body, nil
}