diff --git a/apps/builder/src/features/blocks/logic/setVariable/components/SetVariableContent.tsx b/apps/builder/src/features/blocks/logic/setVariable/components/SetVariableContent.tsx index 7d2e4825a..d8dc4bdc5 100644 --- a/apps/builder/src/features/blocks/logic/setVariable/components/SetVariableContent.tsx +++ b/apps/builder/src/features/blocks/logic/setVariable/components/SetVariableContent.tsx @@ -62,6 +62,7 @@ const Expression = ({ case 'Tomorrow': case 'User ID': case 'Moment of the day': + case 'Environment name': case 'Yesterday': { return ( diff --git a/apps/builder/src/features/blocks/logic/setVariable/components/SetVariableSettings.tsx b/apps/builder/src/features/blocks/logic/setVariable/components/SetVariableSettings.tsx index 9920b5a56..7bb7d4414 100644 --- a/apps/builder/src/features/blocks/logic/setVariable/components/SetVariableSettings.tsx +++ b/apps/builder/src/features/blocks/logic/setVariable/components/SetVariableSettings.tsx @@ -158,6 +158,17 @@ const SetVariableValue = ({ ) } + case 'Environment name': { + return ( + + + + Will return either web or{' '} + whatsapp. + + + ) + } case 'Contact name': case 'Phone number': case 'Random ID': diff --git a/apps/docs/openapi/builder/_spec_.json b/apps/docs/openapi/builder/_spec_.json index ef15c9c97..460dc0c08 100644 --- a/apps/docs/openapi/builder/_spec_.json +++ b/apps/docs/openapi/builder/_spec_.json @@ -1979,6 +1979,7 @@ "enum": [ "Custom", "Empty", + "Environment name", "User ID", "Now", "Today", @@ -6367,6 +6368,7 @@ "enum": [ "Custom", "Empty", + "Environment name", "User ID", "Now", "Today", @@ -10396,6 +10398,7 @@ "enum": [ "Custom", "Empty", + "Environment name", "User ID", "Now", "Today", @@ -14565,6 +14568,7 @@ "enum": [ "Custom", "Empty", + "Environment name", "User ID", "Now", "Today", @@ -18614,6 +18618,7 @@ "enum": [ "Custom", "Empty", + "Environment name", "User ID", "Now", "Today", @@ -22718,6 +22723,7 @@ "enum": [ "Custom", "Empty", + "Environment name", "User ID", "Now", "Today", @@ -26885,6 +26891,7 @@ "enum": [ "Custom", "Empty", + "Environment name", "User ID", "Now", "Today", diff --git a/apps/docs/openapi/chat/_spec_.json b/apps/docs/openapi/chat/_spec_.json index d5b8d9c3b..8d5ef97f4 100644 --- a/apps/docs/openapi/chat/_spec_.json +++ b/apps/docs/openapi/chat/_spec_.json @@ -1574,6 +1574,7 @@ "enum": [ "Custom", "Empty", + "Environment name", "User ID", "Now", "Today", diff --git a/packages/bot-engine/blocks/logic/setVariable/executeSetVariable.ts b/packages/bot-engine/blocks/logic/setVariable/executeSetVariable.ts index 891ed35a1..5584ec19d 100644 --- a/packages/bot-engine/blocks/logic/setVariable/executeSetVariable.ts +++ b/packages/bot-engine/blocks/logic/setVariable/executeSetVariable.ts @@ -77,9 +77,11 @@ const getExpressionToEvaluate = (options: SetVariableBlock['options']): string | null => { switch (options.type) { case 'Contact name': - return state.whatsApp?.contact.name ?? '' - case 'Phone number': - return `"${state.whatsApp?.contact.phoneNumber}"` ?? '' + return state.whatsApp?.contact.name ?? null + case 'Phone number': { + const phoneNumber = state.whatsApp?.contact.phoneNumber + return phoneNumber ? `"${state.whatsApp?.contact.phoneNumber}"` : null + } case 'Now': case 'Today': return 'new Date().toISOString()' @@ -112,6 +114,9 @@ const getExpressionToEvaluate = if(now.getHours() >= 18) return 'evening' if(now.getHours() >= 22 || now.getHours() < 6) return 'night'` } + case 'Environment name': { + return state.whatsApp ? 'whatsapp' : 'web' + } case 'Custom': case undefined: { return options.expressionToEvaluate ?? null diff --git a/packages/bot-engine/startSession.ts b/packages/bot-engine/startSession.ts index 16748422d..d451c0e33 100644 --- a/packages/bot-engine/startSession.ts +++ b/packages/bot-engine/startSession.ts @@ -30,11 +30,13 @@ import { injectVariablesFromExistingResult } from './variables/injectVariablesFr type Props = { startParams: StartParams userId: string | undefined + initialSessionState?: Pick } export const startSession = async ({ startParams, userId, + initialSessionState, }: Props): Promise => { if (!startParams) throw new TRPCError({ @@ -108,6 +110,7 @@ export const startSession = async ({ dynamicTheme: parseDynamicThemeInState(typebot.theme), isStreamEnabled: startParams.isStreamEnabled, typingEmulation: typebot.settings.typingEmulation, + ...initialSessionState, } if (startParams.isOnlyRegistering) { diff --git a/packages/bot-engine/whatsapp/resumeWhatsAppFlow.ts b/packages/bot-engine/whatsapp/resumeWhatsAppFlow.ts index 26ca28204..2cb7bddb7 100644 --- a/packages/bot-engine/whatsapp/resumeWhatsAppFlow.ts +++ b/packages/bot-engine/whatsapp/resumeWhatsAppFlow.ts @@ -46,16 +46,6 @@ export const resumeWhatsAppFlow = async ({ typebotId: typebot?.id, }) - const sessionState = - isPreview && session?.state - ? ({ - ...session?.state, - whatsApp: { - contact, - }, - } satisfies SessionState) - : session?.state - const credentials = await getCredentials({ credentialsId, isPreview }) if (!credentials) { @@ -71,8 +61,8 @@ export const resumeWhatsAppFlow = async ({ session?.updatedAt.getTime() + session.state.expiryTimeout < Date.now() const resumeResponse = - sessionState && !isSessionExpired - ? await continueBotFlow(sessionState)(messageContent) + session && !isSessionExpired + ? await continueBotFlow(session.state)(messageContent) : workspaceId ? await startWhatsAppSession({ incomingMessage: messageContent, diff --git a/packages/bot-engine/whatsapp/startWhatsAppSession.ts b/packages/bot-engine/whatsapp/startWhatsAppSession.ts index 96b64699d..e8af5d19a 100644 --- a/packages/bot-engine/whatsapp/startWhatsAppSession.ts +++ b/packages/bot-engine/whatsapp/startWhatsAppSession.ts @@ -10,7 +10,6 @@ import { } from '@typebot.io/schemas' import { WhatsAppCredentials, - WhatsAppIncomingMessage, defaultSessionExpiryTimeout, } from '@typebot.io/schemas/features/whatsapp' import { isInputBlock, isNotDefined } from '@typebot.io/lib/utils' @@ -73,47 +72,50 @@ export const startWhatsAppSession = async ({ if (isNotDefined(publicTypebot)) return - let session = await startSession({ - startParams: { - typebot: publicTypebot.typebot.publicId as string, - }, - userId: undefined, - }) - - // If first block is an input block, we can directly continue the bot flow - const firstEdgeId = - session.newSessionState.typebotsQueue[0].typebot.groups[0].blocks[0] - .outgoingEdgeId - const nextGroup = await getNextGroup(session.newSessionState)(firstEdgeId) - const firstBlock = nextGroup.group?.blocks.at(0) - if (firstBlock && isInputBlock(firstBlock)) { - const resultId = session.newSessionState.typebotsQueue[0].resultId - if (resultId) - await upsertResult({ - hasStarted: true, - isCompleted: false, - resultId, - typebot: session.newSessionState.typebotsQueue[0].typebot, - }) - session = await continueBotFlow({ - ...session.newSessionState, - currentBlock: { groupId: firstBlock.groupId, blockId: firstBlock.id }, - })(incomingMessage) - } - const sessionExpiryTimeoutHours = publicTypebot.settings.whatsApp?.sessionExpiryTimeout ?? defaultSessionExpiryTimeout - return { - ...session, - newSessionState: { - ...session.newSessionState, + const session = await startSession({ + startParams: { + typebot: publicTypebot.typebot.publicId as string, + }, + userId: undefined, + initialSessionState: { whatsApp: { contact, }, expiryTimeout: sessionExpiryTimeoutHours * 60 * 60 * 1000, }, + }) + + let newSessionState: SessionState = session.newSessionState + + // If first block is an input block, we can directly continue the bot flow + const firstEdgeId = + newSessionState.typebotsQueue[0].typebot.groups[0].blocks[0].outgoingEdgeId + const nextGroup = await getNextGroup(newSessionState)(firstEdgeId) + const firstBlock = nextGroup.group?.blocks.at(0) + if (firstBlock && isInputBlock(firstBlock)) { + const resultId = newSessionState.typebotsQueue[0].resultId + if (resultId) + await upsertResult({ + hasStarted: true, + isCompleted: false, + resultId, + typebot: newSessionState.typebotsQueue[0].typebot, + }) + newSessionState = ( + await continueBotFlow({ + ...newSessionState, + currentBlock: { groupId: firstBlock.groupId, blockId: firstBlock.id }, + })(incomingMessage) + ).newSessionState + } + + return { + ...session, + newSessionState, } } diff --git a/packages/schemas/features/blocks/logic/setVariable.ts b/packages/schemas/features/blocks/logic/setVariable.ts index 9984aa563..27fda7379 100644 --- a/packages/schemas/features/blocks/logic/setVariable.ts +++ b/packages/schemas/features/blocks/logic/setVariable.ts @@ -5,6 +5,7 @@ import { LogicBlockType } from './enums' export const valueTypes = [ 'Custom', 'Empty', + 'Environment name', 'User ID', 'Now', 'Today',