2023-01-25 19:42:18 +00:00
|
|
|
package bot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.faercol.me/faercol/public-ip-tracker/tracker/config"
|
2023-01-25 20:06:00 +00:00
|
|
|
"git.faercol.me/faercol/public-ip-tracker/tracker/ip"
|
2023-01-25 19:42:18 +00:00
|
|
|
"github.com/ahugues/go-telegram-api/bot"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Notifier struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel context.CancelFunc
|
|
|
|
tgBot *bot.ConcreteBot
|
|
|
|
notifChannel int64
|
2023-01-25 20:06:00 +00:00
|
|
|
ipGetter *ip.IPGetter
|
2023-01-25 19:42:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Notifier) SendInitMessage() error {
|
2023-01-25 20:06:00 +00:00
|
|
|
publicIP, err := n.ipGetter.GetCurrentPublicIP(n.ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get current public IP: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
initMsg := fmt.Sprintf("Public IP tracker initialized at %v, public IP is %s", time.Now(), publicIP)
|
2023-01-25 19:42:18 +00:00
|
|
|
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,
|
2023-01-25 20:06:00 +00:00
|
|
|
ipGetter: ip.New(),
|
2023-01-25 19:42:18 +00:00
|
|
|
}
|
|
|
|
}
|