-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathserver.js
77 lines (67 loc) · 1.91 KB
/
server.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
require("dotenv").config();
const express = require("express");
const ExpressWs = require("express-ws");
const { TextToSpeechService } = require("./tts-service");
const { TranscriptionService } = require("./transcription-service");
const app = express();
ExpressWs(app);
const PORT = 3000;
app.post("/incoming", (req, res) => {
res.status(200);
res.type("text/xml");
res.end(`
<Response>
<Connect>
<Stream url="wss://${process.env.SERVER}/connection" />
</Connect>
</Response>
`);
});
app.ws("/connection", (ws, req) => {
ws.on("error", console.error);
// Filled in from start message
let streamSid;
const transcriptionService = new TranscriptionService();
const ttsService = new TextToSpeechService({});
// Incoming from MediaStream
ws.on("message", function message(data) {
const msg = JSON.parse(data);
if (msg.event === "start") {
streamSid = msg.start.streamSid;
console.log(`Starting Media Stream for ${streamSid}`);
} else if (msg.event === "media") {
transcriptionService.send(msg.media.payload);
} else if (msg.event === "mark") {
const label = msg.mark.name;
console.log(`Media completed mark (${msg.sequenceNumber}): ${label}`)
}
});
transcriptionService.on("transcription", (text) => {
console.log(`Received transcription: ${text}`);
ttsService.generate(text);
});
ttsService.on("speech", (audio, label) => {
console.log(`Sending audio to Twilio ${audio.length} b64 characters`);
ws.send(
JSON.stringify({
streamSid,
event: "media",
media: {
payload: audio,
},
})
);
// When the media completes you will receive a `mark` message with the label
ws.send(
JSON.stringify({
streamSid,
event: "mark",
mark: {
name: label
}
})
)
});
});
app.listen(PORT);
console.log(`Server running on port ${PORT}`);