From 935ea24d54cca534e8c4fdc93bfd235199067b12 Mon Sep 17 00:00:00 2001 From: NanamiAdmin Date: Wed, 19 Aug 2026 20:02:18 +0800 Subject: [PATCH] feat(controller): enhance command handlers to accept source parameter for improved message formatting --- internal/controller/processor.go | 12 +++---- internal/controller/trigger.go | 4 +-- internal/node/tracker.go | 8 ++--- internal/template/template.go | 62 +++++++++++++++++++++----------- 4 files changed, 53 insertions(+), 33 deletions(-) diff --git a/internal/controller/processor.go b/internal/controller/processor.go index 765d732..b270778 100644 --- a/internal/controller/processor.go +++ b/internal/controller/processor.go @@ -10,16 +10,16 @@ import ( "nukumizu-backend/internal/template" ) -func handleHelp() (string, error) { +func handleHelp(cmd Command) (string, error) { cfg := config.GetConfig() params := template.BuildBotInitializationMsgParams() - return template.Render(cfg.ControllerMessage.BotHelp, params), nil + return template.Render(cfg.ControllerMessage.BotHelp, params, cmd.Source), nil } -func handleList() (string, error) { +func handleList(cmd Command) (string, error) { cfg := config.GetConfig() params := template.BuildParamsFromServerList() - return template.Render(cfg.ControllerMessage.ServerList, params), nil + return template.Render(cfg.ControllerMessage.ServerList, params, cmd.Source), nil } func handleStatus(cmd Command) (string, error) { @@ -130,7 +130,7 @@ func handleRun(cmd Command) (string, error) { cfg := config.GetConfig() params := template.BuildParamsFromExecResult(uuidArg, uuidArg, command, formatTaskResults(results)) - return template.Render(cfg.ControllerMessage.ServerExecuteResult, params), nil + return template.Render(cfg.ControllerMessage.ServerExecuteResult, params, cmd.Source), nil } func handleInfo(cmd Command) (string, error) { @@ -180,7 +180,7 @@ func handleInfo(cmd Command) (string, error) { func telegram_handleStart() (string, error) { cfg := config.GetConfig() params := template.BuildBotInitializationMsgParams() - return template.Render(cfg.ControllerMessage.Tg_BotStart, params), nil + return template.Render(cfg.ControllerMessage.Tg_BotStart, params, "telegram"), nil } func handleGetIP(cmd Command) (string, error) { diff --git a/internal/controller/trigger.go b/internal/controller/trigger.go index 607cedf..6e7091e 100644 --- a/internal/controller/trigger.go +++ b/internal/controller/trigger.go @@ -51,9 +51,9 @@ func (m *Manager) RouteCommand(cmd Command) (string, error) { } switch cmd.Command { case "help": - return handleHelp() + return handleHelp(cmd) case "list": - return handleList() + return handleList(cmd) case "status": return handleStatus(cmd) case "shutdown": diff --git a/internal/node/tracker.go b/internal/node/tracker.go index fcdacc5..7b6c077 100644 --- a/internal/node/tracker.go +++ b/internal/node/tracker.go @@ -456,9 +456,9 @@ func (t *Tracker) GetOnlineServers() []string { for uuid := range t.onlineSet { name := t.uuidToName[uuid] if name != "" { - result = append(result, fmt.Sprintf("- %s (%s)", name, uuid)) + result = append(result, fmt.Sprintf("- %s (`%s`)", name, uuid)) } else { - result = append(result, fmt.Sprintf("- %s", uuid)) + result = append(result, fmt.Sprintf("- `%s`", uuid)) } } return result @@ -478,9 +478,9 @@ func (t *Tracker) GetOfflineServers() []string { for uuid := range offlineSet { name := t.uuidToName[uuid] if name != "" { - result = append(result, fmt.Sprintf("- %s (%s)", name, uuid)) + result = append(result, fmt.Sprintf("- %s (`%s`)", name, uuid)) } else { - result = append(result, fmt.Sprintf("- %s", uuid)) + result = append(result, fmt.Sprintf("- `%s`", uuid)) } } return result diff --git a/internal/template/template.go b/internal/template/template.go index d4aeb41..2d146be 100644 --- a/internal/template/template.go +++ b/internal/template/template.go @@ -99,36 +99,56 @@ func BuildParamsFromExecResult(serverName, serverUUID, command, result string) P // - {{ softwareBuildTime }} — software build time // - {{ softwareDeveloper }} — software developer // - {{ softwareDescription }} — software description -func Render(tmpl string, params Params) string { +func Render(tmpl string, params Params, source ...string) string { result := tmpl - result = strings.ReplaceAll(result, "{{ time }}", params.Time) - result = strings.ReplaceAll(result, "{{ serverName }}", params.ServerName) - result = strings.ReplaceAll(result, "{{ serverUUID }}", params.ServerUUID) - result = strings.ReplaceAll(result, "{{ upStatus }}", params.UpStatus) - result = strings.ReplaceAll(result, "{{ event }}", params.Event) - result = strings.ReplaceAll(result, "{{ message }}", params.Message) - result = strings.ReplaceAll(result, "{{ command }}", params.Command) - result = strings.ReplaceAll(result, "{{ result }}", params.Result) - result = strings.ReplaceAll(result, "{{ list.onlineServers }}", params.OnlineServers) - result = strings.ReplaceAll(result, "{{ list.offlineServers }}", params.OfflineServers) - result = strings.ReplaceAll(result, "{{ softwareVersion }}", params.SoftwareVersion) - result = strings.ReplaceAll(result, "{{ softwareBuildVer }}", fmt.Sprintf("%d", params.SoftwareBuildVer)) - result = strings.ReplaceAll(result, "{{ softwareCommitHash }}", params.SoftwareCommitHash) - result = strings.ReplaceAll(result, "{{ softwareBuildType }}", params.SoftwareBuildType) - result = strings.ReplaceAll(result, "{{ softwareBuildTime }}", params.SoftwareBuildTime) - result = strings.ReplaceAll(result, "{{ softwareDeveloper }}", params.SoftwareDeveloper) - result = strings.ReplaceAll(result, "{{ softwareDescription }}", params.SoftwareDescription) - + if len(source) > 0 && source[0] == "telegram" { + // Telegram requires special formatting for code blocks and inline code. + result = strings.ReplaceAll(result, "{{ time }}", "**" + params.Time + "**") + result = strings.ReplaceAll(result, "{{ serverName }}", "**" + params.ServerName + "**") + result = strings.ReplaceAll(result, "{{ serverUUID }}", "`" + params.ServerUUID + "`") + result = strings.ReplaceAll(result, "{{ upStatus }}", "**" + params.UpStatus + "**") + result = strings.ReplaceAll(result, "{{ event }}", "**" + params.Event + "**") + result = strings.ReplaceAll(result, "{{ message }}", "`" + params.Message + "`") + result = strings.ReplaceAll(result, "{{ command }}", "`" + params.Command + "`") + result = strings.ReplaceAll(result, "{{ result }}", "```bash\n" + params.Result + "\n```") + result = strings.ReplaceAll(result, "{{ list.onlineServers }}", params.OnlineServers) + result = strings.ReplaceAll(result, "{{ list.offlineServers }}", params.OfflineServers) + result = strings.ReplaceAll(result, "{{ softwareVersion }}", params.SoftwareVersion) + result = strings.ReplaceAll(result, "{{ softwareBuildVer }}", fmt.Sprintf("%d", params.SoftwareBuildVer)) + result = strings.ReplaceAll(result, "{{ softwareCommitHash }}", params.SoftwareCommitHash) + result = strings.ReplaceAll(result, "{{ softwareBuildType }}", params.SoftwareBuildType) + result = strings.ReplaceAll(result, "{{ softwareBuildTime }}", params.SoftwareBuildTime) + result = strings.ReplaceAll(result, "{{ softwareDeveloper }}", params.SoftwareDeveloper) + result = strings.ReplaceAll(result, "{{ softwareDescription }}", params.SoftwareDescription) + } else { + result = strings.ReplaceAll(result, "{{ time }}", params.Time) + result = strings.ReplaceAll(result, "{{ serverName }}", params.ServerName) + result = strings.ReplaceAll(result, "{{ serverUUID }}", params.ServerUUID) + result = strings.ReplaceAll(result, "{{ upStatus }}", params.UpStatus) + result = strings.ReplaceAll(result, "{{ event }}", params.Event) + result = strings.ReplaceAll(result, "{{ message }}", params.Message) + result = strings.ReplaceAll(result, "{{ command }}", params.Command) + result = strings.ReplaceAll(result, "{{ result }}", params.Result) + result = strings.ReplaceAll(result, "{{ list.onlineServers }}", params.OnlineServers) + result = strings.ReplaceAll(result, "{{ list.offlineServers }}", params.OfflineServers) + result = strings.ReplaceAll(result, "{{ softwareVersion }}", params.SoftwareVersion) + result = strings.ReplaceAll(result, "{{ softwareBuildVer }}", fmt.Sprintf("%d", params.SoftwareBuildVer)) + result = strings.ReplaceAll(result, "{{ softwareCommitHash }}", params.SoftwareCommitHash) + result = strings.ReplaceAll(result, "{{ softwareBuildType }}", params.SoftwareBuildType) + result = strings.ReplaceAll(result, "{{ softwareBuildTime }}", params.SoftwareBuildTime) + result = strings.ReplaceAll(result, "{{ softwareDeveloper }}", params.SoftwareDeveloper) + result = strings.ReplaceAll(result, "{{ softwareDescription }}", params.SoftwareDescription) + } return result } // FormatServerListEntry formats a single server entry for list display. func FormatServerListEntry(name, uuid string) string { if name == "" { - return fmt.Sprintf("- %s", uuid) + return fmt.Sprintf("- `%s`", uuid) } - return fmt.Sprintf("- %s (%s)", name, uuid) + return fmt.Sprintf("- %s (`%s`)", name, uuid) } // JoinServerListEntries joins formatted server entries with newlines.