mirror of
https://github.com/simstudioai/sim.git
synced 2026-04-28 03:00:29 -04:00
feat(webhooks): testing whatsapp webhook in prod
This commit is contained in:
@@ -12,12 +12,19 @@ export async function POST(request: NextRequest) {
|
||||
try {
|
||||
// Get the webhook path from the URL
|
||||
const url = new URL(request.url)
|
||||
const path = url.pathname.replace('/api/webhooks/trigger', '')
|
||||
let path = url.pathname.replace('/api/webhooks/trigger', '')
|
||||
|
||||
if (!path || path === '/') {
|
||||
// Remove leading slash if present to match how it's stored in the database
|
||||
if (path.startsWith('/')) {
|
||||
path = path.substring(1)
|
||||
}
|
||||
|
||||
if (!path || path === '') {
|
||||
return new NextResponse('Invalid webhook path', { status: 400 })
|
||||
}
|
||||
|
||||
console.log('Looking for webhook with path:', path)
|
||||
|
||||
// Find the webhook in the database
|
||||
const webhooks = await db
|
||||
.select({
|
||||
@@ -118,12 +125,19 @@ export async function GET(request: NextRequest) {
|
||||
try {
|
||||
// Get the webhook path from the URL
|
||||
const url = new URL(request.url)
|
||||
const path = url.pathname.replace('/api/webhooks/trigger', '')
|
||||
let path = url.pathname.replace('/api/webhooks/trigger', '')
|
||||
|
||||
if (!path || path === '/') {
|
||||
// Remove leading slash if present to match how it's stored in the database
|
||||
if (path.startsWith('/')) {
|
||||
path = path.substring(1)
|
||||
}
|
||||
|
||||
if (!path || path === '') {
|
||||
return new NextResponse('Invalid webhook path', { status: 400 })
|
||||
}
|
||||
|
||||
console.log('Looking for webhook with path:', path)
|
||||
|
||||
// Find the webhook in the database
|
||||
const webhooks = await db
|
||||
.select({
|
||||
@@ -132,10 +146,11 @@ export async function GET(request: NextRequest) {
|
||||
})
|
||||
.from(webhook)
|
||||
.innerJoin(workflow, eq(webhook.workflowId, workflow.id))
|
||||
.where(and(eq(webhook.path, path), eq(webhook.isActive, true), eq(workflow.isDeployed, true)))
|
||||
.where(and(eq(webhook.path, path), eq(webhook.isActive, true)))
|
||||
.limit(1)
|
||||
|
||||
if (webhooks.length === 0) {
|
||||
console.log('Webhook not found for path:', path)
|
||||
return new NextResponse('Webhook not found', { status: 404 })
|
||||
}
|
||||
|
||||
|
||||
@@ -15,11 +15,15 @@ export async function GET(request: NextRequest) {
|
||||
const token = searchParams.get('hub.verify_token')
|
||||
const challenge = searchParams.get('hub.challenge')
|
||||
|
||||
console.log('WhatsApp verification request:', { mode, token, challenge })
|
||||
|
||||
if (!mode || !token || !challenge) {
|
||||
console.log('Missing verification parameters')
|
||||
return new NextResponse('Missing verification parameters', { status: 400 })
|
||||
}
|
||||
|
||||
if (mode !== 'subscribe') {
|
||||
console.log('Invalid mode:', mode)
|
||||
return new NextResponse('Invalid mode', { status: 400 })
|
||||
}
|
||||
|
||||
@@ -31,16 +35,26 @@ export async function GET(request: NextRequest) {
|
||||
.from(webhook)
|
||||
.where(and(eq(webhook.provider, 'whatsapp'), eq(webhook.isActive, true)))
|
||||
|
||||
console.log('Found WhatsApp webhooks:', webhooks.length)
|
||||
|
||||
// Check if any webhook has a matching verification token
|
||||
for (const { webhook: wh } of webhooks) {
|
||||
const providerConfig = (wh.providerConfig as Record<string, any>) || {}
|
||||
const verificationToken = providerConfig.verificationToken
|
||||
|
||||
console.log('Checking webhook:', {
|
||||
id: wh.id,
|
||||
path: wh.path,
|
||||
token: verificationToken ? verificationToken.substring(0, 3) + '...' : 'none',
|
||||
})
|
||||
|
||||
if (verificationToken && token === verificationToken) {
|
||||
console.log('Verification successful, returning challenge')
|
||||
return new NextResponse(challenge, { status: 200 })
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Verification failed, no matching token found')
|
||||
return new NextResponse('Verification failed', { status: 403 })
|
||||
}
|
||||
|
||||
|
||||
@@ -99,7 +99,8 @@ export function WebhookModal({
|
||||
}
|
||||
}, [webhookId, webhookProvider])
|
||||
|
||||
// Format the path to ensure it starts with a slash
|
||||
// Format the path to ensure it starts with a slash for URL display
|
||||
// but will be saved without the slash to match the database
|
||||
const formattedPath =
|
||||
webhookPath && webhookPath.trim() !== ''
|
||||
? webhookPath.startsWith('/')
|
||||
@@ -140,10 +141,12 @@ export function WebhookModal({
|
||||
// Call the onSave callback with the path and provider-specific config
|
||||
if (onSave) {
|
||||
const providerConfig = getProviderConfig()
|
||||
// Use the path without the leading slash
|
||||
// Always save the path without the leading slash to match how it's queried in the API
|
||||
const pathToSave = formattedPath.startsWith('/')
|
||||
? formattedPath.substring(1)
|
||||
: formattedPath
|
||||
|
||||
console.log('Saving webhook with path:', pathToSave)
|
||||
await onSave(pathToSave, providerConfig)
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
Reference in New Issue
Block a user