Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
2.9 kB ยท 109 lines
TypeScript
at incident-api
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110import { googleChatDataSchema } from "@openstatus/db/src/schema";import type { NotificationContext } from "@openstatus/notification-base";import { safeFetch } from "@openstatus/utils";
const postToWebhook = async (content: string, webhookUrl: string) => { const res = await safeFetch(webhookUrl, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ text: content, }), }); if (!res.ok) { throw new Error( `Failed to send Google Chat webhook: ${res.status} ${res.statusText}`, ); }};
export const sendAlert = async ({ monitor, notification, statusCode, message, cronTimestamp,}: NotificationContext) => { const notificationData = googleChatDataSchema.parse( JSON.parse(notification.data), ); const { "google-chat": webhookUrl } = notificationData; // webhook url const { name } = monitor;
try { await postToWebhook( `*๐จ Alert <${monitor.url}|${name}>*\nStatus Code: ${ statusCode || "_empty_" }\nMessage: ${ message || "_empty_" }\nCron Timestamp: ${cronTimestamp} (${new Date( cronTimestamp, ).toISOString()})\n> Check your <https://www.openstatus.dev/app/|Dashboard>.\n`, webhookUrl, ); } catch (err) { console.error(err); throw err; }};
export const sendRecovery = async ({ monitor, notification,}: NotificationContext) => { const notificationData = googleChatDataSchema.parse( JSON.parse(notification.data), ); const { "google-chat": webhookUrl } = notificationData; // webhook url const { name } = monitor;
try { await postToWebhook( `*โ
Recovered <${monitor.url}|${name}>*\n> Check your <https://www.openstatus.dev/app/|Dashboard>.\n`, webhookUrl, ); } catch (err) { console.error(err); throw err; }};
export const sendDegraded = async ({ monitor, notification,}: NotificationContext) => { const notificationData = googleChatDataSchema.parse( JSON.parse(notification.data), ); const { "google-chat": webhookUrl } = notificationData; // webhook url const { name } = monitor;
try { await postToWebhook( `*โ ๏ธ Degraded <${monitor.url}|${name}>*\n> Check your <https://www.openstatus.dev/app/|Dashboard>.\n`, webhookUrl, ); } catch (err) { console.error(err); throw err; }};
export const sendTest = async (webhookUrl: string) => { if (!webhookUrl) { return false; } console.log(webhookUrl); try { await postToWebhook( "*๐งช Test <https://www.openstatus.dev/|OpenStatus>*\nIf you can read this, your Google Chat webhook is functioning correctly!\n> Check your <https://www.openstatus.dev/app/|Dashboard>.\n", webhookUrl, ); return true; } catch (_err) { console.log(_err); return false; }};