Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support ntfy template parameter #453

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkg/services/ntfy/ntfy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ func (service *Service) Send(message string, params *types.Params) error {
return err
}

// Prevent enabling templated messages without custom data
if config.Template && message == "" {
return fmt.Errorf("body must be set if template is enabled")
}

if config.Template && config.Message == "" && config.Title == "" {
return fmt.Errorf("at least title or message must be set if template is enabled")
}

if config.Message != "" && !config.Template {
return fmt.Errorf("message should not be filled if template is disabled, use body instead")
}

if err := service.sendAPI(config, message); err != nil {
return fmt.Errorf("failed to send ntfy notification: %w", err)
}
Expand Down Expand Up @@ -74,6 +87,10 @@ func (service *Service) sendAPI(config *Config, message string) error {
if !config.Firebase {
headers.Add("Firebase", "no")
}
if config.Template {
headers.Add("Template", "yes")
headers.Add("Message", config.Message)
}

if err := jsonClient.Post(config.GetAPIURL(), request, &response); err != nil {
if jsonClient.ErrorResponse(err, &response) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/services/ntfy/ntfy_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Config struct {
Icon string `key:"icon" optional:"" desc:"URL to use as notification icon"`
Cache bool `key:"cache" default:"yes" desc:"Cache messages"`
Firebase bool `key:"firebase" default:"yes" desc:"Send to firebase"`
Template bool `key:"template" default:"no" desc:"Use message and title as template"`
Message string `key:"message" optional:"" desc:"Message, to be used only then template is set to true"`
}

// Enums implements types.ServiceConfig
Expand Down
53 changes: 51 additions & 2 deletions pkg/services/ntfy/ntfy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,65 @@ var _ = Describe("the ntfy service", func() {
Priority: 3,
Firebase: true,
Cache: true,
Template: false,
Message: "",
}))
})
})

When("validate that template behaves correctly", func() {
BeforeEach(func() {
httpmock.Activate()
})
AfterEach(func() {
httpmock.DeactivateAndReset()
})

It("should not allow templated messages if message and title are not set", func() {
serviceURL := testutils.URLMust("ntfy://hostname/topic?template=true&message=&title=hello")
Expect(service.Initialize(serviceURL, logger)).ShouldNot(HaveOccurred())
Expect(service.Send("", nil)).Should(HaveOccurred())
})
It("should allow templated messages if title is set", func() {
serviceURL := testutils.URLMust("ntfy://:devicekey@hostname/topic?template=true&message=&title=hello")
Expect(service.Initialize(serviceURL, logger)).ShouldNot(HaveOccurred())

httpmock.RegisterResponder("POST", service.config.GetAPIURL(), testutils.JSONRespondMust(200, apiResponse{
Code: http.StatusOK,
Message: "OK",
}))
Expect(service.Send("{}", nil)).To(Succeed())
})
It("should allow templated messages if message is set", func() {
serviceURL := testutils.URLMust("ntfy://:devicekey@hostname/topic?template=true&message=hello&title=")
Expect(service.Initialize(serviceURL, logger)).ShouldNot(HaveOccurred())

httpmock.RegisterResponder("POST", service.config.GetAPIURL(), testutils.JSONRespondMust(200, apiResponse{
Code: http.StatusOK,
Message: "OK",
}))
Expect(service.Send("{}", nil)).To(Succeed())
})
It("should not allow templated messages if body is empty", func() {
serviceURL := testutils.URLMust("ntfy://hostname/topic?template=true&message=hello&title=hello")
Expect(service.Initialize(serviceURL, logger)).ShouldNot(HaveOccurred())
Expect(service.Send("", nil)).Should(HaveOccurred())
})
It("should validate that message is empty if template is disabled", func() {
serviceURL := testutils.URLMust("ntfy://hostname/topic?template=false&message=test")
Expect(service.Initialize(serviceURL, logger)).ShouldNot(HaveOccurred())
Expect(service.Send("test", nil)).Should(HaveOccurred())
})
})

When("parsing the configuration URL", func() {
It("should be identical after de-/serialization", func() {
testURL := "ntfy://user:[email protected]:2225/topic?cache=No&click=CLICK&firebase=No&icon=ICON&priority=Max&scheme=http&title=TITLE"
testURL := "ntfy://user:[email protected]:2225/topic?cache=No&click=CLICK&firebase=No&icon=ICON&priority=Max&scheme=http&template=Yes&title=TITLE"
config := &Config{}
pkr := format.NewPropKeyResolver(config)
Expect(config.setURL(&pkr, testutils.URLMust(testURL))).To(Succeed(), "verifying")
Expect(config.GetURL().String()).To(Equal(testURL))
Expect(config.Template).To(Equal(true))
})
})
})
Expand Down Expand Up @@ -132,7 +181,7 @@ var _ = Describe("the ntfy service", func() {
testutils.TestConfigSetDefaultValues(&Config{})

testutils.TestConfigGetEnumsCount(&Config{}, 1)
testutils.TestConfigGetFieldsCount(&Config{}, 15)
testutils.TestConfigGetFieldsCount(&Config{}, 17)
})
})
Describe("the service instance", func() {
Expand Down