Skip to content

Scheduling infrastructure for absolutely everyone.

feat: integrate formbricks in help feedback box (#12276)

Shubham Palriwalaf848a44f1a3a
diff --git a/.env.example b/.env.example
index 71357c2d7c9..e15bdefcd9c 100644
--- a/.env.example
+++ b/.env.example
@@ -133,6 +133,11 @@ NEXT_PUBLIC_SENDGRID_SENDER_NAME=
# Used for capturing exceptions and logging messages
NEXT_PUBLIC_SENTRY_DSN=
+# Formbricks Experience Management Integration
+FORMBRICKS_HOST_URL=https://app.formbricks.com
+FORMBRICKS_ENVIRONMENT_ID=
+FORMBRICKS_FEEDBACK_SURVEY_ID=
+
# Twilio
# Used to send SMS reminders in workflows
TWILIO_SID=
diff --git a/packages/lib/formbricks.ts b/packages/lib/formbricks.ts
new file mode 100644
index 00000000000..b5ed06a546e
--- /dev/null
+++ b/packages/lib/formbricks.ts
@@ -0,0 +1,42 @@
+import { FormbricksAPI } from "@formbricks/api";
+
+import type { Feedback } from "@calcom/emails/templates/feedback-email";
+
+enum Rating {
+ "Extremely unsatisfied" = 1,
+ "Unsatisfied" = 2,
+ "Satisfied" = 3,
+ "Extremely satisfied" = 4,
+}
+
+export const sendFeedbackFormbricks = async (userId: number, feedback: Feedback) => {
+ if (!process.env.FORMBRICKS_HOST_URL || !process.env.FORMBRICKS_ENVIRONMENT_ID)
+ throw new Error("Missing FORMBRICKS_HOST_URL or FORMBRICKS_ENVIRONMENT_ID env variable");
+ const api = new FormbricksAPI({
+ apiHost: process.env.FORMBRICKS_HOST_URL,
+ environmentId: process.env.FORMBRICKS_ENVIRONMENT_ID,
+ });
+ if (process.env.FORMBRICKS_FEEDBACK_SURVEY_ID) {
+ const formbricksUserId = userId.toString();
+ const ratingValue = Object.keys(Rating).includes(feedback.rating)
+ ? Rating[feedback.rating as keyof typeof Rating]
+ : undefined;
+ if (ratingValue === undefined) throw new Error("Invalid rating value");
+
+ await api.client.response.create({
+ surveyId: process.env.FORMBRICKS_FEEDBACK_SURVEY_ID,
+ userId: formbricksUserId,
+ finished: true,
+ data: {
+ "formbricks-share-comments-question": feedback.comment,
+ "formbricks-rating-question": ratingValue,
+ },
+ });
+ await api.client.people.update(formbricksUserId, {
+ attributes: {
+ email: feedback.email,
+ username: feedback.username,
+ },
+ });
+ }
+};
diff --git a/packages/lib/package.json b/packages/lib/package.json
index b2ada1df571..fae3a15b8f4 100644
--- a/packages/lib/package.json
+++ b/packages/lib/package.json
@@ -14,6 +14,7 @@
"dependencies": {
"@calcom/config": "*",
"@calcom/dayjs": "*",
+ "@formbricks/api": "^1.1.0",
"@sendgrid/client": "^7.7.0",
"@vercel/og": "^0.5.0",
"bcryptjs": "^2.4.3",
diff --git a/packages/trpc/server/routers/loggedInViewer/submitFeedback.handler.ts b/packages/trpc/server/routers/loggedInViewer/submitFeedback.handler.ts
index 27db135abbb..e72932778b1 100644
--- a/packages/trpc/server/routers/loggedInViewer/submitFeedback.handler.ts
+++ b/packages/trpc/server/routers/loggedInViewer/submitFeedback.handler.ts
@@ -1,5 +1,6 @@
import dayjs from "@calcom/dayjs";
import { sendFeedbackEmail } from "@calcom/emails";
+import { sendFeedbackFormbricks } from "@calcom/lib/formbricks";
import { prisma } from "@calcom/prisma";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
@@ -30,6 +31,8 @@ export const submitFeedbackHandler = async ({ ctx, input }: SubmitFeedbackOption
comment: comment,
},
});
+ if (process.env.FORMBRICKS_HOST_URL && process.env.FORMBRICKS_ENVIRONMENT_ID)
+ sendFeedbackFormbricks(ctx.user.id, feedback);
if (process.env.SEND_FEEDBACK_EMAIL && comment) sendFeedbackEmail(feedback);
};
diff --git a/turbo.json b/turbo.json
index b0ed2de782c..e7f02741413 100644
--- a/turbo.json
+++ b/turbo.json
@@ -256,6 +256,9 @@
"EMAIL_SERVER_USER",
"EMAIL_SERVER",
"EXCHANGE_DEFAULT_EWS_URL",
+ "FORMBRICKS_HOST_URL",
+ "FORMBRICKS_ENVIRONMENT_ID",
+ "FORMBRICKS_FEEDBACK_SURVEY_ID",
"GIPHY_API_KEY",
"GITHUB_API_REPO_TOKEN",
"GOOGLE_API_CREDENTIALS",