11package push
22
33import (
4+ "context"
45 "errors"
56 "strconv"
67 "sync"
78
89 "github.com/evcc-io/evcc/util"
9- tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
10+ "github.com/go-telegram/bot"
11+ "github.com/go-telegram/bot/models"
1012)
1113
1214func init () {
13- registry .Add ("telegram" , NewTelegramFromConfig )
15+ registry .AddCtx ("telegram" , NewTelegramFromConfig )
1416}
1517
1618// Telegram implements the Telegram messenger
1719type Telegram struct {
1820 log * util.Logger
1921 sync.Mutex
20- bot * tgbotapi. BotAPI
22+ bot * bot. Bot
2123 chats map [int64 ]struct {}
2224}
2325
2426// NewTelegramFromConfig creates new pushover messenger
25- func NewTelegramFromConfig (other map [string ]interface {}) (Messenger , error ) {
27+ func NewTelegramFromConfig (ctx context. Context , other map [string ]interface {}) (Messenger , error ) {
2628 var cc struct {
2729 Token string
2830 Chats []int64
@@ -32,60 +34,61 @@ func NewTelegramFromConfig(other map[string]interface{}) (Messenger, error) {
3234 return nil , err
3335 }
3436
35- bot , err := tgbotapi .NewBotAPI (cc .Token )
36- if err != nil {
37- return nil , errors .New ("telegram: invalid bot token" )
38- }
39-
4037 log := util .NewLogger ("telegram" ).Redact (cc .Token )
41- _ = tgbotapi .SetLogger (log .ERROR )
42-
43- for _ , i := range cc .Chats {
44- log .Redact (strconv .FormatInt (i , 10 ))
45- }
4638
4739 m := & Telegram {
4840 log : log ,
49- bot : bot ,
5041 chats : make (map [int64 ]struct {}),
5142 }
5243
44+ bot , err := bot .New (cc .Token , bot .WithDefaultHandler (m .handler ), bot .WithErrorsHandler (func (err error ) {
45+ log .ERROR .Println (err )
46+ }), bot .WithDebugHandler (func (format string , args ... interface {}) {
47+ log .TRACE .Printf (format , args ... )
48+ }))
49+ if err != nil {
50+ return nil , errors .New ("invalid bot token" )
51+ }
52+
53+ m .bot = bot
54+
55+ go bot .Start (ctx )
56+
5357 for _ , chat := range cc .Chats {
58+ log .Redact (strconv .FormatInt (chat , 10 ))
5459 m .chats [chat ] = struct {}{}
5560 }
5661
57- go m .trackChats ()
58-
5962 return m , nil
6063}
6164
62- // trackChats captures ids of all chats that bot participates in
63- func (m * Telegram ) trackChats () {
64- conf := tgbotapi .NewUpdate (0 )
65- conf .Timeout = 1000
65+ // handler captures ids of all chats that bot participates in
66+ func (m * Telegram ) handler (ctx context.Context , b * bot.Bot , update * models.Update ) {
67+ if update .Message == nil {
68+ return
69+ }
6670
67- for update := range m .bot .GetUpdatesChan (conf ) {
68- if update .Message == nil || update .Message .Chat == nil {
69- continue
70- }
71- m .Lock ()
72- if _ , ok := m .chats [update .Message .Chat .ID ]; ! ok {
73- m .log .INFO .Printf ("new chat id: %d" , update .Message .Chat .ID )
74- }
75- m .Unlock ()
71+ m .Lock ()
72+ defer m .Unlock ()
73+
74+ if _ , ok := m .chats [update .Message .Chat .ID ]; ! ok {
75+ m .log .INFO .Printf ("new chat id: %d" , update .Message .Chat .ID )
7676 }
7777}
7878
7979// Send sends to all receivers
8080func (m * Telegram ) Send (title , msg string ) {
8181 m .Lock ()
82+ defer m .Unlock ()
83+
8284 for chat := range m .chats {
8385 m .log .DEBUG .Printf ("sending to %d" , chat )
8486
85- msg := tgbotapi .NewMessage (chat , msg )
86- if _ , err := m .bot .Send (msg ); err != nil {
87+ if _ , err := m .bot .SendMessage (context .Background (), & bot.SendMessageParams {
88+ ChatID : chat ,
89+ Text : msg ,
90+ }); err != nil {
8791 m .log .ERROR .Println ("send:" , err )
8892 }
8993 }
90- m .Unlock ()
9194}
0 commit comments