Skip to content

Instantly share code, notes, and snippets.

@aabhirb
Last active August 6, 2024 18:17
Show Gist options
  • Select an option

  • Save aabhirb/a6317022fbb114c6185b17702c89039a to your computer and use it in GitHub Desktop.

Select an option

Save aabhirb/a6317022fbb114c6185b17702c89039a to your computer and use it in GitHub Desktop.
Includes Metadata components supporting the Regal Dialer Widget
<apex:page controller="RegalDialerWidgetController" >
<style>
.ant-layout-sider-children {
display: none !important;
}
</style>
<script src="/support/api/40.0/lightning/opencti_min.js"></script>
<!-- Insert Regal URL Below :
Production : https://app.regal.io/agent
Staging : https://app.staging.regal.io/agent
Dev : https://d1qfs6oamrzmm0.cloudfront.net/dev/dev05/agent
-->
<apex:iframe src="https://app.regal.io/agent" width="720px" height="530px" frameborder="false" />
<script>
let isConsoleView = false;
let defaultLookupIdentifier = "ID_LOOKUP";
let cachedLookupIdentifier;
function postMessage(data) {
document.getElementsByTagName('iframe')[0].contentWindow.postMessage(data, '*');
}
var callback = function(response) {
if (response.success) {
console.log('API method call executed successfully! returnValue:', response.returnValue);
} else {
console.error('Something went wrong! Errors:', response.errors);
}
};
function setSoftphonePanelIcon() {
sforce.opencti.setSoftphonePanelIcon({key:"dialing", callback: callback});
}
setSoftphonePanelIcon();
sforce.opencti.enableClickToDial();
sforce.opencti.onClickToDial({
listener: async function(result) {
if (result.recordId) {
sforce.opencti.setSoftphonePanelVisibility({ visible: true });
const asyncFunction = async () => {
if (!cachedLookupIdentifier) {
cachedLookupIdentifier = await getLookupConfigFromApex();
}
let searchIdentifier = result.recordId;
if (cachedLookupIdentifier == "PHONE_LOOKUP") {
searchIdentifier = result.number;
}
postMessage({
source: 'salesforce',
phoneNumber: searchIdentifier,
action: 'open-dialer',
});
};
setTimeout(asyncFunction, 350);
} else {
console.error("Something wrong with CTI config, click-to-dial not enabled. Pls contact regal support.");
}
}
});
window.addEventListener(
"message",
(event) => {
console.log('event received from regal', event);
if (event.data) {
const action = event.data.action;
if (action === "open-record") {
handleOpenRecordMessage(event.data);
} else if (action === "task-assigned" ) {
handleTaskAssignedMessage(event.data);
} else if (action === "task-accepted" ) {
handleTaskAcceptedMessage(event.data);
} else if (action === "call-disconnected" ) {
handleCallDisconnectedMessage(event.data);
}
}
},
false,
);
function handleOpenRecordMessage(data) {
openRecord(data);
}
function handleTaskAcceptedMessage(data) {
openRecord(data);
}
function handleCallDisconnectedMessage(data) {
sforce.opencti.isSoftphonePanelVisible({
callback: response => {
if (response.success) {
if (response.returnValue && !response.returnValue.visible) {
sforce.opencti.setSoftphonePanelVisibility({ visible: true });
}
} else {
sforce.opencti.setSoftphonePanelVisibility({ visible: true });
}
}
});
}
function handleTaskAssignedMessage(data) {
console.log('inside handleTaskAssignedMessage');
debugger;
sforce.opencti.isSoftphonePanelVisible({
callback: response => {
if (response.success) {
if (response.returnValue && !response.returnValue.visible) {
sforce.opencti.setSoftphonePanelVisibility({ visible: true });
}
} else {
sforce.opencti.setSoftphonePanelVisibility({ visible: true });
}
}
});
}
function openRecord(data) {
const relatedRecordId = data.objectId;
const profileURL = data.profileLink;
let param;
if (relatedRecordId) {
param = { recordId: relatedRecordId };
} else if (!relatedRecordId && profileURL) {
const recordIdFromURL = extractRecordIdFromUrl(profileURL);
param = { recordId: recordIdFromURL };
} else {
return;
}
sforce.opencti.screenPop({
type: sforce.opencti.SCREENPOP_TYPE.SOBJECT,
params: param
});
}
function extractRecordIdFromUrl(url) {
// Regular expressions to match Salesforce record URLs in Lightning Experience and Salesforce Classic
const regexes = [
/(?:\b\w+\.)?salesforce\.com\/(?:\w+\/)?([a-zA-Z0-9]{15,18})/,
/(?:\/lightning\/)?(?:\/r\/|\?id=)([a-zA-Z0-9]{15,18})\/(view|edit|detail)/,
/(?:\/lightning\/)?(?:\/r\/)?([a-zA-Z0-9]{15,18})/,
];
for (const regex of regexes) {
const match = url.match(regex);
if (match) {
console.log('inside match : ', match)
// The record ID is captured in the first group of the regex match
return match[1];
}
}
return null; // URL doesn't match any Salesforce record format
}
async function getLookupConfigFromApex() {
return new Promise((resolve, reject) => {
Visualforce.remoting.Manager.invokeAction(
'{!$RemoteAction.RegalDialerWidgetController.getLookUpConfig}',
function(result, event){
if (event.status) {
resolve(result);
} else if (event.type === 'exception') {
console.error(event.message);
}
resolve(defaultLookupIdentifier);
},
{escape: true}
);
});
}
</script>
</apex:page>
global without sharing class RegalDialerWidgetController {
global static final String LOOKUP_CONFIG = 'Lookup_Identifier';
global static final String PHONE_LOOKUP_IDENTIFIER = 'PHONE_LOOKUP';
global static final String ID_LOOKUP_IDENTIFIER = 'ID_LOOKUP';
@RemoteAction
global static String getLookUpConfig() {
regal_voice__Regal_Constants__mdt lookupConfig = regal_voice__Regal_Constants__mdt.getInstance(LOOKUP_CONFIG);
return (lookupConfig != null ? lookupConfig.regal_voice__Value__c : ID_LOOKUP_IDENTIFIER);
}
// This is commented as we are not using the console view check atm ,
// sforce cti lib handles both console and standard views
// but we might need to reintroduce this in future hence keeping it here.
/*@RemoteAction
global static Boolean isLoggedIntoConsoleView() {
String navType = getNavType();
return (navType == 'Console');
}
public static String getNavType() {
UserAppInfo userAppInfo = [SELECT Id, AppDefinitionId FROM UserAppInfo WHERE UserId = :UserInfo.getUserId() LIMIT 1];
AppDefinition appDefinition = [SELECT DurableId, NavType FROM AppDefinition Where DurableId = :userAppInfo.AppDefinitionId LIMIT 1];
return appDefinition.NavType;
}*/
}
@IsTest
private class RegalDialerWidgetControllerTest {
@IsTest
static void itShouldReturnIdLookupIdentifierByDefault() {
Test.startTest();
String lookupConfig = RegalDialerWidgetController.getLookUpConfig();
System.assertEquals(RegalDialerWidgetController.ID_LOOKUP_IDENTIFIER, lookupConfig);
Test.stopTest();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment