const express = require("express"); const app = express(); const PORT = process.env.PORT || 3000; // ====================================================== // Ruby Bot — מנוע פנימי // ====================================================== const conversations = new Map(); function getConversation(sessionId) { if (!conversations.has(sessionId)) { conversations.set(sessionId, []); } return conversations.get(sessionId); } function rubyEngine(message, history) { const text = message.trim().toLowerCase(); if (!text) { return "כתוב לי משהו 🙂"; } // -------------------------------------------- // שלום // -------------------------------------------- if ( text.includes("שלום") || text === "היי" || text === "הי" || text.includes("אהלן") ) { return "היי! 👋 אני רובי. במה אפשר לעזור לך?"; } // -------------------------------------------- // מי אני // -------------------------------------------- if ( text.includes("מי אתה") || text.includes("מי את") || text.includes("מה אתה") ) { return "אני רובי 🤖 — הבוט של האתר הזה."; } // -------------------------------------------- // יכולות // -------------------------------------------- if ( text.includes("מה אתה יודע") || text.includes("מה אפשר לעשות") || text.includes("מה אתה יכול") ) { return [ "אני רובי 🤖", "", "כרגע אני יודע:", "• לנהל שיחה", "• לזכור את השיחה הנוכחית", "• לזהות שאלות בסיסיות", "• לענות לפי חוקים", "• לזכור מידע שנאמר במהלך השיחה", "", "בהמשך אפשר לחבר אותי למנוע AI אמיתי. 🚀" ].join("\n"); } // -------------------------------------------- // תודה // -------------------------------------------- if ( text.includes("תודה רבה") || text.includes("תודה") ) { return "בשמחה! ❤️"; } // -------------------------------------------- // עזרה // -------------------------------------------- if ( text.includes("עזרה") || text.includes("help") ) { return "בטח! ספר לי מה אתה רוצה לעשות ואנסה לעזור."; } // -------------------------------------------- // שעה // -------------------------------------------- if (text.includes("מה השעה")) { return `השעה כרגע היא ${new Date().toLocaleTimeString("he-IL")}.`; } // -------------------------------------------- // תאריך // -------------------------------------------- if ( text.includes("תאריך") || text.includes("איזה יום") ) { return `היום ${new Date().toLocaleDateString("he-IL")}.`; } // -------------------------------------------- // בדיקה // -------------------------------------------- if ( text.includes("בדיקה") || text.includes("טסט") ) { return "המערכת עובדת מצוין ✅"; } // -------------------------------------------- // זיכרון בסיסי — שם // -------------------------------------------- const nameMatch = text.match( /(?:קוראים לי|שמי|אני)\s+([א-תA-Za-z]+)/ ); if (nameMatch) { return `נעים מאוד, ${nameMatch[1]}! 👋 אני אזכור את זה במהלך השיחה.`; } // -------------------------------------------- // ניסיון לזהות שם מההיסטוריה // -------------------------------------------- if ( text.includes("איך קוראים לי") || text.includes("מה השם שלי") ) { for (let i = history.length - 1; i >= 0; i--) { const item = history[i]; if ( item.role === "user" && typeof item.content === "string" ) { const match = item.content.match( /(?:קוראים לי|שמי|אני)\s+([א-תA-Za-z]+)/ ); if (match) { return `קוראים לך ${match[1]} 😊`; } } } return "עדיין לא סיפרת לי איך קוראים לך."; } // -------------------------------------------- // ברירת מחדל // -------------------------------------------- return [ `קיבלתי את ההודעה שלך:`, "", `"${message}"`, "", "אני עדיין בגרסה הראשונה של רובי, ולכן המנוע שלי כרגע בסיסי.", "", "אבל התשתית מוכנה — אפשר להפוך אותי למנוע AI מלא בהמשך. 🚀" ].join("\n"); } // ====================================================== // API // ====================================================== app.use(express.json({ limit: "100kb" })); // ====================================================== // POST /api/chat // ====================================================== app.post("/api/chat", (req, res) => { try { const { sessionId, message } = req.body; if ( typeof sessionId !== "string" || !sessionId.trim() ) { return res.status(400).json({ error: "sessionId חסר" }); } if ( typeof message !== "string" || !message.trim() ) { return res.status(400).json({ error: "הודעה ריקה" }); } const cleanSessionId = sessionId.trim(); const cleanMessage = message.trim(); const history = getConversation(cleanSessionId); const reply = rubyEngine( cleanMessage, history ); // שומרים את הודעת המשתמש history.push({ role: "user", content: cleanMessage, time: Date.now() }); // שומרים את תשובת רובי history.push({ role: "assistant", content: reply, time: Date.now() }); // שומרים רק 50 הודעות אחרונות if (history.length > 50) { history.splice( 0, history.length - 50 ); } return res.json({ reply }); } catch (error) { console.error("Chat error:", error); return res.status(500).json({ error: "שגיאת שרת" }); } }); // ====================================================== // DELETE /api/chat/:sessionId // ====================================================== app.delete( "/api/chat/:sessionId", (req, res) => { const sessionId = req.params.sessionId; if (sessionId) { conversations.delete(sessionId); } return res.json({ success: true }); } ); // ====================================================== // GET / // האתר // ====================================================== app.get("/", (req, res) => { res.send(`