chenhg5/cc-connect · error

telegram: send preview: %w

Error message

telegram: send preview: %w

What it means

SendPreviewStart (platform/telegram/telegram.go:1462) wraps any error returned by the Telegram Bot API SendMessage call with the 'telegram: send preview' prefix. The underlying error comes from go-telegram-bot/telegram (network failure, API error such as 'chat not found', 'bot was blocked by the user', or message parse failure after HTML fallback).

Source

Thrown at platform/telegram/telegram.go:1462

				"html_prefix", truncateForLog(html, 200),
				"html_len", len(html),
			)
			params.Text = content
			params.ParseMode = ""
			sent, err = bot.SendMessage(ctx, params)
		} else if strings.Contains(errMsg, "message is too long") {
			// Preview messages shouldn't be chunked; fall back to plain text
			slog.Warn("telegram: preview too long, sending as plain text",
				"method", "SendPreviewStart",
				"html_len", len(html),
				"content_len", len(content),
			)
			params.Text = content
			params.ParseMode = ""
			sent, err = bot.SendMessage(ctx, params)
		}
		if err != nil {
			return nil, fmt.Errorf("telegram: send preview: %w", err)
		}
	}
	return &telegramPreviewHandle{chatID: rc.chatID, threadID: rc.threadID, messageID: sent.ID}, nil
}

// UpdateMessage edits an existing message identified by previewHandle.
func (p *Platform) UpdateMessage(ctx context.Context, previewHandle any, content string) error {
	h, ok := previewHandle.(*telegramPreviewHandle)
	if !ok {
		return fmt.Errorf("telegram: invalid preview handle type %T", previewHandle)
	}
	bot, err := p.connectedBot("update message")
	if err != nil {
		return err
	}

	html := core.MarkdownToSimpleHTML(content)
	slog.Debug("telegram: UpdateMessage",

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Unwrap the %w error and read the underlying Telegram API message (e.g. 'chat not found', 'Forbidden: bot was blocked')
  2. Verify the bot is still a member of the target chat and has permission to send messages
  3. Reconnect the bot (check bot token validity) via the platform's connect flow
  4. If the message is too long, split or truncate content before previewing
  5. Retry with exponential backoff on transient network/5xx errors

Example fix

// before
if _, err := bot.SendMessage(ctx, params); err != nil {
    return nil, err
}
// after
sent, err := bot.SendMessage(ctx, params)
if err != nil {
    var apiErr *telegram.Error
    if errors.As(err, &apiErr) && strings.Contains(apiErr.Message, "chat not found") {
        return nil, fmt.Errorf("telegram: send preview: chat unavailable: %w", err)
    }
    return retryable(err) // backoff and retry on 429/5xx
}
Defensive patterns

Strategy: try-catch

Validate before calling

if err := p.ensureConnected(ctx); err != nil {
    return fmt.Errorf("bot not connected before preview: %w", err)
}

Try / catch

handle, err := p.SendPreviewStart(ctx, rc, content)
var apiErr *telegram.Error
if err != nil && errors.As(err, &apiErr) {
    if strings.Contains(apiErr.Message, "chat not found") || strings.Contains(apiErr.Message, "blocked") {
        markChatUnavailable(rc.chatID)
    } else if apiErr.Code == 429 {
        time.AfterFunc(apiErr.RetryAfter, func() { retryPreview() })
    }
}

Prevention

When it happens

Trigger: Calling SendPreviewStart when the bot token is invalid, network is down, the chat ID no longer exists, the bot lacks permission to post in the chat, or both HTML and plain-text send attempts fail.

Common situations: Bot kicked from a group or blocked by a user between sessions; wrong chat ID in the reply context; Telegram API outages; message content rejected by the API even as plain text (e.g. too long).

Understand the failure class

Background: "API request failed": what wrapped HTTP errors from external APIs mean and how to find the real cause — this error's family across 29 libraries.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/c94fe43f27ff53c7. Report an issue: GitHub.