add channel argument to the notify method

This commit is contained in:
c9s 2020-10-21 17:10:47 +08:00
parent 58265d14f9
commit 1f71fa623c
5 changed files with 15 additions and 30 deletions

View File

@ -12,6 +12,7 @@ import (
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/cmd/cmdutil" "github.com/c9s/bbgo/pkg/cmd/cmdutil"
"github.com/c9s/bbgo/pkg/config" "github.com/c9s/bbgo/pkg/config"
"github.com/c9s/bbgo/pkg/notifier/slacknotifier"
"github.com/c9s/bbgo/pkg/slack/slacklog" "github.com/c9s/bbgo/pkg/slack/slacklog"
) )
@ -55,6 +56,9 @@ var runCmd = &cobra.Command{
log.AddHook(slacklog.NewLogHook(slackToken, viper.GetString("slack-error-channel"))) log.AddHook(slacklog.NewLogHook(slackToken, viper.GetString("slack-error-channel")))
var notifier = slacknotifier.New(slackToken)
_ = notifier
db, err := cmdutil.ConnectMySQL() db, err := cmdutil.ConnectMySQL()
if err != nil { if err != nil {
return err return err

View File

@ -6,7 +6,7 @@ import (
) )
type Notifier interface { type Notifier interface {
Notify(format string, args ...interface{}) Notify(channel, format string, args ...interface{})
NotifyTrade(trade *types.Trade) NotifyTrade(trade *types.Trade)
NotifyPnL(report *pnl.AverageCostPnlReport) NotifyPnL(report *pnl.AverageCostPnlReport)
} }

View File

@ -41,7 +41,7 @@ func (m *Notifiability) AddNotifier(notifier Notifier) {
func (m *Notifiability) Notify(msg string, args ...interface{}) { func (m *Notifiability) Notify(msg string, args ...interface{}) {
for _, n := range m.notifiers { for _, n := range m.notifiers {
n.Notify(msg, args...) n.Notify("", msg, args...)
} }
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
// NewExchange constructor exchange object from viper config.
func NewExchange(n types.ExchangeName) (types.Exchange, error) { func NewExchange(n types.ExchangeName) (types.Exchange, error) {
switch n { switch n {

View File

@ -3,14 +3,9 @@ package slacknotifier
import ( import (
"context" "context"
"fmt" "fmt"
"time"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/slack-go/slack" "github.com/slack-go/slack"
"github.com/c9s/bbgo/pkg/accounting/pnl"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
) )
type SlackAttachmentCreator interface { type SlackAttachmentCreator interface {
@ -19,34 +14,15 @@ type SlackAttachmentCreator interface {
type Notifier struct { type Notifier struct {
client *slack.Client client *slack.Client
Channel string
TradeChannel string
PnlChannel string
} }
type NotifyOption func(notifier *Notifier) type NotifyOption func(notifier *Notifier)
func TradeChannel(channel string) NotifyOption { func New(token string, options ...NotifyOption) *Notifier {
return func(notifier *Notifier) {
notifier.TradeChannel = channel
}
}
func PnlChannel(channel string) NotifyOption {
return func(notifier *Notifier) {
notifier.PnlChannel = channel
}
}
func New(token string, channel string, options ...NotifyOption) *Notifier {
var client = slack.New(token, slack.OptionDebug(true)) var client = slack.New(token, slack.OptionDebug(true))
notifier := &Notifier{ notifier := &Notifier{
client: client, client: client,
Channel: channel,
TradeChannel: channel,
PnlChannel: channel,
} }
for _, o := range options { for _, o := range options {
@ -56,7 +32,7 @@ func New(token string, channel string, options ...NotifyOption) *Notifier {
return notifier return notifier
} }
func (n *Notifier) Notify(format string, args ...interface{}) { func (n *Notifier) Notify(channel, format string, args ...interface{}) {
var slackAttachments []slack.Attachment var slackAttachments []slack.Attachment
var slackArgsOffset = -1 var slackArgsOffset = -1
@ -88,7 +64,7 @@ func (n *Notifier) Notify(format string, args ...interface{}) {
logrus.Infof(format, nonSlackArgs...) logrus.Infof(format, nonSlackArgs...)
_, _, err := n.client.PostMessageContext(context.Background(), n.Channel, _, _, err := n.client.PostMessageContext(context.Background(), channel,
slack.MsgOptionText(fmt.Sprintf(format, nonSlackArgs...), true), slack.MsgOptionText(fmt.Sprintf(format, nonSlackArgs...), true),
slack.MsgOptionAttachments(slackAttachments...)) slack.MsgOptionAttachments(slackAttachments...))
if err != nil { if err != nil {
@ -96,6 +72,7 @@ func (n *Notifier) Notify(format string, args ...interface{}) {
} }
} }
/*
func (n *Notifier) NotifyTrade(trade *types.Trade) { func (n *Notifier) NotifyTrade(trade *types.Trade) {
_, _, err := n.client.PostMessageContext(context.Background(), n.TradeChannel, _, _, err := n.client.PostMessageContext(context.Background(), n.TradeChannel,
slack.MsgOptionText(util.Render(`:handshake: {{ .Symbol }} {{ .Side }} Trade Execution @ {{ .Price }}`, trade), true), slack.MsgOptionText(util.Render(`:handshake: {{ .Symbol }} {{ .Side }} Trade Execution @ {{ .Price }}`, trade), true),
@ -105,7 +82,9 @@ func (n *Notifier) NotifyTrade(trade *types.Trade) {
logrus.WithError(err).Error("slack send error") logrus.WithError(err).Error("slack send error")
} }
} }
*/
/*
func (n *Notifier) NotifyPnL(report *pnl.AverageCostPnlReport) { func (n *Notifier) NotifyPnL(report *pnl.AverageCostPnlReport) {
attachment := report.SlackAttachment() attachment := report.SlackAttachment()
@ -122,3 +101,4 @@ func (n *Notifier) NotifyPnL(report *pnl.AverageCostPnlReport) {
logrus.WithError(err).Errorf("slack send error") logrus.WithError(err).Errorf("slack send error")
} }
} }
*/