Message templates
Many services send JSON webhooks and do not let you choose the text. A template turns this JSON into a readable message. NotIf provides three ready-to-use templates: GitHub, Grafana and Alertmanager.
The template parameter chooses the template. tpl is an alias. Since these services do not always let you add headers, put it in the webhook address.
The request body must be valid JSON. Templates work with POST and PUT on a topic, but not with a JSON publication at the root or with a GET request.
GitHub
In your repository, open Settings, then Webhooks, then Add webhook:
- Payload URL:
https://usenotif.com/my-repos?template=github - Content type:
application/json
Here is what an issue opened event gives:
curl -H "Template: github" -d '{"action":"opened","repository":{"full_name":"me/app"},"sender":{"login":"marie"},"issue":{"title":"Broken button","html_url":"https://github.com/me/app/issues/1"}}' https://usenotif.com/my-repos
The title will be Issue opened: Broken button and the message marie in me/app: https://github.com/me/app/issues/1.
| Event | Title |
|---|---|
| Pull request | Pull request opened: title |
| Issue | Issue opened: title |
| Push | marie pushed to app, with the list of commits as the message |
| Release | Release published: name |
| Other event with an action | me/app: action |
| Everything else | me/app event |
Grafana
Create a Webhook contact point with this address:
https://usenotif.com/my-dashboards?template=grafana
The template uses Grafana's title and message. If they are missing, it lists the alerts. The priority is high for the firing state, and default otherwise.
curl -H "Template: grafana" -d '{"status":"firing","title":"High temperature","message":"Server room at 31 degrees"}' https://usenotif.com/my-dashboards
Alertmanager
In the Alertmanager configuration:
receivers:
- name: notif
webhook_configs:
- url: https://usenotif.com/my-alerts?template=alertmanager
The title is alert name: state. The message lists each alert, with its summary if there is one. The priority follows the same rule as Grafana.
curl -H "Template: alertmanager" -d '{"status":"firing","commonLabels":{"alertname":"Disk"},"alerts":[{"status":"firing","annotations":{"summary":"Disk full"},"labels":{"alertname":"Disk"}}]}' https://usenotif.com/my-alerts
Reserved topic
On a reserved topic, add the auth parameter to the webhook address, since these services do not always send your token:
https://usenotif.com/my-private-topic?template=github&auth=YOUR_AUTH_VALUE
Template in the request
With Template: yes, you write the template yourself. The title and the message can then use the fields of the JSON.
curl -H "Template: yes" -H "Title: Sale {{.order.id}}" -H 'Message: Total ${{.order.total}}' -d '{"order":{"id":7,"total":"19.99"}}' https://usenotif.com/my-shop
Result: the title Sale 7 and the message Total $19.99.
If the service does not let you add headers, put everything in the address. Braces are then written %7B and %7D.
curl -d '{"order_number":1042,"total":"59.00"}' "https://usenotif.com/my-shop?tpl=yes&t=Order+%7B%7B.order_number%7D%7D&m=Total+%24%7B%7B.total%7D%7D"
Without Message, the message contains the JSON received as is.
A named template keeps the options it does not define. For example, Priority: high applies to the github template, which does not set a priority. With grafana and alertmanager, their own priority wins.
Syntax
Templates use a safe subset of the Go template syntax, with common Sprig functions.
| Syntax | Effect |
|---|---|
{{.a.b}} |
Inserts a field |
{{.name | upper}} |
Passes a value to a function |
{{if eq .state "firing"}}...{{else}}...{{end}} |
Condition |
{{range .items}}{{.}}{{end}} |
Loop |
{{with .a}}...{{end}} |
Moves into a field |
{{$x := .a}} |
Variable |
{{/* note */}} |
Comment |
{{- and -}} |
Trim the spaces around |
Available functions:
| Family | Functions |
|---|---|
| Comparison | eq, ne, lt, le, gt, ge, and, or, not |
| Text | upper, lower, title, trim, trimPrefix, trimSuffix, trunc, abbrev, replace, contains, hasPrefix, hasSuffix, repeat, indent, nindent, quote, squote, split, splitList, join |
| Format | print, println, printf, html, urlquery, js, toJson, toString |
| Values | default, empty, coalesce, ternary |
| Numbers | int, atoi, add, sub, mul, div, mod |
| Lists | len, index, first, last, list, keys, values, has, hasKey, get |
| Time | now, unixEpoch |
define, template, block and call are refused.
Example of a default value:
curl -H "Template: yes" -H 'Message: {{ .name | upper }}: {{ .note | default "no note" }}' -d '{"name":"marie"}' https://usenotif.com/my-tests
Errors
| Code | Cause |
|---|---|
| 40042 | Unknown template |
| 40043 | The body is not valid JSON |
| 40044 | Syntax error in the template |