public-ip-tracker/tracker/bot/bot.go
Melora Hugues 54de3b84cd
All checks were successful
continuous-integration/drone/push Build is passing
Add link to Telegram
Ref #2

This commit adds a link to a Go Telegram API to send messages to a given
telegram channel provided with a JSON config file. At the moment the
program sends a simple status message to the bot when starting up, but
this allows testing whether the link to Telegram is correctly working.
2023-01-25 20:42:43 +01:00

37 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,
}
}