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 {
|
2020-12-11 06:40:04 +00:00
|
|
|
interaction *Interaction
|
2020-12-05 06:20:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type NotifyOption func(notifier *Notifier)
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
// New
|
|
|
|
// TODO: register interaction with channel, so that we can route message to the specific telegram bot
|
2020-12-11 06:40:04 +00:00
|
|
|
func New(interaction *Interaction, options ...NotifyOption) *Notifier {
|
2020-12-06 05:02:21 +00:00
|
|
|
notifier := &Notifier{
|
2020-12-11 06:40:04 +00:00
|
|
|
interaction: interaction,
|
2020-12-06 05:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range options {
|
|
|
|
o(notifier)
|
|
|
|
}
|
|
|
|
|
2020-12-05 06:20:27 +00:00
|
|
|
return notifier
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func (n *Notifier) Notify(obj interface{}, args ...interface{}) {
|
|
|
|
n.NotifyTo("", obj, args...)
|
2020-12-05 06:20:27 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func filterPlaintextMessages(args []interface{}) (texts []string, pureArgs []interface{}) {
|
2021-05-15 01:59:17 +00:00
|
|
|
var firstObjectOffset = -1
|
2020-12-11 07:58:05 +00:00
|
|
|
for idx, arg := range args {
|
|
|
|
switch a := arg.(type) {
|
|
|
|
|
|
|
|
case types.PlainText:
|
|
|
|
texts = append(texts, a.PlainText())
|
2021-05-15 01:59:17 +00:00
|
|
|
if firstObjectOffset == -1 {
|
|
|
|
firstObjectOffset = idx
|
2021-05-14 06:57:22 +00:00
|
|
|
}
|
2020-12-11 07:58:05 +00:00
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
case types.Stringer:
|
|
|
|
texts = append(texts, a.String())
|
2021-05-15 01:59:17 +00:00
|
|
|
if firstObjectOffset == -1 {
|
|
|
|
firstObjectOffset = idx
|
2021-05-14 06:57:22 +00:00
|
|
|
}
|
2020-12-11 07:58:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-11 06:40:04 +00:00
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
pureArgs = args
|
2021-05-15 01:59:17 +00:00
|
|
|
if firstObjectOffset > -1 {
|
|
|
|
pureArgs = args[:firstObjectOffset]
|
2020-12-05 06:20:27 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
return texts, pureArgs
|
|
|
|
}
|
2020-12-05 06:20:27 +00:00
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
func (n *Notifier) NotifyTo(channel string, obj interface{}, args ...interface{}) {
|
|
|
|
var texts, pureArgs = filterPlaintextMessages(args)
|
|
|
|
var message string
|
|
|
|
|
|
|
|
switch a := obj.(type) {
|
|
|
|
|
|
|
|
case string:
|
|
|
|
log.Infof(a, pureArgs...)
|
|
|
|
message = fmt.Sprintf(a, pureArgs...)
|
2020-12-11 07:58:05 +00:00
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
case types.PlainText:
|
|
|
|
message = a.PlainText()
|
|
|
|
|
2021-05-15 17:21:35 +00:00
|
|
|
case types.Stringer:
|
|
|
|
message = a.String()
|
|
|
|
|
2021-05-12 04:37:48 +00:00
|
|
|
default:
|
|
|
|
log.Errorf("unsupported notification format: %T %+v", a, a)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|