This repository has been archived by the owner on Aug 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
response.go
70 lines (57 loc) · 1.73 KB
/
response.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
59
60
61
62
63
64
65
66
67
68
69
70
package telegram
import (
"fmt"
"io"
"time"
"github.com/jfk9w-go/flu"
)
// responseParameters contains information about why a request was unsuccessful.
// See https://core.telegram.org/bots/api#responseparameters
type responseParameters struct {
MigrateToChatID ID `json:"migrate_to_chat_id"`
RetryAfter int `json:"retry_after"`
}
// response is a generic Telegram Bot API response.
// See https://core.telegram.org/bots/api#making-requests
type response struct {
Ok bool `json:"ok"`
ErrorCode int `json:"error_code"`
Description string `json:"description"`
Result interface{} `json:"result"`
Parameters *responseParameters `json:"parameters"`
}
func newResponse(value interface{}) *response {
return &response{
Result: value,
}
}
func (r *response) DecodeFrom(reader io.Reader) error {
if err := flu.JSON(r).DecodeFrom(reader); err != nil {
return err
}
if !r.Ok {
if r.Parameters != nil && r.Parameters.RetryAfter > 0 {
return TooManyMessages{time.Duration(r.Parameters.RetryAfter) * time.Second}
}
return Error{r.ErrorCode, r.Description}
}
return nil
}
// Error is an error returned by Telegram Bot API.
// See https://core.telegram.org/bots/api#making-requests
type Error struct {
// ErrorCode is an integer error code.
ErrorCode int
// Description explains the error.
Description string
}
func (e Error) Error() string {
return fmt.Sprintf("%d %s", e.ErrorCode, e.Description)
}
// TooManyMessages is returned in case of exceeding flood control.
type TooManyMessages struct {
RetryAfter time.Duration
}
func (e TooManyMessages) Error() string {
return fmt.Sprintf("too many messages, retry after %.0f seconds", e.RetryAfter.Seconds())
}