From 8318d1da7f2301c5d75f43d1a1198f9ee5a55a3d Mon Sep 17 00:00:00 2001 From: mingmxren Date: Sun, 1 Mar 2026 02:27:07 +0800 Subject: [PATCH] refactor(commands): centralize built-in command definitions --- pkg/channels/telegram/telegram_commands.go | 28 +++++++++++++++---- pkg/commands/builtin.go | 32 ++++++++++++++++++++++ pkg/commands/builtin_test.go | 16 +++++++++++ 3 files changed, 71 insertions(+), 5 deletions(-) create mode 100644 pkg/commands/builtin.go create mode 100644 pkg/commands/builtin_test.go diff --git a/pkg/channels/telegram/telegram_commands.go b/pkg/channels/telegram/telegram_commands.go index 496fc5e4f..c17961835 100644 --- a/pkg/channels/telegram/telegram_commands.go +++ b/pkg/channels/telegram/telegram_commands.go @@ -7,6 +7,7 @@ import ( "github.com/mymmrac/telego" + "github.com/sipeed/picoclaw/pkg/commands" "github.com/sipeed/picoclaw/pkg/config" ) @@ -38,11 +39,8 @@ func commandArgs(text string) string { } func (c *cmd) Help(ctx context.Context, message telego.Message) error { - msg := `/start - Start the bot -/help - Show this help message -/show [model|channel] - Show current configuration -/list [models|channels] - List available options - ` + defs := commands.NewRegistry(commands.BuiltinDefinitions(c.config)).ForChannel("telegram") + msg := formatHelpMessage(defs) _, err := c.bot.SendMessage(ctx, &telego.SendMessageParams{ ChatID: telego.ChatID{ID: message.Chat.ID}, Text: msg, @@ -53,6 +51,26 @@ func (c *cmd) Help(ctx context.Context, message telego.Message) error { return err } +func formatHelpMessage(defs []commands.Definition) string { + if len(defs) == 0 { + return "No commands available." + } + + lines := make([]string, 0, len(defs)) + for _, def := range defs { + usage := def.Usage + if usage == "" { + usage = "/" + def.Name + } + desc := def.Description + if desc == "" { + desc = "No description" + } + lines = append(lines, fmt.Sprintf("%s - %s", usage, desc)) + } + return strings.Join(lines, "\n") +} + func (c *cmd) Start(ctx context.Context, message telego.Message) error { _, err := c.bot.SendMessage(ctx, &telego.SendMessageParams{ ChatID: telego.ChatID{ID: message.Chat.ID}, diff --git a/pkg/commands/builtin.go b/pkg/commands/builtin.go new file mode 100644 index 000000000..4b97a14a4 --- /dev/null +++ b/pkg/commands/builtin.go @@ -0,0 +1,32 @@ +package commands + +import "github.com/sipeed/picoclaw/pkg/config" + +func BuiltinDefinitions(_ *config.Config) []Definition { + return []Definition{ + { + Name: "start", + Description: "Start the bot", + Usage: "/start", + Channels: []string{"telegram", "whatsapp", "whatsapp_native"}, + }, + { + Name: "help", + Description: "Show this help message", + Usage: "/help", + Channels: []string{"telegram", "whatsapp", "whatsapp_native"}, + }, + { + Name: "show", + Description: "Show current configuration", + Usage: "/show [model|channel]", + Channels: []string{"telegram", "whatsapp", "whatsapp_native"}, + }, + { + Name: "list", + Description: "List available options", + Usage: "/list [models|channels]", + Channels: []string{"telegram", "whatsapp", "whatsapp_native"}, + }, + } +} diff --git a/pkg/commands/builtin_test.go b/pkg/commands/builtin_test.go new file mode 100644 index 000000000..a84cb65bd --- /dev/null +++ b/pkg/commands/builtin_test.go @@ -0,0 +1,16 @@ +package commands + +import "testing" + +func TestBuiltinDefinitions_ContainsTelegramDefaults(t *testing.T) { + defs := BuiltinDefinitions(nil) + names := map[string]bool{} + for _, d := range defs { + names[d.Name] = true + } + for _, want := range []string{"help", "start", "show", "list"} { + if !names[want] { + t.Fatalf("missing command %q", want) + } + } +}