示例
这些示例需要按你的情况调整:把频道名称换成你自己的、难以猜到的名称。
脚本运行结束
在耗时任务的末尾加一行。
./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%E8%BF%9B%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": "已处理 1200 个文件",
"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: '已处理 1200 个文件',
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 }}:${{ github.ref_name }}" \
https://usenotif.com/mes-builds
对于预留的频道,请把你的令牌保存在仓库的 secret 中:
- name: Notify
if: always()
run: |
curl -H "Authorization: Bearer ${{ secrets.NOTIF_TOKEN }}" \
-d "Build ${{ job.status }}" \
https://usenotif.com/mes-builds
要在不改动工作流的情况下跟踪仓库事件,请参见 GitHub 模板。