chenhg5/cc-connect · error

telegram: invalid session key %q

Error message

telegram: invalid session key %q

What it means

This error comes from the Telegram platform's session-key parser (platform/telegram/telegram.go:1379). Session keys encode chat/topic/user routing as colon-separated segments like 'telegram:{chatID}' or 'telegram:{chatID}:{threadID}:{userID}'. The parser rejects any key that has fewer than 2 segments or whose first segment is not the literal 'telegram', because such a key cannot be routed to a Telegram chat.

Source

Thrown at platform/telegram/telegram.go:1379

	if err != nil {
		return nil, fmt.Errorf("download file %s: %w", fileID, err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("download file %s: status %d", fileID, resp.StatusCode)
	}
	return io.ReadAll(resp.Body)
}

func (p *Platform) ReconstructReplyCtx(sessionKey string) (any, error) {
	// Formats:
	//   telegram:{chatID}                      - shared session, no topic
	//   telegram:{chatID}:{threadID}           - shared session, with topic
	//   telegram:{chatID}:{userID}             - per-user session, no topic
	//   telegram:{chatID}:{threadID}:{userID}  - per-user session, with topic
	parts := strings.SplitN(sessionKey, ":", 5)
	if len(parts) < 2 || parts[0] != "telegram" {
		return nil, fmt.Errorf("telegram: invalid session key %q", sessionKey)
	}
	chatID, err := strconv.ParseInt(parts[1], 10, 64)
	if err != nil {
		return nil, fmt.Errorf("telegram: invalid chat ID in %q", sessionKey)
	}

	threadID := 0
	switch len(parts) {
	case 2:
		// telegram:{chatID}
	case 3:
		if p.shareSessionInChannel {
			// telegram:{chatID}:{threadID}
			threadID, err = strconv.Atoi(parts[2])
			if err != nil {
				slog.Warn("telegram: invalid thread ID", "raw", parts[2], "error", err)
			}
		}

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Verify the session key starts with the literal prefix 'telegram:' followed by a base-10 integer chat ID
  2. Regenerate the session through the normal telegram platform flow instead of restoring a stored key
  3. If migrating from another platform, discard keys from other adapters and create a fresh Telegram session
  4. Check for stale/legacy session records and delete or migrate them to the current format

Example fix

// before
sess, err := p.parseSessionKey("12345:67890")
// after
sess, err := p.parseSessionKey("telegram:12345:67890")
Defensive patterns

Strategy: validation

Validate before calling

func validTelegramSessionKey(key string) bool {
    parts := strings.SplitN(key, ":", 5)
    return len(parts) >= 2 && parts[0] == "telegram"
}
if !validTelegramSessionKey(key) {
    return fmt.Errorf("skipping malformed session key %q", key)
}

Type guard

func isTelegramSessionKey(v any) (*telegramSession, bool) {
    s, ok := v.(string)
    if !ok || !strings.HasPrefix(s, "telegram:") {
        return nil, false
    }
    return parseTelegramSessionKey(s)
}

Try / catch

sess, err := parseSessionKey(key)
if err != nil {
    slog.Warn("unparseable session key, creating new session", "key", key, "err", err)
    sess, err = newSession(chatID)
}

Prevention

When it happens

Trigger: Calling the session-restore/parse function with a session key that was produced by a different platform (e.g. 'feishu:123'), a key missing the 'telegram:' prefix (e.g. '12345:-100'), or an empty/empty-segment key stored by an older version of the app.

Common situations: Mixing persisted sessions across platform adapters after switching messaging backends; hand-editing session keys in a database or config file; loading sessions serialized by a version that used a different key format; passing a raw chat ID instead of a full key.

Understand the failure class

Background: "invalid id" errors: invalid identifier format — why libraries reject IDs before lookup, and how to fix them — this error's family across 37 libraries.

Related errors


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