bbgo_origin/pkg/notifier/telegramnotifier/telegram.go

109 lines
2.3 KiB
Go
Raw Normal View History

2020-12-05 06:20:27 +00:00
package telegramnotifier
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
tb "gopkg.in/tucnak/telebot.v2"
)
type Notifier struct {
Bot *tb.Bot
chatUser *tb.User
channel string
}
type NotifyOption func(notifier *Notifier)
// start bot daemon
func New(botToken, initToken string, options ...NotifyOption) *Notifier {
notifier := &Notifier{
chatUser: &tb.User{},
Bot: &tb.Bot{},
}
for _, o := range options {
o(notifier)
}
2020-12-05 06:20:27 +00:00
bot, err := tb.NewBot(tb.Settings{
// You can also set custom API URL.
// If field is empty it equals to "https://api.telegram.org".
// URL: "http://195.129.111.17:8012",
Token: botToken,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
})
if err != nil {
panic(err)
}
bot.Handle("/help", func(m *tb.Message) {
helpMsg := `
help - print help message
init - initialize telegram bot with initToken. ex. /init my-token
2020-12-06 04:34:43 +00:00
info - print information about current chat
`
bot.Send(m.Sender, helpMsg)
})
2020-12-05 06:20:27 +00:00
// init check initToken and then set sender id
bot.Handle("/init", func(m *tb.Message) {
log.Info("Receive message: ", m) //debug
if m.Payload == initToken {
notifier.chatUser = m.Sender
bot.Send(m.Sender, "Bot initialized")
2020-12-05 06:20:27 +00:00
} else {
bot.Send(m.Sender, "Error: bot intialize failed. Init token not match!")
}
})
2020-12-06 04:34:43 +00:00
bot.Handle("/info", func(m *tb.Message) {
if m.Sender.ID == notifier.chatUser.ID {
bot.Send(notifier.chatUser,
2020-12-06 04:34:43 +00:00
fmt.Sprintf("Welcome! your username: %s, user ID: %s",
notifier.chatUser.Username,
notifier.chatUser.ID,
2020-12-06 04:34:43 +00:00
))
2020-12-05 06:20:27 +00:00
} else {
log.Warningf("Incorrect user tried to access bot! sender id: %s", m.Sender)
2020-12-05 06:20:27 +00:00
}
})
notifier.Bot = bot
2020-12-05 06:20:27 +00:00
return notifier
}
func (n *Notifier) Notify(format string, args ...interface{}) {
n.NotifyTo(n.channel, format, args...)
}
func (n *Notifier) NotifyTo(channel, format string, args ...interface{}) {
2020-12-06 04:34:43 +00:00
if n.chatUser.ID == 0 {
log.Warningf("Telegram bot not initialized. Skip notification")
return
}
2020-12-05 06:20:27 +00:00
var telegramArgsOffset = -1
var nonTelegramArgs = args
if telegramArgsOffset > -1 {
nonTelegramArgs = args[:telegramArgsOffset]
}
log.Infof(format, nonTelegramArgs...)
_, err := n.Bot.Send(n.chatUser, fmt.Sprintf(format, nonTelegramArgs...))
if err != nil {
log.WithError(err).
WithField("chatUser", n.chatUser).
Errorf("telegram error: %s", err.Error())
}
return
}