refactor(commands): centralize built-in command definitions
This commit is contained in:
parent
6975ab1945
commit
8318d1da7f
3 changed files with 71 additions and 5 deletions
|
|
@ -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},
|
||||
|
|
|
|||
32
pkg/commands/builtin.go
Normal file
32
pkg/commands/builtin.go
Normal file
|
|
@ -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"},
|
||||
},
|
||||
}
|
||||
}
|
||||
16
pkg/commands/builtin_test.go
Normal file
16
pkg/commands/builtin_test.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue