bbgo_origin/pkg/notifier/slacknotifier/slack.go

113 lines
2.6 KiB
Go
Raw Normal View History

2020-09-19 01:05:06 +00:00
package slacknotifier
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
"github.com/slack-go/slack"
)
type SlackAttachmentCreator interface {
SlackAttachment() slack.Attachment
}
type Notifier struct {
2020-10-22 02:47:54 +00:00
client *slack.Client
channel string
2020-09-19 01:05:06 +00:00
}
type NotifyOption func(notifier *Notifier)
2020-10-22 02:47:54 +00:00
func New(token, channel string, options ...NotifyOption) *Notifier {
2020-09-19 01:05:06 +00:00
var client = slack.New(token, slack.OptionDebug(true))
2020-09-19 01:05:06 +00:00
notifier := &Notifier{
2020-10-22 02:47:54 +00:00
channel: channel,
client: client,
2020-09-19 01:05:06 +00:00
}
for _, o := range options {
o(notifier)
}
return notifier
}
2020-10-22 02:47:54 +00:00
func (n *Notifier) Notify(format string, args ...interface{}) error {
return n.NotifyTo(n.channel, format, args...)
}
func (n *Notifier) NotifyTo(channel, format string, args ...interface{}) error {
2020-09-19 01:05:06 +00:00
var slackAttachments []slack.Attachment
var slackArgsOffset = -1
for idx, arg := range args {
switch a := arg.(type) {
// concrete type assert first
case slack.Attachment:
if slackArgsOffset == -1 {
slackArgsOffset = idx
}
slackAttachments = append(slackAttachments, a)
case SlackAttachmentCreator:
if slackArgsOffset == -1 {
slackArgsOffset = idx
}
slackAttachments = append(slackAttachments, a.SlackAttachment())
}
}
var nonSlackArgs = args
if slackArgsOffset > -1 {
nonSlackArgs = args[:slackArgsOffset]
}
logrus.Infof(format, nonSlackArgs...)
_, _, err := n.client.PostMessageContext(context.Background(), channel,
2020-09-19 01:05:06 +00:00
slack.MsgOptionText(fmt.Sprintf(format, nonSlackArgs...), true),
slack.MsgOptionAttachments(slackAttachments...))
if err != nil {
logrus.WithError(err).Errorf("slack error: %s", err.Error())
}
2020-10-21 11:52:55 +00:00
return err
2020-09-19 01:05:06 +00:00
}
/*
2020-09-19 01:05:06 +00:00
func (n *Notifier) NotifyTrade(trade *types.Trade) {
_, _, err := n.client.PostMessageContext(context.Background(), n.TradeChannel,
slack.MsgOptionText(util.Render(`:handshake: {{ .Symbol }} {{ .Side }} Trade Execution @ {{ .Price }}`, trade), true),
slack.MsgOptionAttachments(trade.SlackAttachment()))
if err != nil {
logrus.WithError(err).Error("slack send error")
}
}
2020-10-22 02:47:54 +00:00
*/
2020-09-19 01:05:06 +00:00
/*
2020-10-16 02:21:37 +00:00
func (n *Notifier) NotifyPnL(report *pnl.AverageCostPnlReport) {
2020-09-19 01:05:06 +00:00
attachment := report.SlackAttachment()
_, _, err := n.client.PostMessageContext(context.Background(), n.PnlChannel,
slack.MsgOptionText(util.Render(
`:heavy_dollar_sign: Here is your *{{ .symbol }}* PnL report collected since *{{ .startTime }}*`,
map[string]interface{}{
"symbol": report.Symbol,
"startTime": report.StartTime.Format(time.RFC822),
}), true),
slack.MsgOptionAttachments(attachment))
if err != nil {
logrus.WithError(err).Errorf("slack send error")
}
}
*/