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

View File

@ -157,7 +157,9 @@ func (tm *Telegram) Start(context.Context) {
if reply.set { if reply.set {
reply.build() reply.build()
checkSendErr(tm.Bot.Send(m.Chat, reply.message, reply.menu)) if len(reply.message) > 0 || reply.menu != nil {
checkSendErr(tm.Bot.Send(m.Chat, reply.message, reply.menu))
}
} }
}) })