例
これらの例は、状況に合わせて変更してください。トピック名は、推測されにくい自分のものに置き換えます。
スクリプトの完了
時間のかかる処理の最後に1行追加します。
./conversion-videos.sh
curl -d "変換が完了しました" https://usenotif.com/mes-scripts
成功と失敗を区別するには:
if ./conversion-videos.sh; then
curl -H "Tags: white_check_mark" -d "変換に成功しました" https://usenotif.com/mes-scripts
else
curl -H "Tags: x" -H "Priority: high" -d "変換に失敗しました" https://usenotif.com/mes-scripts
fi
バックアップ
メッセージにはコマンドの出力を含められます。
#!/bin/sh
if restic backup /home; then
curl -H "Title: バックアップ" -H "Tags: floppy_disk" -d "$(date +%F) のバックアップが完了しました" https://usenotif.com/mes-sauvegardes
else
curl -H "Title: バックアップ" -H "Tags: x" -H "Priority: urgent" -d "$(date +%F) のバックアップに失敗しました" https://usenotif.com/mes-sauvegardes
fi
ログを添付するには:
curl -T sauvegarde.log -H "Filename: sauvegarde.log" -H "Message: 昨夜のログ" https://usenotif.com/mes-sauvegardes
Cron
このタスクは、毎週月曜日の8時にリマインダーを送ります。
0 8 * * 1 curl -s -H "Tags: bell" -d "資源ごみを出す" https://usenotif.com/ma-maison > /dev/null
こちらは、ディスク使用率が90%を超えたら通知します。
#!/bin/sh
USAGE=$(df / | awk 'NR==2 {print $5}' | tr -d '%')
if [ "$USAGE" -gt 90 ]; then
curl -H "Title: ディスク容量" -H "Priority: high" -H "Tags: warning" -d "ディスク使用率 $USAGE %" https://usenotif.com/mon-serveur
fi
ネットショップの注文
ほとんどのネットショップは、注文ごとに JSON の Webhook を送ります。アドレスにテンプレートを入れるだけで、コードを書く必要はありません。このアドレスを Webhook の URL として使ってください。
https://usenotif.com/ma-boutique?tpl=yes&t=Commande+%7B%7B.order_number%7D%7D&m=Total+%7B%7B.total%7D%7D+%24&ta=shopping_cart
試すには、Webhook を模擬的に送ります。order_number と total は、ショップが送るフィールドに合わせて変更してください。
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&ta=shopping_cart"
タイトル Commande 1042 とメッセージ Total 59.00 $ が、カートのアイコン付きで届きます。
自宅のセンサー
Raspberry Pi でセンサーを監視して通知できます。
#!/bin/sh
TEMP=$(cat /sys/class/thermal/thermal_zone0/temp)
TEMP=$((TEMP / 1000))
if [ "$TEMP" -gt 70 ]; then
curl -H "Title: Temperature" -H "Tags: thermometer" -H "Priority: high" -d "$TEMP 度" https://usenotif.com/ma-maison
fi
アドレスを呼び出すことしかできない小さなデバイスでは、GET リクエストを使えます。
curl "https://usenotif.com/ma-maison/publish?message=%E5%9C%B0%E4%B8%8B%E5%AE%A4%E3%81%AB%E6%B0%B4&priority=urgent&tags=droplet"
時刻指定のリマインダー
curl -H "Delay: 45m" -H "Tags: bell" -d "ケーキをオーブンから出す" https://usenotif.com/ma-cuisine
curl -H "At: tomorrow, 7am" -H "Timezone: America/Toronto" -d "チケットを受け取る" https://usenotif.com/mes-rappels
Python
標準ライブラリだけで十分です。JSON なら日本語もそのまま保たれます。
import json
import urllib.request
corps = {
"topic": "mes-scripts",
"title": "処理が完了しました",
"message": "1,200 件のファイルを処理しました",
"priority": 4,
"tags": ["white_check_mark"],
}
requete = urllib.request.Request(
"https://usenotif.com",
data=json.dumps(corps).encode("utf-8"),
method="POST",
)
urllib.request.urlopen(requete)
保護されたトピックの場合は、Request に headers={"Authorization": "Bearer tk_votrejeton"} を追加します。
JavaScript
このコードは Node.js でもブラウザでも動きます。
await fetch('https://usenotif.com', {
method: 'POST',
body: JSON.stringify({
topic: 'mes-scripts',
title: '処理が完了しました',
message: '1,200 件のファイルを処理しました',
priority: 4,
tags: ['white_check_mark'],
}),
});
ウェブページでメッセージを受け取るには:
const flux = new EventSource('https://usenotif.com/mes-scripts/sse');
flux.onmessage = e => {
const m = JSON.parse(e.data);
if (m.event === 'message') console.log(m.title, m.message);
};
PowerShell
Windows PowerShell では、curl は別のプログラムです。Invoke-RestMethod を使ってください。日本語を保つため、本文を UTF-8 に変換します。
$corps = @{
topic = 'mes-scripts'
title = 'バックアップ完了'
message = 'コピーに成功しました'
priority = 4
tags = @('floppy_disk')
} | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri 'https://usenotif.com' -Body ([Text.Encoding]::UTF8.GetBytes($corps))
シンプルなメッセージの場合:
Invoke-RestMethod -Method Post -Uri 'https://usenotif.com/mes-scripts' -Headers @{ Title = 'Disk'; Priority = 'high'; Tags = 'warning' } -Body ([Text.Encoding]::UTF8.GetBytes('ディスクの空きがわずかです'))
表示される応答では、日本語が文字化けして見えることがあります。購読者が受け取るメッセージは正しく表示されます。
GitHub Actions
ジョブの最後にこのステップを追加します。if: always() を付けると、失敗したときにも実行されます。
- name: Notify
if: always()
run: |
curl -H "Title: ${{ github.repository }}" -H "Tags: package" \
-d "Build ${{ job.status }} on ${{ github.ref_name }}" \
https://usenotif.com/mes-builds
予約済みトピックの場合は、トークンをリポジトリのシークレットに保存します。
- name: Notify
if: always()
run: |
curl -H "Authorization: Bearer ${{ secrets.NOTIF_TOKEN }}" \
-d "Build ${{ job.status }}" \
https://usenotif.com/mes-builds
ワークフローを変更せずにリポジトリのイベントを追うには、GitHub のテンプレートをご覧ください。