kod-temp
Version:
Workano Communications SDK
228 lines (227 loc) • 11.1 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_1 = __importDefault(require("../../lib/index"));
class default_1 {
static connect(props) {
return __awaiter(this, void 0, void 0, function* () {
yield index_1.default.Phone.connect({
media: {
audio: true,
video: true,
},
}, props.sipLine);
function isCallOwner(callSession) {
var _a;
const session = index_1.default.Auth.session;
if (!session)
return false;
let lines = session === null || session === void 0 ? void 0 : session.allLines();
return (_a = lines === null || lines === void 0 ? void 0 : lines.some((l) => { var _a; return l.id === ((_a = callSession.call) === null || _a === void 0 ? void 0 : _a.lineId); })) !== null && _a !== void 0 ? _a : false;
}
const handleCallAccept = (callSession) => {
const id = callSession.getId();
if (id in this.answeredCalls) {
clearTimeout(this.answeredCalls[id].timeout);
callSession.updateFrom(this.answeredCalls[id].callSession);
props.onCallAnswered(callSession);
delete this.answeredCalls[id];
}
else {
this.answeredCalls[id] = {
timeout: setTimeout(() => {
props.onCallAnswered(callSession);
delete this.answeredCalls[id];
}, 2000),
callSession,
};
}
};
index_1.default.Phone.on(index_1.default.Phone.ON_CALL_INCOMING, props.onIncomingCall);
index_1.default.Phone.on(index_1.default.Phone.ON_CALL_ACCEPTED, handleCallAccept);
index_1.default.Phone.on(index_1.default.Phone.ON_CALL_FAILED, props.onCallFailed);
index_1.default.Phone.on(index_1.default.Phone.ON_CALL_CANCELED, props.onCallCanceled);
index_1.default.Phone.on(index_1.default.Phone.ON_CALL_ENDED, props.onCallEnded);
index_1.default.Phone.on(index_1.default.Phone.ON_CALL_REJECTED, props.onCallRejected);
if (props.onDisconnected)
index_1.default.Phone.on(index_1.default.Phone.ON_DISCONNECTED, props.onDisconnected);
if (props.onCallMuted)
index_1.default.Phone.on(index_1.default.Phone.ON_CALL_MUTED, props.onCallMuted);
if (props.onCallUnMuted)
index_1.default.Phone.on(index_1.default.Phone.ON_CALL_UNMUTED, props.onCallUnMuted);
if (props.onNetworkStats)
index_1.default.Phone.on(index_1.default.Phone.ON_NETWORK_STATS, props.onNetworkStats);
if (props.onPlayProgressSound) {
index_1.default.Phone.on(index_1.default.Phone.ON_PLAY_PROGRESS_SOUND, props.onPlayProgressSound);
}
if (props.onProgress) {
index_1.default.Phone.on(index_1.default.Phone.ON_PROGRESS, props.onProgress);
}
if (props.onPlayRingSound) {
index_1.default.Phone.on(index_1.default.Phone.ON_PLAY_RING_SOUND, props.onPlayRingSound);
}
if (props.onPlayHangupSound) {
index_1.default.Phone.on(index_1.default.Phone.ON_PLAY_HANGUP_SOUND, props.onPlayHangupSound);
}
if (props.onMessage) {
index_1.default.Phone.on(index_1.default.Phone.ON_MESSAGE, props.onMessage);
}
if (props.onSignal) {
index_1.default.Phone.on(index_1.default.Phone.ON_SIGNAL, props.onSignal);
}
index_1.default.Websocket.on(index_1.default.Websocket.CALL_ANSWERED, ({ data }) => {
const call = index_1.default.domain.Call.parse(data);
const answeredCallSession = index_1.default.domain.CallSession.parseCall(call);
if (!isCallOwner(answeredCallSession))
return;
const id = answeredCallSession.getId();
if (id in this.answeredCalls) {
clearTimeout(this.answeredCalls[id].timeout);
answeredCallSession.updateFrom(this.answeredCalls[id].callSession);
props.onCallAnswered(answeredCallSession);
delete this.answeredCalls[id];
console.log(this.answeredCalls);
}
else {
this.answeredCalls[id] = {
timeout: setTimeout(() => {
props.onCallAnswered(answeredCallSession);
delete this.answeredCalls[id];
}, 2000),
callSession: answeredCallSession,
};
}
});
index_1.default.Websocket.on(index_1.default.Websocket.CALL_CREATED, ({ data }) => {
const call = index_1.default.domain.Call.parse(data);
const createdCallSession = index_1.default.domain.CallSession.parseCall(call);
if (!isCallOwner(createdCallSession))
return;
props.onCallCreated(createdCallSession);
});
index_1.default.Websocket.on(index_1.default.Websocket.CALL_UPDATED, ({ data }) => {
const call = index_1.default.domain.Call.parse(data);
const updatedCallSession = index_1.default.domain.CallSession.parseCall(call);
if (!isCallOwner(updatedCallSession))
return;
props.onCallUpdated(updatedCallSession);
});
});
}
static call(_a) {
return __awaiter(this, arguments, void 0, function* ({ number, withCamera, monitoringInterval, }) {
let callSession = yield index_1.default.Phone.call(number, withCamera);
if (callSession) {
index_1.default.Phone.startNetworkMonitoring(callSession, monitoringInterval);
}
return callSession;
});
}
/**
* Make a call between specified line and number
* @param {string} number - number to be called;
* @param {boolean} isMobile - call from your mobile to specified number;
* @param {boolean} callbackAllLines - try all of your lines to make a call to the number;
* @param {Object} line - target line for cdr;
*/
static makeCall(props) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
if (!index_1.default.Auth.session) {
throw new Error("Please login before use.");
}
const primeLine = index_1.default.Auth.session.primaryLine();
const ctiPhone = new index_1.default.domain.CTIPhone(index_1.default.Auth.session, props.isMobile, props.callbackAllLines);
return yield ctiPhone.makeCall(props.number, (_a = props.line) !== null && _a !== void 0 ? _a : primeLine);
});
}
static accept(callSession, withCamera) {
return __awaiter(this, void 0, void 0, function* () {
return yield index_1.default.Phone.accept(callSession, withCamera);
});
}
static hangup(callSession) {
return __awaiter(this, void 0, void 0, function* () {
return yield index_1.default.Phone.hangup(callSession);
});
}
static mute(callSession) {
index_1.default.Phone.mute(callSession);
}
static unmute(callSession) {
index_1.default.Phone.unmute(callSession);
}
static turnCameraOn(callSession) {
return __awaiter(this, void 0, void 0, function* () {
const { cameraEnabled, muted, screensharing } = callSession;
if (!cameraEnabled && !screensharing) {
yield index_1.default.Phone.reinvite(callSession, {
video: true,
screen: false,
audio: !muted,
});
}
index_1.default.Phone.turnCameraOn(callSession);
if (this.toggleCameraTimeout) {
clearTimeout(this.toggleCameraTimeout);
}
this.toggleCameraTimeout = setTimeout(() => {
index_1.default.Phone.sendSignal({
type: "audioCall/cameraEnabled",
callIds: callSession.getTalkingToIds(),
cameraEnabled: true,
});
}, cameraEnabled || screensharing ? 0 : 5000);
});
}
static turnCameraOff(callSession) {
return __awaiter(this, void 0, void 0, function* () {
const { cameraEnabled, muted, screensharing } = callSession;
if (cameraEnabled && !screensharing) {
yield index_1.default.Phone.reinvite(callSession, {
video: false,
screen: false,
audio: !muted,
});
}
index_1.default.Phone.turnCameraOff(callSession);
this.toggleCameraTimeout = setTimeout(() => {
index_1.default.Phone.sendSignal({
type: "audioCall/cameraEnabled",
callIds: callSession.getTalkingToIds(),
cameraEnabled: false,
});
}, cameraEnabled || screensharing ? 0 : 5000);
});
}
static getLocalVideoStream(callSession) {
return index_1.default.Phone.getLocalVideoStream(callSession);
}
static getRemoteVideoStream(callSession) {
return index_1.default.Phone.getRemoteVideoStream(callSession);
}
static disconnect() {
return __awaiter(this, void 0, void 0, function* () {
index_1.default.Phone.disconnect();
});
}
static reject(callSession) {
return __awaiter(this, void 0, void 0, function* () {
return yield index_1.default.Phone.reject(callSession);
});
}
}
default_1.toggleCameraTimeout = null;
default_1.answeredCalls = {};
exports.default = default_1;