Skip to content

Instantly share code, notes, and snippets.

@Nbc66
Created May 7, 2024 15:59
Show Gist options
  • Save Nbc66/42cb525102be73a8d67f253fc84a9dcd to your computer and use it in GitHub Desktop.
Save Nbc66/42cb525102be73a8d67f253fc84a9dcd to your computer and use it in GitHub Desktop.
A Cloudflare Worker script to be used with statuspage.io websites that allow you to subscibe to updates using a webhook
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
// Verify request method
if (request.method !== 'POST') {
return new Response('Method Not Allowed', { status: 405 })
}
// Verify request headers
const contentType = request.headers.get('content-type')
if (!contentType || contentType.indexOf('application/json') === -1) {
return new Response('Unsupported Media Type', { status: 415 })
}
// Extract JSON body
const body = await request.json()
// Check if request is from statsupage.io
if (!body.component_update && !body.incident) {
return new Response('Invalid Webhook Payload', { status: 400 })
}
// Status page link
const statusPageLink = `https://www.githubstatus.com/`
// Format webhook data for Discord
let discordPayload = {
embeds: []
}
if (body.component_update) {
const componentUpdate = body.component_update
const color = componentUpdate.new_status === 'operational' ? 65280 : 16763904; // Green or Orange
discordPayload.embeds.push({
title: 'Component Update',
color: color,
fields: [
{ name: 'Component Name', value: body.component.name, inline: true },
{ name: 'Old Status', value: componentUpdate.old_status, inline: true },
{ name: 'New Status', value: componentUpdate.new_status, inline: true },
{ name: 'Status Page', value: `[View Status](${statusPageLink})`, inline: false }
]
})
}
if (body.incident) {
const incident = body.incident
const latestUpdate = incident.incident_updates[0]
const color = latestUpdate.status === 'resolved' ? 65280 : 16711680; // Green or Red
discordPayload.embeds.push({
title: 'Incident Update',
color: color,
fields: [
{ name: 'Incident Name', value: incident.name, inline: true },
{ name: 'Status', value: latestUpdate.status, inline: true },
{ name: 'Impact', value: incident.impact, inline: true },
{ name: 'Latest Update', value: latestUpdate.body },
{ name: 'Status Page', value: `[View Status](${statusPageLink})`, inline: false }
]
})
}
// Post to Discord webhook URL
const discordWebhookURL = 'DISCORD_WEBHOOK_HERE'
const discordResponse = await fetch(discordWebhookURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(discordPayload)
})
if (!discordResponse.ok) {
return new Response('Failed to send Discord message', { status: 500 })
}
return new Response('Webhook processed successfully', { status: 200 })
}
@Nbc66
Copy link
Author

Nbc66 commented May 7, 2024

make sure you use the worker URL when subscribing to the webhook on a statuspage.io website

This webhook can be used on basically any statuspage.io website that supports webhook updates

just edit the script's status page link and you are good to go

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment