Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { NextRequest, NextResponse } from 'next/server'; |
| 2 | |
| 3 | const SLACK_WEBHOOK_URL = process.env.SLACK_FEEDBACK_WEBHOOK_URL; |
| 4 | |
| 5 | function parseUserAgent(ua: string | undefined): string { |
| 6 | if (!ua) return 'Unknown'; |
| 7 | |
| 8 | // Detect browser |
| 9 | let browser = 'Unknown'; |
| 10 | if (ua.includes('Firefox/')) { |
| 11 | const match = ua.match(/Firefox\/(\d+)/); |
| 12 | browser = `Firefox ${match?.[1] || ''}`; |
| 13 | } else if (ua.includes('Edg/')) { |
| 14 | const match = ua.match(/Edg\/(\d+)/); |
| 15 | browser = `Edge ${match?.[1] || ''}`; |
| 16 | } else if (ua.includes('Chrome/')) { |
| 17 | const match = ua.match(/Chrome\/(\d+)/); |
| 18 | browser = `Chrome ${match?.[1] || ''}`; |
| 19 | } else if (ua.includes('Safari/') && !ua.includes('Chrome')) { |
| 20 | const match = ua.match(/Version\/(\d+)/); |
| 21 | browser = `Safari ${match?.[1] || ''}`; |
| 22 | } |
| 23 | |
| 24 | // Detect OS |
| 25 | let os = ''; |
| 26 | if (ua.includes('Windows')) os = 'Windows'; |
| 27 | else if (ua.includes('Mac OS')) os = 'macOS'; |
| 28 | else if (ua.includes('Linux')) os = 'Linux'; |
| 29 | else if (ua.includes('Android')) os = 'Android'; |
| 30 | else if (ua.includes('iPhone') || ua.includes('iPad')) os = 'iOS'; |
| 31 | |
| 32 | // Detect mobile |
| 33 | const isMobile = /Mobile|Android|iPhone|iPad/.test(ua); |
| 34 | const device = isMobile ? '📱' : '💻'; |
| 35 | |
| 36 | return `${device} ${browser}${os ? ` on ${os}` : ''}`; |
| 37 | } |
| 38 | |
| 39 | const sentimentEmoji: Record<string, string> = { |
| 40 | positive: '😊', |
| 41 | neutral: '😐', |
| 42 | negative: '😞', |
| 43 | }; |
| 44 | |
| 45 | export async function POST(request: NextRequest) { |
| 46 | if (!SLACK_WEBHOOK_URL) { |
| 47 | console.error('SLACK_FEEDBACK_WEBHOOK_URL is not configured'); |
| 48 | return NextResponse.json({ error: 'Feedback not configured' }, { status: 500 }); |
| 49 | } |
| 50 | |
| 51 | try { |
| 52 | const { page, pageTitle, sentiment, message, email, userAgent, referrer, viewport, timestamp } = await request.json(); |
| 53 | |
| 54 | if (!message?.trim()) { |
| 55 | return NextResponse.json({ error: 'Message is required' }, { status: 400 }); |
| 56 | } |
| 57 | |
| 58 | const emoji = sentiment ? sentimentEmoji[sentiment] || '' : ''; |
| 59 | |
| 60 | // Parse user agent for a cleaner display |
| 61 | const browserInfo = parseUserAgent(userAgent); |
| 62 | |
| 63 | const slackMessage = { |
| 64 | blocks: [ |
| 65 | { |
| 66 | type: 'header', |
| 67 | text: { |
| 68 | type: 'plain_text', |
| 69 | text: `${emoji} New Docs Feedback`, |
| 70 | emoji: true, |
| 71 | }, |
| 72 | }, |
| 73 | { |
| 74 | type: 'section', |
| 75 | fields: [ |
| 76 | { |
| 77 | type: 'mrkdwn', |
| 78 | text: `*Page:*\n<https://docs.composio.dev${page}|${pageTitle || page}>`, |
| 79 | }, |
| 80 | { |
| 81 | type: 'mrkdwn', |
| 82 | text: `*Sentiment:*\n${sentiment || 'Not specified'}`, |
| 83 | }, |
| 84 | ], |
| 85 | }, |
| 86 | { |
| 87 | type: 'section', |
| 88 | text: { |
| 89 | type: 'mrkdwn', |
| 90 | text: `*Feedback:*\n${message}`, |
| 91 | }, |
| 92 | }, |
| 93 | |
| 94 | ? [ |
| 95 | { |
| 96 | type: 'section', |
| 97 | text: { |
| 98 | type: 'mrkdwn', |
| 99 | text: `*Email:*\n${email}`, |
| 100 | }, |
| 101 | }, |
| 102 | ] |
| 103 | : []), |
| 104 | { |
| 105 | type: 'context', |
| 106 | elements: [ |
| 107 | { |
| 108 | type: 'mrkdwn', |
| 109 | text: `🖥️ ${browserInfo} • 📐 ${viewport || 'Unknown'}${referrer ? ` • 🔗 From: ${referrer}` : ''} • 🕐 ${timestamp ? new Date(timestamp).toLocaleString('en-US', { timeZone: 'UTC', dateStyle: 'short', timeStyle: 'short' }) + ' UTC' : 'Unknown'}`, |
| 110 | }, |
| 111 | ], |
| 112 | }, |
| 113 | ], |
| 114 | }; |
| 115 | |
| 116 | const response = await fetch(SLACK_WEBHOOK_URL, { |
| 117 | method: 'POST', |
| 118 | headers: { 'Content-Type': 'application/json' }, |
| 119 | body: JSON.stringify(slackMessage), |
| 120 | }); |
| 121 | |
| 122 | if (!response.ok) { |
| 123 | throw new Error('Failed to send to Slack'); |
| 124 | } |
| 125 | |
| 126 | return NextResponse.json({ success: true }); |
| 127 | } catch (error) { |
| 128 | console.error('Feedback error:', error); |
| 129 | return NextResponse.json({ error: 'Failed to submit feedback' }, { status: 500 }); |
| 130 | } |
| 131 | } |