]> git.giacomolli.uk - onechat/blobdiff - telegram_api.go
Explain daemon log levels and journal filtering
[onechat] / telegram_api.go
index 7b9457f6d207dc6abeb03fcd926e6ed5916d4aff..07d2bf1b00b2aec00243614c63dffbfffd54c331 100644 (file)
@@ -52,11 +52,13 @@ func newTelegramAPI(base, token string) *TelegramAPI {
 // the Bot API asks for, or 0.
 func telegramRetryable(err error) (bool, time.Duration) {
        var network *TelegramNetworkError
+
        if errors.As(err, &network) {
                return true, 0
        }
 
        var answer *TelegramError
+
        if errors.As(err, &answer) {
                return answer.Status == http.StatusTooManyRequests || answer.Status >= 500, answer.RetryAfter
        }
@@ -70,6 +72,7 @@ func telegramRetryable(err error) (bool, time.Duration) {
 // tell this, so it is not a refusal.
 func telegramRejected(err error) bool {
        var answer *TelegramError
+
        return errors.As(err, &answer) && answer.Refused && (answer.Status == http.StatusBadRequest || answer.Status == http.StatusNotFound)
 }
 
@@ -89,6 +92,7 @@ type TelegramAPI struct {
 // messages are asked for.
 func (b *TelegramAPI) GetUpdates(ctx context.Context, offset int64) ([]TelegramUpdate, error) {
        var updates []TelegramUpdate
+
        err := b.call(ctx, telegramPollTimeout, "getUpdates", map[string]any{
                "offset":          offset,
                "timeout":         telegramPollSeconds,
@@ -155,6 +159,7 @@ func (b *TelegramAPI) call(ctx context.Context, timeout time.Duration, method st
        }
 
        request.Header.Set("Content-Type", "application/json")
+
        response, err := b.http.Do(request)
        if err != nil {
                return b.networkError("no answer", err, timeout)
@@ -167,6 +172,7 @@ func (b *TelegramAPI) call(ctx context.Context, timeout time.Duration, method st
        }
 
        var answer TelegramAnswer
+
        err = json.Unmarshal(data, &answer)
        if err != nil && response.StatusCode != http.StatusOK {
                return &TelegramError{
@@ -221,6 +227,7 @@ func (b *TelegramAPI) networkError(what string, err error, timeout time.Duration
 // cause is kept.
 func (b *TelegramAPI) redact(err error) string {
        var urlError *url.Error
+
        if errors.As(err, &urlError) {
                err = urlError.Err
        }