chenhg5/cc-connect · warning

telegram: setMyCommands failed: %w

Error message

telegram: setMyCommands failed: %w

What it means

Registering the bot's command list via Telegram's setMyCommands API failed. This is called at startup (or config reload) to make slash-commands appear in the bot's UI menu. The error wraps the underlying Bot API error; the commands may not be registered so the client menu will be stale or empty.

Source

Thrown at platform/telegram/telegram.go:1719

		tgCommands = append(tgCommands, models.BotCommand{
			Command:     cmd,
			Description: desc,
		})
	}

	// Limit to 100 commands
	if len(tgCommands) > 100 {
		tgCommands = tgCommands[:100]
	}

	if len(tgCommands) == 0 {
		slog.Debug("telegram: no commands to register")
		return nil
	}

	ctx := context.Background()
	if _, err := bot.SetMyCommands(ctx, &tgbot.SetMyCommandsParams{Commands: tgCommands}); err != nil {
		return fmt.Errorf("telegram: setMyCommands failed: %w", err)
	}

	slog.Info("telegram: registered bot commands", "count", len(tgCommands))
	return nil
}

// extractEntityText extracts a substring from text using Telegram's UTF-16 code unit
// offset and length. Telegram Bot API entity offsets are measured in UTF-16 code units,
// not bytes or Unicode code points, so direct byte slicing produces wrong results
// when the text contains non-ASCII characters (e.g. Chinese, emoji).
func extractEntityText(text string, offsetUTF16, lengthUTF16 int) string {
	encoded := utf16.Encode([]rune(text))
	endUTF16 := offsetUTF16 + lengthUTF16
	if offsetUTF16 < 0 || lengthUTF16 < 0 || endUTF16 > len(encoded) {
		return ""
	}
	return string(utf16.Decode(encoded[offsetUTF16:endUTF16]))
}

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Validate command names (lowercase letters, digits, underscore, ≤32 chars) and descriptions (≤256 chars) before calling.
  2. Verify the bot token is valid — run getMe with the same token.
  3. Check the wrapped cause: 401 = bad token, 400 = invalid command payload, 429 = rate limited (retry after).
  4. This error is usually non-fatal at startup; consider logging a warning instead of aborting registration.

Example fix

// before
return fmt.Errorf("telegram: setMyCommands failed: %w", err)
// after
slog.Warn("telegram: setMyCommands failed; command menu may be stale", "err", err)
return nil // non-fatal: bot still works without registered menu
Defensive patterns

Strategy: fallback

Validate before calling

for _, c := range cmds {
    if len(c.Command) > 32 || !regexp.MustCompile(`^[a-z0-9_]+$`).MatchString(c.Command) {
        return fmt.Errorf("invalid command name %q", c.Command)
    }
    if len(c.Description) > 256 { return fmt.Errorf("description too long for %q", c.Command) }
}

Try / catch

if err := registerCommands(ctx, bot, cmds); err != nil {
    slog.Warn("telegram: command registration failed, continuing", "err", err)
}

Prevention

When it happens

Trigger: Calling setMyCommands with a command list Telegram rejects: command names not matching ^[a-z0-9_]+$ or longer than 32 chars, more than 100 commands, descriptions exceeding 256 chars, or an invalid/expired bot token making the API call fail.

Common situations: Bot token misconfigured or revoked; a newly added command name contains uppercase or hyphens; description too long after editing config; Telegram API outage or rate limit at startup.

Related errors


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