2
0

📝 Add env WHATSAPP_CLOUD_API_URL to possible change https://gr… (#1170)

refs #1117 

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## 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.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: Baptiste Arnaud <baptiste.arnaud95@gmail.com>
This commit is contained in:
Clairton Rodrigo Heinzen
2024-01-25 07:53:57 -03:00
committed by Baptiste Arnaud
parent ef05b71869
commit 47af9a9a59
6 changed files with 20 additions and 10 deletions

View File

@ -5,6 +5,7 @@ import prisma from '@typebot.io/lib/prisma'
import { decrypt } from '@typebot.io/lib/api/encryption/decrypt' import { decrypt } from '@typebot.io/lib/api/encryption/decrypt'
import { TRPCError } from '@trpc/server' import { TRPCError } from '@trpc/server'
import { WhatsAppCredentials } from '@typebot.io/schemas/features/whatsapp' import { WhatsAppCredentials } from '@typebot.io/schemas/features/whatsapp'
import { env } from '@typebot.io/env'
const inputSchema = z.object({ const inputSchema = z.object({
credentialsId: z.string().optional(), credentialsId: z.string().optional(),
@ -22,7 +23,7 @@ export const getPhoneNumber = authenticatedProcedure
message: 'Credentials not found', message: 'Credentials not found',
}) })
const { display_phone_number } = (await got( const { display_phone_number } = (await got(
`https://graph.facebook.com/v17.0/${credentials.phoneNumberId}`, `${env.WHATSAPP_CLOUD_API_URL}/v17.0/${credentials.phoneNumberId}`,
{ {
headers: { headers: {
Authorization: `Bearer ${credentials.systemUserAccessToken}`, Authorization: `Bearer ${credentials.systemUserAccessToken}`,

View File

@ -5,6 +5,7 @@ import { TRPCError } from '@trpc/server'
import { WhatsAppCredentials } from '@typebot.io/schemas/features/whatsapp' import { WhatsAppCredentials } from '@typebot.io/schemas/features/whatsapp'
import prisma from '@typebot.io/lib/prisma' import prisma from '@typebot.io/lib/prisma'
import { decrypt } from '@typebot.io/lib/api/encryption/decrypt' import { decrypt } from '@typebot.io/lib/api/encryption/decrypt'
import { env } from '@typebot.io/env'
const inputSchema = z.object({ const inputSchema = z.object({
token: z.string().optional(), token: z.string().optional(),
@ -28,7 +29,7 @@ export const getSystemTokenInfo = authenticatedProcedure
const { const {
data: { expires_at, scopes, app_id, application }, data: { expires_at, scopes, app_id, application },
} = (await got( } = (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: { headers: {
Authorization: `Bearer ${credentials.systemUserAccessToken}`, Authorization: `Bearer ${credentials.systemUserAccessToken}`,

View File

@ -10,6 +10,7 @@ import { isReadWorkspaceFobidden } from '@/features/workspace/helpers/isReadWork
import { WhatsAppCredentials } from '@typebot.io/schemas/features/whatsapp' import { WhatsAppCredentials } from '@typebot.io/schemas/features/whatsapp'
import got from 'got' import got from 'got'
import { decrypt } from '@typebot.io/lib/api/encryption/decrypt' import { decrypt } from '@typebot.io/lib/api/encryption/decrypt'
import { env } from '@typebot.io/env'
const handler = async (req: NextApiRequest, res: NextApiResponse) => { const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === 'GET') { if (req.method === 'GET') {
@ -61,7 +62,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
)) as WhatsAppCredentials['data'] )) as WhatsAppCredentials['data']
const { body } = await got.get({ const { body } = await got.get({
url: `https://graph.facebook.com/v17.0/${mediaId}`, url: `${env.WHATSAPP_CLOUD_API_URL}/v17.0/${mediaId}`,
headers: { headers: {
Authorization: `Bearer ${credentialsData.systemUserAccessToken}`, Authorization: `Bearer ${credentialsData.systemUserAccessToken}`,
}, },

View File

@ -238,12 +238,13 @@ In order to be able to test your bot on WhatsApp from the Preview drawer, you ne
</Accordion> </Accordion>
| Parameter | Default | Description | | Parameter | Default | Description |
| ------------------------------------- | ------- | ------------------------------------------------------- | | ------------------------------------- | -------------------------- | ------------------------------------------------------- |
| META_SYSTEM_USER_TOKEN | | The system user token used to send WhatsApp messages | | 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_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_NAME | | The preview start template message name |
| WHATSAPP_PREVIEW_TEMPLATE_LANG | en | 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 ## Others

View File

@ -3,6 +3,7 @@ import {
WhatsAppCredentials, WhatsAppCredentials,
WhatsAppSendingMessage, WhatsAppSendingMessage,
} from '@typebot.io/schemas/features/whatsapp' } from '@typebot.io/schemas/features/whatsapp'
import { env } from '@typebot.io/env'
type Props = { type Props = {
to: string to: string
@ -16,7 +17,7 @@ export const sendWhatsAppMessage = async ({
credentials, credentials,
}: Props) => }: Props) =>
got.post({ got.post({
url: `https://graph.facebook.com/v17.0/${credentials.phoneNumberId}/messages`, url: `${env.WHATSAPP_CLOUD_API_URL}/v17.0/${credentials.phoneNumberId}/messages`,
headers: { headers: {
Authorization: `Bearer ${credentials.systemUserAccessToken}`, Authorization: `Bearer ${credentials.systemUserAccessToken}`,
}, },

5
packages/env/env.ts vendored
View File

@ -283,6 +283,11 @@ const whatsAppEnv = {
WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID: z.string().min(1).optional(), WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID: z.string().min(1).optional(),
WHATSAPP_PREVIEW_TEMPLATE_NAME: 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_PREVIEW_TEMPLATE_LANG: z.string().min(1).optional().default('en'),
WHATSAPP_CLOUD_API_URL: z
.string()
.url()
.optional()
.default('https://graph.facebook.com'),
}, },
} }