feat(menu): sort Telegram command suggestions by name

The command menu was in module load order, which split related commands across modules. Sorting by name keeps /thoitiet next to /thoitiethomnay. The weather module also lists /thoitiet first so /help matches.
This commit is contained in:
tiennm99 committed 2026-10-08 17:10:42 +07:00
1 parent 9801a64e18
commit fe552f43e4
4 files changed
+9 -6

No files matched your search

+5 -2
View File
@@ -3,6 +3,8 @@ package main
import (
"context"
"errors"
"slices"
"strings"
"time"
"github.com/go-telegram/bot"
@@ -14,8 +16,8 @@ import (
const commandMenuTimeout = 8 * time.Second
// botCommandMenu builds the Telegram command menu from the loaded public
// commands. It intentionally follows registry module order so MODULES controls
// both enabled commands and their menu grouping.
// commands, sorted by name so related commands such as /thoitiet and
// /thoitiethomnay sit together whatever their module order.
func botCommandMenu(reg *modules.Registry) []models.BotCommand {
if reg == nil {
return nil
@@ -32,6 +34,7 @@ func botCommandMenu(reg *modules.Registry) []models.BotCommand {
})
}
}
slices.SortFunc(out, func(a, b models.BotCommand) int { return strings.Compare(a.Command, b.Command) })
return out
}
+2 -2
View File
@@ -16,7 +16,7 @@ import (
"github.com/tiennm99/tiennm99bot/internal/testutil"
)
func TestBotCommandMenu_UsesLoadedPublicCommandsInModuleOrder(t *testing.T) {
func TestBotCommandMenu_ListsLoadedPublicCommandsByName(t *testing.T) {
reg := &modules.Registry{
Modules: []modules.Module{
{
@@ -39,8 +39,8 @@ func TestBotCommandMenu_UsesLoadedPublicCommandsInModuleOrder(t *testing.T) {
got := botCommandMenu(reg)
want := []models.BotCommand{
{Command: "beta_public", Description: "<value>. Beta public."},
{Command: "alpha_public", Description: "Alpha public."},
{Command: "beta_public", Description: "<value>. Beta public."},
}
if len(got) != len(want) {
t.Fatalf("commands = %v, want %v", got, want)
+1 -1
View File
@@ -77,8 +77,8 @@ func New(deps modules.Deps) modules.Module {
}
return modules.Module{
Commands: append([]modules.Command{
command("thoitiethomnay", "Thời tiết hôm nay (mặc định Tân Thuận, Q.7)", todayView),
command("thoitiet", "Thời tiết từng giờ trong 6 giờ tới (mặc định Tân Thuận, Q.7)", hourlyView),
command("thoitiethomnay", "Thời tiết hôm nay (mặc định Tân Thuận, Q.7)", todayView),
command("thoitietngaymai", "Thời tiết ngày mai (mặc định Tân Thuận, Q.7)", tomorrowView),
command("thoitiettuannay", "Thời tiết 7 ngày tới (mặc định Tân Thuận, Q.7)", weekView),
}, fl.commands()...),
+1 -1
View File
@@ -105,8 +105,8 @@ func send(rb *testutil.RecordingBot, text string) string {
func TestCommands_RegistrationAndParameters(t *testing.T) {
mod := New(modules.Deps{Store: storage.NewMemoryProvider().Collection(CollectionName)})
want := []struct{ name, parameters string }{
{"thoitiethomnay", "[location... | lat,long]"},
{"thoitiet", "[location... | lat,long]"},
{"thoitiethomnay", "[location... | lat,long]"},
{"thoitietngaymai", "[location... | lat,long]"},
{"thoitiettuannay", "[location... | lat,long]"},
{"thuyvan", ""},