18 var _0x2ced=['localhost','emit','application/json','info','Getting\x20agent\x20with\x20email','format','total_entries','entries','then','Agent\x20found!','%s/users/me','remoteUri','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','debug','stringify','GET','authType','%s:%s','password','token','%s/token:%s','username','Basic\x20%s','toString','base64','%s/customers/search','Enduser\x20found!','_embedded','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','%s/customers','POST','work','length','map','forEach','nameField','customField','toLowerCase','content','type','string','custom_fields','variable','variableName','idField','customVariable','Variable','name','priority','Creating\x20new\x20ticket...','merge','Subjects','phone','xCALLY','Motion','CTI','concat','/api/v2/users/%d','getString','Descriptions','Fields','%s/customers/%d/cases','queue','stripTrailingSlash','serverUrl','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','Authorization','email','The\x20User\x20ID\x20is:','calleridname','calleridnum','The\x20Enduser\x20ID\x20is:','Ticket\x20created!','The\x20Ticket\x20ID\x20is:','integrationTab','Displaying\x20ticket\x20to\x20agent...','user:%s','trigger:desk:display','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','trigger:browser:url','https://xcally.desk.com/web/agent/case/%d','error','Desk\x20account\x20type\x20not\x20recognized','catch','outbound','none','[OUTBOUND]','destcalleridname','newTab','%s/agent/tickets/%s','abandoned','[ABANDONED]','lodash','request-promise','md5','util','ioredis','../../../../config/logger','redis','defaults'];(function(_0x46bb73,_0x44f394){var _0x44f271=function(_0x2a96bc){while(--_0x2a96bc){_0x46bb73['push'](_0x46bb73['shift']());}};_0x44f271(++_0x44f394);}(_0x2ced,0x133));var _0xd2ce=function(_0x4bb794,_0x1e6e9d){_0x4bb794=_0x4bb794-0x0;var _0xc03b38=_0x2ced[_0x4bb794];return _0xc03b38;};'use strict';var _=require(_0xd2ce('0x0'));var rp=require(_0xd2ce('0x1'));var md5=require(_0xd2ce('0x2'));var util=require(_0xd2ce('0x3'));var Redis=require(_0xd2ce('0x4'));var intUtil=require('./util');var config=require('../../../../config/environment');var logger=require(_0xd2ce('0x5'))('desk');config[_0xd2ce('0x6')]=_[_0xd2ce('0x7')](config[_0xd2ce('0x6')],{'host':_0xd2ce('0x8'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config['redis']));function emit(_0x191103,_0x257d51,_0x2cf45e){io['to'](_0x191103)[_0xd2ce('0x9')](_0x257d51,_0x2cf45e);}var headers={'Content-Type':_0xd2ce('0xa'),'Accept':_0xd2ce('0xa')};function getUser(_0x478a60,_0x427b92){if(intUtil['validateEmail'](_0x427b92)===!![]){logger[_0xd2ce('0xb')](_0xd2ce('0xc'),_0x427b92);return request({'uri':util[_0xd2ce('0xd')]('%s/users',_0x478a60['remoteUri'])})['then'](function(_0x2bc5c7){if(_0x2bc5c7[_0xd2ce('0xe')]){return _['find'](_0x2bc5c7['_embedded'][_0xd2ce('0xf')],{'email':_0x427b92});}})[_0xd2ce('0x10')](function(_0x901bca){if(_0x901bca){logger[_0xd2ce('0xb')](_0xd2ce('0x11'));return _0x901bca;}else{logger[_0xd2ce('0xb')]('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');return request({'uri':util['format'](_0xd2ce('0x12'),_0x478a60[_0xd2ce('0x13')])});}});}else{logger[_0xd2ce('0xb')](_0xd2ce('0x14'));return request({'uri':util['format'](_0xd2ce('0x12'),_0x478a60[_0xd2ce('0x13')])});}}function request(_0x2bb43b){logger[_0xd2ce('0x15')]('[HTTP]',JSON[_0xd2ce('0x16')](_0x2bb43b));return rp(_['defaults'](_0x2bb43b,{'method':_0xd2ce('0x17'),'headers':headers,'json':!![]}));}function getAuth(_0x1afb85){var _0x43dbb8;if(_0x1afb85[_0xd2ce('0x18')]==='basic'){_0x43dbb8=util[_0xd2ce('0xd')](_0xd2ce('0x19'),_0x1afb85['username'],_0x1afb85[_0xd2ce('0x1a')]);}else if(_0x1afb85['authType']===_0xd2ce('0x1b')){_0x43dbb8=util[_0xd2ce('0xd')](_0xd2ce('0x1c'),_0x1afb85[_0xd2ce('0x1d')],_0x1afb85[_0xd2ce('0x1b')]);}return util['format'](_0xd2ce('0x1e'),new Buffer(_0x43dbb8)[_0xd2ce('0x1f')](_0xd2ce('0x20')));}function getEndUser(_0x194e3a,_0x5f4fdc,_0x2a3f8a){logger['info']('Searching\x20enduser\x20with\x20phone',_0x2a3f8a);return request({'uri':util[_0xd2ce('0xd')](_0xd2ce('0x21'),_0x194e3a['remoteUri']),'qs':{'phone':_0x2a3f8a}})[_0xd2ce('0x10')](function(_0x377fb2){if(_0x377fb2[_0xd2ce('0xe')]){logger[_0xd2ce('0xb')](_0xd2ce('0x22'));return _0x377fb2[_0xd2ce('0x23')][_0xd2ce('0xf')][0x0];}else{logger[_0xd2ce('0xb')](_0xd2ce('0x24'));return request({'uri':util['format'](_0xd2ce('0x25'),_0x194e3a[_0xd2ce('0x13')]),'method':_0xd2ce('0x26'),'body':{'first_name':_0x5f4fdc,'title':_0x5f4fdc,'phone_numbers':[{'type':_0xd2ce('0x27'),'value':_0x2a3f8a}]}});}});}function getTags(_0x170462,_0x51382b){var _0x36a515=[];if(_0x170462&&_0x170462[_0xd2ce('0x28')]>0x0){_0x36a515=_[_0xd2ce('0x29')](_0x170462,_0x51382b);}return _0x36a515;}function getCustomFields(_0x48b05e,_0x4afaff){var _0x25fde4=[];var _0x38a9b8={'custom_fields':{}};if(_0x48b05e[_0xd2ce('0x28')]>0x0){_[_0xd2ce('0x2a')](_0x48b05e,function(_0x1a5077){if(_0x1a5077['type']==='string'&&_0x1a5077[_0xd2ce('0x2b')]&&!_0x1a5077[_0xd2ce('0x2c')]){_0x38a9b8[_0x1a5077[_0xd2ce('0x2b')][_0xd2ce('0x2d')]()]=_0x1a5077[_0xd2ce('0x2e')][_0xd2ce('0x1f')]();}else if(_0x1a5077[_0xd2ce('0x2f')]===_0xd2ce('0x30')&&_0x1a5077['idField']&&_0x1a5077[_0xd2ce('0x2c')]){_0x38a9b8[_0xd2ce('0x31')][_0x1a5077['idField']]=_0x1a5077[_0xd2ce('0x2e')]['toString']();}else if(_0x1a5077['type']===_0xd2ce('0x32')&&_0x1a5077[_0xd2ce('0x33')]&&_0x1a5077[_0xd2ce('0x34')]&&_0x1a5077[_0xd2ce('0x2c')]){_0x4afaff[_0x1a5077[_0xd2ce('0x33')]]&&(_0x38a9b8[_0xd2ce('0x31')][_0x1a5077[_0xd2ce('0x34')]]=_0x4afaff[_0x1a5077[_0xd2ce('0x33')]][_0xd2ce('0x1f')]());}else if(_0x1a5077[_0xd2ce('0x2f')]===_0xd2ce('0x35')&&_0x1a5077[_0xd2ce('0x36')]&&_0x1a5077[_0xd2ce('0x36')][_0xd2ce('0x37')]&&_0x1a5077['idField']&&_0x1a5077['customField']){_0x4afaff[_0x1a5077[_0xd2ce('0x36')][_0xd2ce('0x37')][_0xd2ce('0x2d')]()]&&(_0x38a9b8[_0xd2ce('0x31')][_0x1a5077[_0xd2ce('0x34')]]=_0x4afaff[_0x1a5077['Variable'][_0xd2ce('0x37')][_0xd2ce('0x2d')]()]['toString']());}});}if(_0x38a9b8[_0xd2ce('0x38')]){_0x38a9b8[_0xd2ce('0x38')]=Number(_0x38a9b8[_0xd2ce('0x38')]);}return _0x38a9b8;}function getTicket(_0xa879fe,_0x1747d2,_0x3ce7f7,_0x4b1552,_0x3b7e7a){logger['info'](_0xd2ce('0x39'));var _0x4d007f=_[_0xd2ce('0x3a')]({'subject':intUtil['getString'](_0x4b1552[_0xd2ce('0x3b')],_0x3b7e7a,'\x20'),'type':_0xd2ce('0x3c'),'status':'new','labels':[_0xd2ce('0x3d'),_0xd2ce('0x3e'),_0xd2ce('0x3f')][_0xd2ce('0x40')](getTags(_0x4b1552['Tags'],_0xd2ce('0x37'))),'description':intUtil['getString'](_0x4b1552['Descriptions'],_0x3b7e7a,'\x0a'),'_links':{'assigned_user':{'href':util[_0xd2ce('0xd')](_0xd2ce('0x41'),_0x1747d2['id']),'class':'user'}},'message':{'direction':'in','body':intUtil[_0xd2ce('0x42')](_0x4b1552[_0xd2ce('0x43')],_0x3b7e7a,'\x0a')}},getCustomFields(_0x4b1552[_0xd2ce('0x44')],_0x3b7e7a));return request({'uri':util[_0xd2ce('0xd')](_0xd2ce('0x45'),_0xa879fe[_0xd2ce('0x13')],_0x3ce7f7['id']),'method':_0xd2ce('0x26'),'body':_0x4d007f});}exports[_0xd2ce('0x46')]=function(_0x395d77,_0x427315,_0x50078c,_0x170b3e,_0x543070,_0x4aa289){var _0x2997ac,_0x479a83;_0x4aa289[_0xd2ce('0x13')]=intUtil[_0xd2ce('0x47')](_0x4aa289[_0xd2ce('0x13')]);_0x4aa289[_0xd2ce('0x48')]=intUtil['stripTrailingSlash'](_0x4aa289[_0xd2ce('0x48')]);if(_0x543070&&_0x543070[_0xd2ce('0x49')]){logger[_0xd2ce('0xb')](_0xd2ce('0x4a'),_0xd2ce('0x4b'));_0x427315[_0xd2ce('0x4c')]=util[_0xd2ce('0xd')](_0xd2ce('0x4d'),_0x4aa289[_0xd2ce('0x48')],_0x427315['uniqueid'],md5(_0x427315[_0xd2ce('0x4e')]));}else{_0x427315[_0xd2ce('0x4c')]='';}headers[_0xd2ce('0x4f')]=getAuth(_0x4aa289);return getUser(_0x4aa289,_0x170b3e[_0xd2ce('0x50')])['then'](function(_0x18afff){_0x2997ac=_0x18afff;logger[_0xd2ce('0xb')](_0xd2ce('0x51'),_0x2997ac['id']);return getEndUser(_0x4aa289,_0x427315[_0xd2ce('0x52')],_0x427315[_0xd2ce('0x53')]);})[_0xd2ce('0x10')](function(_0x21a32b){_0x479a83=_0x21a32b;logger[_0xd2ce('0xb')](_0xd2ce('0x54'),_0x479a83['id']);return getTicket(_0x4aa289,_0x2997ac,_0x479a83,_0x50078c,_0x427315);})[_0xd2ce('0x10')](function(_0x5d70f5){if(_0x5d70f5){logger[_0xd2ce('0xb')](_0xd2ce('0x55'));logger['info'](_0xd2ce('0x56'),_0x5d70f5['id']);switch(_0x4aa289[_0xd2ce('0x2f')]){case _0xd2ce('0x57'):logger['info'](_0xd2ce('0x4a'),_0xd2ce('0x58'));emit(util[_0xd2ce('0xd')](_0xd2ce('0x59'),_0x170b3e[_0xd2ce('0x37')]),_0xd2ce('0x5a'),{'recordId':_0x5d70f5['id']});break;case'newTab':logger[_0xd2ce('0xb')](_0xd2ce('0x4a'),_0xd2ce('0x5b'));emit(util[_0xd2ce('0xd')](_0xd2ce('0x59'),_0x170b3e['name']),_0xd2ce('0x5c'),{'uri':util['format'](_0xd2ce('0x5d'),_0x5d70f5['id'])});break;default:logger[_0xd2ce('0x5e')]('[QUEUE]',_0xd2ce('0x5f'));}}})[_0xd2ce('0x60')](function(_0x4b0510){console[_0xd2ce('0x5e')](_0x4b0510);logger[_0xd2ce('0x5e')](_0xd2ce('0x4a'),JSON['stringify'](_0x4b0510));});};exports[_0xd2ce('0x61')]=function(_0x8efb29,_0x446c72,_0x3d6b36,_0x45179e,_0x4c822a,_0x4de6a6,_0x13bcad){var _0x5eeea1,_0x582800;_0x4de6a6[_0xd2ce('0x13')]=intUtil[_0xd2ce('0x47')](_0x4de6a6['remoteUri']);_0x4de6a6['serverUrl']=intUtil[_0xd2ce('0x47')](_0x4de6a6[_0xd2ce('0x48')]);if(_0x13bcad&&_0x13bcad['recordingFormat']&&_0x13bcad['recordingFormat']!==_0xd2ce('0x62')){logger[_0xd2ce('0xb')](_0xd2ce('0x63'),_0xd2ce('0x4b'));_0x446c72[_0xd2ce('0x4c')]=util['format'](_0xd2ce('0x4d'),_0x4de6a6[_0xd2ce('0x48')],_0x446c72[_0xd2ce('0x4e')],md5(_0x446c72[_0xd2ce('0x4e')]));}headers[_0xd2ce('0x4f')]=getAuth(_0x4de6a6);return getUser(_0x4de6a6,_0x45179e[_0xd2ce('0x50')])['then'](function(_0x283ba7){_0x5eeea1=_0x283ba7;logger[_0xd2ce('0xb')](_0xd2ce('0x51'),_0x5eeea1['id']);return getEndUser(_0x4de6a6,_0x446c72[_0xd2ce('0x64')],_0x446c72['destcalleridnum']);})['then'](function(_0x511644){_0x582800=_0x511644;logger['info'](_0xd2ce('0x54'),_0x582800['id']);return getTicket(_0x4de6a6,_0x5eeea1,_0x582800,_0x3d6b36,_0x446c72);})[_0xd2ce('0x10')](function(_0x1adb6c){if(_0x1adb6c){logger[_0xd2ce('0xb')]('Ticket\x20created!');logger['info'](_0xd2ce('0x56'),_0x1adb6c['id']);switch(_0x4de6a6['type']){case _0xd2ce('0x57'):logger[_0xd2ce('0xb')](_0xd2ce('0x4a'),_0xd2ce('0x58'));emit(util[_0xd2ce('0xd')](_0xd2ce('0x59'),_0x45179e[_0xd2ce('0x37')]),_0xd2ce('0x5a'),{'recordId':_0x1adb6c['id']});break;case _0xd2ce('0x65'):logger[_0xd2ce('0xb')](_0xd2ce('0x4a'),_0xd2ce('0x5b'));emit(util[_0xd2ce('0xd')]('user:%s',_0x45179e['name']),_0xd2ce('0x5c'),{'uri':util[_0xd2ce('0xd')](_0xd2ce('0x66'),_0x4de6a6[_0xd2ce('0x13')],_0x1adb6c['id'])});break;default:logger[_0xd2ce('0x5e')](_0xd2ce('0x63'),_0xd2ce('0x5f'));}}})[_0xd2ce('0x60')](function(_0x563c3d){logger['error'](_0xd2ce('0x63'),JSON[_0xd2ce('0x16')](_0x563c3d));});};exports[_0xd2ce('0x67')]=function(_0x25b9aa,_0x4935e7,_0x21fc66,_0x34ff5a,_0x16fb90,_0x2a143d){var _0x5d20cf,_0x2544c2;_0x2a143d[_0xd2ce('0x13')]=intUtil[_0xd2ce('0x47')](_0x2a143d[_0xd2ce('0x13')]);_0x2a143d[_0xd2ce('0x48')]=intUtil[_0xd2ce('0x47')](_0x2a143d['serverUrl']);if(_0x16fb90&&_0x16fb90['monitor_format']){logger['info']('[ABANDONED]',_0xd2ce('0x4b'));_0x4935e7[_0xd2ce('0x4c')]=util[_0xd2ce('0xd')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x2a143d[_0xd2ce('0x48')],_0x4935e7['uniqueid'],md5(_0x4935e7['uniqueid']));}else{_0x4935e7[_0xd2ce('0x4c')]='';}headers[_0xd2ce('0x4f')]=getAuth(_0x2a143d);return getUser(_0x2a143d)[_0xd2ce('0x10')](function(_0x7645c0){_0x5d20cf=_0x7645c0;logger[_0xd2ce('0xb')](_0xd2ce('0x51'),_0x5d20cf['id']);return getEndUser(_0x2a143d,_0x4935e7[_0xd2ce('0x52')],_0x4935e7[_0xd2ce('0x53')]);})[_0xd2ce('0x10')](function(_0x3d2bf9){_0x2544c2=_0x3d2bf9;logger['info']('The\x20Enduser\x20ID\x20is:',_0x2544c2['id']);return getTicket(_0x2a143d,_0x5d20cf,_0x2544c2,_0x21fc66,_0x4935e7);})[_0xd2ce('0x10')](function(_0x2789fb){if(_0x2789fb){logger[_0xd2ce('0xb')]('Ticket\x20created!');logger[_0xd2ce('0xb')](_0xd2ce('0x56'),_0x2789fb['id']);}})[_0xd2ce('0x60')](function(_0x55ef21){logger[_0xd2ce('0x5e')](_0xd2ce('0x68'),JSON['stringify'](_0x55ef21));});};