diff --git a/apps/builder/src/features/preview/api/sendWhatsAppInitialMessage.ts b/apps/builder/src/features/preview/api/sendWhatsAppInitialMessage.ts index 8eeae7d6b3..87b1827caf 100644 --- a/apps/builder/src/features/preview/api/sendWhatsAppInitialMessage.ts +++ b/apps/builder/src/features/preview/api/sendWhatsAppInitialMessage.ts @@ -17,6 +17,9 @@ export const sendWhatsAppInitialMessage = authenticatedProcedure async ({ input: { to, typebotId, startGroupId }, ctx: { user } }) => { const apiToken = await prisma.apiToken.findFirst({ where: { ownerId: user.id }, + select: { + token: true, + }, }) if (!apiToken) throw new TRPCError({ diff --git a/apps/builder/src/features/preview/components/RuntimeMenu.tsx b/apps/builder/src/features/preview/components/RuntimeMenu.tsx index 31913183f9..55a1e3ff9c 100644 --- a/apps/builder/src/features/preview/components/RuntimeMenu.tsx +++ b/apps/builder/src/features/preview/components/RuntimeMenu.tsx @@ -10,7 +10,7 @@ import { Text, } from '@chakra-ui/react' import { runtimes } from '../data' -import { getFeatureFlags } from '@/features/telemetry/posthog' +import { isWhatsAppAvailable } from '@/features/telemetry/posthog' type Runtime = (typeof runtimes)[number] @@ -38,9 +38,7 @@ export const RuntimeMenu = ({ selectedRuntime, onSelectRuntime }: Props) => { {runtimes .filter((runtime) => runtime.name !== selectedRuntime.name) .filter((runtime) => - runtime.name === 'WhatsApp' - ? getFeatureFlags().includes('whatsApp') - : true + runtime.name === 'WhatsApp' ? isWhatsAppAvailable() : true ) .map((runtime) => ( ) => { - if (getFeatureFlags().includes('whatsApp')) + if (isWhatsAppAvailable()) return ( { posthog.identify(userId) } -export const getFeatureFlags = () => { - return posthog.__loaded && - posthog.isFeatureEnabled('whatsApp', { send_event: false }) - ? ['whatsApp'] - : [] +export const isWhatsAppAvailable = () => { + if (!env.NEXT_PUBLIC_POSTHOG_KEY || !posthog) return true + const isWhatsAppEnabled = posthog.getFeatureFlag('whatsApp', { + send_event: false, + }) + if (isWhatsAppEnabled === undefined) return true + return posthog.__loaded && isWhatsAppEnabled } export { posthog } diff --git a/apps/viewer/src/features/chat/api/sendMessage.ts b/apps/viewer/src/features/chat/api/sendMessage.ts index 3ec81db9d9..e79f00780a 100644 --- a/apps/viewer/src/features/chat/api/sendMessage.ts +++ b/apps/viewer/src/features/chat/api/sendMessage.ts @@ -9,6 +9,7 @@ import { chatReplySchema, sendMessageInputSchema, } from '@typebot.io/schemas/features/chat/schema' +import { TRPCError } from '@trpc/server' export const sendMessage = publicProcedure .meta({ @@ -30,6 +31,11 @@ export const sendMessage = publicProcedure const session = sessionId ? await getSession(sessionId) : null if (!session) { + if (!startParams) + throw new TRPCError({ + code: 'BAD_REQUEST', + message: 'Missing startParams', + }) const { typebot, messages, @@ -39,7 +45,7 @@ export const sendMessage = publicProcedure logs, clientSideActions, newSessionState, - } = await startSession(startParams, user?.id) + } = await startSession({ startParams, userId: user?.id }) const allLogs = clientLogs ? [...(logs ?? []), ...clientLogs] : logs diff --git a/apps/viewer/src/features/chat/helpers/saveStateToDatabase.ts b/apps/viewer/src/features/chat/helpers/saveStateToDatabase.ts index 107bdc4025..62d71374fc 100644 --- a/apps/viewer/src/features/chat/helpers/saveStateToDatabase.ts +++ b/apps/viewer/src/features/chat/helpers/saveStateToDatabase.ts @@ -22,7 +22,10 @@ export const saveStateToDatabase = async ({ clientSideActions, }: Props) => { const containsSetVariableClientSideAction = clientSideActions?.some( - (action) => 'setVariable' in action + (action) => + 'setVariable' in action || + 'webhookToExecute' in action || + 'streamOpenAiChatCompletion' in action ) const isCompleted = Boolean(!input && !containsSetVariableClientSideAction) diff --git a/apps/viewer/src/features/chat/helpers/startSession.ts b/apps/viewer/src/features/chat/helpers/startSession.ts index 6c9779d01e..b0c11e70bc 100644 --- a/apps/viewer/src/features/chat/helpers/startSession.ts +++ b/apps/viewer/src/features/chat/helpers/startSession.ts @@ -27,10 +27,15 @@ import parse, { NodeType } from 'node-html-parser' import { parseDynamicTheme } from './parseDynamicTheme' import { env } from '@typebot.io/env' -export const startSession = async ( - startParams?: StartParams, - userId?: string -): Promise => { +type Props = { + startParams: StartParams + userId: string | undefined +} + +export const startSession = async ({ + startParams, + userId, +}: Props): Promise => { if (!startParams) throw new TRPCError({ code: 'BAD_REQUEST', diff --git a/apps/viewer/src/features/whatsApp/api/startWhatsAppPreview.ts b/apps/viewer/src/features/whatsApp/api/startWhatsAppPreview.ts index ad59fd1ba0..a5e3186ced 100644 --- a/apps/viewer/src/features/whatsApp/api/startWhatsAppPreview.ts +++ b/apps/viewer/src/features/whatsApp/api/startWhatsAppPreview.ts @@ -70,10 +70,13 @@ export const startWhatsAppPreview = publicProcedure const { newSessionState, messages, input, clientSideActions, logs } = await startSession({ - isOnlyRegistering: !canSendDirectMessagesToUser, - typebot: typebotId, - isPreview: true, - startGroupId, + startParams: { + isOnlyRegistering: !canSendDirectMessagesToUser, + typebot: typebotId, + isPreview: true, + startGroupId, + }, + userId: user.id, }) if (canSendDirectMessagesToUser) { diff --git a/apps/viewer/src/features/whatsApp/helpers/startWhatsAppSession.ts b/apps/viewer/src/features/whatsApp/helpers/startWhatsAppSession.ts index d76462dcd6..d47bc7e053 100644 --- a/apps/viewer/src/features/whatsApp/helpers/startWhatsAppSession.ts +++ b/apps/viewer/src/features/whatsApp/helpers/startWhatsAppSession.ts @@ -84,7 +84,10 @@ export const startWhatsAppSession = async ({ if (credentials.phoneNumberId !== phoneNumberId) return const session = await startSession({ - typebot: publicTypebot.typebot.publicId as string, + startParams: { + typebot: publicTypebot.typebot.publicId as string, + }, + userId: undefined, }) return { diff --git a/apps/viewer/src/helpers/server/context.ts b/apps/viewer/src/helpers/server/context.ts index 13832f9394..2b5adc59f0 100644 --- a/apps/viewer/src/helpers/server/context.ts +++ b/apps/viewer/src/helpers/server/context.ts @@ -21,12 +21,18 @@ const getAuthenticatedUser = async ( } const authenticateByToken = async ( - apiToken: string + token: string ): Promise => { if (typeof window !== 'undefined') return - return (await prisma.user.findFirst({ - where: { apiTokens: { some: { token: apiToken } } }, - })) as User + const apiToken = await prisma.apiToken.findFirst({ + where: { + token, + }, + select: { + owner: true, + }, + }) + return apiToken?.owner } const extractBearerToken = (req: NextApiRequest) =>