2020-12-05 06:20:27 +00:00
|
|
|
package telegramnotifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
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
|
2020-12-08 06:20:49 +00:00
|
|
|
func New(bot *tb.Bot, authToken string, options ...NotifyOption) *Notifier {
|
2020-12-06 05:02:21 +00:00
|
|
|
notifier := &Notifier{
|
|
|
|
chatUser: &tb.User{},
|
2020-12-08 06:20:49 +00:00
|
|
|
Bot: bot,
|
2020-12-06 05:02:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, o := range options {
|
|
|
|
o(notifier)
|
|
|
|
}
|
|
|
|
|
2020-12-06 04:11:27 +00:00
|
|
|
bot.Handle("/help", func(m *tb.Message) {
|
|
|
|
helpMsg := `
|
|
|
|
help - print help message
|
2020-12-06 05:59:47 +00:00
|
|
|
auth - authorize current telegram user to access telegram bot with authToken. ex. /auth my-token
|
2020-12-06 04:34:43 +00:00
|
|
|
info - print information about current chat
|
2020-12-06 04:11:27 +00:00
|
|
|
`
|
|
|
|
bot.Send(m.Sender, helpMsg)
|
|
|
|
})
|
|
|
|
|
2020-12-06 05:59:47 +00:00
|
|
|
// auth check authToken and then set sender id
|
|
|
|
bot.Handle("/auth", func(m *tb.Message) {
|
2020-12-06 04:11:27 +00:00
|
|
|
log.Info("Receive message: ", m) //debug
|
2020-12-06 05:59:47 +00:00
|
|
|
if m.Payload == authToken {
|
2020-12-06 05:02:21 +00:00
|
|
|
notifier.chatUser = m.Sender
|
2020-12-06 05:59:47 +00:00
|
|
|
bot.Send(m.Sender, "User authorized")
|
2020-12-05 06:20:27 +00:00
|
|
|
} else {
|
2020-12-06 05:59:47 +00:00
|
|
|
bot.Send(m.Sender, "Error: User authorization failed. Auth token does not match!")
|
2020-12-05 06:20:27 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-12-06 04:34:43 +00:00
|
|
|
bot.Handle("/info", func(m *tb.Message) {
|
2020-12-06 05:02:21 +00:00
|
|
|
if m.Sender.ID == notifier.chatUser.ID {
|
|
|
|
bot.Send(notifier.chatUser,
|
2020-12-06 05:47:52 +00:00
|
|
|
fmt.Sprintf("Welcome! your username: %s, user ID: %d",
|
2020-12-06 05:02:21 +00:00
|
|
|
notifier.chatUser.Username,
|
|
|
|
notifier.chatUser.ID,
|
2020-12-06 04:34:43 +00:00
|
|
|
))
|
2020-12-05 06:20:27 +00:00
|
|
|
} else {
|
2020-12-06 05:47:52 +00:00
|
|
|
log.Warningf("Incorrect user tried to access bot! sender username: %s id: %d", m.Sender.Username, m.Sender.ID)
|
2020-12-05 06:20:27 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-12-06 05:02:21 +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 {
|
2020-12-06 05:59:47 +00:00
|
|
|
log.Warningf("Telegram bot has no authenticated user. Skip notification")
|
2020-12-06 04:34:43 +00:00
|
|
|
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
|
|
|
|
}
|