interact: add command description

This commit is contained in:
c9s 2022-01-14 02:57:39 +08:00
parent 145c340d5b
commit 832faf91f8
6 changed files with 38 additions and 14 deletions

View File

@ -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"} {

View File

@ -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 {

View File

@ -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{}),

View File

@ -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)...)
})

View File

@ -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 {

View File

@ -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 {