From 1ca3ef481b7fcfdbd9a406dc815d72047daa24b9 Mon Sep 17 00:00:00 2001 From: Julien Brochet Date: Sun, 28 Jun 2020 20:09:23 +0200 Subject: [PATCH] feat(telegram): add new variable RecordType in template --- README.md | 5 +++-- config/container.go | 2 +- config/data.go | 2 +- ddns/ddns.go | 2 ++ notifier/telegram.go | 14 +++++++++++--- 5 files changed, 18 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index a9e937b..76179fa 100644 --- a/README.md +++ b/README.md @@ -76,12 +76,13 @@ telegram: enabled: true token: __TELEGRAM_TOKEN__ chat_id: __TELEGRAM_CHAT_ID__ - template: DNS record *{{ .Record }}.{{ .Domain }}* has been updated + template: DNS record *{{ .RecordName }}.{{ .Domain }}* has been updated ``` The following variables can be used in the template message. -* Record +* RecordName +* RecordType * Domain * PreviousIP * NewIP diff --git a/config/container.go b/config/container.go index bf1c586..2156cd6 100644 --- a/config/container.go +++ b/config/container.go @@ -8,7 +8,7 @@ import ( // Notifier interface to represent any notifier type Notifier interface { - Notify(domain string, record string, previousIP string, newIP string) error + Notify(domain string, recordName string, recordType string, previousIP string, newIP string) error } // Container structure to hold global objects diff --git a/config/data.go b/config/data.go index b44d359..14980c5 100644 --- a/config/data.go +++ b/config/data.go @@ -68,7 +68,7 @@ var ( // DefaultTelegramConfig is the default configuration to use Telegram notifications DefaultTelegramConfig = TelegramConfig{ Enabled: false, - Template: "DNS record *{{ .Record }}.{{ .Domain }}* has been updated from *{{ .PreviousIP }}* to *{{ .NewIP }}*", + Template: "DNS record *{{ .RecordName }}.{{ .Domain }}* has been updated from *{{ .PreviousIP }}* to *{{ .NewIP }}*", } // DefaultConfig is the global default configuration. diff --git a/ddns/ddns.go b/ddns/ddns.go index eace52a..1480c05 100644 --- a/ddns/ddns.go +++ b/ddns/ddns.go @@ -28,6 +28,7 @@ func NewDynamicDNSUpdater( func (d *DynamicDNSUpdater) Start() { cfg := d.container.Config + // Start the first update now d.doStart() ticker := time.NewTicker(time.Duration(cfg.Interval) * time.Second) @@ -140,6 +141,7 @@ func (d *DynamicDNSUpdater) UpdateRecord( err := notifier.Notify( domain.Name, domain.Record, + recordType, scalewayIP, currentIP, ) diff --git a/notifier/telegram.go b/notifier/telegram.go index 95fc500..cad324e 100644 --- a/notifier/telegram.go +++ b/notifier/telegram.go @@ -17,7 +17,8 @@ type Telegram struct { // TelegramMessageData holds information for the Telegram template message type TelegramMessageData struct { Domain string - Record string + RecordName string + RecordType string PreviousIP string NewIP string } @@ -36,7 +37,13 @@ func NewTelegram( } // Notify launches a new message on Telegram when the IP has changed -func (t *Telegram) Notify(domain string, record string, previousIP string, newIP string) error { +func (t *Telegram) Notify( + domain string, + recordName string, + recordType string, + previousIP string, + newIP string, +) error { bot, err := tgbotapi.NewBotAPI(t.token) if err != nil { return err @@ -54,7 +61,8 @@ func (t *Telegram) Notify(domain string, record string, previousIP string, newIP var message bytes.Buffer err = template.Execute(&message, &TelegramMessageData{ Domain: domain, - Record: record, + RecordName: recordName, + RecordType: recordType, PreviousIP: previousIP, NewIP: newIP, })