-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ (whatsapp) Add custom session expiration #842
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import { saveStateToDatabase } from '@typebot.io/bot-engine/saveStateToDatabase' | |
import { restartSession } from '@typebot.io/bot-engine/queries/restartSession' | ||
import { continueBotFlow } from '@typebot.io/bot-engine/continueBotFlow' | ||
import { parseDynamicTheme } from '@typebot.io/bot-engine/parseDynamicTheme' | ||
import { isDefined } from '@typebot.io/lib/utils' | ||
|
||
export const sendMessage = publicProcedure | ||
.meta({ | ||
|
@@ -30,6 +31,17 @@ export const sendMessage = publicProcedure | |
}) => { | ||
const session = sessionId ? await getSession(sessionId) : null | ||
|
||
const isSessionExpired = | ||
session && | ||
isDefined(session.state.expiryTimeout) && | ||
session.updatedAt.getTime() + session.state.expiryTimeout < Date.now() | ||
Comment on lines
+34
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding null checks for - const isSessionExpired =
- session &&
- isDefined(session.state.expiryTimeout) &&
- session.updatedAt.getTime() + session.state.expiryTimeout < Date.now()
+ const isSessionExpired =
+ session &&
+ session.state &&
+ session.updatedAt &&
+ isDefined(session.state.expiryTimeout) &&
+ session.updatedAt.getTime() + session.state.expiryTimeout * 60 * 60 * 1000 < Date.now() |
||
|
||
if (isSessionExpired) | ||
throw new TRPCError({ | ||
code: 'NOT_FOUND', | ||
message: 'Session expired. You need to start a new session.', | ||
}) | ||
Comment on lines
31
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic for checking session expiration seems correct, but it's important to ensure that the - session.updatedAt.getTime() + session.state.expiryTimeout < Date.now()
+ session.updatedAt.getTime() + session.state.expiryTimeout * 60 * 60 * 1000 < Date.now() |
||
|
||
if (!session) { | ||
if (!startParams) | ||
throw new TRPCError({ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import { continueBotFlow } from '../continueBotFlow' | |
import { decrypt } from '@typebot.io/lib/api' | ||
import { saveStateToDatabase } from '../saveStateToDatabase' | ||
import prisma from '@typebot.io/lib/prisma' | ||
import { isDefined } from '@typebot.io/lib/utils' | ||
|
||
export const resumeWhatsAppFlow = async ({ | ||
receivedMessage, | ||
|
@@ -64,17 +65,23 @@ export const resumeWhatsAppFlow = async ({ | |
} | ||
} | ||
|
||
const resumeResponse = sessionState | ||
? await continueBotFlow(sessionState)(messageContent) | ||
: workspaceId | ||
? await startWhatsAppSession({ | ||
message: receivedMessage, | ||
sessionId, | ||
workspaceId, | ||
credentials: { ...credentials, id: credentialsId as string }, | ||
contact, | ||
}) | ||
: undefined | ||
const isSessionExpired = | ||
session && | ||
isDefined(session.state.expiryTimeout) && | ||
session?.updatedAt.getTime() + session.state.expiryTimeout < Date.now() | ||
Comment on lines
+68
to
+71
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The session expiration check is not correctly implemented. The - session?.updatedAt.getTime() + session.state.expiryTimeout < Date.now()
+ session?.updatedAt.getTime() + session.state.expiryTimeout * 60 * 60 * 1000 < Date.now() |
||
|
||
const resumeResponse = | ||
sessionState && !isSessionExpired | ||
? await continueBotFlow(sessionState)(messageContent) | ||
: workspaceId | ||
? await startWhatsAppSession({ | ||
message: receivedMessage, | ||
sessionId, | ||
workspaceId, | ||
credentials: { ...credentials, id: credentialsId as string }, | ||
contact, | ||
}) | ||
: undefined | ||
|
||
if (!resumeResponse) { | ||
console.error('Could not find or create session', sessionId) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spacing between the form label and the input field has been made dynamic based on the
direction
prop. Ifdirection
is "column", the spacing is 2; otherwise, it's 3. This allows for more flexible layout configurations.This change will ensure that the spacing remains consistent with the previous version when the direction is not 'column'.