-
Notifications
You must be signed in to change notification settings - Fork 0
/
getFile.go
57 lines (46 loc) · 1.35 KB
/
getFile.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
package telegram
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
type GetFileResultT struct {
Ok bool `json:"ok"`
ErrorCode int `json:"error_code,omitempty"`
Description string `json:"description,omitempty"`
Result *FileT `json:"result"`
}
func (t *BotT) GetFile(fileID string) (*FileT, []byte, error) {
data := url.Values{}
data.Add("file_id", fileID)
body, httpStatusCode, err := t.sendRaw("getFile", data)
if err != nil {
return nil, nil, err
}
result := &GetFileResultT{}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, nil, fmt.Errorf("telegram http status: %d. error: %s", httpStatusCode, err.Error())
}
if !result.Ok {
return nil, nil, fmt.Errorf("telegram http status: %d. error code: %d. %s", httpStatusCode, result.ErrorCode, result.Description)
}
if t.Debug {
fmt.Println("https://api.telegram.org/file/bot" + t.Token + "/" + result.Result.Path)
}
resp, err := t.client.Get("https://api.telegram.org/file/bot" + t.Token + "/" + result.Result.Path)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf("telegram http status: %d. file %s unavailable", httpStatusCode, fileID)
}
body, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
return result.Result, body, nil
}