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.2 kB ยท 89 lines
TypeScript
at incident-api
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990import { telegramDataSchema } from "@openstatus/db/src/schema";import type { NotificationContext } from "@openstatus/notification-base";
export const sendAlert = async ({ monitor, notification, statusCode, message,}: NotificationContext) => { const notificationData = telegramDataSchema.parse( JSON.parse(notification.data), ); const { name } = monitor;
const body = `Your monitor ${name} / ${monitor.url} is down with ${ statusCode ? `status code ${statusCode}` : `error: ${message}` }`;
await sendMessage({ chatId: notificationData.telegram.chatId, message: body, });};
export const sendRecovery = async ({ monitor, notification,}: NotificationContext) => { const notificationData = telegramDataSchema.parse( JSON.parse(notification.data), ); const { name } = monitor;
const body = `Your monitor ${name} / ${monitor.url} is up again`; await sendMessage({ chatId: notificationData.telegram.chatId, message: body, });};
export const sendDegraded = async ({ monitor, notification,}: NotificationContext) => { const notificationData = telegramDataSchema.parse( JSON.parse(notification.data), ); const { name } = monitor;
const body = `Your monitor ${name} / ${monitor.url} is degraded `;
await sendMessage({ chatId: notificationData.telegram.chatId, message: body, });};
export const sendTest = async ({ chatId }: { chatId: string }) => { try { await sendMessage({ chatId, message: "This is a test message from OpenStatus. You are good to go!", }); } catch (err) { console.log(err); return false; } return true;};
export async function sendMessage({ chatId, message,}: { chatId: string; message: string;}) { if (!process.env.TELEGRAM_BOT_TOKEN) { throw new Error("TELEGRAM_BOT_TOKEN is not set"); } const params = new URLSearchParams({ chat_id: chatId, text: message }); const res = await fetch( `https://api.telegram.org/bot${process.env.TELEGRAM_BOT_TOKEN}/sendMessage?${params}`, ); if (!res.ok) { throw new Error(`Failed to send telegram message: ${res.statusText}`); } return res;}