implement PlainText for telegram bot

This commit is contained in:
c9s 2020-12-11 15:58:05 +08:00
parent 6af88a2a87
commit f4ef19e5d6
6 changed files with 75 additions and 8 deletions

View File

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

View File

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

View File

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

View File

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

5
pkg/types/plaintext.go Normal file
View File

@ -0,0 +1,5 @@
package types
type PlainText interface {
PlainText() string
}

View File

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