NotIf 文档 价格 应用

消息模板

许多服务会以 JSON 格式发送 webhook,而且不让你选择文字内容。模板可以把这些 JSON 转换成易读的消息。NotIf 提供三个现成的模板:GitHub、Grafana 和 Alertmanager。

template 参数用于选择模板,tpl 是它的别名。由于这些服务不一定允许添加请求头,请把它放在 webhook 地址中。

请求体必须是有效的 JSON。模板适用于向频道发送的 POST 和 PUT 请求,但不适用于向根地址进行的 JSON 发布,也不适用于 GET 请求。

GitHub

在你的仓库中,依次打开 Settings、Webhooks、Add webhook:

以下是新建 issue 事件的效果:

curl -H "Template: github" -d '{"action":"opened","repository":{"full_name":"moi/app"},"sender":{"login":"marie"},"issue":{"title":"按钮失灵","html_url":"https://github.com/moi/app/issues/1"}}' https://usenotif.com/mes-depots

标题将是 Issue opened: 按钮失灵,消息将是 marie in moi/app: https://github.com/moi/app/issues/1。

事件 标题
Pull request Pull request opened: 标题
Issue Issue opened: 标题
Push marie pushed to app,消息中列出提交
版本(release) Release published: 名称
其他带有 action 的事件 moi/app: action
其余所有事件 moi/app event

Grafana

创建一个 Webhook 类型的联系点(contact point),地址如下:

https://usenotif.com/mes-graphiques?template=grafana

该模板使用 Grafana 的标题和消息。如果没有,就列出各条警报。状态为 firing 时优先级为 high,否则为 default。

curl -H "Template: grafana" -d '{"status":"firing","title":"温度过高","message":"服务器机房温度达到 31 度"}' https://usenotif.com/mes-graphiques

Alertmanager

在 Alertmanager 的配置中:

receivers:
  - name: notif
    webhook_configs:
      - url: https://usenotif.com/mes-alertes?template=alertmanager

标题为 警报名称: 状态。消息会列出每条警报,以及它的摘要(如果有)。优先级规则与 Grafana 相同。

curl -H "Template: alertmanager" -d '{"status":"firing","commonLabels":{"alertname":"Disk"},"alerts":[{"status":"firing","annotations":{"summary":"磁盘已满"},"labels":{"alertname":"Disk"}}]}' https://usenotif.com/mes-alertes

预留的频道

对于预留的频道,请在 webhook 地址中加上 auth 参数,因为这些服务不一定会发送你的令牌:

https://usenotif.com/mon-canal-prive?template=github&auth=VOTRE_VALEUR_AUTH

在请求中编写模板

使用 Template: yes 时,由你自己编写模板。这样,标题和消息就可以使用 JSON 中的字段。

curl -H "Template: yes" -H "Title: 销售 {{.order.id}}" -H "Message: 总计 {{.order.total}} $" -d '{"order":{"id":7,"total":"19.99"}}' https://usenotif.com/ma-boutique

结果:标题为 销售 7,消息为 总计 19.99 $。

如果服务不允许添加请求头,就把所有内容都放在地址中。这时花括号要写成 %7B 和 %7D。

curl -d '{"order_number":1042,"total":"59.00"}' "https://usenotif.com/ma-boutique?tpl=yes&t=Commande+%7B%7B.order_number%7D%7D&m=Total+%7B%7B.total%7D%7D+%24"

没有 Message 时,消息会原样包含收到的 JSON。

命名模板会保留它没有定义的选项。例如,Priority: high 会作用于不设定优先级的 github 模板。对于 grafana 和 alertmanager,以它们自己的优先级为准。

语法

模板使用 Go 模板语法的一个安全子集,并带有 Sprig 的常用函数。

语法 效果
{{.a.b}} 插入一个字段
{{.nom | upper}} 把值传给一个函数
{{if eq .etat "firing"}}...{{else}}...{{end}} 条件
{{range .items}}{{.}}{{end}} 循环
{{with .a}}...{{end}} 进入某个字段
{{$x := .a}} 变量
{{/* note */}} 注释
{{- 和 -}} 去除两侧的空白

可用的函数:

类别 函数
比较 eq, ne, lt, le, gt, ge, and, or, not
文本 upper, lower, title, trim, trimPrefix, trimSuffix, trunc, abbrev, replace, contains, hasPrefix, hasSuffix, repeat, indent, nindent, quote, squote, split, splitList, join
格式 print, println, printf, html, urlquery, js, toJson, toString
值 default, empty, coalesce, ternary
数字 int, atoi, add, sub, mul, div, mod
列表 len, index, first, last, list, keys, values, has, hasKey, get
时间 now, unixEpoch

define、template、block 和 call 会被拒绝。

默认值示例:

curl -H "Template: yes" -H 'Message: {{ .nom | upper }}:{{ .note | default "没有备注" }}' -d '{"nom":"marie"}' https://usenotif.com/mes-essais

错误

代码 原因
40042 未知模板
40043 请求体不是有效的 JSON
40044 模板中有语法错误