forked from baptisteArno/typebot.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
⚡ (whatsapp) Improve whatsApp management and media collection
Closes baptisteArno#796
- Loading branch information
1 parent
6650d8b
commit 45d92d1
Showing
22 changed files
with
479 additions
and
426 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
apps/builder/src/pages/api/typebots/[typebotId]/whatsapp/media/[mediaId].ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import prisma from '@typebot.io/lib/prisma' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
import { getAuthenticatedUser } from '@/features/auth/helpers/getAuthenticatedUser' | ||
import { | ||
decrypt, | ||
methodNotAllowed, | ||
notAuthenticated, | ||
notFound, | ||
} from '@typebot.io/lib/api' | ||
import { isReadWorkspaceFobidden } from '@/features/workspace/helpers/isReadWorkspaceFobidden' | ||
import { WhatsAppCredentials } from '@typebot.io/schemas/features/whatsapp' | ||
import got from 'got' | ||
|
||
const handler = async (req: NextApiRequest, res: NextApiResponse) => { | ||
if (req.method === 'GET') { | ||
const user = await getAuthenticatedUser(req, res) | ||
if (!user) return notAuthenticated(res) | ||
|
||
const typebotId = req.query.typebotId as string | ||
|
||
const typebot = await prisma.typebot.findFirst({ | ||
where: { | ||
id: typebotId, | ||
}, | ||
select: { | ||
whatsAppCredentialsId: true, | ||
workspace: { | ||
select: { | ||
credentials: { | ||
where: { | ||
type: 'whatsApp', | ||
}, | ||
}, | ||
members: { | ||
select: { | ||
userId: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
if (!typebot?.workspace || isReadWorkspaceFobidden(typebot.workspace, user)) | ||
return notFound(res, 'Workspace not found') | ||
|
||
if (!typebot) return notFound(res, 'Typebot not found') | ||
|
||
const mediaId = req.query.mediaId as string | ||
const credentialsId = typebot.whatsAppCredentialsId | ||
|
||
const credentials = typebot.workspace.credentials.find( | ||
(credential) => credential.id === credentialsId | ||
) | ||
|
||
if (!credentials) return notFound(res, 'Credentials not found') | ||
|
||
const credentialsData = (await decrypt( | ||
credentials.data, | ||
credentials.iv | ||
)) as WhatsAppCredentials['data'] | ||
|
||
const { body } = await got.get({ | ||
url: `https://graph.facebook.com/v17.0/${mediaId}`, | ||
headers: { | ||
Authorization: `Bearer ${credentialsData.systemUserAccessToken}`, | ||
}, | ||
}) | ||
|
||
const parsedBody = JSON.parse(body) as { url: string; mime_type: string } | ||
|
||
const buffer = await got(parsedBody.url, { | ||
headers: { | ||
Authorization: `Bearer ${credentialsData.systemUserAccessToken}`, | ||
}, | ||
}).buffer() | ||
|
||
res.setHeader('Content-Type', parsedBody.mime_type) | ||
res.setHeader('Cache-Control', 'public, max-age=86400') | ||
|
||
return res.send(buffer) | ||
} | ||
return methodNotAllowed(res) | ||
} | ||
|
||
export default handler |
Oops, something went wrong.