👷 Add vercel function to benefit from waitUntil method for WA webhook
This commit is contained in:
41
apps/builder/api/v1/whatsapp/preview/webhook.ts
Normal file
41
apps/builder/api/v1/whatsapp/preview/webhook.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import {
|
||||
WhatsAppWebhookRequestBody,
|
||||
whatsAppWebhookRequestBodySchema,
|
||||
} from '@typebot.io/schemas/features/whatsapp'
|
||||
import { resumeWhatsAppFlow } from '@typebot.io/bot-engine/whatsapp/resumeWhatsAppFlow'
|
||||
import { isNotDefined } from '@typebot.io/lib'
|
||||
import { env } from '@typebot.io/env'
|
||||
import type { RequestContext } from '@vercel/edge'
|
||||
|
||||
const processWhatsAppReply = async (
|
||||
entry: WhatsAppWebhookRequestBody['entry']
|
||||
) => {
|
||||
const receivedMessage = entry.at(0)?.changes.at(0)?.value.messages?.at(0)
|
||||
if (isNotDefined(receivedMessage)) return { message: 'No message found' }
|
||||
const contactName =
|
||||
entry.at(0)?.changes.at(0)?.value?.contacts?.at(0)?.profile?.name ?? ''
|
||||
const contactPhoneNumber =
|
||||
entry.at(0)?.changes.at(0)?.value?.messages?.at(0)?.from ?? ''
|
||||
return resumeWhatsAppFlow({
|
||||
receivedMessage,
|
||||
sessionId: `wa-preview-${receivedMessage.from}`,
|
||||
contact: {
|
||||
name: contactName,
|
||||
phoneNumber: contactPhoneNumber,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export async function POST(request: Request, context: RequestContext) {
|
||||
if (!env.WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID)
|
||||
return new Response(
|
||||
'WHATSAPP_PREVIEW_FROM_PHONE_NUMBER_ID is not defined',
|
||||
{
|
||||
status: 500,
|
||||
}
|
||||
)
|
||||
const body = await request.json()
|
||||
const { entry } = whatsAppWebhookRequestBodySchema.parse(body)
|
||||
context.waitUntil(processWhatsAppReply(entry))
|
||||
return new Response('Message is being processed.', { status: 200 })
|
||||
}
|
||||
@@ -67,9 +67,9 @@
|
||||
"framer-motion": "10.3.0",
|
||||
"google-auth-library": "8.9.0",
|
||||
"google-spreadsheet": "4.1.1",
|
||||
"ky": "1.2.3",
|
||||
"immer": "10.0.2",
|
||||
"jsonwebtoken": "9.0.1",
|
||||
"ky": "1.2.3",
|
||||
"libphonenumber-js": "1.10.37",
|
||||
"micro": "10.0.1",
|
||||
"micro-cors": "0.1.1",
|
||||
@@ -122,6 +122,7 @@
|
||||
"@types/qs": "6.9.7",
|
||||
"@types/react": "18.2.15",
|
||||
"@types/tinycolor2": "1.4.3",
|
||||
"@vercel/edge": "1.1.1",
|
||||
"dotenv-cli": "7.2.1",
|
||||
"eslint": "8.44.0",
|
||||
"eslint-config-custom": "workspace:*",
|
||||
|
||||
Reference in New Issue
Block a user