# Advisor Desktop Widget APIs The Advisor Desktop Widget API provides methods and events that can be used to develop third party integrations with the eGain Advisor Desktop user interface. The JavaScript APIs are called from a *promise* Javascript object within the Advisor Desktop and *resolve*, *reject* are passed as arguments to the hooks. The *resolve/reject* are used to resolve the promise while invoking the hook. Hooks auto resolve by default instantly unless return is false and in which case all hooks will resolve after 120 seconds + resolve - resolves the promise ``` var AgentHookImpl = { beforeSocialCkeditorInit: function (resolve, reject, config) { // do async processing setTimeout(function () { console.log(config); config.extaplugin += ",myplugin"; resolve(); // resolve promise }, 100); return false; // return false to prevent promise from auto resolving } }; ``` + reject - rejects the promise For example reject parameters can be used to show custom error messages for example. TODO: Add example code snippet ### afterChatComplete This method is called after a chat is completed by the agent in the advisor desktop. ##### Parameters + resolve - resolve function + reject - reject function + activityId - ID of activity being completed ``` var AgentHookImpl = { afterChatComplete: function (resolve, reject, activityId) { // Do something with the event console.log(activityId); }); } ``` ### afterLeaveOrCompleteCallback Call this method after the callback for an agent or customer leaving or completing a chat is executed. ##### Parameters + resolve - resolve function + reject - reject function + result - json object returned by Leave or Complete call + isLeave - true/false + activityId - ID of selected chat activity ``` var AgentHookImpl = { afterLeaveOrCompleteCallback: function (resolve, reject, result, isLeave, activityId) { // Do something with the event console.log (activityId); }); } ``` ### beforeSendMessage This method is called to change and manipulate a message before it is sent out. ##### Parameters + resolve - resolve function + reject - reject function + obj - object has the following properties + message: message to be sent (if this property is modified a modified message will be sent) ``` var AgentHookImpl = { beforeSendMessage: function (resolve, reject, obj) { "use strict"; var message = obj.message, activity = obj.activity; if(message && message.indexOf("fool")> -1){ reject("Fool is not allowed in chat message"); //message to be show to agent }else{ resolve() } } }; ``` ### afterSendMessage Call this method to change and manipulate a message after it is sent out. ##### Parameters + resolve - resolve function + reject - reject function + obj - object has the following properties: + message: message to be sent (if this property is modified a modified message is sent) ``` var AgentHookImpl = { afterSendMessage: function (resolve, reject, messageobj) { // Do something with the event } }; ``` ### beforeGetChatMessage Call this method before the system fetches a message. ##### Parameters + resolve - resolve function + reject - reject function + messageObj - is the message object received from polling for example: + eglvsid: activity id for the received message + eglvmsg: message object + Any change in the message object changes the resulting message ``` var AgentHookImpl = { beforeGetChatMessage: function (resolve, reject, messageobj) { // Do something with the event } }; ``` ### afterGetChatMessage Call this method after the system fetches a message. ##### Parameters + resolve - resolve function + reject - reject function + messageObj - is the message object received from polling for example: + eglvsid: activity id for the received message + eglvmsg: message object + Any change in the message object changes the resulting message ``` var AgentHookImpl = { aftereGetChatMessage: function (resolve, reject, messageobj) { // Do something with the event } }; ``` ### beforePullChatCallback Call this method before the callback for the pulling a chat is executed. ##### Parameters + resolve - resolve function + reject - reject function + result - json object returned by Pull Chat jsp call ``` var AgentHookImpl = { beforeGetChatMessage: function (resolve, reject, result) { // Do something with the event } }; ``` ### beforeChatSelect Call this method before the chat is selected. ##### Parameters + resolve - resolve function + reject - reject function + activityId - Id of selected chat activity ``` var AgentHookImpl = { beforeChatSelect: function (resolve, reject, activityId) { // Do something with the event } }; ``` ### aftereChatSelect Call this method after the chat is selected. ##### Parameters + resolve - resolve function + reject - reject function + activityId - Id of selected chat activity ``` var AgentHookImpl = { afterChatSelect: function (resolve, reject, activityId) { // Do something with the event } }; ``` ### beforeChatCKeditorInit Call this method before the chat CKEditor is invoked. ##### Parameters + resolve - resolve function + reject - reject function + config - ckeditor config object ``` var AgentHookImpl = { beforeChatCkeditorInit: function (resolve, reject, config) { // Do something with the event } }; ``` ### getChatReferrerNameClassMapFunction Call this method to get custom classes for chat icon mapping function based on referrer name. ##### Parameters + resolve - resolve function + reject - reject function + referrer - ``` getChatReferrerNameClassMapFunction: function (resolve, reject) { resolve(function(referrer){ return { "facebook": 'fb-chat', "App": 'mobile-chat', 'Apple': 'apple-chat', 'SMS': 'sms-chat' }[referrer] }); } here class apple-chat refers to something like below class name .icon-egain-stream-chat.icon-egain-stream-chat-apple-chat:before, .apple-chat:before{ content: "\e011" !important; } ``` ## UI Hooks: ### addTopToolbarButtons Call this method to add a custom button in the main drop down menu of the toolbar. To return a button or an array of buttons use resolve. ##### Parameters + resolve - resolve function + reject - reject function ##### Example code: ``` var AgentHookImpl = { addTopToolbarButtons: function (resolve, reject) { resolve({ text: 'Test', handler: function(){ alert(1); } }) } }; ``` ### addChatToolbarButtons Call this method to add a custom button to the chat toolbar. To return a button or an array of buttons use resolve. ##### Parameters + resolve - resolve function + reject - reject function ##### Example code: ``` To add buttons return a button or an array of buttons with resolve. The scope of callback handler is always AgentHook * example: * function(resolve, reject){ * resolve({ * text: 'Test', * tooltip: 'TestTooltip', * position: 'toolbar', //position can be 'toolbar' or 'menu' * handler: function(){ * alert(1); * } * }) * * } * @param resolve * @param reject ``` ## Email Hooks: ### beforeOnAddressBookLoad Call this method to add custom code before loading the address book. ##### Parameters + resolve - resolve function + reject - reject function + store - address book store ``` var AgentHookImpl = { beforeOnAddressBookLoad: function (resolve, reject, store) { // Do something with the event } }; ``` ### beforeSendAndCompleteEmail Call this method to add custom code before the "Send and Complete" of an email. ##### Parameters + resolve - resolve function + reject - reject function + replyType - email reply type ``` var AgentHookImpl = { beforeSendAndCompleteEmail: function (resolve, reject, replyType) { // Do something with the event } }; ``` ### afterSendAndCompleteEmailCallbackSuccess Call this method to add custom code after the execution of the call back for a successful "Send and Complete" of an email. ##### Parameters + resolve - resolve function + reject - reject function + result - json object returned by Send and Complete Email jsp call ``` var AgentHookImpl = { afterSendAndCompleteEmailCallbackSuccess: function (resolve, reject, result) { // Do something with the event } }; ``` ### beforeSendEmail Call this method to add custom code before the "Send " of an email. ##### Parameters + resolve - resolve function + reject - reject function ``` var AgentHookImpl = { afterSendAndCompleteEmailCallbackSuccess: function (resolve, reject) { // Do something with the event } }; ``` ### beforeEmailCkeditorInit Call this method to add custom code before the email CKEditor is invoked. ##### Parameters + resolve - resolve function + reject - reject function + config - CKEditor config object ``` var AgentHookImpl = { beforeEmailCkeditorInit: function (resolve, reject, config) { // Do something with the event } }; ``` ### General Hooks: ### beforeNonChatSelect Call this method to add custom code before a no chat activity is selected. ##### Parameters + resolve - resolve function + reject - reject function + activityId - Id of selected chat activity + activityType - activity type of activity selected ``` var AgentHookImpl = { beforeNonChatSelect: function (resolve, reject, activityId, activityType) { // Do something with the event } }; ``` ### afterNonChatSelectCallback Call this method to add custom code after the callback for selecting a non chat activity is executed. ##### Parameters + resolve - resolve function + reject - reject function + activityId - Id of selected chat activity + activityType - activity type of activity selected ``` var AgentHookImpl = { afterNonChatSelectCallback: function (resolve, reject, activityId, activityType) { // Do something with the event } }; ``` ### beforeNonChatComplete Call this method to add custom code before a non-chat type of activity is completed. ##### Parameters + resolve - resolve function + reject - reject function + activityIds - all selected activity ids [] ``` var AgentHookImpl = { beforeNonChatComplete: function (resolve, reject, activityIds) { // Do something with the event } }; ``` ### afterNonChatCompleteCallbackSuccess Call this method to add custom code after the successful execution of the callback for the completion on a non-chat activity. ##### Parameters + resolve - resolve function + reject - reject function + activityIds - all selected activity ids [] ``` var AgentHookImpl = { afterNonChatCompleteCallbackSuccess: function (resolve, reject, activityIds) { // Do something with the event } }; ``` ### beforeTaskCkeditorInit Call this method to add custom code before the task CKEditor is invoked. ##### Parameters + resolve - resolve function + reject - reject function + config - CKEditor config object ``` var AgentHookImpl = { beforeTaskCkeditorInit: function (resolve, reject, config) { // Do something with the event } }; ``` ### beforeSocialCkeditorInit Call this method to add custom code before the social CKEditor is invoked. ##### Parameters + resolve - resolve function + reject - reject function + config - CKEditor config object ``` var AgentHookImpl = { beforeSocialCkeditorInit: function (resolve, reject, config) { // Do something with the event } }; ``` ### getInfoEventActivityData Call this method to manipulate activity data sent out as part of DUO event. ##### Parameters + eventName - name of event getting fired + activity - Activity object + Return: it returns either an object with activity data or undefined/null; #### example code: ``` getInfoEventActivityData: function (eventName, activity) { var retObj = {}; if(activity.getActivityType && activity.getActivityType() ==2000){ retObj = { type: 'chat', id: activity.getId(), subject: activity.get('subject'), caseId: activity.get('caseId'), customerId: activity.get('customerId'), customerName: activity.data.contactPerson, queueName: activity.data.queue_name, createdAt: new Date(activity.data.whenCreated).toISOString() } }else if(activity.entityName){ retObj = { type: activity.getActivityType(), id: activity.getId(), subject: activity.get('subject'), caseId: activity.get('caseId'), customerId: activity.get('customerId'), customerName: (activity.data.contactPerson_firstName || "") + " " + (activity.data.contactPerson_lastName || ""), queueName: activity._queue && activity._queue.data && activity._queue.data.name, createdAt: activity.data.createdAt.toISOString(), mode: activity._mode && activity._mode.data && activity._mode.data.value } }else{ retObj = { type: activity.type? activity.type.value: '', id: activity.id, subject: activity.subject, caseId: activity.case ? activity.case.id : -1, customerId: activity.customer ? activity.customer.id : -1, customerName: (activity.data.contactPerson_firstName || "") + " " + (activity.data.contactPerson_lastName || ""), queueName: activity._queue && activity._queue.data && activity._queue.data.name, createdAt: activity.data.createdAt && new Date(activity.data.whenCreated).toISOString() } } return retObj; } ```