plivo
Version:
A Node.js SDK to make voice calls and send SMS using Plivo and to generate Plivo XML
1,296 lines (1,102 loc) • 53.6 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.QueuedCallResource = exports.LiveCallResource = exports.CallInterface = exports.Call = exports.StartStreamResponse = exports.RecordCallResponse = exports.SendDigitsResponse = exports.StartSpeakingTextResponse = exports.StartPlayingMusicResponse = exports.ListAllCallsResponse = exports.RetrieveCallResponse = exports.GetLiveCallResponse = exports.GetQueuedCallResponse = exports.CreateCallResponse = exports.ListAllLiveCallResponse = exports.ListAllQueuedCalls = exports.CallTransferResponse = undefined;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _lodash = require('lodash');
var _ = _interopRequireWildcard(_lodash);
var _base = require('../base');
var _common = require('../utils/common.js');
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var clientKey = Symbol();
var action = 'Call/';
var idField = 'callUuid';
var CallTransferResponse = exports.CallTransferResponse = function CallTransferResponse(params) {
_classCallCheck(this, CallTransferResponse);
params = params || {};
this.apiId = params.apiId;
this.callUuids = params.callUuids;
this.message = params.message;
};
var ListAllQueuedCalls = exports.ListAllQueuedCalls = function ListAllQueuedCalls(params) {
_classCallCheck(this, ListAllQueuedCalls);
params = params || {};
this.apiId = params.id;
this.calls = params.calls;
};
var ListAllLiveCallResponse = exports.ListAllLiveCallResponse = function ListAllLiveCallResponse(params) {
_classCallCheck(this, ListAllLiveCallResponse);
params = params || {};
this.apiId = params.id;
this.callUuid = params.callUuid;
};
var CreateCallResponse = exports.CreateCallResponse = function CreateCallResponse(params) {
_classCallCheck(this, CreateCallResponse);
params = params || {};
this.apiId = params.apiId;
this.message = params.message;
this.requestUuid = params.requestUuid;
};
var GetQueuedCallResponse = exports.GetQueuedCallResponse = function GetQueuedCallResponse(params) {
_classCallCheck(this, GetQueuedCallResponse);
params = params || {};
this.apiId = params.apiId;
this.direction = params.direction;
this.from = params.from;
this.callStatus = params.callStatus;
this.to = params.to;
this.callerName = params.callerName;
this.callUuid = params.callUuid;
this.requestUuid = params.requestUuid;
};
var GetLiveCallResponse = exports.GetLiveCallResponse = function GetLiveCallResponse(params) {
_classCallCheck(this, GetLiveCallResponse);
params = params || {};
this.apiId = params.apiId;
this.callStatus = params.callStatus;
this.callUuid = params.callUuid;
this.callerName = params.callerName;
this.direction = params.direction;
this.from = params.from;
this.requestUuid = params.requestUuid;
this.sessionStart = params.sessionStart;
this.to = params.to;
this.stirVerification = params.stirVerification;
this.stirAttestation = params.stirAttestation;
};
var RetrieveCallResponse = exports.RetrieveCallResponse = function RetrieveCallResponse(params) {
_classCallCheck(this, RetrieveCallResponse);
params = params || {};
this.apiId = params.apiId;
this.answerTime = params.answerTime;
this.billDuration = params.billDuration;
this.billedDuration = params.billedDuration;
this.callDirection = params.callDirection;
this.callDuration = params.callDuration;
this.callState = params.callState;
this.callUuid = params.callUuid;
this.conferenceUuid = params.conferenceUuid;
this.endTime = params.endTime;
this.fromNumber = params.fromNumber;
this.hangupCauseCode = params.hangupCauseCode;
this.hangupCauseName = params.hangupCauseName;
this.hangupSource = params.hangupSource;
this.initiationTime = params.initiationTime;
this.parentCallUuid = params.parentCallUuid;
this.resourceUri = params.resourceUri;
this.toNumber = params.toNumber;
this.totalAmount = params.totalAmount;
this.totalRate = params.totalRate;
this.stirVerification = params.stirVerification;
this.stirAttestation = params.stirAttestation;
this.sourceIp = params.sourceIp;
this.cnamLookup = params.cnamLookup;
};
var ListAllCallsResponse = exports.ListAllCallsResponse = function ListAllCallsResponse(params) {
_classCallCheck(this, ListAllCallsResponse);
params = params || {};
this.answerTime = params.answerTime;
this.billDuration = params.billDuration;
this.billedDuration = params.billedDuration;
this.callDirection = params.callDirection;
this.callDuration = params.callDuration;
this.callState = params.callState;
this.callUuid = params.callUuid;
this.conferenceUuid = params.conferenceUuid;
this.endTime = params.endTime;
this.fromNumber = params.fromNumber;
this.hangupCauseCode = params.hangupCauseCode;
this.hangupCauseName = params.hangupCauseName;
this.hangupSource = params.hangupSource;
this.initiationTime = params.initiationTime;
this.parentCallUuid = params.parentCallUuid;
this.resourceUri = params.resourceUri;
this.toNumber = params.toNumber;
this.totalAmount = params.totalAmount;
this.totalRate = params.totalRate;
this.stirVerification = params.stirVerification;
this.stirAttestation = params.stirAttestation;
this.sourceIp = params.sourceIp;
this.cnamLookup = params.cnamLookup;
};
var StartPlayingMusicResponse = exports.StartPlayingMusicResponse = function StartPlayingMusicResponse(params) {
_classCallCheck(this, StartPlayingMusicResponse);
params = params || {};
this.apiId = params.apiId;
this.message = params.message;
};
var StartSpeakingTextResponse = exports.StartSpeakingTextResponse = function StartSpeakingTextResponse(params) {
_classCallCheck(this, StartSpeakingTextResponse);
params = params || {};
this.apiId = params.apiId;
this.message = params.message;
};
var SendDigitsResponse = exports.SendDigitsResponse = function SendDigitsResponse(params) {
_classCallCheck(this, SendDigitsResponse);
params = params || {};
this.apiId = params.apiId;
this.message = params.message;
};
var RecordCallResponse = exports.RecordCallResponse = function RecordCallResponse(params) {
_classCallCheck(this, RecordCallResponse);
params = params || {};
this.apiId = params.apiId;
this.message = params.message;
this.recordingId = params.recordingId;
this.url = params.url;
};
var StartStreamResponse = exports.StartStreamResponse = function StartStreamResponse(params) {
_classCallCheck(this, StartStreamResponse);
params = params || {};
this.apiId = params.apiId;
this.message = params.message;
this.streamId = params.streamId;
};
/**
* Represents a Call
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of call
*/
var Call = exports.Call = function (_PlivoResource) {
_inherits(Call, _PlivoResource);
function Call(client) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, Call);
var _this = _possibleConstructorReturn(this, (Call.__proto__ || Object.getPrototypeOf(Call)).call(this, action, Call, idField, client));
if (idField in data) {
_this.id = data[idField];
}
(0, _common.extend)(_this, data);
_this[clientKey] = client;
return _this;
}
/**
* hangup call
* @method
* @promise {Boolean} return true if call hung up
* @fail {Error} return Error
*/
_createClass(Call, [{
key: 'hangup',
value: function hangup() {
var params = {};
params.isVoiceRequest = 'true';
return _get(Call.prototype.__proto__ || Object.getPrototypeOf(Call.prototype), 'delete', this).call(this, params);
}
/**
* transfer call
* @method
* @param {object} params - optional params to transfer a call
* @param {string} [params.legs] aleg, bleg or both Defaults to aleg. aleg will transfer call_uuid ; bleg will transfer the bridged leg (if found) of call_uuid ; both will transfer call_uuid and bridged leg of call_uuid
* @param {string} [params.alegUrl] URL to transfer for aleg, if legs is aleg or both, then aleg_url has to be specified.
* @param {string} [params.alegMethod] HTTP method to invoke aleg_url. Defaults to POST.
* @param {string} [params.blegUrl] URL to transfer for bridged leg, if legs is bleg or both, then bleg_url has to be specified.
* @param {string} [params.blegMethod] HTTP method to invoke bleg_url. Defaults to POST.
* @promise {object} return call object
* @fail {Error} return Error
*/
}, {
key: 'transfer',
value: function transfer(params, callUUID) {
params.isVoiceRequest = 'true';
var client = this[clientKey];
var that = this;
callUUID = typeof callUUID !== 'undefined' ? callUUID : that.callUUID;
return new Promise(function (resolve, reject) {
client('POST', action + callUUID + '/', params).then(function (response) {
(0, _common.extend)(that, response.body);
if (params.hasOwnProperty('isVoiceRequest')) {
delete params.isVoiceRequest;
}
(0, _common.extend)(that, params);
resolve(new CallTransferResponse(that));
}).catch(function (error) {
reject(error);
});
});
}
/**
* start audio stream over a call
* @method
* @param {object} params - to start stream
* @promise {object} return PlivoGenericResponse Object
* @fail {Error} return Error
*/
}, {
key: 'stream',
value: function stream() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
params.isVoiceRequest = 'true';
return this.startStreaming(params);
}
/**
* start audio stream over a call
* @method
* @param {object} params - to start stream
* @promise {object} return PlivoGenericResponse Object
* @fail {Error} return Error
*/
}, {
key: 'startStreaming',
value: function startStreaming(params) {
var _this2 = this;
var client = this[clientKey];
return new Promise(function (resolve, reject) {
client('POST', action + _this2.id + '/Stream/', params).then(function (response) {
resolve(new StartStreamResponse(response.body, idField));
}).catch(function (error) {
reject(error);
});
});
}
}, {
key: 'stopStream',
value: function stopStream() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
params.isVoiceRequest = 'true';
return _get(Call.prototype.__proto__ || Object.getPrototypeOf(Call.prototype), 'executeAction', this).call(this, this.id + '/Stream/' + this.secondaryId, 'DELETE', params);
}
}, {
key: 'stopAllStream',
value: function stopAllStream() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
params.isVoiceRequest = 'true';
return _get(Call.prototype.__proto__ || Object.getPrototypeOf(Call.prototype), 'executeAction', this).call(this, this.id + '/Stream/', 'DELETE', params);
}
}, {
key: 'getStream',
value: function getStream() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
params.isVoiceRequest = 'true';
return _get(Call.prototype.__proto__ || Object.getPrototypeOf(Call.prototype), 'executeAction', this).call(this, this.id + '/Stream/' + this.secondaryId, 'GET', params);
}
}, {
key: 'getAllStream',
value: function getAllStream() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
params.isVoiceRequest = 'true';
return _get(Call.prototype.__proto__ || Object.getPrototypeOf(Call.prototype), 'executeAction', this).call(this, this.id + '/Stream/', 'GET', params);
}
/**
* record call
* @method
* @param {object} params - to record call
* @promise {object} return PlivoGenericResponse Object
* @fail {Error} return Error
*/
}, {
key: 'record',
value: function record() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
params.isVoiceRequest = 'true';
return this.startRecording(params);
}
/**
* record call
* @method
* @param {object} params - to record call
* @promise {object} return PlivoGenericResponse Object
* @fail {Error} return Error
*/
}, {
key: 'startRecording',
value: function startRecording(params) {
var _this3 = this;
var client = this[clientKey];
return new Promise(function (resolve, reject) {
client('POST', action + _this3.id + '/Record/', params).then(function (response) {
resolve(new RecordCallResponse(response.body, idField));
}).catch(function (error) {
reject(error);
});
});
}
/**
* stop recording call
* @method
* @param {object} params - to stop recording call
* @promise {object} return PlivoGenericResponse Object
* @fail {Error} return Error
*/
}, {
key: 'stopRecording',
value: function stopRecording() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
params.isVoiceRequest = 'true';
return _get(Call.prototype.__proto__ || Object.getPrototypeOf(Call.prototype), 'executeAction', this).call(this, this.id + '/Record/', 'DELETE', params);
}
/**
* play music for call
* @method
* @param {string} url - url which contains audio to play for call
* @param {object} optionalParams - to stop recording call
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'playMusic',
value: function playMusic(url, optionalParams) {
return this.startPlayingMusic(url, optionalParams);
}
/**
* play music for call
* @method
* @param {string} url - url which contains audio to play for call
* @param {object} optionalParams - to stop recording call
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'startPlayingMusic',
value: function startPlayingMusic(urls, optionalParams) {
var _this4 = this;
var params = optionalParams || {};
params.urls = urls;
params.isVoiceRequest = 'true';
var errors = (0, _common.validate)([{
field: 'urls',
value: urls,
validators: ['isRequired', 'isString']
}]);
if (errors) {
return errors;
}
var client = this[clientKey];
return new Promise(function (resolve, reject) {
client('POST', action + _this4.id + '/Play/', params).then(function (response) {
resolve(new StartPlayingMusicResponse(response.body, idField));
}).catch(function (error) {
reject(error);
});
});
}
/**
* stop playing music for call
* @method
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'stopPlayingMusic',
value: function stopPlayingMusic() {
var params = {};
params.isVoiceRequest = 'true';
return _get(Call.prototype.__proto__ || Object.getPrototypeOf(Call.prototype), 'executeAction', this).call(this, this.id + '/Play/', 'DELETE', params);
}
/**
* speak text for call
* @method
* @param {string} text - text to speak for call
* @param {object} optionalParams - to speak for call
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'speakText',
value: function speakText(text, optionalParams) {
return this.startSpeakingText(text, optionalParams);
}
/**
* speak text for call
* @method
* @param {string} text - text to speak for call
* @param {object} optionalParams - to speak for call
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'startSpeakingText',
value: function startSpeakingText(text, optionalParams) {
var _this5 = this;
var errors = (0, _common.validate)([{
field: 'text',
value: text,
validators: ['isRequired', 'isString']
}]);
if (errors) {
return errors;
}
var params = optionalParams || {};
params.text = text;
params.isVoiceRequest = 'true';
var client = this[clientKey];
return new Promise(function (resolve, reject) {
client('POST', action + _this5.id + '/Speak/', params).then(function (response) {
resolve(new StartSpeakingTextResponse(response.body, idField));
}).catch(function (error) {
reject(error);
});
});
}
/**
* stop speaking text for call
* @method
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'stopSpeakingText',
value: function stopSpeakingText() {
var params = {};
params.isVoiceRequest = 'true';
return _get(Call.prototype.__proto__ || Object.getPrototypeOf(Call.prototype), 'executeAction', this).call(this, this.id + '/Speak/', 'DELETE', params);
}
/**
* Send digits on a call
* @method
* @param {string} digits - digits to be send
* @param {object} optionalParams - to send digits for call
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'sendDigits',
value: function sendDigits(digits, optionalParams) {
var _this6 = this;
var errors = (0, _common.validate)([{
field: 'digits',
value: digits,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
var params = optionalParams || {};
params.digits = digits;
params.isVoiceRequest = 'true';
var client = this[clientKey];
return new Promise(function (resolve, reject) {
client('POST', action + _this6.id + '/DTMF/', params).then(function (response) {
resolve(new SendDigitsResponse(response.body, idField));
}).catch(function (error) {
reject(error);
});
});
}
/**
* Hangup a Call Request
* @method
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'cancel',
value: function cancel() {
var params = {};
params.isVoiceRequest = 'true';
return _get(Call.prototype.__proto__ || Object.getPrototypeOf(Call.prototype), 'executeAction', this).call(this, 'Request/' + this.id + '/', 'DELETE', params, '');
}
}]);
return Call;
}(_base.PlivoResource);
var liveCallInterfaceKey = Symbol('liveCallInterface');
var queuedCallInterfaceKey = Symbol('queuedCallInterface');
/**
* Represents a Call Interface
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of call
*/
var CallInterface = exports.CallInterface = function (_PlivoResourceInterfa) {
_inherits(CallInterface, _PlivoResourceInterfa);
function CallInterface(client) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, CallInterface);
var _this7 = _possibleConstructorReturn(this, (CallInterface.__proto__ || Object.getPrototypeOf(CallInterface)).call(this, action, Call, idField, client));
(0, _common.extend)(_this7, data);
_this7[clientKey] = client;
_this7[liveCallInterfaceKey] = new LiveCallInterface(client);
_this7[queuedCallInterfaceKey] = new QueuedCallInterface(client);
return _this7;
}
/**
* Get A Call Detail
* @method
* @param {string} id - call uuid to get information of.
* @promise {object} returns Call Object
* @fail {Error} returns Error
*/
_createClass(CallInterface, [{
key: 'get',
value: function get(id) {
var _this8 = this;
var errors = (0, _common.validate)([{
field: 'id',
value: id,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
var params = {};
params.isVoiceRequest = 'true';
var client = this[clientKey];
return new Promise(function (resolve, reject) {
if (action !== '' && !id) {
reject(new Error(_this8[idKey] + ' must be set'));
}
client('GET', action + (id ? id + '/' : ''), params).then(function (response) {
resolve(new RetrieveCallResponse(response.body, client));
}).catch(function (error) {
reject(error);
});
});
}
/**
* Get All Call Detail
* @method
* @param {object} params - params to get all call details.
* @promise {object[]} returns list of Call Object
* @fail {Error} returns Error
*/
}, {
key: 'list',
value: function list() {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
params.isVoiceRequest = 'true';
var client = this[clientKey];
return new Promise(function (resolve, reject) {
client('GET', action, params).then(function (response) {
var objects = [];
Object.defineProperty(objects, 'meta', {
value: response.body.meta,
enumerable: true
});
response.body.objects.forEach(function (item) {
objects.push(new ListAllCallsResponse(item, client));
});
resolve(objects);
}).catch(function (error) {
reject(error);
});
});
}
/**
* Create a call
* @method
* @param {string} from - The phone number to be used as the caller id (with the country code).For e.g, a USA caller id number could be, 15677654321, with '1' for the country code.
* @param {string} to - The regular number(s) or sip endpoint(s) to call. Regular number must be prefixed with country code but without the + sign). For e.g, to dial a number in the USA, the number could be, 15677654321, with '1' for the country code. Multiple numbers can be sent by using a delimiter. For e.g. 15677654321<12077657621<12047657621. Sip endpoints must be prefixed with sip: E.g., sip:john1234@phone.plivo.com. To make bulk calls, the delimiter < is used. For example, 15677654321<15673464321<sip:john1234@phone.plivo.com Yes, you can mix regular numbers and sip endpoints.
* @param {string} answerUrl - The URL invoked by Plivo when the outbound call is answered.
* @param {object} params - optional params to make a call
* @param {string} [params.answerUrl] The URL invoked by Plivo when a call executes this application.
* @param {string} [params.answerMethod] The method used to call the answer_url. Defaults to POST.
* @param {string} [params.hangupUrl] The URL that is notified by Plivo when the call hangs up.
* @param {string} [params.hangupMethod] The method used to call the hangup_url. Defaults to POST
* @param {string} [params.fallbackAnswerUrl] Invoked by Plivo only if answer_url is unavailable or the XML response is invalid. Should contain a XML response.
* @param {string} [params.fallbackMethod] The method used to call the fallback_answer_url. Defaults to POST.
* @param {string} [params.callerName] Caller name to use with the call.
* @param {string} [params.sendDigits] Each 'w' character waits 0.5 second before sending a digit. Each 'W' character waits 1 second before sending a digit. You can also add the tone duration in ms by appending @duration after the string (default duration is 2000 ms). For example, 1w2w3@1000
* @param {boolean} [params.sendOnPreanswer] If set to true and send_digits is also set, digits are sent when the call is in preanswer state. Defaults to false.
* @param {number} [params.timeLimit] Schedules the call for hangup at a specified time after the call is answered. Value should be an integer > 0(in seconds).
* @param {number} [params.hangupOnRing] Schedules the call for hangup at a specified time after the call starts ringing. Value should be an integer >= 0 (in seconds).
* @param {string} [params.machineDetection] Used to detect if the call has been answered by a machine. The valid values are true and hangup.
* @param {number} [params.machineDetectionTime] Time allotted to analyze if the call has been answered by a machine. It should be an integer >= 2000 and <= 10000 and the unit is ms. The default value is 5000 ms.
* @param {string} [params.machineDetectionUrl] A URL where machine detection parameters will be sent by Plivo. This parameter should be used to make machine detection asynchronous
* @param {string} [params.machineDetectionMethod] The HTTP method which will be used by Plivo to request the machine_detection_url. Defaults to POST.
* @param {string} [params.sipHeaders] List of SIP headers in the form of 'key=value' pairs, separated by commas.
* @param {number} [params.ringTimeout] Determines the time in seconds the call should ring. If the call is not answered within the ring_timeout value or the default value of 120s, it is canceled.
* @param {string} [params.parentCallUuid] The call_uuid of the first leg in an ongoing conference call. It is recommended to use this parameter in scenarios where a member who is already present in the conference intends to add new members by initiating outbound API calls.
* @param {boolean} [params.errorIfParentNotFound] if set to true and the parent_call_uuid cannot be found, the API request would return an error. If set to false, the outbound call API request will be executed even if the parent_call_uuid is not found. Defaults to false.
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'create',
value: function create(from, to, answerUrl) {
var params = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
var errors = (0, _common.validate)([{
field: 'from',
value: from,
validators: ['isRequired']
}, {
field: 'to',
value: to,
validators: ['isRequired']
}, {
field: 'answer_url',
value: answerUrl,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
params.from = from;
params.to = _.isArray(to) ? _.join(to, '<') : to;
params.answer_url = answerUrl;
params.isVoiceRequest = 'true';
var client = this[clientKey];
return new Promise(function (resolve, reject) {
client('POST', action, params).then(function (response) {
resolve(new CreateCallResponse(response.body, idField));
}).catch(function (error) {
reject(error);
});
});
}
/**
* Hangup A Specific Call
* @method
* @param {string} callUUID - call uuid to hangup call
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'hangup',
value: function hangup(callUUID) {
var errors = (0, _common.validate)([{
field: 'call_uuid',
value: callUUID,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: callUUID
}).hangup();
}
/**
* Transfer a Call
* @method
* @param {string} callUUID - call uuid to transfer call
* @param {object} params - optional params to transfer a call
* @param {string} [params.legs] aleg, bleg or both Defaults to aleg. aleg will transfer call_uuid ; bleg will transfer the bridged leg (if found) of call_uuid ; both will transfer call_uuid and bridged leg of call_uuid
* @param {string} [params.alegUrl] URL to transfer for aleg, if legs is aleg or both, then aleg_url has to be specified.
* @param {string} [params.alegMethod] HTTP method to invoke aleg_url. Defaults to POST.
* @param {string} [params.blegUrl] URL to transfer for bridged leg, if legs is bleg or both, then bleg_url has to be specified.
* @param {string} [params.blegMethod] HTTP method to invoke bleg_url. Defaults to POST.
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'transfer',
value: function transfer(callUUID, params) {
var errors = (0, _common.validate)([{
field: 'call_uuid',
value: callUUID,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: callUUID
}).transfer(params, callUUID);
}
/**
* Start a Stream over a Call
* @method
* @param {string} serviceUrl - Wss url over which data packets will be send.
* @param {string} callUuid - For this callUuid audio streaming will start.
* @param {object} params - optional params to start a stream
* @param {string} [params.bidirectional] Specifies if the audio being streamed over web-sockets is oneway (read only for the wss service) only or bidirectional (the wss service can read as well as write audio back).
* @param {string} [params.audioTrack] The audio track (inbound or outbound) of the underlying call which Plivo will fork and stream to the wss service. Inbound [default], outbound, both. Note: only inbound is allowed if bidirectional is true.
* @param {string} [params.streamTimeout] Maximum duration, in seconds, for which audio will be streamed once streaming starts. At the end of the specified duration, streaming will stop. This will have no impact on the rest of the call flow. Defaults to 86400 (24 hrs).
* @param {string} [params.statusCallbackUrl] URL that is notified by Plivo when stream is connected, stopped, failed to connect or disconnected. Note: not called when the call gets disconnected.
* @param {string} [params.statusCallbackMethod] POST[default], GET.
* @param {string} [params.contentType] Preferred audio codec and sampling rate. Allowed, audio/x-l16;rate=8000 [default], audio/x-l16;rate=16000 and audio/x-mulaw;rate=8000.
* @param {string} [params.extraHeaders] These are key value pairs which will be passed to the wss service along with your stream. Total length of the string being passed should be less than equal to 512 bytes.
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'stream',
value: function stream(callUUID, serviceUrl) {
var optionalParams = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var errors = (0, _common.validate)([{
field: 'serviceUrl',
value: serviceUrl,
validators: ['isRequired']
}, {
field: 'callUUID',
value: callUUID,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
optionalParams.serviceUrl = serviceUrl;
return new Call(this[clientKey], {
id: callUUID
}).stream(optionalParams);
}
}, {
key: 'stopStream',
value: function stopStream(callUUID, streamId) {
var errors = (0, _common.validate)([{
field: 'callUUID',
value: callUUID,
validators: ['isRequired']
}, {
field: 'streamId',
value: streamId,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: callUUID,
secondaryId: streamId
}).stopStream();
}
}, {
key: 'stopAllStream',
value: function stopAllStream(callUUID) {
var errors = (0, _common.validate)([{
field: 'callUUID',
value: callUUID,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: callUUID
}).stopAllStream();
}
}, {
key: 'getStream',
value: function getStream(callUUID, streamId) {
var errors = (0, _common.validate)([{
field: 'callUUID',
value: callUUID,
validators: ['isRequired']
}, {
field: 'streamId',
value: streamId,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: callUUID,
secondaryId: streamId
}).getStream();
}
}, {
key: 'getAllStream',
value: function getAllStream(callUUID) {
var errors = (0, _common.validate)([{
field: 'callUUID',
value: callUUID,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: callUUID
}).getAllStream();
}
/**
* Record a Call
* @method
* @param {string} callUUID - call uuid to record call
* @param {object} optionalParams - optional params to record a call
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'record',
value: function record(callUUID) {
var optionalParams = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var errors = (0, _common.validate)([{
field: 'call_uuid',
value: callUUID,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: callUUID
}).record(optionalParams);
}
/**
* Stop Recording a Call
* @method
* @param {string} callUUID - call uuid to stop recording a call
* @param {object} optionalParams - optional params to stop recording a call
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'stopRecording',
value: function stopRecording(callUUID) {
var optionalParams = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var errors = (0, _common.validate)([{
field: 'call_uuid',
value: callUUID,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: callUUID
}).stopRecording(optionalParams);
}
/**
* Play a music file
* @method
* @param {string} callUUID - call uuid to play music file
* @param {string} url - A single URL or a list of comma separated URLs linking to an mp3 or wav file.
* @param {object} optionalParams - optional params to play music file.
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'playMusic',
value: function playMusic(callUUID, urls, optionalParams) {
var errors = (0, _common.validate)([{
field: 'call_uuid',
value: callUUID,
validators: ['isRequired']
}, {
field: 'urls',
value: urls,
validators: ['isRequired', 'isString']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: callUUID
}).playMusic(urls, optionalParams);
}
/**
* Stop Playing a music file
* @method
* @param {string} callUUID - call uuid to stop plaing music file
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'stopPlayingMusic',
value: function stopPlayingMusic(callUUID) {
var errors = (0, _common.validate)([{
field: 'call_uuid',
value: callUUID,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: callUUID
}).stopPlayingMusic();
}
/**
* Speak text during a call
* @method
* @param {string} callUUID - call uuid to speak text during a call
* @param {string} text - text to be played.
* @param {object} optionalParams - optional params to speak text during a call
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'speakText',
value: function speakText(callUUID, text, optionalParams) {
var errors = (0, _common.validate)([{
field: 'call_uuid',
value: callUUID,
validators: ['isRequired']
}, {
field: 'text',
value: text,
validators: ['isRequired', 'isString']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: callUUID
}).speakText(text, optionalParams);
}
/**
* Stop Speaking text during a call
* @method
* @param {string} callUUID - call uuid to stop speaking text during a call
* @param {object} optionalParams - optional params to stop speaking text during a call
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'stopSpeakingText',
value: function stopSpeakingText(callUUID) {
var errors = (0, _common.validate)([{
field: 'call_uuid',
value: callUUID,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: callUUID
}).stopSpeakingText();
}
/**
* Send digits on a call
* @method
* @param {string} callUUID - call uuid to send digits on a call
* @param {string} digits - digits to be send
* @param {object} optionalParams - optional params to send digits
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'sendDigits',
value: function sendDigits(callUUID, digits, optionalParams) {
var errors = (0, _common.validate)([{
field: 'call_uuid',
value: callUUID,
validators: ['isRequired']
}, {
field: 'digits',
value: digits,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: callUUID
}).sendDigits(digits, optionalParams);
}
/**
* Hangup a call request
* @method
* @param {string} callUUID - call uuid to send digits on a call
* @promise {object} returns PlivoGenericResponse Object
* @fail {Error} returns Error
*/
}, {
key: 'cancel',
value: function cancel(id) {
var errors = (0, _common.validate)([{
field: 'call_uuid',
value: id,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
return new Call(this[clientKey], {
id: id
}).cancel();
}
}, {
key: 'listLiveCalls',
value: function listLiveCalls(params) {
return this[liveCallInterfaceKey].list(params);
}
}, {
key: 'getLiveCall',
value: function getLiveCall(id) {
return this[liveCallInterfaceKey].get(id);
}
}, {
key: 'listQueuedCalls',
value: function listQueuedCalls() {
return this[queuedCallInterfaceKey].list();
}
}, {
key: 'getQueuedCall',
value: function getQueuedCall(id) {
return this[queuedCallInterfaceKey].get(id);
}
}]);
return CallInterface;
}(_base.PlivoResourceInterface);
var LiveCallResource = exports.LiveCallResource = function (_PlivoResource2) {
_inherits(LiveCallResource, _PlivoResource2);
function LiveCallResource(client) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, LiveCallResource);
var _this9 = _possibleConstructorReturn(this, (LiveCallResource.__proto__ || Object.getPrototypeOf(LiveCallResource)).call(this, action, LiveCallResource, idField, client));
if (idField in data) {
_this9.id = data[idField];
}
(0, _common.extend)(_this9, data);
_this9[clientKey] = client;
return _this9;
}
return LiveCallResource;
}(_base.PlivoResource);
var QueuedCallResource = exports.QueuedCallResource = function (_PlivoResource3) {
_inherits(QueuedCallResource, _PlivoResource3);
function QueuedCallResource(client) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, QueuedCallResource);
var _this10 = _possibleConstructorReturn(this, (QueuedCallResource.__proto__ || Object.getPrototypeOf(QueuedCallResource)).call(this, action, QueuedCallResource, idField, client));
if (idField in data) {
_this10.id = data[idField];
}
(0, _common.extend)(_this10, data);
_this10[clientKey] = client;
return _this10;
}
return QueuedCallResource;
}(_base.PlivoResource);
/**
* Represents a LiveCall interface
* @constructor
* @param {function} client - make api call
* @param {object} [data] - data of call
*/
var LiveCallInterface = function (_PlivoResourceInterfa2) {
_inherits(LiveCallInterface, _PlivoResourceInterfa2);
function LiveCallInterface(client) {
var data = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, LiveCallInterface);
var _this11 = _possibleConstructorReturn(this, (LiveCallInterface.__proto__ || Object.getPrototypeOf(LiveCallInterface)).call(this, action, LiveCallResource, idField, client));
(0, _common.extend)(_this11, data);
_this11[clientKey] = client;
return _this11;
}
/**
* Get A Live Call Detail
* @method
* @param {string} id - call uuid to get information of.
* @promise {object} returns LiveCallResource Object
* @fail {Error} returns Error
*/
_createClass(LiveCallInterface, [{
key: 'get',
value: function get(id) {
var _this12 = this;
var errors = (0, _common.validate)([{
field: 'id',
value: id,
validators: ['isRequired']
}]);
if (errors) {
return errors;
}
var client = this[clientKey];
return new Promise(function (resolve, reject) {
if (action !== '' && !id) {
reject(new Error(_this12[idKey] + ' must be set'));
}
client('GET', action + id, {
isVoiceRequest: 'true', status: 'live'
}).then(function (response) {
resolve(new GetLiveCallResponse(response.body, client));
}).catch(function (error) {
reject(error);
});
});
}
//List all Live calls
}, {
key: 'list',