mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 17:13:51 +00:00
Merge pull request #71 from c9s/refactor/telegram-bot
refactor: telegram bot auth with persistence
This commit is contained in:
commit
af0f45fe19
|
@ -102,6 +102,12 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config) error {
|
|||
}
|
||||
}
|
||||
|
||||
if userConfig.Persistence != nil {
|
||||
if err := environ.ConfigurePersistence(userConfig.Persistence); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
notification := bbgo.Notifiability{
|
||||
SymbolChannelRouter: bbgo.NewPatternChannelRouter(nil),
|
||||
SessionChannelRouter: bbgo.NewPatternChannelRouter(nil),
|
||||
|
@ -141,7 +147,11 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config) error {
|
|||
return err
|
||||
}
|
||||
|
||||
go bot.Start()
|
||||
var options []telegramnotifier.NotifyOption
|
||||
|
||||
if environ.PersistenceServiceFacade != nil && environ.PersistenceServiceFacade.Redis != nil {
|
||||
options = append(options, telegramnotifier.WithRedisPersistence(environ.PersistenceServiceFacade.Redis))
|
||||
}
|
||||
|
||||
log.Infof("send the following command to the bbgo bot you created to enable the notification...")
|
||||
log.Infof("===========================================")
|
||||
|
@ -149,7 +159,7 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config) error {
|
|||
log.Infof(" /auth %s", telegramAuthToken)
|
||||
log.Infof("")
|
||||
log.Infof("===========================================")
|
||||
var notifier = telegramnotifier.New(bot, telegramAuthToken)
|
||||
var notifier = telegramnotifier.New(bot, telegramAuthToken, options...)
|
||||
notification.AddNotifier(notifier)
|
||||
}
|
||||
|
||||
|
@ -161,11 +171,6 @@ func runConfig(basectx context.Context, userConfig *bbgo.Config) error {
|
|||
}
|
||||
}
|
||||
|
||||
if userConfig.Persistence != nil {
|
||||
if err := environ.ConfigurePersistence(userConfig.Persistence); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
trader := bbgo.NewTrader(environ)
|
||||
|
||||
|
|
|
@ -5,16 +5,26 @@ import (
|
|||
|
||||
log "github.com/sirupsen/logrus"
|
||||
tb "gopkg.in/tucnak/telebot.v2"
|
||||
|
||||
"github.com/c9s/bbgo/pkg/bbgo"
|
||||
)
|
||||
|
||||
type Notifier struct {
|
||||
Bot *tb.Bot
|
||||
chatUser *tb.User
|
||||
channel string
|
||||
|
||||
redis *bbgo.RedisPersistenceService
|
||||
}
|
||||
|
||||
type NotifyOption func(notifier *Notifier)
|
||||
|
||||
func WithRedisPersistence(redis *bbgo.RedisPersistenceService) NotifyOption {
|
||||
return func(notifier *Notifier) {
|
||||
notifier.redis = redis
|
||||
}
|
||||
}
|
||||
|
||||
// start bot daemon
|
||||
func New(bot *tb.Bot, authToken string, options ...NotifyOption) *Notifier {
|
||||
notifier := &Notifier{
|
||||
|
@ -26,6 +36,11 @@ func New(bot *tb.Bot, authToken string, options ...NotifyOption) *Notifier {
|
|||
o(notifier)
|
||||
}
|
||||
|
||||
store := notifier.redis.NewStore("bbgo", "telegram")
|
||||
if err := store.Load(notifier.chatUser) ; err == nil {
|
||||
bot.Send(notifier.chatUser, fmt.Sprintf("Hi %s, I'm back", notifier.chatUser.Username))
|
||||
}
|
||||
|
||||
bot.Handle("/help", func(m *tb.Message) {
|
||||
helpMsg := `
|
||||
help - print help message
|
||||
|
@ -37,12 +52,20 @@ info - print information about current chat
|
|||
|
||||
// auth check authToken and then set sender id
|
||||
bot.Handle("/auth", func(m *tb.Message) {
|
||||
log.Info("Receive message: ", m) //debug
|
||||
log.Info("receive message: ", m) //debug
|
||||
if m.Payload == authToken {
|
||||
notifier.chatUser = m.Sender
|
||||
bot.Send(m.Sender, "User authorized")
|
||||
if err := store.Save(notifier.chatUser); err != nil {
|
||||
log.WithError(err).Error("can not persist telegram chat user")
|
||||
}
|
||||
|
||||
if _, err := bot.Send(m.Sender, fmt.Sprintf("Hi %s, I know you, I will send you the notifications!", m.Sender.Username)) ; err != nil {
|
||||
log.WithError(err).Error("telegram send error")
|
||||
}
|
||||
} else {
|
||||
bot.Send(m.Sender, "Error: User authorization failed. Auth token does not match!")
|
||||
if _, err := bot.Send(m.Sender, "Authorization failed. please check your auth token") ; err != nil {
|
||||
log.WithError(err).Error("telegram send error")
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
@ -58,6 +81,8 @@ info - print information about current chat
|
|||
}
|
||||
})
|
||||
|
||||
go bot.Start()
|
||||
|
||||
notifier.Bot = bot
|
||||
return notifier
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user