mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 16:25:16 +00:00
implement PlainText for telegram bot
This commit is contained in:
parent
6af88a2a87
commit
f4ef19e5d6
|
@ -168,6 +168,7 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
interaction := telegramnotifier.NewInteraction(bot, store)
|
interaction := telegramnotifier.NewInteraction(bot, store)
|
||||||
|
interaction.SetAuthToken(telegramBotAuthToken)
|
||||||
go interaction.Start()
|
go interaction.Start()
|
||||||
|
|
||||||
log.Infof("send the following command to the bbgo bot you created to enable the notification...")
|
log.Infof("send the following command to the bbgo bot you created to enable the notification...")
|
||||||
|
|
|
@ -38,6 +38,10 @@ func NewInteraction(bot *telebot.Bot, store bbgo.Store) *Interaction {
|
||||||
return interaction
|
return interaction
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (it *Interaction) SetAuthToken(token string) {
|
||||||
|
it.AuthToken = token
|
||||||
|
}
|
||||||
|
|
||||||
func (it *Interaction) HandleInfo(m *telebot.Message) {
|
func (it *Interaction) HandleInfo(m *telebot.Message) {
|
||||||
if it.Owner == nil {
|
if it.Owner == nil {
|
||||||
return
|
return
|
||||||
|
@ -58,7 +62,6 @@ func (it *Interaction) HandleInfo(m *telebot.Message) {
|
||||||
|
|
||||||
func (it *Interaction) SendToOwner(message string) {
|
func (it *Interaction) SendToOwner(message string) {
|
||||||
if it.Owner == nil {
|
if it.Owner == nil {
|
||||||
log.Warnf("the telegram owner is authorized yet")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ package telegramnotifier
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Notifier struct {
|
type Notifier struct {
|
||||||
|
@ -28,15 +30,31 @@ func (n *Notifier) Notify(format string, args ...interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Notifier) NotifyTo(_, format string, args ...interface{}) {
|
func (n *Notifier) NotifyTo(_, format string, args ...interface{}) {
|
||||||
var telegramArgsOffset = -1
|
var textArgsOffset = -1
|
||||||
var nonTelegramArgs = args
|
var texts []string
|
||||||
|
|
||||||
if telegramArgsOffset > -1 {
|
for idx, arg := range args {
|
||||||
nonTelegramArgs = args[:telegramArgsOffset]
|
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)
|
n.interaction.SendToOwner(message)
|
||||||
|
|
||||||
|
for _, text := range texts {
|
||||||
|
n.interaction.SendToOwner(text)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,16 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/slack-go/slack"
|
"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
|
// OrderType define order type
|
||||||
type OrderType string
|
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)
|
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 {
|
func (o *SubmitOrder) SlackAttachment() slack.Attachment {
|
||||||
var fields = []slack.AttachmentField{
|
var fields = []slack.AttachmentField{
|
||||||
{Title: "Symbol", Value: o.Symbol, Short: true},
|
{Title: "Symbol", Value: o.Symbol, Short: true},
|
||||||
|
@ -101,3 +113,14 @@ type Order struct {
|
||||||
func (o Order) String() string {
|
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)
|
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
5
pkg/types/plaintext.go
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
package types
|
||||||
|
|
||||||
|
type PlainText interface {
|
||||||
|
PlainText() string
|
||||||
|
}
|
|
@ -9,6 +9,12 @@ import (
|
||||||
"github.com/c9s/bbgo/pkg/util"
|
"github.com/c9s/bbgo/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// make sure we can cast Trade to PlainText
|
||||||
|
_ = PlainText(Trade{})
|
||||||
|
_ = PlainText(&Trade{})
|
||||||
|
}
|
||||||
|
|
||||||
type Trade struct {
|
type Trade struct {
|
||||||
// GID is the global ID
|
// GID is the global ID
|
||||||
GID int64 `json:"gid" db:"gid"`
|
GID int64 `json:"gid" db:"gid"`
|
||||||
|
@ -30,6 +36,16 @@ type Trade struct {
|
||||||
FeeCurrency string `json:"feeCurrency" db:"fee_currency"`
|
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 {
|
func (trade Trade) SlackAttachment() slack.Attachment {
|
||||||
var color = "#DC143C"
|
var color = "#DC143C"
|
||||||
|
|
||||||
|
@ -43,9 +59,10 @@ func (trade Trade) SlackAttachment() slack.Attachment {
|
||||||
// Pretext: "",
|
// Pretext: "",
|
||||||
// Text: "",
|
// Text: "",
|
||||||
Fields: []slack.AttachmentField{
|
Fields: []slack.AttachmentField{
|
||||||
|
{Title: "Exchange", Value: trade.Exchange, Short: true},
|
||||||
{Title: "Price", Value: util.FormatFloat(trade.Price, 2), Short: true},
|
{Title: "Price", Value: util.FormatFloat(trade.Price, 2), Short: true},
|
||||||
{Title: "Volume", Value: util.FormatFloat(trade.Quantity, 4), 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: "Fee", Value: util.FormatFloat(trade.Fee, 4), Short: true},
|
||||||
{Title: "FeeCurrency", Value: trade.FeeCurrency, Short: true},
|
{Title: "FeeCurrency", Value: trade.FeeCurrency, Short: true},
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue
Block a user