chenhg5/cc-connect · error

wecom-ws: SendImage: invalid reply context type %T

Error message

wecom-ws: SendImage: invalid reply context type %T

What it means

WSPlatform.SendImage requires the reply context argument (rctx) to be the internal wsReplyContext struct, which carries the target chatID for the WeCom AI Bot WebSocket API. This error means the caller passed some other value (nil, a different platform's reply context, or a wrong-typed value), so the platform cannot determine where to send the image. It is a defensive type assertion (`rctx.(wsReplyContext)`) guarding the platform's internal contract.

Source

Thrown at platform/wecom/websocket_outbound_media.go:25

	"encoding/hex"
	"encoding/json"
	"fmt"
	"path/filepath"
	"strings"

	"github.com/chenhg5/cc-connect/core"
)

const (
	wecomWSUploadChunkSize = 512 * 1024
	wecomWSUploadMaxChunks = 100
)

// SendImage uploads and sends an image through the WeCom AI Bot WebSocket API.
func (p *WSPlatform) SendImage(ctx context.Context, rctx any, img core.ImageAttachment) error {
	rc, ok := rctx.(wsReplyContext)
	if !ok {
		return fmt.Errorf("wecom-ws: SendImage: invalid reply context type %T", rctx)
	}
	if rc.chatID == "" {
		return fmt.Errorf("wecom-ws: chatID is empty, cannot send image")
	}
	if len(img.Data) == 0 {
		return fmt.Errorf("wecom-ws: image data is empty")
	}

	mediaID, err := p.uploadWSMedia(ctx, "image", wsImageFileName(img), img.Data)
	if err != nil {
		return fmt.Errorf("wecom-ws: send image: %w", err)
	}
	if err := p.sendWSMediaMessage(ctx, rc.chatID, "image", mediaID); err != nil {
		return fmt.Errorf("wecom-ws: send image: %w", err)
	}
	return nil
}

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Use the reply context value returned by the same wecom WSPlatform instance (e.g. from its ReceiveMessage/message handling path), never a context from another platform.
  2. If you only have a chatid string, obtain a wsReplyContext via the platform's own APIs rather than synthesizing one; the type is unexported so it must come from the library itself.
  3. Check that you are calling SendImage on the WSPlatform that produced the original inbound message, not on a second platform instance.
  4. In tests, exercise SendImage indirectly through the engine/ReceiveMessage flow so the correct wsReplyContext is produced automatically.

Example fix

// before
err := platform.SendImage(ctx, nil, img) // or a foreign reply context
// after
err := platform.SendImage(ctx, wsReplyCtx, img) // wsReplyCtx came from this WSPlatform's message handling
Defensive patterns

Strategy: type-guard

Validate before calling

if _, ok := rctx.(wecom.WSReplyContextChecker); !ok {
    return fmt.Errorf("cannot send image: reply context is not a wecom WS reply context")
}

Type guard

func isWSReplyContext(rctx any) bool {
    if rctx == nil { return false }
    return fmt.Sprintf("%T", rctx) == "wecom.wsReplyContext"
}

Try / catch

if err := platform.SendImage(ctx, rctx, img); err != nil {
    if strings.Contains(err.Error(), "invalid reply context type") {
        // wrong platform/context; skip or re-route
    }
}

Prevention

When it happens

Trigger: Calling SendImage with rctx that is not a wecom.wsReplyContext: passing nil, passing a reply context captured from a different platform (e.g. a feishu or telegram context), passing a raw string/int chatID instead of the context struct, or constructing a structurally similar but differently typed value.

Common situations: Bridging code that forwards reply contexts across multiple platforms and mixes them up; tests calling SendImage directly with a placeholder context; code refactors where SendText/SendImage were given different context types; using an older reply-context type after a library upgrade changed the internal type.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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