38 lines
866 B
Go
38 lines
866 B
Go
|
package bot
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"time"
|
||
|
|
||
|
"git.faercol.me/faercol/public-ip-tracker/tracker/config"
|
||
|
"github.com/ahugues/go-telegram-api/bot"
|
||
|
)
|
||
|
|
||
|
type Notifier struct {
|
||
|
ctx context.Context
|
||
|
cancel context.CancelFunc
|
||
|
tgBot *bot.ConcreteBot
|
||
|
notifChannel int64
|
||
|
}
|
||
|
|
||
|
func (n *Notifier) SendInitMessage() error {
|
||
|
initMsg := fmt.Sprintf("Public IP tracked initialized at %v", time.Now())
|
||
|
if err := n.tgBot.SendMessage(n.ctx, n.notifChannel, initMsg); err != nil {
|
||
|
return fmt.Errorf("failed to send initialization message: %w", err)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func New(ctx context.Context, config *config.Config) *Notifier {
|
||
|
subCtx, cancel := context.WithCancel(ctx)
|
||
|
tgBot := bot.New(config.Telegram.Token)
|
||
|
|
||
|
return &Notifier{
|
||
|
ctx: subCtx,
|
||
|
cancel: cancel,
|
||
|
tgBot: tgBot,
|
||
|
notifChannel: config.Telegram.ChannelID,
|
||
|
}
|
||
|
}
|