chenhg5/cc-connect · error

qqbot: invalid reply context

Error message

qqbot: invalid reply context

What it means

Send() requires replyCtx to be the platform's internal *replyContext type produced when the platform originally received a message. The type assertion rctx, ok := replyCtx.(*replyContext) fails for any other value, and Send returns this error instead of panicking. It means the caller passed a reply context this platform did not issue.

Source

Thrown at platform/qqbot/qqbot.go:221

	if err := p.connectGateway(ctx); err != nil {
		cancel()
		return fmt.Errorf("qqbot: failed to connect gateway: %w", err)
	}

	slog.Info("qqbot: connected to QQ Bot gateway", "sandbox", p.sandbox)
	return nil
}

// Reply sends a message as a reply to an incoming message.
func (p *Platform) Reply(ctx context.Context, replyCtx any, content string) error {
	return p.Send(ctx, replyCtx, content)
}

// Send sends a message to the conversation identified by replyCtx.
func (p *Platform) Send(ctx context.Context, replyCtx any, content string) error {
	rctx, ok := replyCtx.(*replyContext)
	if !ok {
		return fmt.Errorf("qqbot: invalid reply context")
	}

	chunks := core.SplitMessageCodeFenceAware(content, messageMaxLen)
	for _, chunk := range chunks {
		if err := p.sendMessage(rctx, chunk); err != nil {
			return err
		}
	}
	return nil
}

// SendImage uploads and sends an image via QQ Bot rich media API.
// Implements core.ImageSender.
func (p *Platform) SendImage(ctx context.Context, replyCtx any, img core.ImageAttachment) error {
	rctx, ok := replyCtx.(*replyContext)
	if !ok {
		return fmt.Errorf("qqbot: SendImage: invalid reply context type %T", replyCtx)
	}

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Pass the replyCtx value exactly as delivered by the platform's incoming-message event — do not rebuild or transform it.
  2. Ensure the message is dispatched to the same platform instance that produced the reply context.
  3. If contexts cross system boundaries, keep the original pointer in a registry keyed by conversation ID rather than re-creating it.
  4. Add a type assertion check in the caller before invoking Send to fail fast with better diagnostics.

Example fix

// before
err := qqPlatform.Send(ctx, conversationID, text) // conversationID is a string
// after
var replyCtx any = originalEvent.ReplyContext() // keep platform-issued context
err := qqPlatform.Send(ctx, replyCtx, text)
Defensive patterns

Strategy: type-guard

Type guard

func validReplyCtx(replyCtx any) bool {
    _, ok := replyCtx.(*replyContext)
    return ok
}

Try / catch

if err := p.Send(ctx, replyCtx, text); err != nil {
    if strings.Contains(err.Error(), "invalid reply context") {
        // replyCtx did not come from this platform; re-route or drop
    }
}

Prevention

When it happens

Trigger: Calling Send() with nil, a string chat ID, a *replyContext from a different platform package, or a replyCtx that was copied/converted into another type before being passed back.

Common situations: Routing a message through the wrong platform adapter (reply context captured from Telegram, sent via qqbot); storing reply contexts as interface{} and reconstructing them incorrectly; middleware serializing/deserializing the context and losing the pointer type.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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