From dea783589a48bcaaeb0af90bb685aaa3fb9d9db9 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 3 Nov 2024 10:38:14 +0100 Subject: [PATCH 1/4] Upgrade telegram api --- go.mod | 2 +- go.sum | 4 +-- push/config.go | 39 ++++++++++------------------ push/telegram.go | 67 +++++++++++++++++++++++++----------------------- 4 files changed, 51 insertions(+), 61 deletions(-) diff --git a/go.mod b/go.mod index 63f60b33d5..6ce5b6ce5d 100644 --- a/go.mod +++ b/go.mod @@ -35,7 +35,7 @@ require ( github.com/glebarez/sqlite v1.11.0 github.com/go-http-utils/etag v0.0.0-20161124023236-513ea8f21eb1 github.com/go-playground/validator/v10 v10.22.1 - github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 + github.com/go-telegram/bot v1.10.0 github.com/go-viper/mapstructure/v2 v2.2.1 github.com/godbus/dbus/v5 v5.1.0 github.com/gokrazy/updater v0.0.0-20240113102150-4ac511a17e33 diff --git a/go.sum b/go.sum index b8dee5c050..35ad3e1dd1 100644 --- a/go.sum +++ b/go.sum @@ -209,8 +209,8 @@ github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/me github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= -github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= -github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= +github.com/go-telegram/bot v1.10.0 h1:/Y9ZupcZhYQiAOFE1ETCF8P4836XE42nDkLcWHYTg3g= +github.com/go-telegram/bot v1.10.0/go.mod h1:i2TRs7fXWIeaceF3z7KzsMt/he0TwkVC680mvdTFYeM= github.com/go-test/deep v1.1.0 h1:WOcxcdHcvdgThNXjw0t76K42FXTU7HpNQWHpA2HHNlg= github.com/go-test/deep v1.1.0/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss= diff --git a/push/config.go b/push/config.go index 7f059863d7..364f39f0f9 100644 --- a/push/config.go +++ b/push/config.go @@ -1,8 +1,11 @@ package push import ( + "context" "fmt" "strings" + + reg "github.com/evcc-io/evcc/util/registry" ) // Messenger implements message sending @@ -10,35 +13,19 @@ type Messenger interface { Send(title, msg string) } -type senderRegistry map[string]func(map[string]interface{}) (Messenger, error) - -func (r senderRegistry) Add(name string, factory func(map[string]interface{}) (Messenger, error)) { - if _, exists := r[name]; exists { - panic(fmt.Sprintf("cannot register duplicate messenger type: %s", name)) - } - r[name] = factory -} - -func (r senderRegistry) Get(name string) (func(map[string]interface{}) (Messenger, error), error) { - factory, exists := r[name] - if !exists { - return nil, fmt.Errorf("messenger type not registered: %s", name) - } - return factory, nil -} - -var registry senderRegistry = make(map[string]func(map[string]interface{}) (Messenger, error)) +var registry = reg.New[Messenger]("messenger") // NewFromConfig creates messenger from configuration -func NewFromConfig(typ string, other map[string]interface{}) (v Messenger, err error) { +func NewFromConfig(ctx context.Context, typ string, other map[string]interface{}) (Messenger, error) { factory, err := registry.Get(strings.ToLower(typ)) - if err == nil { - if v, err = factory(other); err != nil { - err = fmt.Errorf("cannot create messenger '%s': %w", typ, err) - } - } else { - err = fmt.Errorf("invalid messenger type: %s", typ) + if err != nil { + return nil, err + } + + v, err := factory(ctx, other) + if err != nil { + err = fmt.Errorf("cannot create messenger type '%s': %w", typ, err) } - return + return v, err } diff --git a/push/telegram.go b/push/telegram.go index cbda1ef996..997e80829f 100644 --- a/push/telegram.go +++ b/push/telegram.go @@ -1,28 +1,30 @@ package push import ( + "context" "errors" "strconv" "sync" "github.com/evcc-io/evcc/util" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" + "github.com/go-telegram/bot" + "github.com/go-telegram/bot/models" ) func init() { - registry.Add("telegram", NewTelegramFromConfig) + registry.AddCtx("telegram", NewTelegramFromConfig) } // Telegram implements the Telegram messenger type Telegram struct { log *util.Logger sync.Mutex - bot *tgbotapi.BotAPI + bot *bot.Bot chats map[int64]struct{} } // NewTelegramFromConfig creates new pushover messenger -func NewTelegramFromConfig(other map[string]interface{}) (Messenger, error) { +func NewTelegramFromConfig(ctx context.Context, other map[string]interface{}) (Messenger, error) { var cc struct { Token string Chats []int64 @@ -32,60 +34,61 @@ func NewTelegramFromConfig(other map[string]interface{}) (Messenger, error) { return nil, err } - bot, err := tgbotapi.NewBotAPI(cc.Token) + log := util.NewLogger("telegram").Redact(cc.Token) + + m := &Telegram{ + log: log, + chats: make(map[int64]struct{}), + } + + bot, err := bot.New(cc.Token, bot.WithDefaultHandler(m.handler), bot.WithErrorsHandler(func(err error) { + log.DEBUG.Println(err) + })) if err != nil { - return nil, errors.New("telegram: invalid bot token") + return nil, errors.New("invalid bot token") } - log := util.NewLogger("telegram").Redact(cc.Token) - _ = tgbotapi.SetLogger(log.ERROR) + m.bot = bot + bot.Start(ctx) for _, i := range cc.Chats { log.Redact(strconv.FormatInt(i, 10)) } - m := &Telegram{ - log: log, - bot: bot, - chats: make(map[int64]struct{}), - } - for _, chat := range cc.Chats { m.chats[chat] = struct{}{} } - go m.trackChats() - return m, nil } -// trackChats captures ids of all chats that bot participates in -func (m *Telegram) trackChats() { - conf := tgbotapi.NewUpdate(0) - conf.Timeout = 1000 +// handler captures ids of all chats that bot participates in +func (m *Telegram) handler(ctx context.Context, b *bot.Bot, update *models.Update) { + if update.Message == nil { + return + } + + m.Lock() + defer m.Unlock() - for update := range m.bot.GetUpdatesChan(conf) { - if update.Message == nil || update.Message.Chat == nil { - continue - } - m.Lock() - if _, ok := m.chats[update.Message.Chat.ID]; !ok { - m.log.INFO.Printf("new chat id: %d", update.Message.Chat.ID) - } - m.Unlock() + if _, ok := m.chats[update.Message.Chat.ID]; !ok { + m.log.INFO.Printf("new chat id: %d", update.Message.Chat.ID) } } // Send sends to all receivers func (m *Telegram) Send(title, msg string) { m.Lock() + defer m.Unlock() + for chat := range m.chats { m.log.DEBUG.Printf("sending to %d", chat) - msg := tgbotapi.NewMessage(chat, msg) - if _, err := m.bot.Send(msg); err != nil { + if _, err := m.bot.SendMessage(context.Background(), &bot.SendMessageParams{ + ChatID: chat, + Text: msg, + }); err != nil { m.log.ERROR.Println("send:", err) } } - m.Unlock() } From d8feaaf1b3965832d02611c7c14e9ab9955d3fea Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 3 Nov 2024 10:40:40 +0100 Subject: [PATCH 2/4] wip --- push/telegram.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/push/telegram.go b/push/telegram.go index 997e80829f..d8791590b4 100644 --- a/push/telegram.go +++ b/push/telegram.go @@ -43,6 +43,8 @@ func NewTelegramFromConfig(ctx context.Context, other map[string]interface{}) (M bot, err := bot.New(cc.Token, bot.WithDefaultHandler(m.handler), bot.WithErrorsHandler(func(err error) { log.DEBUG.Println(err) + }), bot.WithDebugHandler(func(format string, args ...interface{}) { + log.TRACE.Printf(format, args...) })) if err != nil { return nil, errors.New("invalid bot token") From 2ccd65d294aad3d1b66385a0f801cd38efece6a6 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 3 Nov 2024 10:44:03 +0100 Subject: [PATCH 3/4] wip --- cmd/setup.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/setup.go b/cmd/setup.go index 6629b9c716..57612eb7fb 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -757,7 +757,7 @@ func configureMessengers(conf globalconfig.Messaging, vehicles push.Vehicles, va } for _, service := range conf.Services { - impl, err := push.NewFromConfig(service.Type, service.Other) + impl, err := push.NewFromConfig(context.TODO(), service.Type, service.Other) if err != nil { return messageChan, fmt.Errorf("failed configuring push service %s: %w", service.Type, err) } From 8e5552c4fee2e0e37f875228760f58e0272725a3 Mon Sep 17 00:00:00 2001 From: andig Date: Sun, 3 Nov 2024 10:48:23 +0100 Subject: [PATCH 4/4] wip --- push/telegram.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/push/telegram.go b/push/telegram.go index d8791590b4..adecb8b437 100644 --- a/push/telegram.go +++ b/push/telegram.go @@ -42,7 +42,7 @@ func NewTelegramFromConfig(ctx context.Context, other map[string]interface{}) (M } bot, err := bot.New(cc.Token, bot.WithDefaultHandler(m.handler), bot.WithErrorsHandler(func(err error) { - log.DEBUG.Println(err) + log.ERROR.Println(err) }), bot.WithDebugHandler(func(format string, args ...interface{}) { log.TRACE.Printf(format, args...) })) @@ -51,13 +51,11 @@ func NewTelegramFromConfig(ctx context.Context, other map[string]interface{}) (M } m.bot = bot - bot.Start(ctx) - for _, i := range cc.Chats { - log.Redact(strconv.FormatInt(i, 10)) - } + go bot.Start(ctx) for _, chat := range cc.Chats { + log.Redact(strconv.FormatInt(chat, 10)) m.chats[chat] = struct{}{} }