Skip to content

Commit

Permalink
Merge pull request #17 from kolaente/feature/telegram-notifications
Browse files Browse the repository at this point in the history
feat: telegram notifications
  • Loading branch information
eidam authored Nov 22, 2020
2 parents 81bcf97 + a41f9d2 commit c5eb49c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,17 @@ jobs:
echo "[env.production]" >> wrangler.toml
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_TELEGRAM_CHAT_ID" ] && echo "Secret SECRET_TELEGRAM_CHAT_ID not set, creating dummy one..." && SECRET_TELEGRAM_CHAT_ID="default-gh-action-secret" || true
postCommands: |
yarn kv-gc
secrets: |
SECRET_SLACK_WEBHOOK_URL
SECRET_TELEGRAM_API_TOKEN
SECRET_TELEGRAM_CHAT_ID
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_TELEGRAM_CHAT_ID: ${{secrets.SECRET_TELEGRAM_CHAT_ID}}
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ You can either deploy with **Cloudflare Deploy Button** using GitHub Actions or
- e.g. `status-page.eidam.dev/*` _\(make sure you include `/*` as the Worker also serve static files\)_
8. _\(optional\)_ Edit [wrangler.toml](./wrangler.toml) to adjust Worker settings or CRON Trigger schedule, especially if you are on [Workers Free plan](#workers-kv-free-tier)

### Telegram notifications

To enable telegram notifications, you'll need to take a few additional steps.

1. [Create a new Bot](https://core.telegram.org/bots#creating-a-new-bot)
2. Set the api token you received when creating the bot as content of the `SECRET_TELEGRAM_API_TOKEN` secret in your github repository.
3. Send a message to the bot from the telegram account which should receive the alerts (Something more than `/start`)
4. Get the chat id with `curl https://api.telegram.org/bot<YOUR TELEGRAM API TOKEN>/getUpdates | jq '.result[0] .message .chat .id'`
5. Set the retrieved chat id in the `SECRET_TELEGRAM_CHAT_ID` secret variable
6. Redeploy the status page using the github action

### Deploy on your own

You can clone the repository yourself and use Wrangler CLI to develop/deploy, extra list of things you need to take care of:
Expand Down
10 changes: 10 additions & 0 deletions src/functions/cronTrigger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import config from '../../config.yaml'

import {
notifySlack,
notifyTelegram,
getCheckLocation,
getKVMonitors,
setKVMonitors,
Expand Down Expand Up @@ -76,6 +77,15 @@ export async function processCronTrigger(event) {
event.waitUntil(notifySlack(monitor, monitorOperational))
}

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

// make sure checkDay exists in checks in cases when needed
if (
(config.settings.collectResponseTimes || !monitorOperational) &&
Expand Down
28 changes: 23 additions & 5 deletions src/functions/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export async function setKVMonitors(data) {
return setKV(kvDataKey, JSON.stringify(data))
}

const getOperationalLabel = operational => {
return operational
? config.settings.monitorLabelOperational
: config.settings.monitorLabelNotOperational
}

export async function setKV(key, value, metadata, expirationTtl) {
return KV_STATUS_PAGE.put(key, value, { metadata, expirationTtl })
}
Expand All @@ -27,11 +33,7 @@ export async function notifySlack(monitor, operational) {
type: 'section',
text: {
type: 'mrkdwn',
text: `Monitor *${monitor.name}* changed status to *${
operational
? config.settings.monitorLabelOperational
: config.settings.monitorLabelNotOperational
}*`,
text: `Monitor *${monitor.name}* changed status to *${getOperationalLabel(operational)}*`,
},
},
{
Expand All @@ -58,6 +60,22 @@ export async function notifySlack(monitor, operational) {
})
}

export async function notifyTelegram(monitor, operational) {
const text = `Monitor *${monitor.name.replace('-', '\\-')}* changed status to *${getOperationalLabel(operational)}*
${operational ? '✅' : '❌'} \`${monitor.method ? monitor.method : "GET"} ${monitor.url}\` \\- 👀 [Status Page](${config.settings.url})`

const payload = new FormData()
payload.append('chat_id', SECRET_TELEGRAM_CHAT_ID)
payload.append('parse_mode', 'MarkdownV2')
payload.append('text', text)

const telegramUrl = `https://api.telegram.org/bot${SECRET_TELEGRAM_API_TOKEN}/sendMessage`
return fetch(telegramUrl, {
body: payload,
method: 'POST',
})
}

export function useKeyPress(targetKey) {
const [keyPressed, setKeyPressed] = useState(false)

Expand Down

0 comments on commit c5eb49c

Please sign in to comment.