-
-
Notifications
You must be signed in to change notification settings - Fork 752
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
add templating for title and message fields #1057
Conversation
I updated the code to use the existing message and title fields. Right now, if the message body is not valid JSON, the message and title fields will just contain the original template placeholders without anything being replaced and without any errors thrown. Let me know if you think this should change. (See line 2671 of server/server_test.go for an example (TestServer_MessageTemplate_MalformedJSONBody)) |
Codecov ReportAttention: Patch coverage is
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## main #1057 +/- ##
==========================================
+ Coverage 64.86% 64.93% +0.06%
==========================================
Files 60 60
Lines 7944 7965 +21
==========================================
+ Hits 5153 5172 +19
- Misses 1930 1931 +1
- Partials 861 862 +1 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks fantastic. Very simple and elegant.
The docs should include lots of real world examples; I can do that when I merge it in. Great work!
server/server.go
Outdated
query := v[1] | ||
result := gjson.Get(peakedBody, query) | ||
if result.Exists() { | ||
m.Message = strings.ReplaceAll(m.Message, fmt.Sprintf("${%s}", query), result.String()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if result
is a number or an array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works fine. See TestServer_MessageTemplate_FancyGJSON (server/server_test.go:L2742)
You should link https://gjson.dev/ in the docs. It's awesome. Here's a real Grafana alert that I get for ntfy sometimes:
Untested, but I think:
Would lead to this: Title:
Message:
|
So elegant and simple. Well done. I'll merge it tomorrow |
Please have a look at the latest commit (dealing with message sizes). All of the tests still pass, and I added specific message size tests, so I don't think I broke anything. But another set of eyes is always good. I think adding a custom GJSON modifier to the code would allow us to match values of the HTTP body and convert it to a priority level. I think including that in a separate PR might be good, though. We can release this and people can start using it before we add anything more. |
server/server.go
Outdated
} | ||
call = readParam(r, "x-call", "call") | ||
if call != "" && (s.config.TwilioAccount == "" || s.userManager == nil) { | ||
return false, false, "", "", false, errHTTPBadRequestPhoneCallsDisabled | ||
print("call: %s", call) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops. Forgot to take this out
Here's an initial working implementation, based on #171.
Right now, only the message and title fields can be templated, each using their own header (since this seemed like the quickest way to get something working).
In a future iteration, I think allowing other fields to be templated (e.g., priority, actions, attachment url, topic?, etc.) would be cool.
In that version, I was imagining an
X-Template
header that includes probably a JSON string for the different fields of a message (e.g.,{"message":"${field.value} is ${field2}", "title":"An error occurred on ${hostname}", "priority":${error.level}, "attachment":"${error.screenshot.url}"}
)