Skip to content

Commit 370951f

Browse files
committed
twilio removed added audio recording lib
1 parent b91f482 commit 370951f

File tree

8 files changed

+73
-17
lines changed

8 files changed

+73
-17
lines changed

examples-recordings/08gg4om23xwg.wav

298 KB
Binary file not shown.

examples-recordings/0hpzpd7nr0tp.wav

Whitespace-only changes.

examples-recordings/0j22t6kq0p2k.wav

Whitespace-only changes.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"express": "^4.18.2",
2929
"fs-extra": "^11.1.1",
3030
"morgan": "^1.10.0",
31+
"node-audiorecorder": "^3.0.0",
3132
"openai": "^3.2.1",
3233
"twilio": "^4.11.2",
3334
"web3": "^1.10.0",

src/handlers/callHandler.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const base_url = "https://api.assemblyai.com/v2";
1010
const headers = { authorization: "ed92dc395436421f83dbb3660f48d237" };
1111

1212
export const answerCall = (req: Request, res: Response) => {
13+
console.log("hit");
1314
const resp = new VoiceResponse();
1415
resp.say(
1516
"Hello, this is AI the emergency helpline. Please state your name, current location, and describe your emergency situation, Cut the call when you are finished, Your request will be registered once you end the call and we will notify you as your request is registered."

src/handlers/test.xml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Response>
3+
<Say>Hello, this is AI the emergency helpline. Please state your name, current location, and describe your emergency situation, Cut the call when you are finished, Your request will be registered once you end the call and we will notify you as your request is registered.</Say>
4+
<Record maxLength="120" action="/handle-recording"/>
5+
<Say>Thank you for your message. Goodbye.</Say>
6+
</Response>

src/handlers/testHandler.ts

+63-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,68 @@
1+
// @ts-nocheck
12
// Download the helper library from https://www.twilio.com/docs/node/install
23
// Find your Account SID and Auth Token at twilio.com/console
34
// and set the environment variables. See http://twil.io/secure
45
import dotenv from "dotenv";
5-
dotenv.config();
6-
const accountSid = process.env.TWILIO_ACCOUNT_SID!;
7-
const authToken = process.env.TWILIO_AUTH_TOKEN!;
8-
9-
const client = require("twilio")(accountSid, authToken);
10-
11-
export const init = async () => {
12-
client.calls
13-
.create({
14-
url: "http://demo.twilio.com/docs/voice.xml",
15-
to: "+916230322486",
16-
from: "+918580732070",
17-
})
18-
.then((call: any) => console.log(call.sid, call))
19-
.catch((err: any) => console.log(accountSid));
6+
import { Request, Response } from "express";
7+
const fs = require("fs");
8+
const path = require("path");
9+
const AudioRecorder = require("node-audiorecorder");
10+
11+
const options = {
12+
program: `rec`, // Which program to use, either `arecord`, `rec`, or `sox`.
13+
device: null, // Recording device to use, e.g. `hw:1,0`
14+
15+
bits: 16, // Sample size. (only for `rec` and `sox`)
16+
channels: 1, // Channel count.
17+
encoding: `signed-integer`, // Encoding type. (only for `rec` and `sox`)
18+
format: `S16_LE`, // Encoding type. (only for `arecord`)
19+
rate: 16000, // Sample rate.
20+
type: `wav`, // Format type.
21+
22+
// Following options only available when using `rec` or `sox`.
23+
silence: 2, // Duration of silence in seconds before it stops recording.
24+
thresholdStart: 0.5, // Silence threshold to start recording.
25+
thresholdStop: 0.5, // Silence threshold to stop recording.
26+
keepSilence: true, // Keep the silence in the recording.
27+
};
28+
29+
const logger = console;
30+
31+
export const record = async (request: Request, response: Response) => {
32+
// Create an instance.
33+
let recorder = new AudioRecorder(options, logger);
34+
// Constants.
35+
const DIRECTORY = "examples-recordings";
36+
37+
// Create path to write recordings to.
38+
if (!fs.existsSync(DIRECTORY)) {
39+
fs.mkdirSync(DIRECTORY);
40+
}
41+
42+
// Create file path with random name.
43+
const fileName = path.join(
44+
DIRECTORY,
45+
Math.random()
46+
.toString(36)
47+
.replace(/[^0-9a-zA-Z]+/g, "")
48+
.concat(".wav")
49+
);
50+
console.log("Writing new recording file at:", fileName);
51+
// Create write stream.
52+
const fileStream = fs.createWriteStream(fileName, { encoding: "binary" });
53+
// Log information on the following events.
54+
recorder.on("error", function () {
55+
console.warn("Recording error.");
56+
});
57+
recorder.on("end", function () {
58+
console.warn("Recording ended.");
59+
});
60+
61+
// Start and write to the file.
62+
63+
recorder.start().stream().pipe(fileStream);
64+
console.log(recorder.stream());
65+
// Keep process alive.
66+
process.stdin.resume();
67+
console.warn("Press ctrl+c to exit.");
2068
};

src/routes/call.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import express from "express";
22
import { answerCall, handleRecording } from "../handlers/callHandler";
3-
import { init } from "../handlers/testHandler";
3+
import { record } from "../handlers/testHandler";
44

55
const CallRouter = express.Router();
66

77
CallRouter.post("/answer", answerCall);
88
CallRouter.post("/handle-call", handleRecording);
9-
CallRouter.get("/test", init);
109

10+
CallRouter.get("/test", record);
1111
export default CallRouter;

0 commit comments

Comments
 (0)