diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2024-01-31 05:38:46 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2024-01-31 05:38:46 +0300 |
commit | fd0d7411c2f5e1e0368d3494318ce05195d0a56d (patch) | |
tree | f2802177abea8a9a2a5a1062f66bcb1f2359715a /telegram/commands.go | |
parent | 20e6d2558e868d61d7168ffe5ca4f3bff0a240c7 (diff) |
Basic Ad-Hoc support for transport commands
Diffstat (limited to 'telegram/commands.go')
-rw-r--r-- | telegram/commands.go | 58 |
1 files changed, 39 insertions, 19 deletions
diff --git a/telegram/commands.go b/telegram/commands.go index 9251ebb..1c10d12 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -48,6 +48,7 @@ var permissionsMember = client.ChatPermissions{ var permissionsReadonly = client.ChatPermissions{} var transportCommands = map[string]command{ + "help": command{"", "help"}, "login": command{"phone", "sign in"}, "logout": command{"", "sign out"}, "cancelauth": command{"", "quit the signin wizard"}, @@ -66,6 +67,7 @@ var transportCommands = map[string]command{ } var chatCommands = map[string]command{ + "help": command{"", "help"}, "d": command{"[n]", "delete your last message(s)"}, "s": command{"edited message", "edit your last message"}, "silent": command{"message", "send a message without sound"}, @@ -110,38 +112,56 @@ type command struct { } type configurationOption command -type helpType int +// CommandType disinguishes command sets by chat +type CommandType int const ( - helpTypeTransport helpType = iota - helpTypeChat + CommandTypeTransport CommandType = iota + CommandTypeChat ) -func helpString(ht helpType) string { - var str strings.Builder +// GetCommands exposes the set of commands +func GetCommands(typ CommandType) map[string]command { var commandMap map[string]command - switch ht { - case helpTypeTransport: + switch typ { + case CommandTypeTransport: commandMap = transportCommands - case helpTypeChat: + case CommandTypeChat: commandMap = chatCommands } + return commandMap +} + +// CommandToHelpString builds a text description of a command +func CommandToHelpString(name string, cmd command) string { + var str strings.Builder + + str.WriteString("/") + str.WriteString(name) + if cmd.arguments != "" { + str.WriteString(" ") + str.WriteString(cmd.arguments) + } + str.WriteString(" — ") + str.WriteString(cmd.description) + + return str.String() +} + +func helpString(typ CommandType) string { + var str strings.Builder + + commandMap := GetCommands(typ) + str.WriteString("Available commands:\n") for name, command := range commandMap { - str.WriteString("/") - str.WriteString(name) - if command.arguments != "" { - str.WriteString(" ") - str.WriteString(command.arguments) - } - str.WriteString(" — ") - str.WriteString(command.description) + str.WriteString(CommandToHelpString(name, command)) str.WriteString("\n") } - if ht == helpTypeTransport { + if typ == CommandTypeTransport { str.WriteString("Configuration options\n") for name, option := range transportConfigurationOptions { str.WriteString(name) @@ -448,7 +468,7 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string case "channel": return c.cmdChannel(args, cmdline) case "help": - return helpString(helpTypeTransport) + return helpString(CommandTypeTransport) } return "" @@ -1088,7 +1108,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) return strings.Join(entries, "\n"), true case "help": - return helpString(helpTypeChat), true + return helpString(CommandTypeChat), true default: return "", false } |