mirror of
https://github.com/formbricks/formbricks.git
synced 2026-01-02 01:00:33 -06:00
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:
@@ -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;
|
||||
|
||||
|
||||
@@ -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());
|
||||
};
|
||||
|
||||
@@ -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}`);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user