Declaratively specify optional and required command arguments
This commit is contained in:
parent
eace19eef7
commit
e3a5191905
|
@ -48,56 +48,56 @@ 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"},
|
||||
"code": command{"", "check one-time code"},
|
||||
"password": command{"", "check 2fa password"},
|
||||
"setusername": command{"", "update @username"},
|
||||
"setname": command{"first last", "update name"},
|
||||
"setbio": command{"", "update about"},
|
||||
"setpassword": command{"[old] [new]", "set or remove password"},
|
||||
"config": command{"[param] [value]", "view or update configuration options"},
|
||||
"report": command{"[chat] [comment]", "report a chat by id or @username"},
|
||||
"add": command{"@username", "add @username to your chat list"},
|
||||
"join": command{"https://t.me/invite_link", "join to chat via invite link or @publicname"},
|
||||
"supergroup": command{"title description", "create new supergroup «title» with «description»"},
|
||||
"channel": command{"title description", "create new channel «title» with «description»"},
|
||||
"help": command{0, []string{}, "help"},
|
||||
"login": command{1, []string{"phone"}, "sign in"},
|
||||
"logout": command{0, []string{}, "sign out"},
|
||||
"cancelauth": command{0, []string{}, "quit the signin wizard"},
|
||||
"code": command{1, []string{"xxxxx"}, "check one-time code"},
|
||||
"password": command{1, []string{"********"}, "check 2fa password"},
|
||||
"setusername": command{0, []string{"@username"}, "update @username"},
|
||||
"setname": command{1, []string{"first", "last"}, "update name"},
|
||||
"setbio": command{0, []string{"Lorem ipsum"}, "update about"},
|
||||
"setpassword": command{0, []string{"old", "new"}, "set or remove password"},
|
||||
"config": command{0, []string{"param", "value"}, "view or update configuration options"},
|
||||
"report": command{2, []string{"chat", "comment"}, "report a chat by id or @username"},
|
||||
"add": command{1, []string{"@username"}, "add @username to your chat list"},
|
||||
"join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname"},
|
||||
"supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»"},
|
||||
"channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»"},
|
||||
}
|
||||
|
||||
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"},
|
||||
"schedule": command{"{online | 2006-01-02T15:04:05 | 15:04:05} message", "schedules a message either to timestamp or to whenever the user goes online"},
|
||||
"forward": command{"message_id target_chat", "forwards a message"},
|
||||
"vcard": command{"", "print vCard as text"},
|
||||
"add": command{"@username", "add @username to your chat list"},
|
||||
"join": command{"https://t.me/invite_link", "join to chat via invite link or @publicname"},
|
||||
"group": command{"title", "create groupchat «title» with current user"},
|
||||
"supergroup": command{"title description", "create new supergroup «title» with «description»"},
|
||||
"channel": command{"title description", "create new channel «title» with «description»"},
|
||||
"secret": command{"", "create secretchat with current user"},
|
||||
"search": command{"string [limit]", "search <string> in current chat"},
|
||||
"history": command{"[limit]", "get last [limit] messages from current chat"},
|
||||
"block": command{"", "blacklist current user"},
|
||||
"unblock": command{"", "unblacklist current user"},
|
||||
"invite": command{"id or @username", "add user to current chat"},
|
||||
"link": command{"", "get invite link for current chat"},
|
||||
"kick": command{"id or @username", "remove user to current chat"},
|
||||
"mute": command{"id or @username [hours]", "mute user in current chat"},
|
||||
"unmute": command{"id or @username", "unrestrict user from current chat"},
|
||||
"ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"},
|
||||
"unban": command{"id or @username", "unbans @username in current chat (and devotes from admins)"},
|
||||
"promote": command{"id or @username [title]", "promote user to admin in current chat"},
|
||||
"leave": command{"", "leave current chat"},
|
||||
"leave!": command{"", "leave current chat (for owners)"},
|
||||
"ttl": command{"", "set secret chat messages TTL before self-destroying (in seconds)"},
|
||||
"close": command{"", "close current secret chat"},
|
||||
"delete": command{"", "delete current chat from chat list"},
|
||||
"members": command{"[query]", "search members [by optional query] in current chat (requires admin rights)"},
|
||||
"help": command{0, []string{}, "help"},
|
||||
"d": command{0, []string{"n"}, "delete your last message(s)"},
|
||||
"s": command{1, []string{"edited message"}, "edit your last message"},
|
||||
"silent": command{1, []string{"message"}, "send a message without sound"},
|
||||
"schedule": command{2, []string{"{online | 2006-01-02T15:04:05 | 15:04:05}", "message"}, "schedules a message either to timestamp or to whenever the user goes online"},
|
||||
"forward": command{2, []string{"message_id", "target_chat"}, "forwards a message"},
|
||||
"vcard": command{0, []string{}, "print vCard as text"},
|
||||
"add": command{1, []string{"@username"}, "add @username to your chat list"},
|
||||
"join": command{1, []string{"https://t.me/invite_link"}, "join to chat via invite link or @publicname"},
|
||||
"group": command{1, []string{"title"}, "create groupchat «title» with current user"},
|
||||
"supergroup": command{1, []string{"title", "description"}, "create new supergroup «title» with «description»"},
|
||||
"channel": command{1, []string{"title", "description"}, "create new channel «title» with «description»"},
|
||||
"secret": command{0, []string{}, "create secretchat with current user"},
|
||||
"search": command{0, []string{"string", "[limit]"}, "search <string> in current chat"},
|
||||
"history": command{0, []string{"limit"}, "get last [limit] messages from current chat"},
|
||||
"block": command{0, []string{}, "blacklist current user"},
|
||||
"unblock": command{0, []string{}, "unblacklist current user"},
|
||||
"invite": command{1, []string{"id or @username"}, "add user to current chat"},
|
||||
"link": command{0, []string{}, "get invite link for current chat"},
|
||||
"kick": command{1, []string{"id or @username"}, "remove user to current chat"},
|
||||
"mute": command{1, []string{"id or @username", "hours"}, "mute user in current chat"},
|
||||
"unmute": command{1, []string{"id or @username"}, "unrestrict user from current chat"},
|
||||
"ban": command{1, []string{"id or @username", "hours"}, "restrict @username from current chat for [hours] or forever"},
|
||||
"unban": command{1, []string{"id or @username"}, "unbans @username in current chat (and devotes from admins)"},
|
||||
"promote": command{1, []string{"id or @username", "title"}, "promote user to admin in current chat"},
|
||||
"leave": command{0, []string{}, "leave current chat"},
|
||||
"leave!": command{0, []string{}, "leave current chat (for owners)"},
|
||||
"ttl": command{0, []string{"seconds"}, "set secret chat messages TTL before self-destroying"},
|
||||
"close": command{0, []string{}, "close current secret chat"},
|
||||
"delete": command{0, []string{}, "delete current chat from chat list"},
|
||||
"members": command{0, []string{"query"}, "search members [by optional query] in current chat (requires admin rights)"},
|
||||
}
|
||||
|
||||
var transportConfigurationOptions = map[string]configurationOption{
|
||||
|
@ -107,10 +107,14 @@ var transportConfigurationOptions = map[string]configurationOption{
|
|||
}
|
||||
|
||||
type command struct {
|
||||
requiredArgs int
|
||||
arguments []string
|
||||
description string
|
||||
}
|
||||
type configurationOption struct {
|
||||
arguments string
|
||||
description string
|
||||
}
|
||||
type configurationOption command
|
||||
|
||||
// CommandType disinguishes command sets by chat
|
||||
type CommandType int
|
||||
|
@ -140,9 +144,16 @@ func CommandToHelpString(name string, cmd command) string {
|
|||
|
||||
str.WriteString("/")
|
||||
str.WriteString(name)
|
||||
if cmd.arguments != "" {
|
||||
for i, arg := range cmd.arguments {
|
||||
optional := i >= cmd.requiredArgs
|
||||
str.WriteString(" ")
|
||||
str.WriteString(cmd.arguments)
|
||||
if optional {
|
||||
str.WriteString("[")
|
||||
}
|
||||
str.WriteString(arg)
|
||||
if optional {
|
||||
str.WriteString("]")
|
||||
}
|
||||
}
|
||||
str.WriteString(" — ")
|
||||
str.WriteString(cmd.description)
|
||||
|
@ -252,16 +263,20 @@ func (c *Client) usernameOrIDToID(username string) (int64, error) {
|
|||
// and returns a response
|
||||
func (c *Client) ProcessTransportCommand(cmdline string, resource string) string {
|
||||
cmd, args := parseCommand(cmdline)
|
||||
command, ok := transportCommands[cmd]
|
||||
if !ok {
|
||||
return "Unknown command"
|
||||
}
|
||||
if len(args) < command.requiredArgs {
|
||||
return notEnoughArguments
|
||||
}
|
||||
|
||||
switch cmd {
|
||||
case "login", "code", "password":
|
||||
if cmd == "login" && c.Session.Login != "" {
|
||||
return "Phone number already provided, use /cancelauth to start over"
|
||||
}
|
||||
|
||||
if len(args) < 1 {
|
||||
return notEnoughArguments
|
||||
}
|
||||
|
||||
if cmd == "login" {
|
||||
err := c.TryLogin(resource, args[0])
|
||||
if err != nil {
|
||||
|
@ -336,11 +351,9 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
|
|||
}
|
||||
// set My Name
|
||||
case "setname":
|
||||
var firstname string
|
||||
firstname := args[0]
|
||||
var lastname string
|
||||
if len(args) > 0 {
|
||||
firstname = args[0]
|
||||
}
|
||||
|
||||
if firstname == "" {
|
||||
return "The name should contain at least one character"
|
||||
}
|
||||
|
@ -439,10 +452,6 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
|
|||
|
||||
return strings.Join(entries, "\n")
|
||||
case "report":
|
||||
if len(args) < 2 {
|
||||
return "Not enough arguments"
|
||||
}
|
||||
|
||||
contact, _, err := c.GetContactByUsername(args[0])
|
||||
if err != nil {
|
||||
return err.Error()
|
||||
|
|
Loading…
Reference in a new issue