From 6e391b22a69fafe597b7bf0dd01a0b85c0addfb6 Mon Sep 17 00:00:00 2001 From: Clairton Rodrigo Heinzen Date: Thu, 25 Jan 2024 07:53:57 -0300 Subject: [PATCH] =?UTF-8?q?:pencil:=20Add=20env=20WHATSAPP=5FCLOUD=5FAPI?= =?UTF-8?q?=5FURL=20to=20possible=20change=20https://gr=E2=80=A6=20(#1170)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit refs #1117 ## Summary by CodeRabbit - **Refactor** - Dynamically construct API URLs for WhatsApp features using environment variables for improved reliability and configurability. - Updated `sendWhatsAppMessage` function to use `env.WHATSAPP_CLOUD_API_URL` for URL construction, allowing for more dynamic configuration. --------- Co-authored-by: Baptiste Arnaud --- .../builder/src/features/whatsapp/getPhoneNumber.ts | 3 ++- .../src/features/whatsapp/getSystemTokenInfo.ts | 3 ++- .../[typebotId]/whatsapp/media/[mediaId].ts | 3 ++- apps/docs/self-hosting/configuration.mdx | 13 +++++++------ packages/bot-engine/whatsapp/sendWhatsAppMessage.ts | 3 ++- packages/env/env.ts | 5 +++++ 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/apps/builder/src/features/whatsapp/getPhoneNumber.ts b/apps/builder/src/features/whatsapp/getPhoneNumber.ts index 7e9d1aa63..06adb8e03 100644 --- a/apps/builder/src/features/whatsapp/getPhoneNumber.ts +++ b/apps/builder/src/features/whatsapp/getPhoneNumber.ts @@ -5,6 +5,7 @@ import prisma from '@typebot.io/lib/prisma' import { decrypt } from '@typebot.io/lib/api/encryption/decrypt' import { TRPCError } from '@trpc/server' import { WhatsAppCredentials } from '@typebot.io/schemas/features/whatsapp' +import { env } from '@typebot.io/env' const inputSchema = z.object({ credentialsId: z.string().optional(), @@ -22,7 +23,7 @@ export const getPhoneNumber = authenticatedProcedure message: 'Credentials not found', }) const { display_phone_number } = (await got( - `https://graph.facebook.com/v17.0/${credentials.phoneNumberId}`, + `${env.WHATSAPP_CLOUD_API_URL}/v17.0/${credentials.phoneNumberId}`, { headers: { Authorization: `Bearer ${credentials.systemUserAccessToken}`, diff --git a/apps/builder/src/features/whatsapp/getSystemTokenInfo.ts b/apps/builder/src/features/whatsapp/getSystemTokenInfo.ts index 5f84bfb86..542f0627a 100644 --- a/apps/builder/src/features/whatsapp/getSystemTokenInfo.ts +++ b/apps/builder/src/features/whatsapp/getSystemTokenInfo.ts @@ -5,6 +5,7 @@ import { TRPCError } from '@trpc/server' import { WhatsAppCredentials } from '@typebot.io/schemas/features/whatsapp' import prisma from '@typebot.io/lib/prisma' import { decrypt } from '@typebot.io/lib/api/encryption/decrypt' +import { env } from '@typebot.io/env' const inputSchema = z.object({ token: z.string().optional(), @@ -28,7 +29,7 @@ export const getSystemTokenInfo = authenticatedProcedure const { data: { expires_at, scopes, app_id, application }, } = (await got( - `https://graph.facebook.com/v17.0/debug_token?input_token=${credentials.systemUserAccessToken}`, + `${env.WHATSAPP_CLOUD_API_URL}/v17.0/debug_token?input_token=${credentials.systemUserAccessToken}`, { headers: { Authorization: `Bearer ${credentials.systemUserAccessToken}`, diff --git a/apps/builder/src/pages/api/typebots/[typebotId]/whatsapp/media/[mediaId].ts b/apps/builder/src/pages/api/typebots/[typebotId]/whatsapp/media/[mediaId].ts index 1dc6394cc..0509b8649 100644 --- a/apps/builder/src/pages/api/typebots/[typebotId]/whatsapp/media/[mediaId].ts +++ b/apps/builder/src/pages/api/typebots/[typebotId]/whatsapp/media/[mediaId].ts @@ -10,6 +10,7 @@ import { isReadWorkspaceFobidden } from '@/features/workspace/helpers/isReadWork import { WhatsAppCredentials } from '@typebot.io/schemas/features/whatsapp' import got from 'got' import { decrypt } from '@typebot.io/lib/api/encryption/decrypt' +import { env } from '@typebot.io/env' const handler = async (req: NextApiRequest, res: NextApiResponse) => { if (req.method === 'GET') { @@ -61,7 +62,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { )) as WhatsAppCredentials['data'] const { body } = await got.get({ - url: `https://graph.facebook.com/v17.0/${mediaId}`, + url: `${env.WHATSAPP_CLOUD_API_URL}/v17.0/${mediaId}`, headers: { Authorization: `Bearer ${credentialsData.systemUserAccessToken}`, }, diff --git a/apps/docs/self-hosting/configuration.mdx b/apps/docs/self-hosting/configuration.mdx index c98979da4..f028f6762 100644 --- a/apps/docs/self-hosting/configuration.mdx +++ b/apps/docs/self-hosting/configuration.mdx @@ -238,12 +238,13 @@ In order to be able to test your bot on WhatsApp from the Preview drawer, you ne -| Parameter | Default | Description | -| ------------------------------------- | ------- | ------------------------------------------------------- | -| META_SYSTEM_USER_TOKEN | | The system user token used to send WhatsApp messages | -| WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID | | The phone number ID from which the message will be sent | -| WHATSAPP_PREVIEW_TEMPLATE_NAME | | The preview start template message name | -| WHATSAPP_PREVIEW_TEMPLATE_LANG | en | The preview start template message name | +| Parameter | Default | Description | +| ------------------------------------- | -------------------------- | ------------------------------------------------------- | +| META_SYSTEM_USER_TOKEN | | The system user token used to send WhatsApp messages | +| WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID | | The phone number ID from which the message will be sent | +| WHATSAPP_PREVIEW_TEMPLATE_NAME | | The preview start template message name | +| WHATSAPP_PREVIEW_TEMPLATE_LANG | en | The preview start template message name | +| WHATSAPP_CLOUD_API_URL | https://graph.facebook.com | The WhatsApp Cloud API base URL | ## Others diff --git a/packages/bot-engine/whatsapp/sendWhatsAppMessage.ts b/packages/bot-engine/whatsapp/sendWhatsAppMessage.ts index ee7eae863..2bcf8bf86 100644 --- a/packages/bot-engine/whatsapp/sendWhatsAppMessage.ts +++ b/packages/bot-engine/whatsapp/sendWhatsAppMessage.ts @@ -3,6 +3,7 @@ import { WhatsAppCredentials, WhatsAppSendingMessage, } from '@typebot.io/schemas/features/whatsapp' +import { env } from '@typebot.io/env' type Props = { to: string @@ -16,7 +17,7 @@ export const sendWhatsAppMessage = async ({ credentials, }: Props) => got.post({ - url: `https://graph.facebook.com/v17.0/${credentials.phoneNumberId}/messages`, + url: `${env.WHATSAPP_CLOUD_API_URL}/v17.0/${credentials.phoneNumberId}/messages`, headers: { Authorization: `Bearer ${credentials.systemUserAccessToken}`, }, diff --git a/packages/env/env.ts b/packages/env/env.ts index 4418ed26e..04be48070 100644 --- a/packages/env/env.ts +++ b/packages/env/env.ts @@ -283,6 +283,11 @@ const whatsAppEnv = { WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID: z.string().min(1).optional(), WHATSAPP_PREVIEW_TEMPLATE_NAME: z.string().min(1).optional(), WHATSAPP_PREVIEW_TEMPLATE_LANG: z.string().min(1).optional().default('en'), + WHATSAPP_CLOUD_API_URL: z + .string() + .url() + .optional() + .default('https://graph.facebook.com'), }, }