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) { 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 // send symbol options
reply.Message("Choose your position") reply.Message("Choose your position")
for _, symbol := range []string{"BTCUSDT", "ETHUSDT"} { for _, symbol := range []string{"BTCUSDT", "ETHUSDT"} {

View File

@ -51,7 +51,7 @@ func (it *AuthInteract) Commands(interact *Interact) {
it.OneTimePasswordKey = key 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") reply.Message("Enter your authentication token")
authorizer.StartAuthorizing() authorizer.StartAuthorizing()
return nil return nil
@ -82,7 +82,7 @@ func (it *AuthInteract) Commands(interact *Interact) {
return ErrAuthenticationFailed return ErrAuthenticationFailed
}) })
} else { } else {
interact.Command("/auth", func(reply Reply) error { interact.Command("/auth", "authorize", func(reply Reply) error {
reply.Message("Enter your authentication code") reply.Message("Enter your authentication code")
return nil return nil
}).NamedNext(StateAuthenticated, func(code string, reply Reply, authorizer Authorizer) error { }).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 is the command name
Name string Name string
// Desc is the command description
Desc string
// StateF is the command handler function // StateF is the command handler function
F interface{} F interface{}
@ -17,9 +20,10 @@ type Command struct {
initState, lastState State initState, lastState State
} }
func NewCommand(name string, f interface{}) *Command { func NewCommand(name, desc string, f interface{}) *Command {
c := &Command{ c := &Command{
Name: name, Name: name,
Desc: desc,
F: f, F: f,
states: make(map[State]State), states: make(map[State]State),
statesFunc: make(map[State]interface{}), statesFunc: make(map[State]interface{}),

View File

@ -37,7 +37,7 @@ type TextMessageResponder interface {
} }
type CommandResponder interface { type CommandResponder interface {
AddCommand(command string, responder Responder) AddCommand(command *Command, responder Responder)
} }
type Messenger interface { type Messenger interface {
@ -84,13 +84,13 @@ func (it *Interact) AddCustomInteraction(custom CustomInteraction) {
} }
func (it *Interact) PrivateCommand(command string, f interface{}) *Command { func (it *Interact) PrivateCommand(command string, f interface{}) *Command {
cmd := NewCommand(command, f) cmd := NewCommand(command, "", f)
it.privateCommands[command] = cmd it.privateCommands[command] = cmd
return cmd return cmd
} }
func (it *Interact) Command(command string, f interface{}) *Command { func (it *Interact) Command(command string, desc string, f interface{}) *Command {
cmd := NewCommand(command, f) cmd := NewCommand(command, desc, f)
it.commands[command] = cmd it.commands[command] = cmd
return cmd return cmd
} }
@ -200,7 +200,7 @@ func (it *Interact) SetMessenger(messenger Messenger) {
// builtin initializes the built-in commands // builtin initializes the built-in commands
func (it *Interact) builtin() error { 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) uptime := time.Since(it.startTime)
reply.Message(fmt.Sprintf("uptime %s", uptime)) reply.Message(fmt.Sprintf("uptime %s", uptime))
return nil return nil
@ -232,7 +232,7 @@ func (it *Interact) init() error {
} }
commandName := n 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) args := parseCommand(message)
return it.runCommand(commandName, args, append(ctxObjects, reply)...) return it.runCommand(commandName, args, append(ctxObjects, reply)...)
}) })

View File

@ -68,7 +68,7 @@ type TestInteraction struct {
} }
func (m *TestInteraction) Commands(interact *Interact) { func (m *TestInteraction) Commands(interact *Interact) {
interact.Command("/closePosition", func(reply Reply) error { interact.Command("/closePosition", "", func(reply Reply) error {
// send symbol options // send symbol options
return nil return nil
}).Next(func(symbol string) error { }).Next(func(symbol string) error {

View File

@ -3,6 +3,7 @@ package interact
import ( import (
"context" "context"
"fmt" "fmt"
"strings"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/tucnak/telebot.v2" "gopkg.in/tucnak/telebot.v2"
@ -78,6 +79,8 @@ type Telegram struct {
// textMessageResponder is used for interact to register its message handler // textMessageResponder is used for interact to register its message handler
textMessageResponder Responder textMessageResponder Responder
commands []*Command
} }
func (tm *Telegram) newAuthorizer(message *telebot.Message) *TelegramAuthorizer { 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() go tm.Bot.Start()
} }
func (tm *Telegram) AddCommand(command string, responder Responder) { func (tm *Telegram) AddCommand(cmd *Command, responder Responder) {
tm.Bot.Handle(command, func(m *telebot.Message) { tm.commands = append(tm.commands, cmd)
tm.Bot.Handle(cmd.Name, func(m *telebot.Message) {
authorizer := tm.newAuthorizer(m) authorizer := tm.newAuthorizer(m)
reply := tm.newReply() reply := tm.newReply()
if err := responder(m.Payload, reply, authorizer); err != nil { if err := responder(m.Payload, reply, authorizer); err != nil {