1
0
mirror of https://github.com/tormachris/cf-workers-status-page.git synced 2025-07-17 04:15:15 +02:00

feat: telegram notifications

This commit is contained in:
kolaente
2020-11-22 15:29:22 +01:00
parent 81bcf9770e
commit a41f9d28c1
4 changed files with 50 additions and 5 deletions

View File

@ -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 })
}
@ -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)}*`,
},
},
{
@ -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)