diff --git a/examples/interact/main.go b/examples/interact/main.go index 8524b4887..ce696bd31 100644 --- a/examples/interact/main.go +++ b/examples/interact/main.go @@ -41,7 +41,7 @@ type PositionInteraction struct { } func (m *PositionInteraction) Commands(i *interact.Interact) { - i.Command("/closePosition", func(reply interact.Reply) error { + i.Command("/closePosition", "", func(reply interact.Reply) error { // send symbol options reply.Message("Choose your position") for _, symbol := range []string{"BTCUSDT", "ETHUSDT"} { diff --git a/pkg/interact/auth.go b/pkg/interact/auth.go index c984247b8..823029c9b 100644 --- a/pkg/interact/auth.go +++ b/pkg/interact/auth.go @@ -51,7 +51,7 @@ func (it *AuthInteract) Commands(interact *Interact) { it.OneTimePasswordKey = key } - interact.Command("/auth", func(reply Reply, authorizer Authorizer) error { + interact.Command("/auth", "authorize", func(reply Reply, authorizer Authorizer) error { reply.Message("Enter your authentication token") authorizer.StartAuthorizing() return nil @@ -82,7 +82,7 @@ func (it *AuthInteract) Commands(interact *Interact) { return ErrAuthenticationFailed }) } else { - interact.Command("/auth", func(reply Reply) error { + interact.Command("/auth", "authorize", func(reply Reply) error { reply.Message("Enter your authentication code") return nil }).NamedNext(StateAuthenticated, func(code string, reply Reply, authorizer Authorizer) error { diff --git a/pkg/interact/command.go b/pkg/interact/command.go index 3487f4e50..08b11223a 100644 --- a/pkg/interact/command.go +++ b/pkg/interact/command.go @@ -8,6 +8,9 @@ type Command struct { // Name is the command name Name string + // Desc is the command description + Desc string + // StateF is the command handler function F interface{} @@ -17,9 +20,10 @@ type Command struct { initState, lastState State } -func NewCommand(name string, f interface{}) *Command { +func NewCommand(name, desc string, f interface{}) *Command { c := &Command{ Name: name, + Desc: desc, F: f, states: make(map[State]State), statesFunc: make(map[State]interface{}), diff --git a/pkg/interact/interact.go b/pkg/interact/interact.go index 707331616..a034a6045 100644 --- a/pkg/interact/interact.go +++ b/pkg/interact/interact.go @@ -37,7 +37,7 @@ type TextMessageResponder interface { } type CommandResponder interface { - AddCommand(command string, responder Responder) + AddCommand(command *Command, responder Responder) } type Messenger interface { @@ -84,13 +84,13 @@ func (it *Interact) AddCustomInteraction(custom CustomInteraction) { } func (it *Interact) PrivateCommand(command string, f interface{}) *Command { - cmd := NewCommand(command, f) + cmd := NewCommand(command, "", f) it.privateCommands[command] = cmd return cmd } -func (it *Interact) Command(command string, f interface{}) *Command { - cmd := NewCommand(command, f) +func (it *Interact) Command(command string, desc string, f interface{}) *Command { + cmd := NewCommand(command, desc, f) it.commands[command] = cmd return cmd } @@ -200,7 +200,7 @@ func (it *Interact) SetMessenger(messenger Messenger) { // builtin initializes the built-in commands func (it *Interact) builtin() error { - it.Command("/uptime", func(reply Reply) error { + it.Command("/uptime", "show bot uptime", func(reply Reply) error { uptime := time.Since(it.startTime) reply.Message(fmt.Sprintf("uptime %s", uptime)) return nil @@ -232,7 +232,7 @@ func (it *Interact) init() error { } commandName := n - it.messenger.AddCommand(commandName, func(message string, reply Reply, ctxObjects ...interface{}) error { + it.messenger.AddCommand(cmd, func(message string, reply Reply, ctxObjects ...interface{}) error { args := parseCommand(message) return it.runCommand(commandName, args, append(ctxObjects, reply)...) }) diff --git a/pkg/interact/interact_test.go b/pkg/interact/interact_test.go index 15065f1ed..153bc9470 100644 --- a/pkg/interact/interact_test.go +++ b/pkg/interact/interact_test.go @@ -68,7 +68,7 @@ type TestInteraction struct { } func (m *TestInteraction) Commands(interact *Interact) { - interact.Command("/closePosition", func(reply Reply) error { + interact.Command("/closePosition", "", func(reply Reply) error { // send symbol options return nil }).Next(func(symbol string) error { diff --git a/pkg/interact/telegram.go b/pkg/interact/telegram.go index e38763cd1..8e53f0842 100644 --- a/pkg/interact/telegram.go +++ b/pkg/interact/telegram.go @@ -3,6 +3,7 @@ package interact import ( "context" "fmt" + "strings" log "github.com/sirupsen/logrus" "gopkg.in/tucnak/telebot.v2" @@ -13,7 +14,7 @@ type TelegramReply struct { message string menu *telebot.ReplyMarkup buttons [][]telebot.Btn - set bool + set bool } func (r *TelegramReply) Message(message string) { @@ -78,6 +79,8 @@ type Telegram struct { // textMessageResponder is used for interact to register its message handler textMessageResponder Responder + + commands []*Command } func (tm *Telegram) newAuthorizer(message *telebot.Message) *TelegramAuthorizer { @@ -121,11 +124,28 @@ func (tm *Telegram) Start(context.Context) { } } }) + + var cmdList []telebot.Command + for _, cmd := range tm.commands { + if len(cmd.Desc) == 0 { + continue + } + + cmdList = append(cmdList, telebot.Command{ + Text: strings.ToLower(strings.TrimLeft(cmd.Name, "/")), + Description: cmd.Desc, + }) + } + if err := tm.Bot.SetCommands(cmdList); err != nil { + log.WithError(err).Errorf("[telegram] set commands error") + } + go tm.Bot.Start() } -func (tm *Telegram) AddCommand(command string, responder Responder) { - tm.Bot.Handle(command, func(m *telebot.Message) { +func (tm *Telegram) AddCommand(cmd *Command, responder Responder) { + tm.commands = append(tm.commands, cmd) + tm.Bot.Handle(cmd.Name, func(m *telebot.Message) { authorizer := tm.newAuthorizer(m) reply := tm.newReply() if err := responder(m.Payload, reply, authorizer); err != nil {