-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
158 lines (130 loc) · 3.85 KB
/
Copy pathserver.js
File metadata and controls
158 lines (130 loc) · 3.85 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import express from "express";
import path from "path";
import { fileURLToPath } from "url";
import dotenv from "dotenv";
import axios from "axios";
import twilio from "twilio";
import routes from "./routes/index.js";
import bodyParser from "body-parser";
import session from "express-session";
const app = express();
app.use(session({
secret: "your-secret-key",
resave: false,
saveUninitialized: true
}));
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
dotenv.config();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));
const MessagingResponse = twilio.twiml.MessagingResponse;
const port = 3000;
app.use("/", routes);
app.get("/", (req, res) => {
res.render('index', { user:req.session.user })
});
app.get("/chat", (req, res) => {
res.render('index', { user:req.session.user })
});
app.post("/chat", async (req, res) => {
const userMessage = req.body.message;
try {
const response = await axios.post(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${process.env.GEMINI_API_KEY}`,
{
contents: [
{
parts: [
{
text: userMessage
}
]
}
]
},
{
headers: {
"Content-Type": "application/json"
}
}
);
const botReply = response.data.candidates[0].content.parts[0].text;
res.json({ reply: botReply });
} catch (err) {
console.error("Gemini API Error:", err.response?.data || err.message);
res.status(500).send("Gemini API Error");
}
});
// Twilio voice endpoint
app.post('/voice', (req, res) => {
const twiml = `
<Response>
<Gather input="speech" action="/process-voice" method="POST">
<Say voice="alice">How can I help you? This is your AI chatbot.</Say>
</Gather>
<Say voice="alice">Thank you!</Say>
</Response>
`;
res.type('text/xml');
res.send(twiml);
});
// Process voice input
app.post('/process-voice', async (req, res) => {
const speechResult = req.body.SpeechResult;
console.log('User said:', speechResult);
let botReply = "Kuch galti ho gayi, try again!";
try {
const response = await axios.post(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${process.env.GEMINI_API_KEY}`,
{
contents: [
{
parts: [{ text: speechResult }]
}
]
},
{ headers: { "Content-Type": "application/json" } }
);
botReply = response.data.candidates[0].content.parts[0].text;
} catch (err) {
console.error("Gemini API Error:", err.response?.data || err.message);
}
const twiml = `
<Response>
<Say voice="alice">${botReply}</Say>
</Response>
`;
res.type('text/xml');
res.send(twiml);
});
// WhatsApp webhook
app.post("/whatsapp-webhook", async (req, res) => {
const twiml = new MessagingResponse();
const incomingMsg = req.body.Body;
try {
const response = await axios.post(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${process.env.GEMINI_API_KEY}`,
{
contents: [
{
parts: [{ text: incomingMsg }]
}
]
},
{ headers: { "Content-Type": "application/json" } }
);
const botReply = response.data.candidates[0].content.parts[0].text;
twiml.message(botReply);
} catch (err) {
console.error("Gemini API Error:", err.response?.data || err.message);
twiml.message("Sorry, AI server error.");
}
res.writeHead(200, { "Content-Type": "text/xml" });
res.end(twiml.toString());
});
app.listen(port, () => {
console.log(`✅ Server running on http://localhost:${port}`);
});