Files
portfolio/app/api/n8n/chat/route.ts
2026-01-07 14:30:00 +01:00

87 lines
3.6 KiB
TypeScript

import { NextResponse } from 'next/server';
export async function POST(request: Request) {
try {
const { message } = await request.json();
if (!message || typeof message !== 'string') {
return NextResponse.json(
{ error: 'Message is required' },
{ status: 400 }
);
}
// Call your n8n chat webhook
const n8nWebhookUrl = process.env.N8N_WEBHOOK_URL;
if (!n8nWebhookUrl) {
console.error('N8N_WEBHOOK_URL not configured');
// Return fallback response
return NextResponse.json({
reply: getFallbackResponse(message)
});
}
const response = await fetch(`${n8nWebhookUrl}/webhook/chat`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
...(process.env.N8N_API_KEY && {
'Authorization': `Bearer ${process.env.N8N_API_KEY}`
}),
},
body: JSON.stringify({ message }),
});
if (!response.ok) {
throw new Error(`n8n webhook failed: ${response.status}`);
}
const data = await response.json();
return NextResponse.json({ reply: data.reply || data.message || data.response });
} catch (error) {
console.error('Chat API error:', error);
// Fallback to mock responses if n8n is down
const { message } = await request.json();
return NextResponse.json(
{ reply: getFallbackResponse(message) }
);
}
}
function getFallbackResponse(message: string): string {
const lowerMessage = message.toLowerCase();
if (lowerMessage.includes('skill') || lowerMessage.includes('tech')) {
return "Dennis specializes in full-stack development with Next.js, Flutter for mobile, and DevOps with Docker Swarm. He's passionate about self-hosting and runs his own infrastructure!";
}
if (lowerMessage.includes('project')) {
return "Dennis has built Clarity (a Flutter app for people with dyslexia) and runs a complete self-hosted infrastructure with Docker Swarm, Traefik, and automated CI/CD pipelines. Check out the Projects section for more!";
}
if (lowerMessage.includes('contact') || lowerMessage.includes('email') || lowerMessage.includes('reach')) {
return "You can reach Dennis via the contact form on this site or email him at contact@dk0.dev. He's always open to discussing new opportunities and interesting projects!";
}
if (lowerMessage.includes('location') || lowerMessage.includes('where')) {
return "Dennis is based in Osnabrück, Germany. He's a student who's passionate about technology and self-hosting.";
}
if (lowerMessage.includes('hobby') || lowerMessage.includes('free time')) {
return "When Dennis isn't coding or managing servers, he enjoys gaming, jogging, and experimenting with new technologies. He also uses pen and paper for notes despite automating everything else!";
}
if (lowerMessage.includes('devops') || lowerMessage.includes('docker') || lowerMessage.includes('infrastructure')) {
return "Dennis runs his own infrastructure on IONOS and OVHcloud using Docker Swarm, Traefik for reverse proxy, and custom CI/CD pipelines. He loves self-hosting and managing game servers!";
}
if (lowerMessage.includes('student') || lowerMessage.includes('study')) {
return "Yes, Dennis is currently a student in Osnabrück while also working on various tech projects and managing his own infrastructure. He's always learning and exploring new technologies!";
}
// Default response
return "That's a great question! Dennis is a full-stack developer and DevOps enthusiast who loves building things with Next.js, Flutter, and Docker. Feel free to ask me more specific questions about his skills, projects, or experience!";
}