17 // pollSeconds is how long one getUpdates call waits for an update.
20 // pollSlack is how much longer than pollSeconds a getUpdates call can
21 // take before the bot gives it up.
22 pollSlack = 20 * time.Second
24 // sendTimeout is how long a sendMessage or sendChatAction call can
25 // take. A connection that stops without a close, such as after a
26 // network change, then fails the call, and the sender tries again,
27 // instead of a wait until TCP gives up. The client has no timeout of
28 // its own, because the long poll of getUpdates needs a longer limit.
29 sendTimeout = 30 * time.Second
31 // botAnswerBytes is the most bytes of one answer of the Bot API.
32 botAnswerBytes = 16 << 20
35 // errBotNetwork tells that a Bot API call got no valid answer, such as after a
36 // network error. A later call can work.
37 var errBotNetwork = errors.New("no valid answer from the Bot API")
39 // botAPI calls the Telegram Bot API. The bot token is a part of each URL, so no
40 // error of botAPI holds a URL, a net/http error, or the token. SendTimeout
41 // limits each sendMessage and sendChatAction call.
46 sendTimeout time.Duration
49 // botError is an answer of the Bot API that tells of a failure. RetryAfter is
50 // the wait that the Bot API asks for after HTTP 429.
51 type botError struct {
55 RetryAfter time.Duration
58 // botAnswer is the envelope of each answer of the Bot API.
59 type botAnswer struct {
61 Result json.RawMessage `json:"result"`
62 ErrorCode int `json:"error_code"`
63 Description string `json:"description"`
65 RetryAfter int `json:"retry_after"`
69 // botUpdate is one update of getUpdates. Message is nil for the kinds of
70 // updates that are not a new message.
71 type botUpdate struct {
72 UpdateID int64 `json:"update_id"`
73 Message *botMessage `json:"message"`
76 // botMessage is a message to the bot. Text is empty for a message that is not
77 // text, such as a photo.
78 type botMessage struct {
79 Chat botChat `json:"chat"`
80 From *botUser `json:"from"`
81 Text string `json:"text"`
84 // botChat is the chat of a message. Type is private for a chat with one user.
87 Type string `json:"type"`
90 // botUser is the sender of a message.
95 // newBotAPI returns the client of the Bot API at base with token.
96 func newBotAPI(base, token string) *botAPI {
100 http: &http.Client{},
101 sendTimeout: sendTimeout,
105 // GetUpdates waits for the updates from offset on, up to pollSeconds, and
106 // returns them. Telegram forgets each update before offset. Only new messages
108 func (b *botAPI) GetUpdates(ctx context.Context, offset int64) ([]botUpdate, error) {
109 ctx, cancel := context.WithTimeout(ctx, pollSeconds*time.Second+pollSlack)
112 var updates []botUpdate
113 err := b.call(ctx, "getUpdates", map[string]any{
115 "timeout": pollSeconds,
116 "allowed_updates": []string{"message"},
122 // SendMessage sends text to chat as plain text, with no parse mode. A call that
123 // takes longer than sendTimeout fails with errBotNetwork.
124 func (b *botAPI) SendMessage(ctx context.Context, chat int64, text string) error {
125 ctx, cancel := context.WithTimeout(ctx, b.sendTimeout)
128 return b.call(ctx, "sendMessage", map[string]any{
134 // SendTyping shows the typing action in chat for about five seconds. A call
135 // that takes longer than sendTimeout fails with errBotNetwork.
136 func (b *botAPI) SendTyping(ctx context.Context, chat int64) error {
137 ctx, cancel := context.WithTimeout(ctx, b.sendTimeout)
140 return b.call(ctx, "sendChatAction", map[string]any{
146 // call sends one call of method with the JSON params and decodes its result
147 // into result, when result is not nil.
148 func (b *botAPI) call(ctx context.Context, method string, params, result any) error {
149 body, err := json.Marshal(params)
151 return fmt.Errorf("%s: encode request: %w", method, err)
154 request, err := http.NewRequestWithContext(ctx, http.MethodPost, b.base+"/bot"+b.token+"/"+method, bytes.NewReader(body))
156 return fmt.Errorf("%s: make request: %s", method, b.redact(err))
159 request.Header.Set("Content-Type", "application/json")
160 response, err := b.http.Do(request)
162 return fmt.Errorf("%s: %w: %s", method, errBotNetwork, b.redact(err))
165 data, err := io.ReadAll(io.LimitReader(response.Body, botAnswerBytes))
166 err = errors.Join(err, response.Body.Close())
168 return fmt.Errorf("%s: %w: read answer: %s", method, errBotNetwork, b.redact(err))
172 err = json.Unmarshal(data, &answer)
173 if err != nil && response.StatusCode != http.StatusOK {
176 Status: response.StatusCode,
177 Description: http.StatusText(response.StatusCode),
182 return fmt.Errorf("%s: %w: decode answer: %s", method, errBotNetwork, b.redact(err))
188 Status: response.StatusCode,
189 Description: b.redact(errors.New(answer.Description)),
190 RetryAfter: time.Duration(answer.Parameters.RetryAfter) * time.Second,
198 err = json.Unmarshal(answer.Result, result)
200 return fmt.Errorf("%s: %w: decode result: %s", method, errBotNetwork, b.redact(err))
206 // redact returns the text of err without a URL and without the token. A
207 // *url.Error holds the URL of the call, which holds the token, so only its
209 func (b *botAPI) redact(err error) string {
210 var urlError *url.Error
211 if errors.As(err, &urlError) {
215 return strings.ReplaceAll(err.Error(), b.token, "TOKEN")
218 func (e *botError) Error() string {
219 return fmt.Sprintf("%s: Bot API answered %d: %s", e.Method, e.Status, e.Description)
222 // retryable tells if a later try of a failed Bot API call can work: after a
223 // network error, HTTP 429, or a server error. It returns the wait that the Bot
224 // API asks for, or 0.
225 func retryable(err error) (bool, time.Duration) {
226 if errors.Is(err, errBotNetwork) {
231 if errors.As(err, &answer) {
232 return answer.Status == http.StatusTooManyRequests || answer.Status >= 500, answer.RetryAfter