Skip to content

Open-source status page, incident response and uptime monitoring

🚀 SMS notifications (#463)

Thibault Le Ouaya4c8e0f3499b
diff --git a/apps/server/.env.example b/apps/server/.env.example
index 90f7308a5..c543d7218 100644
--- a/apps/server/.env.example
+++ b/apps/server/.env.example
@@ -5,4 +5,6 @@ UNKEY_TOKEN=
TINY_BIRD_API_KEY=
UPSTASH_REDIS_REST_URL=
UPSTASH_REDIS_REST_TOKEN=
-RESEND_API_KEY=
\ No newline at end of file
+RESEND_API_KEY=
+TWILLIO_AUTH_TOKEN=your_auth_token
+TWILLIO_ACCOUNT_ID=your_account_id
diff --git a/apps/server/package.json b/apps/server/package.json
index 1c6582aba..ebf91ead3 100644
--- a/apps/server/package.json
+++ b/apps/server/package.json
@@ -17,6 +17,7 @@
"@openstatus/notification-discord": "workspace:*",
"@openstatus/notification-emails": "workspace:*",
"@openstatus/notification-slack": "workspace:*",
+ "@openstatus/notification-twillio-sms": "workspace:*",
"@openstatus/plans": "workspace:*",
"@openstatus/tinybird": "workspace:*",
"@openstatus/upstash": "workspace:*",
diff --git a/apps/server/src/checker/utils.ts b/apps/server/src/checker/utils.ts
index eee5c7a89..6d7e4b804 100644
--- a/apps/server/src/checker/utils.ts
+++ b/apps/server/src/checker/utils.ts
@@ -6,6 +6,7 @@ import type {
import { sendDiscordMessage } from "@openstatus/notification-discord";
import { send as sendEmail } from "@openstatus/notification-emails";
import { sendSlackMessage } from "@openstatus/notification-slack";
+import { sendTextMessage } from "@openstatus/notification-twillio-sms";
import type { flyRegionsDict } from "@openstatus/utils";
type SendNotification = ({
@@ -24,4 +25,5 @@ export const providerToFunction = {
email: sendEmail,
slack: sendSlackMessage,
discord: sendDiscordMessage,
+ sms: sendTextMessage,
} satisfies Record<NotificationProvider, SendNotification>;
diff --git a/apps/server/src/v1/incident.ts b/apps/server/src/v1/incident.ts
index a78cc0330..ceca4336d 100644
--- a/apps/server/src/v1/incident.ts
+++ b/apps/server/src/v1/incident.ts
@@ -7,6 +7,7 @@ import {
incidentUpdate,
} from "@openstatus/db/src/schema";
+import { incidentUpdateSchema } from "./incidentUpdate";
import type { Variables } from "./index";
import { ErrorSchema } from "./shared";
@@ -26,7 +27,7 @@ const ParamsSchema = z.object({
}),
});
-const incidentUpdateSchema = z.object({
+const createIncidentUpdateSchema = z.object({
status: z.enum(incidentStatus).openapi({
description: "The status of the update",
}),
@@ -280,7 +281,7 @@ const postRouteUpdate = createRoute({
description: "the incident update",
content: {
"application/json": {
- schema: incidentUpdateSchema,
+ schema: createIncidentUpdateSchema,
},
},
},
diff --git a/apps/server/src/v1/incidentUpdate.ts b/apps/server/src/v1/incidentUpdate.ts
index 07ebace54..d56b52e85 100644
--- a/apps/server/src/v1/incidentUpdate.ts
+++ b/apps/server/src/v1/incidentUpdate.ts
@@ -26,7 +26,7 @@ const ParamsSelectSchema = z.object({
}),
});
-const incidentUpdateSchema = z.object({
+export const incidentUpdateSchema = z.object({
status: z.enum(incidentStatus).openapi({
description: "The status of the update",
}),
diff --git a/apps/web/src/app/app/(dashboard)/[workspaceSlug]/notifications/edit/page.tsx b/apps/web/src/app/app/(dashboard)/[workspaceSlug]/notifications/edit/page.tsx
index 33c7ec8e6..84b1d66c5 100644
--- a/apps/web/src/app/app/(dashboard)/[workspaceSlug]/notifications/edit/page.tsx
+++ b/apps/web/src/app/app/(dashboard)/[workspaceSlug]/notifications/edit/page.tsx
@@ -13,7 +13,6 @@ const searchParamsSchema = z.object({
});
export default async function EditPage({
- params,
searchParams,
}: {
params: { workspaceSlug: string };
@@ -24,6 +23,7 @@ export default async function EditPage({
if (!search.success) {
return notFound();
}
+ const workspace = await api.workspace.getWorkspace.query();
const { id } = search.data;
@@ -42,7 +42,10 @@ export default async function EditPage({
}
/>
<div className="col-span-full">
- <NotificationForm defaultValues={notification} />
+ <NotificationForm
+ defaultValues={notification}
+ workspacePlan={workspace.plan}
+ />
</div>
</div>
);
diff --git a/apps/web/src/components/forms/monitor-form.tsx b/apps/web/src/components/forms/monitor-form.tsx
index 4b0e83dea..35e7f96f7 100644
--- a/apps/web/src/components/forms/monitor-form.tsx
+++ b/apps/web/src/components/forms/monitor-form.tsx
@@ -711,7 +711,10 @@ export function MonitorForm({
Get alerted when your endpoint is down.
</DialogDescription>
</DialogHeader>
- <NotificationForm onSubmit={() => setOpenDialog(false)} />
+ <NotificationForm
+ onSubmit={() => setOpenDialog(false)}
+ workspacePlan={plan}
+ />
</DialogContent>
<FailedPingAlertConfirmation
monitor={form.getValues()}
diff --git a/apps/web/src/components/forms/notification-form.tsx b/apps/web/src/components/forms/notification-form.tsx
index c549d2c02..62d1c30cf 100644
--- a/apps/web/src/components/forms/notification-form.tsx
+++ b/apps/web/src/components/forms/notification-form.tsx
@@ -8,6 +8,7 @@ import { useForm } from "react-hook-form";
import type {
InsertNotification,
NotificationProvider,
+ WorkspacePlan,
} from "@openstatus/db/src/schema";
import {
insertNotificationSchema,
@@ -73,6 +74,14 @@ function getProviderMetaData(provider: NotificationProvider) {
setupDocLink: "https://support.discord.com/hc/en-us/articles/228383668",
sendTest: sendTestDiscordMessage,
};
+ case "sms":
+ return {
+ dataType: "tel",
+ placeholder: "+123456789",
+ setupDocLink: null,
+ sendTest: null,
+ isPro: true,
+ };
default:
return {
@@ -87,11 +96,13 @@ function getProviderMetaData(provider: NotificationProvider) {
interface Props {
defaultValues?: InsertNotification;
onSubmit?: () => void;
+ workspacePlan: WorkspacePlan;
}
export function NotificationForm({
defaultValues,
onSubmit: onExternalSubmit,
+ workspacePlan,
}: Props) {
const [isPending, startTransition] = useTransition();
const [isTestPending, startTestTransition] = useTransition();
@@ -232,6 +243,9 @@ export function NotificationForm({
type={providerMetaData.dataType}
placeholder={providerMetaData.placeholder}
{...field}
+ disabled={
+ providerMetaData.isPro && workspacePlan !== "pro"
+ }
/>
</FormControl>
<FormDescription className="flex items-center justify-between">
diff --git a/apps/web/src/components/modals/notification-dialog.tsx b/apps/web/src/components/modals/notification-dialog.tsx
deleted file mode 100644
index e76e4f41b..000000000
--- a/apps/web/src/components/modals/notification-dialog.tsx
+++ /dev/null
@@ -1,37 +0,0 @@
-"use client";
-
-import { useState } from "react";
-
-import {
- Button,
- Dialog,
- DialogContent,
- DialogDescription,
- DialogHeader,
- DialogTitle,
- DialogTrigger,
-} from "@openstatus/ui";
-
-import { NotificationForm } from "@/components/forms/notification-form";
-
-export const NotificationDialog = ({}: { workspaceSlug: string }) => {
- const [open, setOpen] = useState(false);
- return (
- <Dialog open={open} onOpenChange={(val) => setOpen(val)}>
- <DialogTrigger asChild>
- <Button variant="default" size="sm">
- Add Notifications
- </Button>
- </DialogTrigger>
- <DialogContent className="sm:max-w-2xl">
- <DialogHeader>
- <DialogTitle>Add Notification</DialogTitle>
- <DialogDescription>
- Get alerted when your endpoint is down.
- </DialogDescription>
- </DialogHeader>
- <NotificationForm onSubmit={() => setOpen(false)} />
- </DialogContent>
- </Dialog>
- );
-};
diff --git a/packages/db/src/schema/notifications/constants.ts b/packages/db/src/schema/notifications/constants.ts
index 5803c5583..90d0790fa 100644
--- a/packages/db/src/schema/notifications/constants.ts
+++ b/packages/db/src/schema/notifications/constants.ts
@@ -1 +1,6 @@
-export const notificationProvider = ["email", "discord", "slack"] as const;
+export const notificationProvider = [
+ "email",
+ "discord",
+ "slack",
+ "sms",
+] as const;
diff --git a/packages/db/src/schema/notifications/notification.ts b/packages/db/src/schema/notifications/notification.ts
index d0bf7e178..9c0df27fb 100644
--- a/packages/db/src/schema/notifications/notification.ts
+++ b/packages/db/src/schema/notifications/notification.ts
@@ -5,8 +5,6 @@ import {
sqliteTable,
text,
} from "drizzle-orm/sqlite-core";
-import { createInsertSchema, createSelectSchema } from "drizzle-zod";
-import * as z from "zod";
import { monitor } from "../monitors";
import { workspace } from "../workspaces";
diff --git a/packages/notifications/twillio-sms/.env.example b/packages/notifications/twillio-sms/.env.example
new file mode 100644
index 000000000..07a929433
--- /dev/null
+++ b/packages/notifications/twillio-sms/.env.example
@@ -0,0 +1,2 @@
+TWILLIO_AUTH_TOKEN=your_auth_token
+TWILLIO_ACCOUNT_ID=your_account_id
diff --git a/packages/notifications/twillio-sms/package.json b/packages/notifications/twillio-sms/package.json
new file mode 100644
index 000000000..7958b5e49
--- /dev/null
+++ b/packages/notifications/twillio-sms/package.json
@@ -0,0 +1,20 @@
+{
+ "name": "@openstatus/notification-twillio-sms",
+ "version": "0.0.0",
+ "main": "src/index.ts",
+ "dependencies": {
+ "@openstatus/db": "workspace:*",
+ "@t3-oss/env-core": "0.7.1",
+ "@types/validator": "13.11.6",
+ "validator": "13.11.0",
+ "zod": "3.22.2"
+ },
+ "devDependencies": {
+ "@openstatus/tsconfig": "workspace:*",
+ "@types/node": "20.8.0",
+ "@types/react": "18.2.24",
+ "@types/react-dom": "18.2.8",
+ "next": "13.5.3",
+ "typescript": "5.2.2"
+ }
+}
diff --git a/packages/notifications/twillio-sms/src/env.ts b/packages/notifications/twillio-sms/src/env.ts
new file mode 100644
index 000000000..49035cca7
--- /dev/null
+++ b/packages/notifications/twillio-sms/src/env.ts
@@ -0,0 +1,30 @@
+import { createEnv } from "@t3-oss/env-core";
+import { z } from "zod";
+
+export const env = createEnv({
+ server: {
+ TWILLIO_AUTH_TOKEN: z.string(),
+ TWILLIO_ACCOUNT_ID: z.string(),
+ },
+
+ /**
+ * What object holds the environment variables at runtime. This is usually
+ * `process.env` or `import.meta.env`.
+ */
+ runtimeEnv: process.env,
+
+ /**
+ * By default, this library will feed the environment variables directly to
+ * the Zod validator.
+ *
+ * This means that if you have an empty string for a value that is supposed
+ * to be a number (e.g. `PORT=` in a ".env" file), Zod will incorrectly flag
+ * it as a type mismatch violation. Additionally, if you have an empty string
+ * for a value that is supposed to be a string with a default value (e.g.
+ * `DOMAIN=` in an ".env" file), the default value will never be applied.
+ *
+ * In order to solve these issues, we recommend that all new projects
+ * explicitly specify this option as true.
+ */
+ skipValidation: true,
+});
diff --git a/packages/notifications/twillio-sms/src/index.ts b/packages/notifications/twillio-sms/src/index.ts
new file mode 100644
index 000000000..173fed415
--- /dev/null
+++ b/packages/notifications/twillio-sms/src/index.ts
@@ -0,0 +1,47 @@
+import type { Monitor, Notification } from "@openstatus/db/src/schema";
+
+import { env } from "./env";
+import { SmsConfigurationSchema } from "./schema/config";
+
+export const sendTextMessage = async ({
+ monitor,
+ notification,
+ region,
+ statusCode,
+}: {
+ monitor: Monitor;
+ notification: Notification;
+ statusCode: number;
+ region: string;
+}) => {
+ const notificationData = SmsConfigurationSchema.parse(
+ JSON.parse(notification.data),
+ );
+ const { name } = monitor;
+
+ const body = new FormData();
+ body.set("To", notificationData.phoneNumber);
+ body.set("From", "+14807252613");
+ body.set(
+ "Body",
+ `Your monitor ${name} / ${monitor.url} is down in ${region} with status code ${statusCode}`,
+ );
+
+ try {
+ fetch(
+ `https://api.twilio.com/2010-04-01/Accounts/${env.TWILLIO_ACCOUNT_ID}/Messages.json`,
+ {
+ method: "post",
+ body,
+ headers: {
+ Authorization: `Basic ${btoa(
+ `${env.TWILLIO_ACCOUNT_ID}:${env.TWILLIO_AUTH_TOKEN}`,
+ )}`,
+ },
+ },
+ );
+ } catch (err) {
+ console.log(err);
+ // Do something
+ }
+};
diff --git a/packages/notifications/twillio-sms/src/schema/config.ts b/packages/notifications/twillio-sms/src/schema/config.ts
new file mode 100644
index 000000000..92e165ec3
--- /dev/null
+++ b/packages/notifications/twillio-sms/src/schema/config.ts
@@ -0,0 +1,6 @@
+import isMobilephone from "validator/lib/isMobilePhone";
+import { z } from "zod";
+
+export const SmsConfigurationSchema = z.object({
+ phoneNumber: z.string().refine(isMobilephone),
+});
diff --git a/packages/notifications/twillio-sms/tsconfig.json b/packages/notifications/twillio-sms/tsconfig.json
new file mode 100644
index 000000000..a8a8c9309
--- /dev/null
+++ b/packages/notifications/twillio-sms/tsconfig.json
@@ -0,0 +1,4 @@
+{
+ "extends": "@openstatus/tsconfig/nextjs.json",
+ "include": ["src", "*.ts"]
+}
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 01107128a..049a1c313 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -55,6 +55,9 @@ importers:
'@openstatus/notification-slack':
specifier: workspace:*
version: link:../../packages/notifications/slack
+ '@openstatus/notification-twillio-sms':
+ specifier: workspace:*
+ version: link:../../packages/notifications/twillio-sms
'@openstatus/plans':
specifier: workspace:*
version: link:../../packages/plans
@@ -661,6 +664,43 @@ importers:
specifier: 5.2.2
version: 5.2.2
+ packages/notifications/twillio-sms:
+ dependencies:
+ '@openstatus/db':
+ specifier: workspace:*
+ version: link:../../db
+ '@t3-oss/env-core':
+ specifier: 0.7.1
+ '@types/validator':
+ specifier: 13.11.6
+ version: 13.11.6
+ validator:
+ specifier: 13.11.0
+ version: 13.11.0
+ zod:
+ specifier: 3.22.2
+ version: 3.22.2
+ devDependencies:
+ '@openstatus/tsconfig':
+ specifier: workspace:*
+ version: link:../../tsconfig
+ '@types/node':
+ specifier: 20.8.0
+ version: 20.8.0
+ '@types/react':
+ specifier: 18.2.24
+ version: 18.2.24
+ '@types/react-dom':
+ specifier: 18.2.8
+ version: 18.2.8
+ next:
+ specifier: 13.5.3
+ typescript:
+ specifier: 5.2.2
+ version: 5.2.2
+
packages/plans:
dependencies:
'@openstatus/db':
@@ -5223,6 +5263,10 @@ packages:
resolution: {integrity: sha512-zC0iXxAv1C1ERURduJueYzkzZ2zaGyc+P2c95hgkikHPr3z8EdUZOlgEQ5X0DRmwDZn+hekycQnoeiiRVrmilQ==}
+ resolution: {integrity: sha512-HUgHujPhKuNzgNXBRZKYexwoG+gHKU+tnfPqjWXFghZAnn73JElicMkuSKJyLGr9JgyA8IgK7fj88IyA9rwYeQ==}
+ dev: false
+
resolution: {integrity: sha512-flUksGIQCnJd6sZ1l5dqCEG/ksaoAg/eUwiLAGTJQcfgvZJKF++Ta4bJA6A5aPSJmsr+xlseHn4KLgVlNnvPTg==}
dependencies:
@@ -12866,6 +12910,11 @@ packages:
builtins: 5.0.1
dev: true
+ resolution: {integrity: sha512-Ii+sehpSfZy+At5nPdnyMhx78fEoPDkR2XW/zimHEL3MyGJQOCQ7WeP20jPYRz7ZCpcKLB21NxuXHF3bxjStBQ==}
+ engines: {node: '>= 0.10'}
+ dev: false
+
resolution: {integrity: sha512-YF23YMyASIIJXpktBa4vIGLJ5Gs88UB/XePgqPmTa7cDA+JeO3yclbpheQYCHjVHBn/yePzrXuygIL+xbvRYHw==}
dependencies:
diff --git a/vitest.workspace.ts b/vitest.workspace.ts
deleted file mode 100644
index c1efc9a8b..000000000
--- a/vitest.workspace.ts
+++ /dev/null
@@ -1 +0,0 @@
-export default ["packages/*"];