UNPKG

kod-temp

Version:

Workano Communications SDK

193 lines (192 loc) 8.15 kB
import workano from "../../lib/index"; export default class default_1 { static toggleCameraTimeout = null; static answeredCalls = {}; static async connect(props) { await workano.Phone.connect({ media: { audio: true, video: true, }, }, props.sipLine); function isCallOwner(callSession) { const session = workano.Auth.session; if (!session) return false; let lines = session?.allLines(); return lines?.some((l) => l.id === callSession.call?.lineId) ?? 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, }; } }; workano.Phone.on(workano.Phone.ON_CALL_INCOMING, props.onIncomingCall); workano.Phone.on(workano.Phone.ON_CALL_ACCEPTED, handleCallAccept); workano.Phone.on(workano.Phone.ON_CALL_FAILED, props.onCallFailed); workano.Phone.on(workano.Phone.ON_CALL_CANCELED, props.onCallCanceled); workano.Phone.on(workano.Phone.ON_CALL_ENDED, props.onCallEnded); workano.Phone.on(workano.Phone.ON_CALL_REJECTED, props.onCallRejected); if (props.onDisconnected) workano.Phone.on(workano.Phone.ON_DISCONNECTED, props.onDisconnected); if (props.onCallMuted) workano.Phone.on(workano.Phone.ON_CALL_MUTED, props.onCallMuted); if (props.onCallUnMuted) workano.Phone.on(workano.Phone.ON_CALL_UNMUTED, props.onCallUnMuted); if (props.onNetworkStats) workano.Phone.on(workano.Phone.ON_NETWORK_STATS, props.onNetworkStats); if (props.onPlayProgressSound) { workano.Phone.on(workano.Phone.ON_PLAY_PROGRESS_SOUND, props.onPlayProgressSound); } if (props.onProgress) { workano.Phone.on(workano.Phone.ON_PROGRESS, props.onProgress); } if (props.onPlayRingSound) { workano.Phone.on(workano.Phone.ON_PLAY_RING_SOUND, props.onPlayRingSound); } if (props.onPlayHangupSound) { workano.Phone.on(workano.Phone.ON_PLAY_HANGUP_SOUND, props.onPlayHangupSound); } if (props.onMessage) { workano.Phone.on(workano.Phone.ON_MESSAGE, props.onMessage); } if (props.onSignal) { workano.Phone.on(workano.Phone.ON_SIGNAL, props.onSignal); } workano.Websocket.on(workano.Websocket.CALL_ANSWERED, ({ data }) => { const call = workano.domain.Call.parse(data); const answeredCallSession = workano.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, }; } }); workano.Websocket.on(workano.Websocket.CALL_CREATED, ({ data }) => { const call = workano.domain.Call.parse(data); const createdCallSession = workano.domain.CallSession.parseCall(call); if (!isCallOwner(createdCallSession)) return; props.onCallCreated(createdCallSession); }); workano.Websocket.on(workano.Websocket.CALL_UPDATED, ({ data }) => { const call = workano.domain.Call.parse(data); const updatedCallSession = workano.domain.CallSession.parseCall(call); if (!isCallOwner(updatedCallSession)) return; props.onCallUpdated(updatedCallSession); }); } static async call({ number, withCamera, monitoringInterval, }) { let callSession = await workano.Phone.call(number, withCamera); if (callSession) { workano.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 async makeCall(props) { if (!workano.Auth.session) { throw new Error("Please login before use."); } const primeLine = workano.Auth.session.primaryLine(); const ctiPhone = new workano.domain.CTIPhone(workano.Auth.session, props.isMobile, props.callbackAllLines); return await ctiPhone.makeCall(props.number, props.line ?? primeLine); } static async accept(callSession, withCamera) { return await workano.Phone.accept(callSession, withCamera); } static async hangup(callSession) { return await workano.Phone.hangup(callSession); } static mute(callSession) { workano.Phone.mute(callSession); } static unmute(callSession) { workano.Phone.unmute(callSession); } static async turnCameraOn(callSession) { const { cameraEnabled, muted, screensharing } = callSession; if (!cameraEnabled && !screensharing) { await workano.Phone.reinvite(callSession, { video: true, screen: false, audio: !muted, }); } workano.Phone.turnCameraOn(callSession); if (this.toggleCameraTimeout) { clearTimeout(this.toggleCameraTimeout); } this.toggleCameraTimeout = setTimeout(() => { workano.Phone.sendSignal({ type: "audioCall/cameraEnabled", callIds: callSession.getTalkingToIds(), cameraEnabled: true, }); }, cameraEnabled || screensharing ? 0 : 5000); } static async turnCameraOff(callSession) { const { cameraEnabled, muted, screensharing } = callSession; if (cameraEnabled && !screensharing) { await workano.Phone.reinvite(callSession, { video: false, screen: false, audio: !muted, }); } workano.Phone.turnCameraOff(callSession); this.toggleCameraTimeout = setTimeout(() => { workano.Phone.sendSignal({ type: "audioCall/cameraEnabled", callIds: callSession.getTalkingToIds(), cameraEnabled: false, }); }, cameraEnabled || screensharing ? 0 : 5000); } static getLocalVideoStream(callSession) { return workano.Phone.getLocalVideoStream(callSession); } static getRemoteVideoStream(callSession) { return workano.Phone.getRemoteVideoStream(callSession); } static async disconnect() { workano.Phone.disconnect(); } static async reject(callSession) { return await workano.Phone.reject(callSession); } }