Skip to content

Commit 1ca742f

Browse files
authored
⚡ (setVariable) Add "Environment name" value in Set variable block (#850)
Closes #848 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ### Summary by CodeRabbit - New Feature: Added "Environment name" as a new value type in the SetVariable function, allowing users to distinguish between 'web' and 'whatsapp' environments. - Refactor: Simplified session state handling in `resumeWhatsAppFlow.ts` for improved code clarity. - Refactor: Updated `startWhatsAppSession.ts` to include an initial session state with WhatsApp contact and expiry timeout, enhancing session management. - Bug Fix: Improved null handling in `executeSetVariable.ts` for 'Contact name' and 'Phone number', preventing potential issues with falsy values. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 7b3cbdb commit 1ca742f

File tree

9 files changed

+58
-37
lines changed

9 files changed

+58
-37
lines changed

apps/builder/src/features/blocks/logic/setVariable/components/SetVariableContent.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const Expression = ({
6262
case 'Tomorrow':
6363
case 'User ID':
6464
case 'Moment of the day':
65+
case 'Environment name':
6566
case 'Yesterday': {
6667
return (
6768
<Text as="span">

apps/builder/src/features/blocks/logic/setVariable/components/SetVariableSettings.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,17 @@ const SetVariableValue = ({
158158
</Alert>
159159
)
160160
}
161+
case 'Environment name': {
162+
return (
163+
<Alert fontSize="sm">
164+
<AlertIcon />
165+
<Text>
166+
Will return either <Tag size="sm">web</Tag> or{' '}
167+
<Tag size="sm">whatsapp</Tag>.
168+
</Text>
169+
</Alert>
170+
)
171+
}
161172
case 'Contact name':
162173
case 'Phone number':
163174
case 'Random ID':

apps/docs/openapi/builder/_spec_.json

+7
Original file line numberDiff line numberDiff line change
@@ -1979,6 +1979,7 @@
19791979
"enum": [
19801980
"Custom",
19811981
"Empty",
1982+
"Environment name",
19821983
"User ID",
19831984
"Now",
19841985
"Today",
@@ -6367,6 +6368,7 @@
63676368
"enum": [
63686369
"Custom",
63696370
"Empty",
6371+
"Environment name",
63706372
"User ID",
63716373
"Now",
63726374
"Today",
@@ -10396,6 +10398,7 @@
1039610398
"enum": [
1039710399
"Custom",
1039810400
"Empty",
10401+
"Environment name",
1039910402
"User ID",
1040010403
"Now",
1040110404
"Today",
@@ -14565,6 +14568,7 @@
1456514568
"enum": [
1456614569
"Custom",
1456714570
"Empty",
14571+
"Environment name",
1456814572
"User ID",
1456914573
"Now",
1457014574
"Today",
@@ -18614,6 +18618,7 @@
1861418618
"enum": [
1861518619
"Custom",
1861618620
"Empty",
18621+
"Environment name",
1861718622
"User ID",
1861818623
"Now",
1861918624
"Today",
@@ -22718,6 +22723,7 @@
2271822723
"enum": [
2271922724
"Custom",
2272022725
"Empty",
22726+
"Environment name",
2272122727
"User ID",
2272222728
"Now",
2272322729
"Today",
@@ -26885,6 +26891,7 @@
2688526891
"enum": [
2688626892
"Custom",
2688726893
"Empty",
26894+
"Environment name",
2688826895
"User ID",
2688926896
"Now",
2689026897
"Today",

apps/docs/openapi/chat/_spec_.json

+1
Original file line numberDiff line numberDiff line change
@@ -1574,6 +1574,7 @@
15741574
"enum": [
15751575
"Custom",
15761576
"Empty",
1577+
"Environment name",
15771578
"User ID",
15781579
"Now",
15791580
"Today",

packages/bot-engine/blocks/logic/setVariable/executeSetVariable.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ const getExpressionToEvaluate =
7777
(options: SetVariableBlock['options']): string | null => {
7878
switch (options.type) {
7979
case 'Contact name':
80-
return state.whatsApp?.contact.name ?? ''
81-
case 'Phone number':
82-
return `"${state.whatsApp?.contact.phoneNumber}"` ?? ''
80+
return state.whatsApp?.contact.name ?? null
81+
case 'Phone number': {
82+
const phoneNumber = state.whatsApp?.contact.phoneNumber
83+
return phoneNumber ? `"${state.whatsApp?.contact.phoneNumber}"` : null
84+
}
8385
case 'Now':
8486
case 'Today':
8587
return 'new Date().toISOString()'
@@ -112,6 +114,9 @@ const getExpressionToEvaluate =
112114
if(now.getHours() >= 18) return 'evening'
113115
if(now.getHours() >= 22 || now.getHours() < 6) return 'night'`
114116
}
117+
case 'Environment name': {
118+
return state.whatsApp ? 'whatsapp' : 'web'
119+
}
115120
case 'Custom':
116121
case undefined: {
117122
return options.expressionToEvaluate ?? null

packages/bot-engine/startSession.ts

+3
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ import { injectVariablesFromExistingResult } from './variables/injectVariablesFr
3030
type Props = {
3131
startParams: StartParams
3232
userId: string | undefined
33+
initialSessionState?: Pick<SessionState, 'whatsApp' | 'expiryTimeout'>
3334
}
3435

3536
export const startSession = async ({
3637
startParams,
3738
userId,
39+
initialSessionState,
3840
}: Props): Promise<ChatReply & { newSessionState: SessionState }> => {
3941
if (!startParams)
4042
throw new TRPCError({
@@ -108,6 +110,7 @@ export const startSession = async ({
108110
dynamicTheme: parseDynamicThemeInState(typebot.theme),
109111
isStreamEnabled: startParams.isStreamEnabled,
110112
typingEmulation: typebot.settings.typingEmulation,
113+
...initialSessionState,
111114
}
112115

113116
if (startParams.isOnlyRegistering) {

packages/bot-engine/whatsapp/resumeWhatsAppFlow.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,6 @@ export const resumeWhatsAppFlow = async ({
4646
typebotId: typebot?.id,
4747
})
4848

49-
const sessionState =
50-
isPreview && session?.state
51-
? ({
52-
...session?.state,
53-
whatsApp: {
54-
contact,
55-
},
56-
} satisfies SessionState)
57-
: session?.state
58-
5949
const credentials = await getCredentials({ credentialsId, isPreview })
6050

6151
if (!credentials) {
@@ -71,8 +61,8 @@ export const resumeWhatsAppFlow = async ({
7161
session?.updatedAt.getTime() + session.state.expiryTimeout < Date.now()
7262

7363
const resumeResponse =
74-
sessionState && !isSessionExpired
75-
? await continueBotFlow(sessionState)(messageContent)
64+
session && !isSessionExpired
65+
? await continueBotFlow(session.state)(messageContent)
7666
: workspaceId
7767
? await startWhatsAppSession({
7868
incomingMessage: messageContent,

packages/bot-engine/whatsapp/startWhatsAppSession.ts

+24-22
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
} from '@typebot.io/schemas'
1111
import {
1212
WhatsAppCredentials,
13-
WhatsAppIncomingMessage,
1413
defaultSessionExpiryTimeout,
1514
} from '@typebot.io/schemas/features/whatsapp'
1615
import { isInputBlock, isNotDefined } from '@typebot.io/lib/utils'
@@ -73,47 +72,50 @@ export const startWhatsAppSession = async ({
7372

7473
if (isNotDefined(publicTypebot)) return
7574

76-
let session = await startSession({
75+
const sessionExpiryTimeoutHours =
76+
publicTypebot.settings.whatsApp?.sessionExpiryTimeout ??
77+
defaultSessionExpiryTimeout
78+
79+
const session = await startSession({
7780
startParams: {
7881
typebot: publicTypebot.typebot.publicId as string,
7982
},
8083
userId: undefined,
84+
initialSessionState: {
85+
whatsApp: {
86+
contact,
87+
},
88+
expiryTimeout: sessionExpiryTimeoutHours * 60 * 60 * 1000,
89+
},
8190
})
8291

92+
let newSessionState: SessionState = session.newSessionState
93+
8394
// If first block is an input block, we can directly continue the bot flow
8495
const firstEdgeId =
85-
session.newSessionState.typebotsQueue[0].typebot.groups[0].blocks[0]
86-
.outgoingEdgeId
87-
const nextGroup = await getNextGroup(session.newSessionState)(firstEdgeId)
96+
newSessionState.typebotsQueue[0].typebot.groups[0].blocks[0].outgoingEdgeId
97+
const nextGroup = await getNextGroup(newSessionState)(firstEdgeId)
8898
const firstBlock = nextGroup.group?.blocks.at(0)
8999
if (firstBlock && isInputBlock(firstBlock)) {
90-
const resultId = session.newSessionState.typebotsQueue[0].resultId
100+
const resultId = newSessionState.typebotsQueue[0].resultId
91101
if (resultId)
92102
await upsertResult({
93103
hasStarted: true,
94104
isCompleted: false,
95105
resultId,
96-
typebot: session.newSessionState.typebotsQueue[0].typebot,
106+
typebot: newSessionState.typebotsQueue[0].typebot,
97107
})
98-
session = await continueBotFlow({
99-
...session.newSessionState,
100-
currentBlock: { groupId: firstBlock.groupId, blockId: firstBlock.id },
101-
})(incomingMessage)
108+
newSessionState = (
109+
await continueBotFlow({
110+
...newSessionState,
111+
currentBlock: { groupId: firstBlock.groupId, blockId: firstBlock.id },
112+
})(incomingMessage)
113+
).newSessionState
102114
}
103115

104-
const sessionExpiryTimeoutHours =
105-
publicTypebot.settings.whatsApp?.sessionExpiryTimeout ??
106-
defaultSessionExpiryTimeout
107-
108116
return {
109117
...session,
110-
newSessionState: {
111-
...session.newSessionState,
112-
whatsApp: {
113-
contact,
114-
},
115-
expiryTimeout: sessionExpiryTimeoutHours * 60 * 60 * 1000,
116-
},
118+
newSessionState,
117119
}
118120
}
119121

packages/schemas/features/blocks/logic/setVariable.ts

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { LogicBlockType } from './enums'
55
export const valueTypes = [
66
'Custom',
77
'Empty',
8+
'Environment name',
89
'User ID',
910
'Now',
1011
'Today',

0 commit comments

Comments
 (0)