chenhg5/cc-connect · error

telegram: invalid reply context type %T

Error message

telegram: invalid reply context type %T

What it means

Reply() asserts that the reply context passed by the engine is the telegram package's internal replyContext type. Receiving any other value (nil, a different platform's context struct, or a raw string) means a wiring bug, so it fails fast with a type-name-bearing error for diagnosis.

Source

Thrown at platform/telegram/telegram.go:1041

	}

	slog.Debug("telegram: ignoring group message not directed at bot", "chat", msg.Chat.ID, "bot", botName, "text", msg.Text, "entities", msg.Entities)
	return false
}

func isCommand(msg *models.Message) bool {
	for _, e := range msg.Entities {
		if e.Type == models.MessageEntityTypeBotCommand && e.Offset == 0 {
			return true
		}
	}
	return false
}

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

	html := core.MarkdownToSimpleHTML(content)
	params := &tgbot.SendMessageParams{
		ChatID:          rc.chatID,
		MessageThreadID: rc.threadID,
		Text:            html,
		ParseMode:       models.ParseModeHTML,
		ReplyParameters: &models.ReplyParameters{MessageID: rc.messageID},
	}

	if _, err := bot.SendMessage(ctx, params); err != nil {
		errMsg := err.Error()
		// Handle HTML parsing errors by falling back to plain text

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Ensure Reply is only called with reply contexts obtained from this telegram Platform's incoming message handling
  2. Check engine routing so each message goes back through the platform that produced it
  3. In tests, build a proper telegram replyContext rather than a mock value
  4. Read %T in the error to identify which wrong type was passed

Example fix

// before
plat.Reply(ctx, someSlackReplyCtx, "hi")
// after
rc, ok := msg.ReplyContext.(telegram.ReplyContext)
if !ok { return fmt.Errorf("not a telegram reply context") }
plat.Reply(ctx, rc, "hi")
Defensive patterns

Strategy: type-guard

Validate before calling

if _, ok := rctx.(telegram.ReplyContext); !ok {
    return fmt.Errorf("wrong platform context for telegram.Reply")
}

Type guard

func isTelegramReplyContext(rc any) bool {
    _, ok := rc.(telegram.ReplyContext)
    return ok
}

Try / catch

if err := plat.Reply(ctx, rctx, content); err != nil {
    if strings.Contains(err.Error(), "invalid reply context type") {
        slog.Error("reply context from wrong platform", "got", fmt.Sprintf("%T", rctx))
    }
    return err
}

Prevention

When it happens

Trigger: Calling Reply(ctx, rctx, content) where rctx is not a telegram.replyContext — typically because the reply context was produced by a different platform adapter or was never created by a telegram ReceiveMessage/handler path.

Common situations: Cross-platform message routing bug where a Slack/Telegram context is fed to the Telegram platform; test code passing a placeholder context; custom code constructing reply contexts manually.

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