From f4ef19e5d6ae08ab2aeb21b6dd2c1456575b2f25 Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 11 Dec 2020 15:58:05 +0800 Subject: [PATCH] implement PlainText for telegram bot --- pkg/cmd/run.go | 1 + pkg/notifier/telegramnotifier/interaction.go | 5 +++- pkg/notifier/telegramnotifier/telegram.go | 30 ++++++++++++++++---- pkg/types/order.go | 23 +++++++++++++++ pkg/types/plaintext.go | 5 ++++ pkg/types/trade.go | 19 ++++++++++++- 6 files changed, 75 insertions(+), 8 deletions(-) create mode 100644 pkg/types/plaintext.go diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index b1382a3d9..b27580692 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -168,6 +168,7 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config) error { } interaction := telegramnotifier.NewInteraction(bot, store) + interaction.SetAuthToken(telegramBotAuthToken) go interaction.Start() log.Infof("send the following command to the bbgo bot you created to enable the notification...") diff --git a/pkg/notifier/telegramnotifier/interaction.go b/pkg/notifier/telegramnotifier/interaction.go index 7df72c002..191f0f41d 100644 --- a/pkg/notifier/telegramnotifier/interaction.go +++ b/pkg/notifier/telegramnotifier/interaction.go @@ -38,6 +38,10 @@ func NewInteraction(bot *telebot.Bot, store bbgo.Store) *Interaction { return interaction } +func (it *Interaction) SetAuthToken(token string) { + it.AuthToken = token +} + func (it *Interaction) HandleInfo(m *telebot.Message) { if it.Owner == nil { return @@ -58,7 +62,6 @@ func (it *Interaction) HandleInfo(m *telebot.Message) { func (it *Interaction) SendToOwner(message string) { if it.Owner == nil { - log.Warnf("the telegram owner is authorized yet") return } diff --git a/pkg/notifier/telegramnotifier/telegram.go b/pkg/notifier/telegramnotifier/telegram.go index b202f9472..feb431b78 100644 --- a/pkg/notifier/telegramnotifier/telegram.go +++ b/pkg/notifier/telegramnotifier/telegram.go @@ -2,6 +2,8 @@ package telegramnotifier import ( "fmt" + + "github.com/c9s/bbgo/pkg/types" ) type Notifier struct { @@ -28,15 +30,31 @@ func (n *Notifier) Notify(format string, args ...interface{}) { } func (n *Notifier) NotifyTo(_, format string, args ...interface{}) { - var telegramArgsOffset = -1 - var nonTelegramArgs = args + var textArgsOffset = -1 + var texts []string - if telegramArgsOffset > -1 { - nonTelegramArgs = args[:telegramArgsOffset] + for idx, arg := range args { + switch a := arg.(type) { + + case types.PlainText: + texts = append(texts, a.PlainText()) + textArgsOffset = idx + + } } - log.Infof(format, nonTelegramArgs...) + var simpleArgs = args + if textArgsOffset > -1 { + simpleArgs = args[:textArgsOffset] + } - message := fmt.Sprintf(format, nonTelegramArgs...) + log.Infof(format, simpleArgs...) + + message := fmt.Sprintf(format, simpleArgs...) n.interaction.SendToOwner(message) + + for _, text := range texts { + n.interaction.SendToOwner(text) + } + } diff --git a/pkg/types/order.go b/pkg/types/order.go index cdd8bf0f5..fe948e80e 100644 --- a/pkg/types/order.go +++ b/pkg/types/order.go @@ -5,8 +5,16 @@ import ( "time" "github.com/slack-go/slack" + + "github.com/c9s/bbgo/pkg/util" ) +func init() { + // make sure we can cast Order to PlainText + _ = PlainText(Order{}) + _ = PlainText(&Order{}) +} + // OrderType define order type type OrderType string @@ -66,6 +74,10 @@ func (o *SubmitOrder) String() string { return fmt.Sprintf("SubmitOrder %s %s %s %f @ %f", o.Symbol, o.Type, o.Side, o.Quantity, o.Price) } +func (o *SubmitOrder) PlainText() string { + return fmt.Sprintf("SubmitOrder %s %s %s %f @ %f", o.Symbol, o.Type, o.Side, o.Quantity, o.Price) +} + func (o *SubmitOrder) SlackAttachment() slack.Attachment { var fields = []slack.AttachmentField{ {Title: "Symbol", Value: o.Symbol, Short: true}, @@ -101,3 +113,14 @@ type Order struct { func (o Order) String() string { return fmt.Sprintf("order %s %f/%f at %f -> %s", o.Side, o.ExecutedQuantity, o.Quantity, o.Price, o.Status) } + +func (o Order) PlainText() string { + return fmt.Sprintf("%s %s Order %s %s price %s, quantity %s/%s status %s", + o.Exchange, + o.Type, + o.Symbol, + o.Side, + util.FormatFloat(o.Price, 2), + util.FormatFloat(o.ExecutedQuantity, 2), + util.FormatFloat(o.Quantity, 4), o.Status) +} diff --git a/pkg/types/plaintext.go b/pkg/types/plaintext.go new file mode 100644 index 000000000..afcb6602a --- /dev/null +++ b/pkg/types/plaintext.go @@ -0,0 +1,5 @@ +package types + +type PlainText interface { + PlainText() string +} diff --git a/pkg/types/trade.go b/pkg/types/trade.go index 8f44ebe14..a86dc4b9d 100644 --- a/pkg/types/trade.go +++ b/pkg/types/trade.go @@ -9,6 +9,12 @@ import ( "github.com/c9s/bbgo/pkg/util" ) +func init() { + // make sure we can cast Trade to PlainText + _ = PlainText(Trade{}) + _ = PlainText(&Trade{}) +} + type Trade struct { // GID is the global ID GID int64 `json:"gid" db:"gid"` @@ -30,6 +36,16 @@ type Trade struct { FeeCurrency string `json:"feeCurrency" db:"fee_currency"` } +func (trade Trade) PlainText() string { + return fmt.Sprintf("%s Trade %s %s price %s, quantity %s, amount %s", + trade.Exchange, + trade.Symbol, + trade.Side, + util.FormatFloat(trade.Price, 2), + util.FormatFloat(trade.Quantity, 4), + util.FormatFloat(trade.QuoteQuantity, 2)) +} + func (trade Trade) SlackAttachment() slack.Attachment { var color = "#DC143C" @@ -43,9 +59,10 @@ func (trade Trade) SlackAttachment() slack.Attachment { // Pretext: "", // Text: "", Fields: []slack.AttachmentField{ + {Title: "Exchange", Value: trade.Exchange, Short: true}, {Title: "Price", Value: util.FormatFloat(trade.Price, 2), Short: true}, {Title: "Volume", Value: util.FormatFloat(trade.Quantity, 4), Short: true}, - {Title: "Amount", Value: util.FormatFloat(trade.QuoteQuantity, 1)}, + {Title: "Amount", Value: util.FormatFloat(trade.QuoteQuantity, 2)}, {Title: "Fee", Value: util.FormatFloat(trade.Fee, 4), Short: true}, {Title: "FeeCurrency", Value: trade.FeeCurrency, Short: true}, },