// newTelegramAPI returns the client of the Bot API at base with token.
func newTelegramAPI(base, token string) *TelegramAPI {
return &TelegramAPI{
- base: base,
- token: token,
- http: &http.Client{},
- sendTimeout: telegramSendTimeout,
+ base: base,
+ token: token,
+ http: &http.Client{},
}
}
}
// TelegramAPI calls the Telegram Bot API. The bot token is a part of each URL,
-// so no error of TelegramAPI holds a URL, a net/http error, or the token. Its
-// sendTimeout field limits each sendMessage, sendRichMessage, and
-// sendChatAction call.
+// so no error of TelegramAPI holds a URL, a net/http error, or the token.
type TelegramAPI struct {
- base string
- token string
- http *http.Client
- sendTimeout time.Duration
+ base string
+ token string
+ http *http.Client
}
// GetUpdates waits for the updates from offset on, up to telegramPollSeconds,
Size int64 `json:"file_size"`
}
- err = b.call(ctx, b.sendTimeout, "getFile", map[string]any{"file_id": id}, &file)
+ err = b.call(ctx, telegramSendTimeout, "getFile", map[string]any{"file_id": id}, &file)
if err != nil {
return nil, fmt.Errorf("get image file: %w", err)
}
return nil, errors.New("invalid image file path")
}
- ctx, cancel := context.WithTimeout(ctx, b.sendTimeout)
+ ctx, cancel := context.WithTimeout(ctx, telegramSendTimeout)
defer cancel()
request, err := http.NewRequestWithContext(ctx, http.MethodGet, b.base+"/file/bot"+b.token+"/"+file.Path, nil)
response, err := b.http.Do(request)
if err != nil {
- return nil, b.networkError("image download failed", err, b.sendTimeout)
+ return nil, b.networkError("image download failed", err, telegramSendTimeout)
}
defer func() {
closeErr := response.Body.Close()
if closeErr != nil {
- err = errors.Join(err, b.networkError("close image download", closeErr, b.sendTimeout))
+ err = errors.Join(err, b.networkError("close image download", closeErr, telegramSendTimeout))
}
}()
data, err = io.ReadAll(io.LimitReader(response.Body, imageMaxBytes+1))
if err != nil {
- return nil, b.networkError("image cut off", err, b.sendTimeout)
+ return nil, b.networkError("image cut off", err, telegramSendTimeout)
}
if len(data) > imageMaxBytes {
params["entities"] = entities
}
- return b.call(ctx, b.sendTimeout, "sendMessage", params, nil)
+ return b.call(ctx, telegramSendTimeout, "sendMessage", params, nil)
}
// SendRichMessage sends markdown to chat as a rich message, which Telegram
// renders itself, tables included. A call that takes longer than
// telegramSendTimeout fails with a TelegramNetworkError.
func (b *TelegramAPI) SendRichMessage(ctx context.Context, chat int64, markdown string) error {
- return b.call(ctx, b.sendTimeout, "sendRichMessage", map[string]any{
+ return b.call(ctx, telegramSendTimeout, "sendRichMessage", map[string]any{
"chat_id": chat,
"rich_message": map[string]any{
"markdown": markdown,
// SendTyping shows the typing action in chat for about five seconds. A call
// that takes longer than telegramSendTimeout fails with a TelegramNetworkError.
func (b *TelegramAPI) SendTyping(ctx context.Context, chat int64) error {
- return b.call(ctx, b.sendTimeout, "sendChatAction", map[string]any{
+ return b.call(ctx, telegramSendTimeout, "sendChatAction", map[string]any{
"chat_id": chat,
"action": "typing",
}, nil)