2020-12-05 06:20:27 +00:00
|
|
|
package telegramnotifier
|
|
|
|
|
|
|
|
import (
|
2022-08-03 06:10:56 +00:00
|
|
|
"bytes"
|
2022-10-17 09:38:03 +00:00
|
|
|
"context"
|
2020-12-05 06:20:27 +00:00
|
|
|
"fmt"
|
2022-06-13 03:29:33 +00:00
|
|
|
"reflect"
|
2022-01-14 03:37:23 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2022-01-14 07:03:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2022-10-17 09:38:03 +00:00
|
|
|
"golang.org/x/time/rate"
|
2022-01-14 03:37:23 +00:00
|
|
|
"gopkg.in/tucnak/telebot.v2"
|
2020-12-11 07:58:05 +00:00
|
|
|
|
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2020-12-05 06:20:27 +00:00
|
|
|
)
|
|
|
|
|
2022-10-19 08:59:00 +00:00
|
|
|
var apiLimiter = rate.NewLimiter(rate.Every(50*time.Millisecond), 20)
|
2022-10-17 09:38:03 +00:00
|
|
|
|
2022-01-14 07:03:19 +00:00
|
|
|
var log = logrus.WithField("service", "telegram")
|
|
|
|
|
2022-10-17 09:38:03 +00:00
|
|
|
type notifyTask struct {
|
|
|
|
message string
|
|
|
|
texts []string
|
|
|
|
photoBuffer *bytes.Buffer
|
|
|
|
}
|
|
|
|
|
2020-12-05 06:20:27 +00:00
|
|
|
type Notifier struct {
|
2022-01-14 16:18:07 +00:00
|
|
|
bot *telebot.Bot
|
2022-01-14 03:37:23 +00:00
|
|
|
|
2022-01-15 16:50:43 +00:00
|
|
|
// Subscribers stores the Chat objects for broadcasting public notification
|
|
|
|
Subscribers map[int64]time.Time `json:"subscribers"`
|
2022-01-14 03:37:23 +00:00
|
|
|
|
2022-01-15 16:50:43 +00:00
|
|
|
// Chats are the private chats that we will send private notification
|
|
|
|
Chats map[int64]*telebot.Chat `json:"chats"`
|
2022-01-14 03:37:23 +00:00
|
|
|
|
|
|
|
broadcast bool
|
2022-10-17 09:38:03 +00:00
|
|
|
|
|
|
|
taskC chan notifyTask
|
2020-12-05 06:20:27 +00:00
|
|
|
}
|
|
|
|
|
2021-10-15 08:10:25 +00:00
|
|
|
type Option func(notifier *Notifier)
|
|
|
|
|
|
|
|
func UseBroadcast() Option {
|
|
|
|
return func(notifier *Notifier) {
|
|
|
|
notifier.broadcast = true
|
|
|
|
}
|
|
|
|
}
|
2020-12-05 06:20:27 +00:00
|
|
|
|
2022-09-11 16:29:12 +00:00
|
|
|
// New returns a telegram notifier instance
|
2022-01-14 16:18:07 +00:00
|
|
|
func New(bot *telebot.Bot, options ...Option) *Notifier {
|
2022-01-15 16:50:43 +00:00
|
|
|
notifier := &Notifier{
|
|
|
|
bot: bot,
|
|
|
|
Chats: make(map[int64]*telebot.Chat),
|
|
|
|
Subscribers: make(map[int64]time.Time),
|
2022-10-17 09:38:03 +00:00
|
|
|
taskC: make(chan notifyTask, 100),
|
2022-01-15 16:50:43 +00:00
|
|
|
}
|
2020-12-06 05:02:21 +00:00
|
|
|
|
|
|
|
for _, o := range options {
|
|
|
|
o(notifier)
|
|
|
|
}
|
|
|
|
|
2022-10-17 09:38:03 +00:00
|
|
|
go notifier.worker()
|
|
|
|
|
2020-12-05 06:20:27 +00:00
|
|
|
return notifier
|
|
|
|
}
|
|
|
|
|
2022-10-17 09:38:03 +00:00
|
|
|
func (n *Notifier) worker() {
|
|
|
|
ctx := context.Background()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case task := <-n.taskC:
|
|
|
|
apiLimiter.Wait(ctx)
|
|
|
|
n.consume(task)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notifier) consume(task notifyTask) {
|
|
|
|
if n.broadcast {
|
|
|
|
if n.Subscribers == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if task.message != "" {
|
|
|
|
n.Broadcast(task.message)
|
|
|
|
}
|
|
|
|
for _, text := range task.texts {
|
|
|
|
n.Broadcast(text)
|
|
|
|
}
|
|
|
|
if task.photoBuffer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for chatID := range n.Subscribers {
|
|
|
|
chat, err := n.bot.ChatByID(strconv.FormatInt(chatID, 10))
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("can not get chat by ID")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
album := telebot.Album{
|
|
|
|
photoFromBuffer(task.photoBuffer),
|
|
|
|
}
|
|
|
|
if _, err := n.bot.SendAlbum(chat, album); err != nil {
|
|
|
|
log.WithError(err).Error("failed to send message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if n.Chats != nil {
|
|
|
|
for _, chat := range n.Chats {
|
|
|
|
if task.message != "" {
|
|
|
|
if _, err := n.bot.Send(chat, task.message); err != nil {
|
|
|
|
log.WithError(err).Error("telegram send error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, text := range task.texts {
|
|
|
|
if _, err := n.bot.Send(chat, text); err != nil {
|
|
|
|
log.WithError(err).Error("telegram send error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if task.photoBuffer != nil {
|
|
|
|
album := telebot.Album{
|
|
|
|
photoFromBuffer(task.photoBuffer),
|
|
|
|
}
|
|
|
|
if _, err := n.bot.SendAlbum(chat, album); err != nil {
|
|
|
|
log.WithError(err).Error("telegram send error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-06-13 03:29:33 +00:00
|
|
|
rt := reflect.TypeOf(arg)
|
|
|
|
if rt.Kind() == reflect.Ptr {
|
|
|
|
switch a := arg.(type) {
|
2022-06-13 04:03:31 +00:00
|
|
|
|
|
|
|
case nil:
|
|
|
|
texts = append(texts, "nil")
|
|
|
|
if firstObjectOffset == -1 {
|
|
|
|
firstObjectOffset = idx
|
|
|
|
}
|
|
|
|
|
2022-06-13 03:29:33 +00:00
|
|
|
case types.PlainText:
|
|
|
|
texts = append(texts, a.PlainText())
|
|
|
|
if firstObjectOffset == -1 {
|
|
|
|
firstObjectOffset = idx
|
|
|
|
}
|
|
|
|
|
|
|
|
case types.Stringer:
|
|
|
|
texts = append(texts, a.String())
|
|
|
|
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:
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-17 09:38:03 +00:00
|
|
|
select {
|
|
|
|
case n.taskC <- notifyTask{
|
|
|
|
texts: texts,
|
|
|
|
message: message,
|
|
|
|
}:
|
|
|
|
default:
|
|
|
|
log.Error("[telegram] cannot send task to notify")
|
2022-01-14 03:37:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 06:10:56 +00:00
|
|
|
func (n *Notifier) SendPhoto(buffer *bytes.Buffer) {
|
|
|
|
n.SendPhotoTo("", buffer)
|
|
|
|
}
|
|
|
|
|
|
|
|
func photoFromBuffer(buffer *bytes.Buffer) telebot.InputMedia {
|
|
|
|
reader := bytes.NewReader(buffer.Bytes())
|
|
|
|
return &telebot.Photo{
|
|
|
|
File: telebot.FromReader(reader),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notifier) SendPhotoTo(channel string, buffer *bytes.Buffer) {
|
2022-10-17 09:38:03 +00:00
|
|
|
select {
|
|
|
|
case n.taskC <- notifyTask{
|
|
|
|
photoBuffer: buffer,
|
|
|
|
}:
|
|
|
|
case <-time.After(50 * time.Millisecond):
|
|
|
|
return
|
2022-08-03 06:10:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:50:43 +00:00
|
|
|
func (n *Notifier) AddChat(c *telebot.Chat) {
|
2022-01-15 17:00:15 +00:00
|
|
|
if n.Chats == nil {
|
|
|
|
n.Chats = make(map[int64]*telebot.Chat)
|
|
|
|
}
|
2022-01-15 16:50:43 +00:00
|
|
|
n.Chats[c.ID] = c
|
|
|
|
}
|
|
|
|
|
2022-01-14 03:37:23 +00:00
|
|
|
func (n *Notifier) AddSubscriber(m *telebot.Message) {
|
2022-01-14 16:18:07 +00:00
|
|
|
if n.Subscribers == nil {
|
|
|
|
n.Subscribers = make(map[int64]time.Time)
|
2022-01-14 03:37:23 +00:00
|
|
|
}
|
|
|
|
|
2022-01-14 16:18:07 +00:00
|
|
|
n.Subscribers[m.Chat.ID] = m.Time()
|
2022-01-14 03:37:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notifier) Broadcast(message string) {
|
2022-01-14 16:18:07 +00:00
|
|
|
if n.Subscribers == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for chatID := range n.Subscribers {
|
|
|
|
chat, err := n.bot.ChatByID(strconv.FormatInt(chatID, 10))
|
2022-01-14 03:37:23 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("can not get chat by ID")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-01-14 16:18:07 +00:00
|
|
|
if _, err := n.bot.Send(chat, message); err != nil {
|
2022-01-14 03:37:23 +00:00
|
|
|
log.WithError(err).Error("failed to send message")
|
2021-10-15 10:01:11 +00:00
|
|
|
}
|
2020-12-11 07:58:05 +00:00
|
|
|
}
|
2020-12-05 06:20:27 +00:00
|
|
|
}
|