Merge pull request #946 from c9s/fix/telegram-error

bbgo: fix telegram message error, there must be one message to send
This commit is contained in:
Yo-An Lin 2022-09-14 10:57:10 +08:00 committed by GitHub
commit cfeb0ba97b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 23 deletions

View File

@ -145,15 +145,13 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
}
position := reader.CurrentPosition()
if position != nil {
reply.Send("Your current position:")
reply.Send(position.PlainText())
if position.Base.IsZero() {
if position == nil || position.Base.IsZero() {
reply.Message(fmt.Sprintf("Strategy %q has no opened position", signature))
return fmt.Errorf("strategy %T has no opened position", strategy)
}
}
reply.Send("Your current position:")
reply.Message(position.PlainText())
if kc, ok := reply.(interact.KeyboardController); ok {
kc.RemoveKeyboard()
@ -285,6 +283,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
}
return nil
}).Next(func(signature string, reply interact.Reply) error {
defer func() {
if kc, ok := reply.(interact.KeyboardController); ok {
kc.RemoveKeyboard()
}
}()
strategy, ok := it.exchangeStrategies[signature]
if !ok {
reply.Message("Strategy not found")
@ -299,10 +303,6 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
status := controller.GetStatus()
if kc, ok := reply.(interact.KeyboardController); ok {
kc.RemoveKeyboard()
}
if status == types.StrategyStatusRunning {
reply.Message(fmt.Sprintf("Strategy %s is running.", signature))
} else if status == types.StrategyStatusStopped {
@ -323,6 +323,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
}
return nil
}).Next(func(signature string, reply interact.Reply) error {
defer func() {
if kc, ok := reply.(interact.KeyboardController); ok {
kc.RemoveKeyboard()
}
}()
strategy, ok := it.exchangeStrategies[signature]
if !ok {
reply.Message("Strategy not found")
@ -341,16 +347,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
return nil
}
if kc, ok := reply.(interact.KeyboardController); ok {
kc.RemoveKeyboard()
}
if err := controller.Suspend(); err != nil {
reply.Message(fmt.Sprintf("Failed to suspend the strategy, %s", err.Error()))
return err
}
reply.Message(fmt.Sprintf("Strategy %s suspended.", signature))
reply.Message(fmt.Sprintf("Strategy %s is now suspended.", signature))
return nil
})
@ -365,6 +367,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
}
return nil
}).Next(func(signature string, reply interact.Reply) error {
defer func() {
if kc, ok := reply.(interact.KeyboardController); ok {
kc.RemoveKeyboard()
}
}()
strategy, ok := it.exchangeStrategies[signature]
if !ok {
reply.Message("Strategy not found")
@ -383,16 +391,12 @@ func (it *CoreInteraction) Commands(i *interact.Interact) {
return nil
}
if kc, ok := reply.(interact.KeyboardController); ok {
kc.RemoveKeyboard()
}
if err := controller.Resume(); err != nil {
reply.Message(fmt.Sprintf("Failed to resume the strategy, %s", err.Error()))
return err
}
reply.Message(fmt.Sprintf("Strategy %s resumed.", signature))
reply.Message(fmt.Sprintf("Strategy %s is now resumed.", signature))
return nil
})

View File

@ -157,8 +157,10 @@ func (tm *Telegram) Start(context.Context) {
if reply.set {
reply.build()
if len(reply.message) > 0 || reply.menu != nil {
checkSendErr(tm.Bot.Send(m.Chat, reply.message, reply.menu))
}
}
})
var cmdList []telebot.Command