chenhg5/cc-connect · error

telegram: send chunk %d: %w

Error message

telegram: send chunk %d: %w

What it means

In the chunked-send path (platform/telegram/telegram.go:1545), when an HTML-formatted SendMessage fails with a parse error, the code retries the same chunk as plain text; if the plain-text retry also fails, the error is wrapped as 'telegram: send chunk %d' with the chunk index. This means the chunk could not be delivered in either format.

Source

Thrown at platform/telegram/telegram.go:1545

func (p *Platform) sendChunked(ctx context.Context, bot telegramBot, rc replyContext, html string) error {
	chunks := core.SplitMessageCodeFenceAware(html, telegramMaxMessageLen)
	for i, chunk := range chunks {
		params := &tgbot.SendMessageParams{
			ChatID:          rc.chatID,
			MessageThreadID: rc.threadID,
			Text:            chunk,
			ParseMode:       models.ParseModeHTML,
		}
		if i == 0 && rc.messageID != 0 {
			params.ReplyParameters = &models.ReplyParameters{MessageID: rc.messageID}
		}
		if _, err := bot.SendMessage(ctx, params); err != nil {
			// If HTML fails, try plain text
			if strings.Contains(err.Error(), "can't parse") {
				params.Text = chunk
				params.ParseMode = ""
				if _, err2 := bot.SendMessage(ctx, params); err2 != nil {
					return fmt.Errorf("telegram: send chunk %d: %w", i, err2)
				}
			} else {
				return fmt.Errorf("telegram: send chunk %d: %w", i, err)
			}
		}
	}
	return nil
}

// sendChunkedWithButtons splits a message that's too long and sends it as multiple messages.
// The first chunk includes the inline keyboard buttons.
func (p *Platform) sendChunkedWithButtons(ctx context.Context, bot telegramBot, rc replyContext, html string, rows [][]models.InlineKeyboardButton) error {
	chunks := core.SplitMessageCodeFenceAware(html, telegramMaxMessageLen)
	for i, chunk := range chunks {
		params := &tgbot.SendMessageParams{
			ChatID:          rc.chatID,
			MessageThreadID: rc.threadID,
			Text:            chunk,

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Inspect the wrapped cause: 429 'Too Many Requests' → honor retry_after and resend the chunk
  2. 'chat not found' / 'Forbidden' → restore bot access before resuming the chunk sequence
  3. Ensure no chunk is empty or only whitespace before sending
  4. Add per-chunk retry with backoff before failing the whole message
  5. Log the chunk index alongside the cause to locate the failing portion of the content

Example fix

// before
if _, err2 := bot.SendMessage(ctx, params); err2 != nil {
    return fmt.Errorf("telegram: send chunk %d: %w", i, err2)
}
// after
if _, err2 := bot.SendMessage(ctx, params); err2 != nil {
    var apiErr *telegram.Error
    if errors.As(err2, &apiErr) && apiErr.RetryAfter > 0 {
        time.Sleep(time.Duration(apiErr.RetryAfter) * time.Second)
        if _, err3 := bot.SendMessage(ctx, params); err3 == nil {
            continue
        }
    }
    return fmt.Errorf("telegram: send chunk %d: %w", i, err2)
}
Defensive patterns

Strategy: try-catch

Validate before calling

chunk = strings.TrimSpace(chunk)
if chunk == "" {
    return nil // nothing to send
}
if len(chunk) > telegramMaxMessageLen {
    return errors.New("chunk exceeds Telegram limit")
}

Try / catch

err := sendChunks(ctx, bot, chunks)
var apiErr *telegram.Error
if err != nil && errors.As(err, &apiErr) && apiErr.Code == 429 {
    time.Sleep(time.Duration(apiErr.RetryAfter) * time.Second)
    err = sendChunks(ctx, bot, chunks[i:]) // resume from failed chunk
}

Prevention

When it happens

Trigger: Calling the chunked send function where chunk i fails both HTML and plain-text delivery — chat inaccessible, bot blocked/kicked, message content rejected by Telegram (e.g. empty text), or network/API failure on both attempts.

Common situations: Long streaming responses split into chunks sent to a chat the bot just lost access to; Telegram rate limiting (429) hitting one chunk mid-sequence; a chunk that is only whitespace/unparseable entities rejected twice.

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/06738ea776ec8793. Report an issue: GitHub.