chenhg5/cc-connect · error

telegram: SendAudio: invalid reply context type %T

Error message

telegram: SendAudio: invalid reply context type %T

What it means

Platform.SendAudio requires rctx to be the telegram replyContext type; any other value fails the assertion and returns this error with the dynamic type. Same guard as the other Send* methods, labeled SendAudio.

Source

Thrown at platform/telegram/telegram.go:1188

		name = "attachment"
	}
	params := &tgbot.SendDocumentParams{
		ChatID:          rc.chatID,
		MessageThreadID: rc.threadID,
		Document:        &models.InputFileUpload{Filename: name, Data: bytes.NewReader(file.Data)},
	}
	if _, err := bot.SendDocument(ctx, params); err != nil {
		return fmt.Errorf("telegram: send file: %w", err)
	}
	return nil
}

// SendAudio sends synthesized audio back to Telegram.
// It prefers voice messages and falls back to audio files for mp3/m4a on sendVoice failure.
func (p *Platform) SendAudio(ctx context.Context, rctx any, audio []byte, format string) error {
	rc, ok := rctx.(replyContext)
	if !ok {
		return fmt.Errorf("telegram: SendAudio: invalid reply context type %T", rctx)
	}

	sendData := audio
	sendFormat := strings.ToLower(strings.TrimSpace(format))
	if sendFormat == "" {
		sendFormat = "ogg"
	}

	switch sendFormat {
	case "ogg", "opus", "mp3", "m4a":
		// Attempt these formats directly with sendVoice first.
	default:
		converted, err := telegramConvertAudioToOpus(ctx, audio, sendFormat)
		if err != nil {
			return fmt.Errorf("telegram: SendAudio: convert %s to opus: %w", sendFormat, err)
		}
		sendData = converted
		sendFormat = "opus"

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Pass the replyContext captured from the telegram incoming message
  2. Nil-check rctx before calling
  3. Construct a telegram replyContext explicitly for proactive sends
  4. Use the %T value in the error to locate the wrong type at the call site

Example fix

// before
p.SendAudio(ctx, chatID, audio, "ogg")
// after
rc := telegramReplyContextFor(chatID, threadID)
p.SendAudio(ctx, rc, audio, "ogg")
Defensive patterns

Strategy: type-guard

Type guard

func toTelegramReplyContext(rctx any) (replyContext, error) {
    rc, ok := rctx.(replyContext)
    if !ok {
        return replyContext{}, fmt.Errorf("got %T, want telegram replyContext", rctx)
    }
    return rc, nil
}

Try / catch

if err := p.SendAudio(ctx, rctx, audio, format); err != nil {
    if strings.Contains(err.Error(), "invalid reply context type") {
        return fmt.Errorf("SendAudio context bug: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling SendAudio with nil, a foreign platform's context, or a raw chat-id value instead of the telegram replyContext.

Common situations: TTS pipelines calling the adapter directly without a captured reply context; cross-platform message routing bugs; tests using the wrong mock type.

Understand the failure class

Background: "is not a compatible type" / "cannot merge" errors: when a value's type doesn't match what the library requires — this error's family across 65 libraries.

Related errors


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