Skip to content

Commit d7fc11d

Browse files
CarinaWolliCarinaWolliUdit-takkarhbjORbjThyMinimalDev
authored
feat: use form responses as workflow variables (calcom#24716)
* remove add variable dropdown * feat: lang support * fix: type errors * feat: select voice agent * refactor: address feedback * refactor: address feedback * refactor: missing import * fix: types * add getAllWorkflowsFromRoutingForm to WorkflowService * fix error caused by undefined evt * fix type error * fix type error * fix tests * feat: add inbound calls * chore: formatting * chore * feat: finish inbound call * chore: formatting * fix: update bug * fix: types * code clean up * final fixes and clean up * remove console.log * remove template text form from triggers * add routing form repoditory function * refactor: Agent Configuration Sheet (calcom#23930) * refactor: agent configuration sheet * chore: use default phone numbre * refactor: improvements * refactor: improvements * fix: types * fix: feedback * fix bug with key * chore: * fix: feedback * fix: prompt * add comments * fix: review * fix: review * refactor: class * refactor: class * fix test * allow cal ai action on form triggers * move any reusable code to scheduleAIPhoneCall * add missing await * use predefined FormSubmissionData type * add .trim() to sms message * pass contextData instead * finish base setup * add missing trigger in update-workflow.input.ts * allow cal.ai action for form triggers in handler * chore: add support for form workflows on api v2 * fixup! chore: add support for form workflows on api v2 * ai phone call on form submissions (WIP) * use existing type for Option array * pass chosen event type id * refactor: rename * Update apps/web/public/static/locales/en/common.json * Update apps/web/public/static/locales/en/common.json * add missing imports * chore: update set value * fix: remove index * fix: type error * fix: update tetss * use only repository functions in update handler * move all prisma queries from list.handler * review suggestions * fix: use logger * chore: handle workflows api v2 * chore: handle workflows api v2, split in 2 endpoints * fix workflow step creation * remove connect agent and fixes types * add type to workflow * chore: use workflow type in apiv2 WorkflowsOutputService * update worklfow type on update * chore: use workflow type in apiv2 WorkflowsOutputService * fix template body for torm trigger * some UI fixes for email subject/body * resetting email body when changing form triggers * use type field to query workflows * clean up all old active on values * remove responseId from all funciton calls * remove undefined from updateTemplate * refactor: don't use static * fix: type * refactor: split routing form and event-type workflows code * refactor: split routing form and event-type workflows code * fix template text when adding action * chore: don't rename WorkflowActivationDto to avoid ci blocking * refine update schedule to use only allowed actions * fix type error * don't allow whatsapp action with form trigger * fix type error * return early if activeOn array is empty * fix: from step type in BaseFormWorkflowStepDto * fixup! fix: from step type in BaseFormWorkflowStepDto * api v2 updates * move all prisma calls to repository (service/workflows.ts) * use FORM_TRIGGER_WORKFLOW_EVENTS for form queries * use userRepository * use FORM_TRIGGER_WORKFLOW_EVENTS in isFormTrigger * code clean up * code clean up * use repository functions in formSubmissionValidation.ts * fix: schema * refactor: * remove action check in update handler * add event type selection * event type selector improvements * adjust update.handler * set outboundEventTypeId * add back trpc import * fix agent repository functions * clean up * fix bugs caused by merge * pass eventTypeId to updateToolsFromAgentId * add migration for outboundEventTypeId * add SMS actions to allowed form action constants * add cal ai to allowed form actions * pick correct event type for web call * pass correct routed event type id * remove unsued import * fixes for offset api v2 * add missing responseId * fix failing test * fix failing test * improve error message * remove unused imports * chore: handle sms step action for form worklfow in dtos * fix typo * missing missing newStep * minor fixes * remove changes * add routedEventTypeId * fix type error * fix type error * fix typ error in executAPIPhoneCall.tsx * add back inboundEventTypeId * remove console.log * remove outdated code * small fixes * don't throw error for missing phone number * add back filtered triggerOptions * fix eventTypeId in testCall handler * fix type error * update migration * fix trigger is not defined * convert eventTypeId to string * only use outboundEventTypeId for FORM_SUBMITTED trigger * show toast when no event type selected * fix type errors * add missing translation * fix type error * remove callType * fix tests * small fixes * clean up AgentConfigurationSheet * remove EventTypeSelector file * code clean up * clean up * clean up * use resusable function for TestPhoneCallDialog and WebCallDialog * rename result * fix types for event type id * use repository runction in workflowReminder.ts * fix type error * pass eventTypeIds correctly * fix typo * custom variables from form responses * remove comment * Update apps/web/public/static/locales/en/common.json Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> * use watch instead of getValues * change to z.record(z.unknown()) instead of any() * fix type of eventTypeId * check permissinon for outBoundEventTypeId * add isNaN check * improve function name * add tests * fixes for custom variables * improve test * update tools when outbound agent event type id changes * handle undefined eventDate in customTemplate * add info how to use form responses as variables * rename responses to routingFormResponses * remove cal ai from allowed steps api v2 * remove old migration file --------- Co-authored-by: CarinaWolli <wollencarina@gmail.com> Co-authored-by: Udit Takkar <udit222001@gmail.com> Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com> Co-authored-by: Benny Joo <sldisek783@gmail.com> Co-authored-by: cal.com <morgan@cal.com> Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com>
1 parent 1ff5d57 commit d7fc11d

8 files changed

Lines changed: 477 additions & 137 deletions

File tree

apps/web/public/static/locales/en/common.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1622,12 +1622,14 @@
16221622
"example_1": "Example 1",
16231623
"example_2": "Example 2",
16241624
"booking_question_identifier": "Booking Question Identifier",
1625+
"form_field_identifier": "Form Field Identifier",
16251626
"company_size": "Company size",
16261627
"what_help_needed": "What do you need help with?",
16271628
"variable_format": "Variable format",
16281629
"webhook_subscriber_url_reserved": "Webhook subscriber url is already defined",
16291630
"custom_input_as_variable_info": "Ignore all special characters of the additional input label (use only letters and numbers), use uppercase for all letters and replace whitespaces with underscores.",
16301631
"using_booking_questions_as_variables": "How do I use booking questions as variables?",
1632+
"using_form_responses_as_variables": "How do I use form responses as variables?",
16311633
"download_desktop_app": "Download desktop app",
16321634
"set_ping_link": "Set Ping link",
16331635
"rate_limit_exceeded": "Rate limit exceeded",
@@ -1923,6 +1925,7 @@
19231925
"show_attendees": "Share attendee information between guests",
19241926
"show_available_seats_count": "Show the number of available seats",
19251927
"how_booking_questions_as_variables": "How to use booking questions as variables?",
1928+
"how_form_responses_as_variables": "How to use form responses as variables?",
19261929
"format": "Format",
19271930
"questions": "Questions",
19281931
"all_info_your_booker_provide": "All the info your booker should provide before booking with you.",
@@ -1939,6 +1942,7 @@
19391942
"billing_portal": "Billing portal",
19401943
"billing_help_cta": "Contact support",
19411944
"ignore_special_characters_booking_questions": "Ignore special characters in your booking question identifier. Use only letters and numbers",
1945+
"ignore_special_characters_form_responses": "Ignore special characters in your form field identifier. Use only letters and numbers",
19421946
"retry": "Retry",
19431947
"fetching_calendars_error": "There was a problem fetching your calendars. Please <1>try again</1> or reach out to customer support.",
19441948
"calendar_connection_fail": "Calendar connection failed",

packages/features/ee/workflows/components/WorkflowStepContainer.tsx

Lines changed: 100 additions & 88 deletions
Large diffs are not rendered by default.

packages/features/ee/workflows/lib/reminders/emailReminderManager.ts

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import type { EventStatus } from "ics";
2-
import { v4 as uuidv4 } from "uuid";
32

43
import dayjs from "@calcom/dayjs";
54
import generateIcsString from "@calcom/emails/lib/generateIcsString";
@@ -24,7 +23,10 @@ import { sendOrScheduleWorkflowEmails } from "./providers/emailProvider";
2423
import type { FormSubmissionData, WorkflowContextData } from "./reminderScheduler";
2524
import type { AttendeeInBookingInfo, BookingInfo, timeUnitLowerCase } from "./smsReminderManager";
2625
import type { VariablesType } from "./templates/customTemplate";
27-
import customTemplate from "./templates/customTemplate";
26+
import customTemplate, {
27+
transformBookingResponsesToVariableFormat,
28+
transformRoutingFormResponsesToVariableFormat,
29+
} from "./templates/customTemplate";
2830
import emailRatingTemplate from "./templates/emailRatingTemplate";
2931
import emailReminderTemplate from "./templates/emailReminderTemplate";
3032

@@ -62,7 +64,12 @@ type SendEmailReminderParams = {
6264
subject: string;
6365
html: string;
6466
replyTo?: string;
65-
attachments?: any[];
67+
attachments?: {
68+
content: string;
69+
filename: string;
70+
contentType: string;
71+
disposition: string;
72+
}[];
6673
sender?: string | null;
6774
};
6875
sendTo: string[];
@@ -159,7 +166,7 @@ const scheduleEmailReminderForEvt = async (args: scheduleEmailReminderArgs & { e
159166
attendeeName = attendeeToBeUsedInMail.name;
160167
timeZone = evt.organizer.timeZone;
161168
break;
162-
case WorkflowActions.EMAIL_ATTENDEE:
169+
case WorkflowActions.EMAIL_ATTENDEE: {
163170
// check if first attendee of sendTo is present in the attendees list, if not take the evt attendee
164171
const attendeeEmailToBeUsedInMailFromEvt = evt.attendees.find(
165172
(attendee) => attendee.email === sendTo[0]
@@ -171,6 +178,7 @@ const scheduleEmailReminderForEvt = async (args: scheduleEmailReminderArgs & { e
171178
attendeeName = evt.organizer.name;
172179
timeZone = attendeeToBeUsedInMail.timeZone;
173180
break;
181+
}
174182
}
175183

176184
let emailContent = {
@@ -199,7 +207,7 @@ const scheduleEmailReminderForEvt = async (args: scheduleEmailReminderArgs & { e
199207
timeZone: timeZone,
200208
location: evt.location,
201209
additionalNotes: evt.additionalNotes,
202-
responses: evt.responses,
210+
responses: transformBookingResponsesToVariableFormat(evt.responses),
203211
meetingUrl: bookingMetadataSchema.parse(evt.metadata || {})?.videoCallUrl,
204212
cancelLink: `${bookerUrl}/booking/${evt.uid}?cancel=true${
205213
recipientEmail ? `&cancelledBy=${encodeURIComponent(recipientEmail)}` : ""
@@ -358,12 +366,16 @@ const scheduleEmailReminderForForm = async (
358366

359367
if (emailBody) {
360368
const timeFormat = getTimeFormatStringFromUserTimeFormat(formData.user.timeFormat);
361-
//todo: add variables
362-
const emailSubjectTemplate = customTemplate(emailSubject, {}, formData.user.locale, timeFormat);
369+
370+
const variables: VariablesType = {
371+
responses: transformRoutingFormResponsesToVariableFormat(formData.responses),
372+
};
373+
374+
const emailSubjectTemplate = customTemplate(emailSubject, variables, formData.user.locale, timeFormat);
363375
emailContent.emailSubject = emailSubjectTemplate.text;
364376
emailContent.emailBody = customTemplate(
365377
emailBody,
366-
{},
378+
variables,
367379
formData.user.locale,
368380
timeFormat,
369381
hideBranding

packages/features/ee/workflows/lib/reminders/smsReminderManager.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import logger from "@calcom/lib/logger";
1010
import { safeStringify } from "@calcom/lib/safeStringify";
1111
import { getTranslation } from "@calcom/lib/server/i18n";
1212
import type { TimeFormat } from "@calcom/lib/timeFormat";
13+
import { getTimeFormatStringFromUserTimeFormat } from "@calcom/lib/timeFormat";
1314
import type { PrismaClient } from "@calcom/prisma";
1415
import prisma from "@calcom/prisma";
1516
import type { Prisma } from "@calcom/prisma/client";
@@ -26,6 +27,7 @@ import type { ScheduleReminderArgs } from "./emailReminderManager";
2627
import { scheduleSmsOrFallbackEmail, sendSmsOrFallbackEmail } from "./messageDispatcher";
2728
import * as twilio from "./providers/twilioProvider";
2829
import type { FormSubmissionData } from "./reminderScheduler";
30+
import customTemplate, { transformRoutingFormResponsesToVariableFormat } from "./templates/customTemplate";
2931
import smsReminderTemplate from "./templates/smsReminderTemplate";
3032

3133
export enum timeUnitLowerCase {
@@ -318,7 +320,18 @@ const scheduleSMSReminderForForm = async (
318320
) => {
319321
const { message, triggerEvent, reminderPhone, sender, userId, teamId, action, formData } = args;
320322

321-
const smsMessage = message;
323+
let smsMessage = message;
324+
325+
if (smsMessage && formData.responses) {
326+
const timeFormat = getTimeFormatStringFromUserTimeFormat(formData.user.timeFormat);
327+
328+
const variables = {
329+
responses: transformRoutingFormResponsesToVariableFormat(formData.responses),
330+
};
331+
332+
const processedMessage = customTemplate(smsMessage, variables, formData.user.locale, timeFormat);
333+
smsMessage = processedMessage.text;
334+
}
322335

323336
if (smsMessage.trim().length > 0) {
324337
const smsMessageWithoutOptOut = await WorkflowOptOutService.addOptOutMessage(

packages/features/ee/workflows/lib/reminders/templates/customTemplate.ts

Lines changed: 81 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,68 @@
11
import { guessEventLocationType } from "@calcom/app-store/locations";
2+
import type { FORM_SUBMITTED_WEBHOOK_RESPONSES } from "@calcom/app-store/routing-forms/lib/formSubmissionUtils";
23
import type { Dayjs } from "@calcom/dayjs";
34
import dayjs from "@calcom/dayjs";
45
import { APP_NAME } from "@calcom/lib/constants";
56
import { TimeFormat } from "@calcom/lib/timeFormat";
67
import type { CalEventResponses } from "@calcom/types/Calendar";
78

9+
export type WorkflowVariableResponses = Record<
10+
string,
11+
{
12+
value:
13+
| string
14+
| number
15+
| boolean
16+
| string[]
17+
| Record<string, string>
18+
| { value: string; optionValue: string };
19+
}
20+
>;
21+
22+
export function transformBookingResponsesToVariableFormat(
23+
responses: CalEventResponses | null | undefined
24+
): WorkflowVariableResponses | null {
25+
if (!responses) return null;
26+
27+
const transformed: WorkflowVariableResponses = {};
28+
29+
for (const [key, response] of Object.entries(responses)) {
30+
if (response?.value !== undefined) {
31+
transformed[key] = {
32+
value: response.value,
33+
};
34+
}
35+
}
36+
37+
return Object.keys(transformed).length > 0 ? transformed : null;
38+
}
39+
40+
export function transformRoutingFormResponsesToVariableFormat(
41+
responses: FORM_SUBMITTED_WEBHOOK_RESPONSES | null | undefined
42+
): WorkflowVariableResponses | null {
43+
if (!responses) return null;
44+
45+
const transformed: WorkflowVariableResponses = {};
46+
47+
for (const [key, response] of Object.entries(responses)) {
48+
if (response?.value !== undefined) {
49+
transformed[key] = {
50+
value: response.value,
51+
};
52+
}
53+
}
54+
55+
return Object.keys(transformed).length > 0 ? transformed : null;
56+
}
57+
58+
export function formatIdentifierToVariable(key: string): string {
59+
return key
60+
.replace(/[^a-zA-Z0-9 ]/g, "")
61+
.trim()
62+
.replaceAll(" ", "_")
63+
.toUpperCase();
64+
}
65+
866
export type VariablesType = {
967
eventName?: string;
1068
organizerName?: string;
@@ -17,7 +75,7 @@ export type VariablesType = {
1775
timeZone?: string;
1876
location?: string | null;
1977
additionalNotes?: string | null;
20-
responses?: CalEventResponses | null;
78+
responses?: WorkflowVariableResponses | null;
2179
meetingUrl?: string;
2280
cancelLink?: string;
2381
cancelReason?: string | null;
@@ -42,12 +100,15 @@ const customTemplate = (
42100
timeFormat?: TimeFormat,
43101
isBrandingDisabled?: boolean
44102
) => {
45-
const translatedDate = new Intl.DateTimeFormat(locale, {
46-
weekday: "long",
47-
month: "long",
48-
day: "numeric",
49-
year: "numeric",
50-
}).format(variables.eventDate?.add(dayjs().tz(variables.timeZone).utcOffset(), "minute").toDate());
103+
const eventDate = variables.eventDate;
104+
const translatedDate = eventDate
105+
? new Intl.DateTimeFormat(locale, {
106+
weekday: "long",
107+
month: "long",
108+
day: "numeric",
109+
year: "numeric",
110+
}).format(eventDate.add(dayjs().tz(variables.timeZone).utcOffset(), "minute").toDate())
111+
: "";
51112

52113
let locationString = variables.location || "";
53114

@@ -132,23 +193,21 @@ const customTemplate = (
132193
return;
133194
}
134195

196+
// handle custom variables from form/booking responses
135197
if (variables.responses) {
136198
Object.keys(variables.responses).forEach((customInput) => {
137-
const formatedToVariable = customInput
138-
.replace(/[^a-zA-Z0-9 ]/g, "")
139-
.trim()
140-
.replaceAll(" ", "_")
141-
.toUpperCase();
142-
143-
if (
144-
variable === formatedToVariable &&
145-
variables.responses &&
146-
variables.responses[customInput as keyof typeof variables.responses].value
147-
) {
148-
dynamicText = dynamicText.replace(
149-
`{${variable}}`,
150-
variables.responses[customInput as keyof typeof variables.responses].value.toString()
151-
);
199+
const formatedToVariable = formatIdentifierToVariable(customInput);
200+
201+
if (variable === formatedToVariable && variables.responses) {
202+
const response = variables.responses[customInput];
203+
if (response?.value !== undefined) {
204+
const responseValue = response.value;
205+
const valueString = Array.isArray(responseValue)
206+
? responseValue.join(", ")
207+
: String(responseValue);
208+
209+
dynamicText = dynamicText.replace(`{${variable}}`, valueString);
210+
}
152211
}
153212
});
154213
}

packages/features/ee/workflows/lib/reminders/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { IMMEDIATE_WORKFLOW_TRIGGER_EVENTS } from "../constants";
88
import { getWorkflowRecipientEmail } from "../getWorkflowReminders";
99
import type { AttendeeInBookingInfo, BookingInfo } from "./smsReminderManager";
1010
import type { VariablesType } from "./templates/customTemplate";
11-
import customTemplate from "./templates/customTemplate";
11+
import customTemplate, { transformBookingResponsesToVariableFormat } from "./templates/customTemplate";
1212

1313
export const bulkShortenLinks = async (links: string[]) => {
1414
if (!process.env.DUB_API_KEY) {
@@ -74,7 +74,7 @@ export const getSMSMessageWithVariables = async (
7474
timeZone: timeZone,
7575
location: evt.location,
7676
additionalNotes: evt.additionalNotes,
77-
responses: evt.responses,
77+
responses: transformBookingResponsesToVariableFormat(evt.responses),
7878
meetingUrl,
7979
cancelLink,
8080
rescheduleLink,

0 commit comments

Comments
 (0)