ivr-tester
Version:
An automated testing framework for IVR call flows
117 lines (116 loc) • 4.93 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MediaStreamRecorder = exports.mediaStreamRecorderPlugin = void 0;
const fs = __importStar(require("fs"));
const fs_1 = require("fs");
const path = __importStar(require("path"));
const TwilioCall_1 = require("../TwilioCall");
const twilio_1 = require("../twilio");
const ivrNumberAndTestNameFilename_1 = require("./filename/ivrNumberAndTestNameFilename");
const ConfigurationError_1 = require("../../configuration/ConfigurationError");
const TwilioCaller_1 = require("../TwilioCaller");
const mediaStreamRecorderPlugin = (config) => {
var _a, _b, _c, _d, _e;
if (!((_a = config.recording) === null || _a === void 0 ? void 0 : _a.audio)) {
return {
initialise() {
/* Intentionally empty */
},
};
}
const recorderConfig = {
outputPath: (_c = (_b = config.recording) === null || _b === void 0 ? void 0 : _b.audio) === null || _c === void 0 ? void 0 : _c.outputPath,
filename: ((_e = (_d = config.recording) === null || _d === void 0 ? void 0 : _d.audio) === null || _e === void 0 ? void 0 : _e.filename) || ivrNumberAndTestNameFilename_1.ivrNumberAndTestNameFilename,
};
if (!recorderConfig.outputPath) {
throw new ConfigurationError_1.ConfigurationError("recording.audio.outputPath", "Path must be defined");
}
if (!fs.existsSync(recorderConfig.outputPath)) {
throw new ConfigurationError_1.ConfigurationError("recording.audio.outputPath", "Path does not exist");
}
return {
initialise() {
// Intentionally empty
},
testStarted(testSession) {
new MediaStreamRecorder(testSession, recorderConfig);
},
};
};
exports.mediaStreamRecorderPlugin = mediaStreamRecorderPlugin;
class MediaStreamRecorder {
constructor(testSession, config) {
this.testSession = testSession;
this.config = config;
this.processMessageRef = this.processMessage.bind(this);
this.closeRef = this.close.bind(this);
const connection = this.testSession.call.getStream();
connection
.on(TwilioCall_1.WebSocketEvents.Message, this.processMessageRef)
.on(TwilioCall_1.WebSocketEvents.Close, this.closeRef);
}
processMessage(message) {
const data = JSON.parse(message);
switch (data.event) {
case twilio_1.TwilioConnectionEvents.MediaStreamStart:
this.createFile(data);
break;
case twilio_1.TwilioConnectionEvents.Media:
this.writeToFile(Buffer.from(data.media.payload, "base64"));
break;
}
}
createFilename(event) {
const call = TwilioCaller_1.TwilioCaller.extractParameters(event);
let filename;
if (typeof this.config.filename === "string") {
filename = this.config.filename;
}
else if (typeof this.config.filename === "function") {
filename = this.config.filename({
sid: event.streamSid,
call,
}, this.testSession.scenario);
}
return `${filename}.${MediaStreamRecorder.FILE_EXT}`;
}
createFile(event) {
const filename = this.createFilename(event);
const filepath = path.join(this.config.outputPath, filename);
console.log(`Recording inbound audio to '${filepath}'`);
fs_1.mkdirSync(this.config.outputPath, { recursive: true });
this.writeStream = fs_1.createWriteStream(filepath);
}
writeToFile(data) {
this.writeStream.write(data);
}
close() {
const connection = this.testSession.call.getStream();
connection
.off(TwilioCall_1.WebSocketEvents.Message, this.processMessageRef)
.off(TwilioCall_1.WebSocketEvents.Close, this.closeRef);
this.writeStream.close();
this.writeStream = null;
}
}
exports.MediaStreamRecorder = MediaStreamRecorder;
MediaStreamRecorder.FILE_EXT = "raw";