-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (57 loc) · 2.14 KB
/
Copy pathserver.js
File metadata and controls
70 lines (57 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require("express");
const bodyParser = require("body-parser");
const fetch = require("node-fetch");
const fs = require("fs");
const twilio = require("twilio");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 3000;
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
console.log("🔑 OpenAI Key:", OPENAI_API_KEY ? "Loaded ✅" : "Missing ❌");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.post("/message", async (req, res) => {
const incomingMsg = req.body.Body;
const sender = req.body.From;
console.log(`📩 Received: ${incomingMsg} from ${sender}`);
// Log to file
const logLine = `${new Date().toISOString()} - ${sender}: ${incomingMsg}\n`;
fs.appendFileSync("messages.log", logLine);
let aiReply = "Sorry, something went wrong.";
// Prepare AI request with timeout
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 30000); // 30 sec
try {
const aiResponse = await fetch("https://openrouter.ai/api/v1/chat/completions", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${OPENAI_API_KEY}`,
"HTTP-Referer": "https://openrouter.ai", // Required by OpenRouter
"X-Title": "whatsapp-bot"
},
body: JSON.stringify({
model: "openai/gpt-3.5-turbo",
messages: [
{ role: "system", content: "You are a helpful assistant for WhatsApp support." },
{ role: "user", content: incomingMsg }
]
}),
signal: controller.signal
});
clearTimeout(timeout);
const data = await aiResponse.json();
console.log("🤖 OpenRouter raw:", data);
aiReply = data.choices?.[0]?.message?.content?.trim() || aiReply;
} catch (error) {
clearTimeout(timeout);
console.error("❌ AI fetch error:", error);
}
// Send AI response to WhatsApp
const twiml = new twilio.twiml.MessagingResponse();
twiml.message(aiReply);
res.type("text/xml").send(twiml.toString());
});
app.listen(port, () => {
console.log(`✅ Server running at http://localhost:${port}`);
});