-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
230 lines (201 loc) · 7.67 KB
/
index.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import Fastify from "fastify";
import WebSocket from "ws";
import dotenv from "dotenv";
import fastifyFormBody from "@fastify/formbody";
import fastifyWs from "@fastify/websocket";
import twilio from "twilio";
// Load environment variables from .env file
dotenv.config();
// Retrieve the necessary environment variables
const {
OPENAI_API_KEY,
TWILIO_ACCOUNT_SID,
TWILIO_AUTH_TOKEN,
TWILIO_PHONE_NUMBER,
} = process.env;
if (
!OPENAI_API_KEY ||
!TWILIO_ACCOUNT_SID ||
!TWILIO_AUTH_TOKEN ||
!TWILIO_PHONE_NUMBER
) {
console.error(
"Missing required environment variables. Please check your .env file."
);
process.exit(1);
}
// Initialize Twilio client
const twilioClient = twilio(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN);
// Initialize Fastify
const fastify = Fastify();
fastify.register(fastifyFormBody);
fastify.register(fastifyWs);
// Constants
const SYSTEM_MESSAGE =
"You are a government solar panel program representative. Start by introducing yourself politely and then follow this conversation flow:\n\n" +
"1. First ask: 'Do you currently have solar panels installed at your home?' (Wait for yes/no)\n" +
"- If NO: Ask 'Would you be interested in learning about our government solar panel installation program?' (Wait for yes/no)\n" +
" - If YES: Ask 'Great! Is your home's roof exposed to direct sunlight for at least 6 hours daily?' (Wait for yes/no)\n" +
" - If YES: Explain available government subsidies and offer to schedule an assessment\n" +
" - If NO: Explain that we'll need to evaluate alternative solutions\n" +
" - If NO: Thank them and provide a contact number for future reference\n" +
"- If YES: Ask 'Have you registered your solar installation with our government database?' (Wait for yes/no)\n" +
" - If NO: Guide them about registration benefits and process\n" +
" - If YES: Ask about their experience and if they need maintenance support\n\n" +
"Keep responses brief and professional. Always wait for clear yes/no answers before proceeding. If the answer is unclear, politely ask for clarification with a yes or no.";
const VOICE = "alloy";
const PORT = process.env.PORT || 5050; // Allow dynamic port assignment
// List of Event Types to log to the console. See OpenAI Realtime API Documentation. (session.updated is handled separately.)
const LOG_EVENT_TYPES = [
"response.content.done",
"rate_limits.updated",
"response.done",
"input_audio_buffer.committed",
"input_audio_buffer.speech_stopped",
"input_audio_buffer.speech_started",
"session.created",
];
// Root Route
fastify.get("/", async (request, reply) => {
reply.send({ message: "Twilio Media Stream Server is running!" });
});
// Route for Twilio to handle incoming and outgoing calls
fastify.all("/incoming-call", async (request, reply) => {
const twimlResponse = `<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>Please wait while we connect your call to the A. I. voice assistant, powered by Twilio and the Open-A.I. Realtime API</Say>
<Pause length="1"/>
<Say>O.K. you can start talking!</Say>
<Connect>
<Stream url="wss://${request.headers.host}/media-stream" />
</Connect>
</Response>`;
reply.type("text/xml").send(twimlResponse);
});
// New route to initiate an outgoing call
fastify.get("/make-call", async (request, reply) => {
try {
const call = await twilioClient.calls.create({
url: `https://${request.headers.host}/incoming-call`,
to: "+919896178450", // The number you want to call
from: TWILIO_PHONE_NUMBER,
});
console.log("Call initiated:", call.sid);
reply.send({ message: "Call initiated", callSid: call.sid });
} catch (error) {
console.error("Error initiating call:", error);
reply.status(500).send({ error: "Failed to initiate call" });
}
});
// WebSocket route for media-stream
fastify.register(async (fastify) => {
fastify.get("/media-stream", { websocket: true }, (connection, req) => {
console.log("Client connected");
const openAiWs = new WebSocket(
"wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01",
{
headers: {
Authorization: `Bearer ${OPENAI_API_KEY}`,
"OpenAI-Beta": "realtime=v1",
},
}
);
let streamSid = null;
const sendSessionUpdate = () => {
const sessionUpdate = {
type: "session.update",
session: {
turn_detection: { type: "server_vad" },
input_audio_format: "g711_ulaw",
output_audio_format: "g711_ulaw",
voice: VOICE,
instructions: SYSTEM_MESSAGE,
modalities: ["text", "audio"],
temperature: 0.8,
},
};
console.log("Sending session update:", JSON.stringify(sessionUpdate));
openAiWs.send(JSON.stringify(sessionUpdate));
};
// Open event for OpenAI WebSocket
openAiWs.on("open", () => {
console.log("Connected to the OpenAI Realtime API");
setTimeout(sendSessionUpdate, 250); // Ensure connection stability, send after .25 seconds
});
// Listen for messages from the OpenAI WebSocket (and send to Twilio if necessary)
openAiWs.on("message", (data) => {
try {
const response = JSON.parse(data);
if (LOG_EVENT_TYPES.includes(response.type)) {
console.log(`Received event: ${response.type}`, response);
}
if (response.type === "session.updated") {
console.log("Session updated successfully:", response);
}
if (response.type === "response.audio.delta" && response.delta) {
const audioDelta = {
event: "media",
streamSid: streamSid,
media: {
payload: Buffer.from(response.delta, "base64").toString("base64"),
},
};
connection.send(JSON.stringify(audioDelta));
}
} catch (error) {
console.error(
"Error processing OpenAI message:",
error,
"Raw message:",
data
);
}
});
// Handle incoming messages from Twilio
connection.on("message", (message) => {
try {
const data = JSON.parse(message);
switch (data.event) {
case "media":
if (openAiWs.readyState === WebSocket.OPEN) {
const audioAppend = {
type: "input_audio_buffer.append",
audio: data.media.payload,
};
openAiWs.send(JSON.stringify(audioAppend));
}
break;
case "start":
streamSid = data.start.streamSid;
console.log("Incoming stream has started", streamSid);
break;
default:
console.log("Received non-media event:", data.event);
break;
}
} catch (error) {
console.error("Error parsing message:", error, "Message:", message);
}
});
// Handle connection close
connection.on("close", () => {
if (openAiWs.readyState === WebSocket.OPEN) openAiWs.close();
console.log("Client disconnected.");
});
// Handle WebSocket close and errors
openAiWs.on("close", () => {
console.log("Disconnected from the OpenAI Realtime API");
});
openAiWs.on("error", (error) => {
console.error("Error in the OpenAI WebSocket:", error);
});
});
});
fastify.listen({ port: PORT }, (err) => {
if (err) {
console.error(err);
process.exit(1);
}
console.log(`Server is listening on port ${PORT}`);
console.log(`To initiate a call, visit: http://localhost:${PORT}/make-call`);
});