178 lines
6.3 KiB
TypeScript
178 lines
6.3 KiB
TypeScript
|
|
import IAdapter from "@universal-call-sdk/common/Interfaces/IAdapter.ts";
|
||
|
|
import {GenericObject, RequestKeys} from "./types.ts";
|
||
|
|
import MessagingType from "../types/MessagingType.ts";
|
||
|
|
import {
|
||
|
|
ameyoHangupUser,
|
||
|
|
attachOmniqueService, autoSelectExtension,
|
||
|
|
getSipAccountInfo,
|
||
|
|
loginInAmeyo,
|
||
|
|
selectCampaign,
|
||
|
|
setAgentActive, setAgentOnBreak,
|
||
|
|
setAutoStatus
|
||
|
|
} from "./api.ts";
|
||
|
|
import {
|
||
|
|
acceptSipCall,
|
||
|
|
loadCallOptions,
|
||
|
|
loadCredentials,
|
||
|
|
sipHangUp,
|
||
|
|
sipMuteCall,
|
||
|
|
sipRegister, sipUnmuteCall
|
||
|
|
} from "./assets/js/sip5ml.service.ts";
|
||
|
|
import registerEventProcessor from "./eventsProcessor.ts";
|
||
|
|
|
||
|
|
class AmeyoAdapter implements IAdapter {
|
||
|
|
private callbacks: { onCallConnected: (data: GenericObject) => void; onCallDisconnected: (data: GenericObject) => void; onCallIncoming: (data: GenericObject) => void };
|
||
|
|
private eventListenerUrl: string;
|
||
|
|
private baseUrl: string;
|
||
|
|
private sessionId: string;
|
||
|
|
private userName: string;
|
||
|
|
private password: string;
|
||
|
|
private currentCallMetadata: GenericObject;
|
||
|
|
private sipAccountInfo: GenericObject;
|
||
|
|
|
||
|
|
constructor(options : GenericObject) {
|
||
|
|
console.log('AmeyoAdapter constructor');
|
||
|
|
const script = document.createElement('script');
|
||
|
|
script.src = 'assets/js/sip5ml.js'; // Assuming it's placed in the public folder
|
||
|
|
script.async = true;
|
||
|
|
document.body.appendChild(script);
|
||
|
|
this.baseUrl = options.baseUrl;
|
||
|
|
this.eventListenerUrl = options.eventListenerUrl;
|
||
|
|
this.callbacks = {
|
||
|
|
onCallIncoming: ()=> {},
|
||
|
|
onCallConnected: ()=> {},
|
||
|
|
onCallDisconnected: ()=> {}
|
||
|
|
};
|
||
|
|
this.sessionId = '';
|
||
|
|
this.userName = options.userName;
|
||
|
|
this.password = options.password;
|
||
|
|
this.sipAccountInfo = {};
|
||
|
|
this.currentCallMetadata= {};
|
||
|
|
window.BASE_AMEYO_URL = this.baseUrl;
|
||
|
|
|
||
|
|
}
|
||
|
|
init() {
|
||
|
|
window.addEventListener('message', this._registerMessageListener);
|
||
|
|
this._initializeAmeyo();
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
_initializeSipStack = () => {
|
||
|
|
//initialize sip stack
|
||
|
|
loadCredentials({
|
||
|
|
displayName: this.sipAccountInfo.displayName,
|
||
|
|
password: this.sipAccountInfo.password,
|
||
|
|
phoneNumber: this.sipAccountInfo.phoneNumber,
|
||
|
|
privateIdentity: this.sipAccountInfo.private,
|
||
|
|
publicIdentity: this.sipAccountInfo.publicIdentity,
|
||
|
|
realm: this.sipAccountInfo.realm});
|
||
|
|
loadCallOptions();
|
||
|
|
sipRegister();
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
_initializeAmeyo = () => {
|
||
|
|
loginInAmeyo(this.userName, this.password);
|
||
|
|
}
|
||
|
|
|
||
|
|
_onListenForCorsBypassResponse = (payload: GenericObject) => {
|
||
|
|
if (payload?.data?.requestKey === RequestKeys.AMEYO_LOGIN) {
|
||
|
|
const sessionId = payload?.data?.response?.userSessionInfo?.sessionId;
|
||
|
|
this.sessionId = sessionId;
|
||
|
|
getSipAccountInfo(sessionId, this.userName?.toLowerCase());
|
||
|
|
registerEventProcessor(this.eventListenerUrl, sessionId);
|
||
|
|
}
|
||
|
|
if (payload?.data?.requestKey === RequestKeys.AMEYO_HEARTBEAT) {
|
||
|
|
console.log('heartbeat response', payload?.data?.response);
|
||
|
|
}
|
||
|
|
if(payload?.data?.requestKey === RequestKeys.SIP_ACCOUNT_INFO) {
|
||
|
|
console.log('sip account info', payload?.data?.response);
|
||
|
|
this._initializeSipStack();
|
||
|
|
this.sipAccountInfo = payload?.data?.response;
|
||
|
|
setAgentActive(this.sessionId);
|
||
|
|
}
|
||
|
|
if(payload?.data?.requestKey === RequestKeys.AMEYO_AVAILABLE) {
|
||
|
|
setAutoStatus(this.sessionId);
|
||
|
|
attachOmniqueService(this.sessionId, this.userName.toLowerCase());
|
||
|
|
|
||
|
|
}
|
||
|
|
if (payload?.data?.requestKey === RequestKeys.OMNIQUEUE_SERVICE) {
|
||
|
|
selectCampaign(this.sessionId, this.userName.toLowerCase());
|
||
|
|
}
|
||
|
|
if(payload?.data?.requestKey === RequestKeys.SELECT_CAMPAIGN) {
|
||
|
|
console.log('campaign selected', payload?.data?.response);
|
||
|
|
autoSelectExtension(this.sessionId, this.userName.toLowerCase());
|
||
|
|
|
||
|
|
}
|
||
|
|
if(payload?.data?.requestKey === RequestKeys.AUTO_SELECT_EXTENSION) {
|
||
|
|
console.log('auto select extension', payload?.data?.response);
|
||
|
|
setAutoStatus(this.sessionId)
|
||
|
|
}
|
||
|
|
if(payload?.data?.requestKey === RequestKeys.AMEYO_ON_BREAK) {
|
||
|
|
setAutoStatus(this.sessionId);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
_registerMessageListener = async ({ data }: GenericObject) => {
|
||
|
|
if (data?.type === MessagingType.SET_RESPONSE_WITHOUT_CORS) {
|
||
|
|
this._onListenForCorsBypassResponse(data);
|
||
|
|
}
|
||
|
|
if (data?.type === 'setAmeyoAvailabilty') {
|
||
|
|
window.postMessage({ type: 'onAmeyoAvailabiltyChange', data: true });
|
||
|
|
}
|
||
|
|
if(data?.type === MessagingType.ON_AMEYO_CALL_INCOMING) {
|
||
|
|
this.callbacks.onCallIncoming(data?.data);
|
||
|
|
this.currentCallMetadata = {...this.currentCallMetadata, ...data?.data}
|
||
|
|
}
|
||
|
|
if(data?.type === MessagingType.ON_AMEYO_CALL_ACCEPTED) {
|
||
|
|
this.callbacks.onCallConnected(data?.data);
|
||
|
|
this.currentCallMetadata = {...this.currentCallMetadata, ...data?.data}
|
||
|
|
}
|
||
|
|
if(data?.type === MessagingType.ON_AMEYO_CALL_DISCONNECTED) {
|
||
|
|
this.callbacks.onCallDisconnected(data?.data);
|
||
|
|
this.currentCallMetadata = {...this.currentCallMetadata, ...data?.data}
|
||
|
|
}
|
||
|
|
};
|
||
|
|
registerOnCallIncoming(callback: ()=> void) {
|
||
|
|
console.log('registerOnCallIncoming');
|
||
|
|
this.callbacks.onCallIncoming = callback;
|
||
|
|
}
|
||
|
|
registerOnCallConnected(callback: ()=> void) {
|
||
|
|
console.log('registerOnCallConnected');
|
||
|
|
this.callbacks.onCallConnected = callback;
|
||
|
|
}
|
||
|
|
registerOnCallDisconnected(callback: ()=> void) {
|
||
|
|
console.log('registerOnCallDisconnected');
|
||
|
|
this.callbacks.onCallDisconnected = callback;
|
||
|
|
}
|
||
|
|
acceptCall() {
|
||
|
|
console.log('acceptCall');
|
||
|
|
acceptSipCall();
|
||
|
|
}
|
||
|
|
rejectCall() {
|
||
|
|
console.log('rejectCall');
|
||
|
|
sipHangUp();
|
||
|
|
ameyoHangupUser(this.sessionId, this.currentCallMetadata?.crtObjectId);
|
||
|
|
}
|
||
|
|
|
||
|
|
setOnBreak() {
|
||
|
|
console.log('setAgentOnBreak');
|
||
|
|
setAgentOnBreak(this.sessionId);
|
||
|
|
}
|
||
|
|
|
||
|
|
setAvailable() {
|
||
|
|
setAgentActive(this.sessionId);
|
||
|
|
}
|
||
|
|
|
||
|
|
muteCall() {
|
||
|
|
console.log('muteCall');
|
||
|
|
sipMuteCall();
|
||
|
|
}
|
||
|
|
unmuteCall() {
|
||
|
|
console.log('unmuteCall');
|
||
|
|
sipUnmuteCall();
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
export default AmeyoAdapter;
|