Add AI-powered chatbot for media outlets
Integrate an AI chatbot feature allowing users to interact with media outlets, fetch chat history, and generate AI responses using OpenAI. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 9a264234-c5d7-4dcc-adf3-a954b149b30d Replit-Commit-Checkpoint-Type: intermediate_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/3df548ff-50ae-432f-9be4-25d34eccc983/9a264234-c5d7-4dcc-adf3-a954b149b30d/d35d7YU
This commit is contained in:
@ -2,7 +2,8 @@ import type { Express } from "express";
|
||||
import { createServer, type Server } from "http";
|
||||
import { storage } from "./storage";
|
||||
import { setupAuth, isAuthenticated } from "./simpleAuth";
|
||||
import { insertArticleSchema, insertMediaOutletRequestSchema, insertBidSchema, insertCommentSchema, insertPredictionBetSchema, insertCommunityPostSchema, insertCommunityReplySchema } from "@shared/schema";
|
||||
import { insertArticleSchema, insertMediaOutletRequestSchema, insertBidSchema, insertCommentSchema, insertPredictionBetSchema, insertCommunityPostSchema, insertCommunityReplySchema, insertChatMessageSchema } from "@shared/schema";
|
||||
import { generateChatbotResponse } from "./chatbot";
|
||||
|
||||
export async function registerRoutes(app: Express): Promise<Server> {
|
||||
// Auth middleware
|
||||
@ -582,6 +583,77 @@ export async function registerRoutes(app: Express): Promise<Server> {
|
||||
}
|
||||
});
|
||||
|
||||
// Chatbot routes
|
||||
app.get('/api/media-outlets/:slug/chat', isAuthenticated, async (req: any, res) => {
|
||||
try {
|
||||
const outlet = await storage.getMediaOutletBySlug(req.params.slug);
|
||||
if (!outlet) {
|
||||
return res.status(404).json({ message: "Media outlet not found" });
|
||||
}
|
||||
|
||||
const userId = req.user.claims?.sub || req.user.id;
|
||||
const messages = await storage.getChatMessages(outlet.id, userId);
|
||||
res.json(messages);
|
||||
} catch (error) {
|
||||
console.error("Error fetching chat messages:", error);
|
||||
res.status(500).json({ message: "Failed to fetch chat messages" });
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/media-outlets/:slug/chat', isAuthenticated, async (req: any, res) => {
|
||||
try {
|
||||
const outlet = await storage.getMediaOutletBySlug(req.params.slug);
|
||||
if (!outlet) {
|
||||
return res.status(404).json({ message: "Media outlet not found" });
|
||||
}
|
||||
|
||||
const userId = req.user.claims?.sub || req.user.id;
|
||||
const { content } = req.body;
|
||||
|
||||
if (!content || typeof content !== 'string') {
|
||||
return res.status(400).json({ message: "Invalid message content" });
|
||||
}
|
||||
|
||||
// Save user message
|
||||
const userMessage = insertChatMessageSchema.parse({
|
||||
mediaOutletId: outlet.id,
|
||||
userId,
|
||||
role: 'user',
|
||||
content
|
||||
});
|
||||
await storage.createChatMessage(userMessage);
|
||||
|
||||
// Get chat history
|
||||
const chatHistory = await storage.getChatMessages(outlet.id, userId);
|
||||
const messages = chatHistory.map(msg => ({
|
||||
role: msg.role as 'user' | 'assistant',
|
||||
content: msg.content
|
||||
}));
|
||||
|
||||
// Generate AI response
|
||||
const aiResponse = await generateChatbotResponse(
|
||||
messages,
|
||||
outlet.name,
|
||||
outlet.category,
|
||||
outlet.description || undefined
|
||||
);
|
||||
|
||||
// Save assistant message
|
||||
const assistantMessage = insertChatMessageSchema.parse({
|
||||
mediaOutletId: outlet.id,
|
||||
userId,
|
||||
role: 'assistant',
|
||||
content: aiResponse
|
||||
});
|
||||
const savedAssistantMessage = await storage.createChatMessage(assistantMessage);
|
||||
|
||||
res.status(201).json(savedAssistantMessage);
|
||||
} catch (error) {
|
||||
console.error("Error processing chat message:", error);
|
||||
res.status(500).json({ message: "Failed to process chat message" });
|
||||
}
|
||||
});
|
||||
|
||||
const httpServer = createServer(app);
|
||||
return httpServer;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user