Skip to content

Commit

Permalink
feat(telegram): add new variable RecordType in template
Browse files Browse the repository at this point in the history
  • Loading branch information
aerialls committed Jun 28, 2020
1 parent ddd08db commit 1ca3ef4
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -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
2 changes: 1 addition & 1 deletion config/container.go
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/data.go
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions ddns/ddns.go
Expand Up @@ -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)
Expand Down Expand Up @@ -140,6 +141,7 @@ func (d *DynamicDNSUpdater) UpdateRecord(
err := notifier.Notify(
domain.Name,
domain.Record,
recordType,
scalewayIP,
currentIP,
)
Expand Down
14 changes: 11 additions & 3 deletions notifier/telegram.go
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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,
})
Expand Down

0 comments on commit 1ca3ef4

Please sign in to comment.