2020-12-11 06:40:04 +00:00
|
|
|
package telegramnotifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/pquerna/otp"
|
2020-12-11 09:07:19 +00:00
|
|
|
"github.com/pquerna/otp/totp"
|
2020-12-11 06:40:04 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gopkg.in/tucnak/telebot.v2"
|
|
|
|
|
2021-02-20 17:01:39 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/service"
|
2020-12-11 06:40:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logrus.WithField("service", "telegram")
|
|
|
|
|
2020-12-11 09:07:19 +00:00
|
|
|
type Session struct {
|
|
|
|
Owner *telebot.User `json:"owner"`
|
2021-04-03 04:50:13 +00:00
|
|
|
Chat *telebot.Chat `json:"chat"`
|
2020-12-11 09:07:19 +00:00
|
|
|
OneTimePasswordKey *otp.Key `json:"otpKey"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSession(key *otp.Key) Session {
|
|
|
|
return Session{
|
|
|
|
Owner: nil,
|
2021-04-03 04:50:13 +00:00
|
|
|
Chat: nil,
|
2020-12-11 09:07:19 +00:00
|
|
|
OneTimePasswordKey: key,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:40:04 +00:00
|
|
|
//go:generate callbackgen -type Interaction
|
|
|
|
type Interaction struct {
|
2021-02-20 17:01:39 +00:00
|
|
|
store service.Store
|
2020-12-11 06:40:04 +00:00
|
|
|
|
2020-12-11 09:07:19 +00:00
|
|
|
bot *telebot.Bot
|
2020-12-11 06:40:04 +00:00
|
|
|
|
2020-12-11 09:07:19 +00:00
|
|
|
AuthToken string
|
|
|
|
|
|
|
|
session *Session
|
2020-12-11 06:40:04 +00:00
|
|
|
|
|
|
|
StartCallbacks []func()
|
|
|
|
AuthCallbacks []func(user *telebot.User)
|
|
|
|
}
|
|
|
|
|
2021-02-20 17:01:39 +00:00
|
|
|
func NewInteraction(bot *telebot.Bot, store service.Store) *Interaction {
|
2020-12-11 06:40:04 +00:00
|
|
|
interaction := &Interaction{
|
|
|
|
store: store,
|
|
|
|
bot: bot,
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.Handle("/help", interaction.HandleHelp)
|
|
|
|
bot.Handle("/auth", interaction.HandleAuth)
|
|
|
|
bot.Handle("/info", interaction.HandleInfo)
|
|
|
|
return interaction
|
|
|
|
}
|
|
|
|
|
2020-12-11 07:58:05 +00:00
|
|
|
func (it *Interaction) SetAuthToken(token string) {
|
|
|
|
it.AuthToken = token
|
|
|
|
}
|
|
|
|
|
2020-12-11 09:07:19 +00:00
|
|
|
func (it *Interaction) Session() *Session {
|
|
|
|
return it.session
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:40:04 +00:00
|
|
|
func (it *Interaction) HandleInfo(m *telebot.Message) {
|
2021-04-03 04:50:13 +00:00
|
|
|
if it.session.Owner == nil || it.session.Chat == nil {
|
2020-12-11 06:40:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-12-11 09:07:19 +00:00
|
|
|
if m.Sender.ID != it.session.Owner.ID {
|
2020-12-11 06:40:04 +00:00
|
|
|
log.Warningf("incorrect user tried to access bot! sender: %+v", m.Sender)
|
|
|
|
} else {
|
2021-04-03 04:50:13 +00:00
|
|
|
if _, err := it.bot.Send(it.session.Chat,
|
2020-12-11 06:40:04 +00:00
|
|
|
fmt.Sprintf("Welcome! your username: %s, user ID: %d",
|
2020-12-11 09:07:19 +00:00
|
|
|
it.session.Owner.Username,
|
|
|
|
it.session.Owner.ID,
|
2020-12-11 06:40:04 +00:00
|
|
|
)); err != nil {
|
|
|
|
log.WithError(err).Error("failed to send telegram message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *Interaction) SendToOwner(message string) {
|
2021-04-03 04:50:13 +00:00
|
|
|
if it.session.Chat == nil {
|
2020-12-11 06:40:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-04-03 04:50:13 +00:00
|
|
|
if _, err := it.bot.Send(it.session.Chat, message); err != nil {
|
2020-12-11 06:40:04 +00:00
|
|
|
log.WithError(err).Error("failed to send message to the owner")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *Interaction) HandleHelp(m *telebot.Message) {
|
|
|
|
message := `
|
|
|
|
help - show this help message
|
2020-12-11 09:07:19 +00:00
|
|
|
auth - authorize current telegram user to access telegram bot with authentication token or one-time password. ex. /auth my-token
|
2020-12-11 06:40:04 +00:00
|
|
|
info - show information about current chat
|
|
|
|
`
|
2021-04-03 04:50:13 +00:00
|
|
|
if _, err := it.bot.Send(m.Chat, message); err != nil {
|
2020-12-11 06:40:04 +00:00
|
|
|
log.WithError(err).Error("failed to send help message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *Interaction) HandleAuth(m *telebot.Message) {
|
2020-12-11 09:07:19 +00:00
|
|
|
if len(it.AuthToken) > 0 && m.Payload == it.AuthToken {
|
|
|
|
it.session.Owner = m.Sender
|
2021-04-03 04:50:13 +00:00
|
|
|
it.session.Chat = m.Chat
|
|
|
|
|
|
|
|
if _, err := it.bot.Send(m.Chat, fmt.Sprintf("Hi %s, I know you, I will send you the notifications!", m.Sender.Username)); err != nil {
|
2020-12-11 06:40:04 +00:00
|
|
|
log.WithError(err).Error("telegram send error")
|
|
|
|
}
|
|
|
|
|
2020-12-11 09:07:19 +00:00
|
|
|
if err := it.store.Save(it.session); err != nil {
|
2020-12-11 06:40:04 +00:00
|
|
|
log.WithError(err).Error("can not persist telegram chat user")
|
|
|
|
}
|
|
|
|
|
|
|
|
it.EmitAuth(m.Sender)
|
2020-12-11 09:07:19 +00:00
|
|
|
|
|
|
|
} else if it.session != nil && it.session.OneTimePasswordKey != nil {
|
|
|
|
|
|
|
|
if totp.Validate(m.Payload, it.session.OneTimePasswordKey.Secret()) {
|
|
|
|
it.session.Owner = m.Sender
|
2021-04-03 04:50:13 +00:00
|
|
|
it.session.Chat = m.Chat
|
2020-12-11 09:07:19 +00:00
|
|
|
|
2021-04-03 04:50:13 +00:00
|
|
|
if _, err := it.bot.Send(m.Chat, fmt.Sprintf("Hi %s, I know you, I will send you the notifications!", m.Sender.Username)); err != nil {
|
2020-12-11 09:07:19 +00:00
|
|
|
log.WithError(err).Error("telegram send error")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := it.store.Save(it.session); err != nil {
|
|
|
|
log.WithError(err).Error("can not persist telegram chat user")
|
|
|
|
}
|
|
|
|
|
|
|
|
it.EmitAuth(m.Sender)
|
|
|
|
|
|
|
|
} else {
|
2021-04-03 04:50:13 +00:00
|
|
|
if _, err := it.bot.Send(m.Chat, "Authorization failed. please check your auth token"); err != nil {
|
2020-12-11 09:07:19 +00:00
|
|
|
log.WithError(err).Error("telegram send error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 06:40:04 +00:00
|
|
|
} else {
|
2021-04-03 04:50:13 +00:00
|
|
|
if _, err := it.bot.Send(m.Chat, "Authorization failed. please check your auth token"); err != nil {
|
2020-12-11 06:40:04 +00:00
|
|
|
log.WithError(err).Error("telegram send error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 09:07:19 +00:00
|
|
|
func (it *Interaction) Start(session Session) {
|
|
|
|
it.session = &session
|
2020-12-11 06:40:04 +00:00
|
|
|
|
2021-04-03 04:50:13 +00:00
|
|
|
if it.session.Owner != nil && it.session.Chat != nil {
|
|
|
|
if _, err := it.bot.Send(it.session.Chat, fmt.Sprintf("Hi %s, I'm back", it.session.Owner.Username)); err != nil {
|
2020-12-11 06:40:04 +00:00
|
|
|
log.WithError(err).Error("failed to send telegram message")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it.bot.Start()
|
|
|
|
}
|