Connect Horus Chat to Telegram (Horus)
This commit is contained in:
@@ -0,0 +1,24 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
import fs from "fs";
|
||||||
|
|
||||||
|
const RESPONSE_FILE = "/tmp/horus-mc-response.txt";
|
||||||
|
|
||||||
|
export async function GET() {
|
||||||
|
try {
|
||||||
|
// Check if there's a response file
|
||||||
|
if (fs.existsSync(RESPONSE_FILE)) {
|
||||||
|
const response = fs.readFileSync(RESPONSE_FILE, "utf-8").trim();
|
||||||
|
|
||||||
|
if (response) {
|
||||||
|
// Clear the file after reading
|
||||||
|
fs.writeFileSync(RESPONSE_FILE, "");
|
||||||
|
return NextResponse.json({ response });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return NextResponse.json({ response: null });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error:", error);
|
||||||
|
return NextResponse.json({ response: null });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
|
// Store messages in temp file
|
||||||
|
const QUEUE_FILE = "/tmp/horus-mc-queue.json";
|
||||||
|
|
||||||
|
async function sendToTelegram(text: string) {
|
||||||
|
const token = process.env.TELEGRAM_BOT_TOKEN;
|
||||||
|
if (!token) {
|
||||||
|
console.log("No Telegram token");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send to Haitham via Telegram
|
||||||
|
const res = await fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({
|
||||||
|
chat_id: "382315644",
|
||||||
|
text: `💬 MC Chat:\n${text}`,
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function POST(request: Request) {
|
||||||
|
try {
|
||||||
|
const { message } = await request.json();
|
||||||
|
|
||||||
|
if (!message) {
|
||||||
|
return NextResponse.json({ error: "Message required" }, { status: 400 });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Forward to Telegram
|
||||||
|
await sendToTelegram(message);
|
||||||
|
|
||||||
|
return NextResponse.json({ status: "sent" });
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error:", error);
|
||||||
|
return NextResponse.json({ error: "Failed" }, { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,7 +10,7 @@ interface Message {
|
|||||||
timestamp: number;
|
timestamp: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function VoiceChat() {
|
export default function HorusChat() {
|
||||||
const [input, setInput] = useState("");
|
const [input, setInput] = useState("");
|
||||||
const [messages, setMessages] = useState<Message[]>([
|
const [messages, setMessages] = useState<Message[]>([
|
||||||
{
|
{
|
||||||
@@ -27,6 +27,34 @@ export default function VoiceChat() {
|
|||||||
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
||||||
}, [messages]);
|
}, [messages]);
|
||||||
|
|
||||||
|
// Poll for responses from Horus (Telegram)
|
||||||
|
useEffect(() => {
|
||||||
|
let pollInterval: NodeJS.Timeout;
|
||||||
|
|
||||||
|
if (isProcessing) {
|
||||||
|
pollInterval = setInterval(async () => {
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/horus-mc-response");
|
||||||
|
const data = await res.json();
|
||||||
|
|
||||||
|
if (data.response) {
|
||||||
|
setMessages(prev => [...prev, {
|
||||||
|
id: `horus_${Date.now()}`,
|
||||||
|
role: "assistant",
|
||||||
|
content: data.response,
|
||||||
|
timestamp: Date.now(),
|
||||||
|
}]);
|
||||||
|
setIsProcessing(false);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Poll error:", e);
|
||||||
|
}
|
||||||
|
}, 3000);
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => clearInterval(pollInterval);
|
||||||
|
}, [isProcessing]);
|
||||||
|
|
||||||
const handleSend = async () => {
|
const handleSend = async () => {
|
||||||
if (!input.trim() || isProcessing) return;
|
if (!input.trim() || isProcessing) return;
|
||||||
|
|
||||||
@@ -42,35 +70,22 @@ export default function VoiceChat() {
|
|||||||
setIsProcessing(true);
|
setIsProcessing(true);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Send to SiteMente AI agent
|
// Send to Horus via Telegram
|
||||||
const res = await fetch("/api/chat/agent", {
|
await fetch("/api/horus-mc-send", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({ message: userMessage.content }),
|
||||||
message: userMessage.content,
|
|
||||||
locale: "es"
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await res.json();
|
// Will receive response via polling
|
||||||
|
|
||||||
const botMessage: Message = {
|
|
||||||
id: `bot_${Date.now()}`,
|
|
||||||
role: "assistant",
|
|
||||||
content: data.response || "Entendido.",
|
|
||||||
timestamp: Date.now(),
|
|
||||||
};
|
|
||||||
|
|
||||||
setMessages(prev => [...prev, botMessage]);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error("Chat error:", e);
|
console.error("Send error:", e);
|
||||||
setMessages(prev => [...prev, {
|
setMessages(prev => [...prev, {
|
||||||
id: `error_${Date.now()}`,
|
id: `error_${Date.now()}`,
|
||||||
role: "assistant",
|
role: "assistant",
|
||||||
content: "Error de conexión. Prueba en Telegram.",
|
content: "Error de conexión. Prueba en Telegram.",
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
}]);
|
}]);
|
||||||
} finally {
|
|
||||||
setIsProcessing(false);
|
setIsProcessing(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -87,8 +102,8 @@ export default function VoiceChat() {
|
|||||||
{/* Header */}
|
{/* Header */}
|
||||||
<div className="flex items-center justify-between px-4 py-3 border-b border-white/10">
|
<div className="flex items-center justify-between px-4 py-3 border-b border-white/10">
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<span className="text-lg">💬</span>
|
<span className="text-lg">👁️</span>
|
||||||
<span className="font-semibold">Horus Chat</span>
|
<span className="font-semibold">Horus</span>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
{isProcessing && (
|
{isProcessing && (
|
||||||
@@ -129,7 +144,7 @@ export default function VoiceChat() {
|
|||||||
value={input}
|
value={input}
|
||||||
onChange={(e) => setInput(e.target.value)}
|
onChange={(e) => setInput(e.target.value)}
|
||||||
onKeyPress={handleKeyPress}
|
onKeyPress={handleKeyPress}
|
||||||
placeholder="Escribe un mensaje..."
|
placeholder="Escribe a Horus..."
|
||||||
className="flex-1 bg-white/5 border border-white/10 rounded-lg px-4 py-2 text-sm text-white placeholder:text-white/40 focus:outline-none focus:border-brand-pink"
|
className="flex-1 bg-white/5 border border-white/10 rounded-lg px-4 py-2 text-sm text-white placeholder:text-white/40 focus:outline-none focus:border-brand-pink"
|
||||||
disabled={isProcessing}
|
disabled={isProcessing}
|
||||||
/>
|
/>
|
||||||
@@ -142,7 +157,7 @@ export default function VoiceChat() {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<p className="text-xs text-white/40 mt-2 text-center">
|
<p className="text-xs text-white/40 mt-2 text-center">
|
||||||
O chatea directamente en Telegram
|
Respondo en Telegram - ¡Escríbeme ahí también!
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user