mirror of
				https://github.com/tormachris/cf-workers-status-page.git
				synced 2025-11-04 12:56:24 +01:00 
			
		
		
		
	feat: telegram notifications
This commit is contained in:
		@@ -2,6 +2,7 @@ import config from '../../config.yaml'
 | 
			
		||||
 | 
			
		||||
import {
 | 
			
		||||
  notifySlack,
 | 
			
		||||
  notifyTelegram,
 | 
			
		||||
  getCheckLocation,
 | 
			
		||||
  getKVMonitors,
 | 
			
		||||
  setKVMonitors,
 | 
			
		||||
@@ -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) &&
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user