From 9152181a00a189c5392d8a16706bb174bbba47a7 Mon Sep 17 00:00:00 2001 From: Kartik Saini <41051387+kart1ka@users.noreply.github.com> Date: Fri, 14 Feb 2025 09:26:19 +0530 Subject: [PATCH] fix: discord webhook not pinging (#4673) Co-authored-by: Piyush Gupta Co-authored-by: Dhruwang Co-authored-by: Dhruwang Jariwala <67850763+Dhruwang@users.noreply.github.com> --- .../webhooks/components/add-webhook-modal.tsx | 6 +++++- apps/web/modules/integrations/webhooks/lib/utils.ts | 6 ++++++ .../web/modules/integrations/webhooks/lib/webhook.ts | 12 ++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/apps/web/modules/integrations/webhooks/components/add-webhook-modal.tsx b/apps/web/modules/integrations/webhooks/components/add-webhook-modal.tsx index 8e46c1d04f..4c86fc3832 100644 --- a/apps/web/modules/integrations/webhooks/components/add-webhook-modal.tsx +++ b/apps/web/modules/integrations/webhooks/components/add-webhook-modal.tsx @@ -3,7 +3,7 @@ import { getFormattedErrorMessage } from "@/lib/utils/helper"; import { SurveyCheckboxGroup } from "@/modules/integrations/webhooks/components/survey-checkbox-group"; import { TriggerCheckboxGroup } from "@/modules/integrations/webhooks/components/trigger-checkbox-group"; -import { validWebHookURL } from "@/modules/integrations/webhooks/lib/utils"; +import { isDiscordWebhook, validWebHookURL } from "@/modules/integrations/webhooks/lib/utils"; import { Button } from "@/modules/ui/components/button"; import { Input } from "@/modules/ui/components/input"; import { Label } from "@/modules/ui/components/label"; @@ -113,6 +113,10 @@ export const AddWebhookModal = ({ environmentId, surveys, open, setOpen }: AddWe throw new Error(t("common.please_select_at_least_one_survey")); } + if (isDiscordWebhook(testEndpointInput)) { + throw new Error(t("environments.integrations.webhooks.discord_webhook_not_supported")); + } + const endpointHitSuccessfully = await handleTestEndpoint(false); if (!endpointHitSuccessfully) return; diff --git a/apps/web/modules/integrations/webhooks/lib/utils.ts b/apps/web/modules/integrations/webhooks/lib/utils.ts index c50a276b0d..4836e67dec 100644 --- a/apps/web/modules/integrations/webhooks/lib/utils.ts +++ b/apps/web/modules/integrations/webhooks/lib/utils.ts @@ -35,3 +35,9 @@ export const validWebHookURL = (urlInput: string) => { return { valid: false, error: "Invalid URL format. Please enter a complete URL including https://" }; } }; + +export const isDiscordWebhook = (urlString: string) => { + const url = new URL(urlString); + const DISCORD_WEBHOOK_URL_PATTERN = /^https:\/\/discord\.com\/api\/webhooks\/\d+\/.+$/; + return DISCORD_WEBHOOK_URL_PATTERN.test(url.toString()); +}; diff --git a/apps/web/modules/integrations/webhooks/lib/webhook.ts b/apps/web/modules/integrations/webhooks/lib/webhook.ts index 054aa871dc..dcab09ce28 100644 --- a/apps/web/modules/integrations/webhooks/lib/webhook.ts +++ b/apps/web/modules/integrations/webhooks/lib/webhook.ts @@ -1,4 +1,5 @@ import { webhookCache } from "@/lib/cache/webhook"; +import { isDiscordWebhook } from "@/modules/integrations/webhooks/lib/utils"; import { Prisma, Webhook } from "@prisma/client"; import { prisma } from "@formbricks/database"; import { cache } from "@formbricks/lib/cache"; @@ -70,6 +71,9 @@ export const deleteWebhook = async (id: string): Promise => { export const createWebhook = async (environmentId: string, webhookInput: TWebhookInput): Promise => { try { + if (isDiscordWebhook(webhookInput.url)) { + throw new UnknownError("Discord webhooks are currently not supported."); + } const createdWebhook = await prisma.webhook.create({ data: { ...webhookInput, @@ -136,6 +140,10 @@ export const testEndpoint = async (url: string): Promise => { const controller = new AbortController(); const timeout = setTimeout(() => controller.abort(), 5000); + if (isDiscordWebhook(url)) { + throw new UnknownError("Discord webhooks are currently not supported."); + } + const response = await fetch(url, { method: "POST", body: JSON.stringify({ @@ -161,6 +169,10 @@ export const testEndpoint = async (url: string): Promise => { if (error.name === "AbortError") { throw new UnknownError("Request timed out after 5 seconds"); } + if (error instanceof UnknownError) { + throw error; + } + throw new UnknownError(`Error while fetching the URL: ${error.message}`); } };