chenhg5/cc-connect · error
telegram: invalid chat ID in %q
Error message
telegram: invalid chat ID in %q
What it means
The session-key parser in platform/telegram/telegram.go:1383 found the 'telegram:' prefix and at least 2 segments, but the second segment (the chat ID) is not a valid base-10 64-bit integer. Telegram chat IDs are int64 values, so a non-numeric or out-of-range chat ID makes the session unusable.
Source
Thrown at platform/telegram/telegram.go:1383
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)
}
}
// else: telegram:{chatID}:{userID} — no threadID
case 4:
// telegram:{chatID}:{threadID}:{userID}
threadID, err = strconv.Atoi(parts[2])View on GitHub (pinned to 4000b2338a)
Solutions
- Print the raw key and confirm segment 2 after 'telegram:' parses as strconv.ParseInt(..., 10, 64)
- Fix the stored key to contain the exact numeric chat ID including any leading '-'
- Regenerate the session from a live Telegram update so the chat ID is captured directly from the API
- Validate chat IDs when persisting sessions (reject non-int64 values at write time)
Example fix
// before key := "telegram:chat_12345" // after key := "telegram:-1001234567890"
Defensive patterns
Strategy: validation
Validate before calling
func validChatID(key string) bool {
parts := strings.SplitN(key, ":", 5)
if len(parts) < 2 || parts[0] != "telegram" {
return false
}
_, err := strconv.ParseInt(parts[1], 10, 64)
return err == nil
} Type guard
func chatIDFromKey(key string) (int64, bool) {
parts := strings.SplitN(key, ":", 5)
if len(parts) < 2 {
return 0, false
}
id, err := strconv.ParseInt(parts[1], 10, 64)
return id, err == nil
} Try / catch
sess, err := parseSessionKey(key)
if err != nil {
slog.Warn("bad telegram chat ID in session key, dropping session", "key", key, "err", err)
return errSessionDropped
} Prevention
- Store chat IDs as int64 end-to-end; avoid float/string round-trips that lose precision
- Preserve the leading '-' on group/supergroup chat IDs when persisting
- Validate keys at write time (on session save) so bad IDs never enter storage
- Copy keys from API responses, never from log lines or manual transcription
When it happens
Trigger: Calling the session-key parse function with keys like 'telegram:abc:123' (non-numeric chat ID), 'telegram:' (empty chat ID), or a chat ID that overflows int64.
Common situations: Hand-crafted or hand-edited session keys; group chat IDs stored without the required minus sign ('-100...') then corrupted by trimming; keys copied from logs with surrounding characters; storing chat IDs as floats losing precision.
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
- telegram: invalid session key %q
- invalid source session key: %w
- invalid session key format: %q
- dingtalk: invalid session key format: %q
- discord: invalid session key %q
AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06).
Data as JSON: /api/errors/1a0436b84ac22085.
Report an issue: GitHub.