chenhg5/cc-connect · error

telegram: send image

Error message

telegram: send image: %w

What it means

Platform.SendImage wraps errors from bot.SendPhoto with "telegram: send image: ". It means the photo upload/delivery to Telegram failed; the image bytes are streamed via InputFileUpload so large payloads and network faults are the usual causes.

Solutions

  1. Check the wrapped cause for the real error (size limit vs network vs chat access)
  2. Resize/compress images to under 10 MB before SendPhoto, or send as a document instead
  3. Verify bot membership in the target chat and valid threadID
  4. Retry transient upload failures with backoff
  5. Validate img.Data is non-empty and a supported format (jpg/png/webp) before sending

Example fix

// before
if len(img.Data) > 10*1024*1024 { /* silently too large */ }
bot.SendPhoto(ctx, params) // fails
// after
if len(img.Data) > 10*1024*1024 {
    return p.SendFile(ctx, rctx, core.FileAttachment{Name: name, Data: img.Data})
}
_, err := bot.SendPhoto(ctx, params)
Defensive patterns

Strategy: validation

Validate before calling

// validate image before upload
if len(img.Data) == 0 {
    return errors.New("empty image data")
}
if len(img.Data) > 10*1024*1024 {
    return errors.New("image exceeds Telegram 10MB photo limit; send as document")
}

Try / catch

if err := p.SendImage(ctx, rc, img); err != nil {
    var apiErr *tgbot.Error
    if errors.As(err, &apiErr) {
        // inspect apiErr.Description for size/chat errors
    }
    return err
}

Prevention

When it happens

Trigger: bot.SendPhoto fails: multipart upload interrupted, image exceeds Telegram's 10 MB photo limit, invalid chat/thread ID, bot lacks access, or Telegram API returns an error.

Common situations: Sending screenshots or generated images larger than 10 MB (Telegram photo limit); flaky networks during multipart upload; bot removed from chat; sending to a topic thread that no longer exists.

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

Appendix: source

Thrown at platform/telegram/telegram.go:1153

		return fmt.Errorf("telegram: invalid reply context type %T", rctx)
	}
	bot, err := p.connectedBot("send image")
	if err != nil {
		return err
	}

	name := img.FileName
	if name == "" {
		name = "image"
	}
	slog.Debug("telegram: sending image", "chat_id", rc.chatID, "name", name, "size", len(img.Data))
	params := &tgbot.SendPhotoParams{
		ChatID:          rc.chatID,
		MessageThreadID: rc.threadID,
		Photo:           &models.InputFileUpload{Filename: name, Data: bytes.NewReader(img.Data)},
	}
	if _, err := bot.SendPhoto(ctx, params); err != nil {
		return fmt.Errorf("telegram: send image: %w", err)
	}
	return nil
}

func (p *Platform) SendFile(ctx context.Context, rctx any, file core.FileAttachment) error {
	rc, ok := rctx.(replyContext)
	if !ok {
		return fmt.Errorf("telegram: invalid reply context type %T", rctx)
	}
	bot, err := p.connectedBot("send file")
	if err != nil {
		return err
	}

	name := file.FileName
	if name == "" {
		name = "attachment"
	}

View on GitHub (pinned to 4000b2338a)