メッセージのテンプレート
多くのサービスは JSON で Webhook を送り、テキストを選ばせてくれません。テンプレートを使うと、その JSON を読みやすいメッセージに変換できます。NotIf には、すぐに使える3つのテンプレートがあります:GitHub、Grafana、Alertmanager。
template パラメーターでテンプレートを選びます。tpl は別名です。これらのサービスではヘッダーを追加できないことがあるため、Webhook のアドレスに含めてください。
リクエストの本文は有効な JSON である必要があります。テンプレートはトピックへの POST と PUT で使えますが、ルートへの JSON 送信や GET リクエストでは使えません。
GitHub
リポジトリで Settings、Webhooks、Add webhook の順に開きます。
- Payload URL:
https://usenotif.com/mes-depots?template=github - Content type:
application/json
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: 名前 |
| アクションのあるその他のイベント | moi/app: action |
| それ以外 | moi/app event |
Grafana
次のアドレスで、Webhook タイプの連絡先(コンタクトポイント)を作成します。
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":"Disque"},"alerts":[{"status":"firing","annotations":{"summary":"ディスクがいっぱいです"},"labels":{"alertname":"Disque"}}]}' 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 | テンプレートの構文エラー |