bbgo_origin/pkg/notifier/telegramnotifier/telegram.go

61 lines
1.0 KiB
Go
Raw Normal View History

2020-12-05 06:20:27 +00:00
package telegramnotifier
import (
"fmt"
2020-12-11 07:58:05 +00:00
"github.com/c9s/bbgo/pkg/types"
2020-12-05 06:20:27 +00:00
)
type Notifier struct {
interaction *Interaction
2020-12-05 06:20:27 +00:00
}
type NotifyOption func(notifier *Notifier)
// start bot daemon
func New(interaction *Interaction, options ...NotifyOption) *Notifier {
notifier := &Notifier{
interaction: interaction,
}
for _, o := range options {
o(notifier)
}
2020-12-05 06:20:27 +00:00
return notifier
}
func (n *Notifier) Notify(format string, args ...interface{}) {
n.NotifyTo("", format, args...)
2020-12-05 06:20:27 +00:00
}
func (n *Notifier) NotifyTo(_, format string, args ...interface{}) {
2020-12-11 07:58:05 +00:00
var textArgsOffset = -1
var texts []string
for idx, arg := range args {
switch a := arg.(type) {
case types.PlainText:
texts = append(texts, a.PlainText())
textArgsOffset = idx
}
}
2020-12-11 07:58:05 +00:00
var simpleArgs = args
if textArgsOffset > -1 {
simpleArgs = args[:textArgsOffset]
2020-12-05 06:20:27 +00:00
}
2020-12-11 07:58:05 +00:00
log.Infof(format, simpleArgs...)
2020-12-05 06:20:27 +00:00
2020-12-11 07:58:05 +00:00
message := fmt.Sprintf(format, simpleArgs...)
n.interaction.SendToOwner(message)
2020-12-11 07:58:05 +00:00
for _, text := range texts {
n.interaction.SendToOwner(text)
}
2020-12-05 06:20:27 +00:00
}