Skip to content

Commit

Permalink
Add ntfy webhook notifier
Browse files Browse the repository at this point in the history
  • Loading branch information
MaximilianGaedig committed Aug 12, 2022
1 parent 98a89f9 commit 29bf2b5
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,28 @@ jobs:
echo "kv_namespaces = [{binding=\"KV_STATUS_PAGE\", id=\"${KV_NAMESPACE_ID}\"}]" >> wrangler.toml
[ -z "$SECRET_SLACK_WEBHOOK_URL" ] && echo "Secret SECRET_SLACK_WEBHOOK_URL not set, creating dummy one..." && SECRET_SLACK_WEBHOOK_URL="default-gh-action-secret" || true
[ -z "$SECRET_TELEGRAM_API_TOKEN" ] && echo "Secret SECRET_TELEGRAM_API_TOKEN not set, creating dummy one..." && SECRET_TELEGRAM_API_TOKEN="default-gh-action-secret" || true
[ -z "$SECRET_NTFY_WEBHOOK_URL" ] && echo "Secret SECRET_NTFY_WEBHOOK_URL not set, creating dummy one..." && SECRET_NTFY_WEBHOOK_URL="default-gh-action-secret" || true
[ -z "$SECRET_NTFY_USERNAME" ] && echo "Secret SECRET_NTFY_USERNAME not set, creating dummy one..." && SECRET_NTFY_USERNAME="default-gh-action-secret" || true
[ -z "$SECRET_NTFY_PASSWORD" ] && echo "Secret SECRET_NTFY_PASSWORD not set, creating dummy one..." && SECRET_NTFY_PASSWORD="default-gh-action-secret" || true
[ -z "$SECRET_TELEGRAM_CHAT_ID" ] && echo "Secret SECRET_TELEGRAM_CHAT_ID not set, creating dummy one..." && SECRET_TELEGRAM_CHAT_ID="default-gh-action-secret" || true
[ -z "$SECRET_DISCORD_WEBHOOK_URL" ] && echo "Secret SECRET_DISCORD_WEBHOOK_URL not set, creating dummy one..." && SECRET_DISCORD_WEBHOOK_URL="default-gh-action-secret" || true
postCommands: |
yarn kv-gc
secrets: |
SECRET_SLACK_WEBHOOK_URL
SECRET_TELEGRAM_API_TOKEN
SECRET_NTFY_WEBHOOK_URL
SECRET_NTFY_USERNAME
SECRET_NTFY_PASSWORD
SECRET_TELEGRAM_CHAT_ID
SECRET_DISCORD_WEBHOOK_URL
environment: production
env:
CF_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
SECRET_SLACK_WEBHOOK_URL: ${{secrets.SECRET_SLACK_WEBHOOK_URL}}
SECRET_TELEGRAM_API_TOKEN: ${{secrets.SECRET_TELEGRAM_API_TOKEN}}
SECRET_NTFY_WEBHOOK_URL: ${{secrets.SECRET_NTFY_WEBHOOK_URL}}
SECRET_NTFY_USERNAME: ${{secrets.SECRET_NTFY_USERNAME}}
SECRET_NTFY_PASSWORD: ${{secrets.SECRET_NTFY_PASSWORD}}
SECRET_TELEGRAM_CHAT_ID: ${{secrets.SECRET_TELEGRAM_CHAT_ID}}
SECRET_DISCORD_WEBHOOK_URL: ${{secrets.SECRET_DISCORD_WEBHOOK_URL}}
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Also, prepare the following secrets
- Cloudflare API token with `Edit Cloudflare Workers` permissions
- Slack incoming webhook \(optional\)
- Discord incoming webhook \(optional\)
- Ntfy incoming webhook \(optional\)
- Ntfy username \(optional\)
- Ntfy password \(optional\)

## Getting started

Expand All @@ -42,6 +45,15 @@ You can either deploy with **Cloudflare Deploy Button** using GitHub Actions or

- Name: SECRET_DISCORD_WEBHOOK_URL (optional)
- Value: your-discord-webhook-url

- Name: SECRET_NTFY_WEBHOOK_URL (optional)
- Value: your-ntfy-webhook-url

- Name: SECRET_NTFY_USERNAME (optional)
- Value: your-ntfy-username

- Name: SECRET_NTFY_PASSWORD (optional)
- Value: your-ntfy-password
```
3. Navigate to the **Actions** settings in your repository and enable them
Expand All @@ -54,6 +66,7 @@ You can either deploy with **Cloudflare Deploy Button** using GitHub Actions or
logo: logo-192x192.png # image in ./public/ folder
daysInHistogram: 90 # number of days you want to display in histogram
collectResponseTimes: false # experimental feature, enable only for <5 monitors or on paid plans
ntfyPriority: 'urgent' # priority for ntfy notifications

# configurable texts across the status page
allmonitorsOperational: 'All Systems Operational'
Expand Down Expand Up @@ -102,6 +115,9 @@ You can clone the repository yourself and use Wrangler CLI to develop/deploy, ex
- create Worker secrets _\(optional\)_
- `SECRET_SLACK_WEBHOOK_URL`
- `SECRET_DISCORD_WEBHOOK_URL`
- `SECRET_NTFY_WEBHOOK_URL`
- `SECRET_NTFY_USERNAME`
- `SECRET_NTFY_PASSWORD`

## Workers KV free tier

Expand Down
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ settings:
logo: logo-192x192.png # image in ./public/ folder
daysInHistogram: 90 # number of days you want to display in histogram
collectResponseTimes: true # collects avg response times from CRON locations
ntfyPriority: 'urgent'

allmonitorsOperational: 'All Systems Operational'
notAllmonitorsOperational: 'Not All Systems Operational'
Expand Down
10 changes: 10 additions & 0 deletions src/functions/cronTrigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getKVMonitors,
setKVMonitors,
notifyDiscord,
notifyNtfy
} from './helpers'

function getDate() {
Expand Down Expand Up @@ -89,6 +90,15 @@ export async function processCronTrigger(event) {
event.waitUntil(notifyTelegram(monitor, monitorOperational))
}

// Send ntfy message on monitor change
if (
monitorStatusChanged &&
typeof SECRET_NTFY_WEBHOOK_URL !== 'undefined' &&
SECRET_NTFY_WEBHOOK_URL !== 'default-gh-action-secret'
) {
event.waitUntil(notifyNtfy(monitor, monitorOperational))
}

// Send Discord message on monitor change
if (
monitorStatusChanged &&
Expand Down
25 changes: 25 additions & 0 deletions src/functions/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,32 @@ export async function notifyTelegram(monitor, operational) {
const telegramUrl = `https://api.telegram.org/bot${SECRET_TELEGRAM_API_TOKEN}/sendMessage`
return fetch(telegramUrl, {
body: payload,
method: 'POST'
})
}

export async function notifyNtfy(monitor, operational) {
const text = `${monitor.method ? monitor.method : 'GET'} ${
monitor.url
}`
const headers = {
'Title': `Monitor ${monitor.name} changed status to ${getOperationalLabel(operational)} ${operational ? '✅' : '❌'}`,
'Priority': config.settings.ntfyPriority,
'Click': `${config.settings.url}`,
'Tags': 'status-page'
}

if (typeof SECRET_NTFY_USERNAME !== 'undefined' &&
SECRET_NTFY_USERNAME !== 'default-gh-action-secret' &&
typeof SECRET_NTFY_PASSWORD !== 'undefined' &&
SECRET_NTFY_PASSWORD !== 'default-gh-action-secret') {
headers.authorization = `Basic ${Buffer.from(`${SECRET_NTFY_USERNAME}:${SECRET_NTFY_PASSWORD}`).toString('base64')}`
}

fetch(SECRET_NTFY_WEBHOOK_URL, {
body: text,
method: 'POST',
headers
})
}

Expand Down

0 comments on commit 29bf2b5

Please sign in to comment.