fix: discord webhook not pinging (#4673)

Co-authored-by: Piyush Gupta <piyushguptaa2z123@gmail.com>
Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com>
Co-authored-by: Dhruwang Jariwala <67850763+Dhruwang@users.noreply.github.com>
This commit is contained in:
Kartik Saini
2025-02-14 09:26:19 +05:30
committed by GitHub
parent 4843cb8789
commit 9152181a00
3 changed files with 23 additions and 1 deletions

View File

@@ -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;

View File

@@ -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());
};

View File

@@ -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<boolean> => {
export const createWebhook = async (environmentId: string, webhookInput: TWebhookInput): Promise<boolean> => {
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<boolean> => {
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<boolean> => {
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}`);
}
};