Built motion from commit ded33658aad58fc8de845a8299a9db59a88cc353. Version 3.0.0...
[motion-next.git] / server / services / ami / trigger / integration / desk.js
1 const a1135_0x32ce=['priority','application/json','basic','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','stripTrailingSlash','%s/customers','destcalleridnum','Ticket\x20created!','%s/token:%s','./util','length','request-promise-native','nameField','validateEmail','[HTTP]','toString','getString','[QUEUE]','calleridname','destcalleridname','../../../../rediscache','idField','authType','base64','Desk\x20account\x20type\x20not\x20recognized','calleridnum','CTI','catch','integrationTab','info','https://xcally.desk.com/web/agent/case/%d','username','stringify','newTab','Variable','toUpperCase','name','variableName','util','emit','md5','Basic\x20%s','variable','Subjects','recordingFormat','Enduser\x20found!','The\x20Ticket\x20ID\x20is:','debug','[OUTBOUND]','string','Descriptions','%s/users/me','remoteUri','serverUrl','desk','The\x20User\x20ID\x20is:','Searching\x20enduser\x20with\x20phone','work','user','Displaying\x20ticket\x20to\x20agent...','total_entries','%s/agent/tickets/%s','Recording\x20is\x20enabled!','token','error','GET','trigger:desk:display','phone','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','customField','type','then','monitor_format','Tags','xCALLY','/api/v2/users/%d','The\x20Enduser\x20ID\x20is:','from','trigger:browser:url','format','Fields','new','email','unmanaged','find','_embedded','lodash','entries','lastevent','../../../../config/logger','password','Creating\x20new\x20ticket...','outbound','POST','toLowerCase','uniqueid','%s/customers/%d/cases','customVariable','content','concat','Authorization','recordingURL','custom_fields','user:%s'];(function(_0x45e398,_0x32ce84){const _0x4b05a3=function(_0x5e6008){while(--_0x5e6008){_0x45e398['push'](_0x45e398['shift']());}};_0x4b05a3(++_0x32ce84);}(a1135_0x32ce,0xb9));const a1135_0x4b05=function(_0x45e398,_0x32ce84){_0x45e398=_0x45e398-0x0;let _0x4b05a3=a1135_0x32ce[_0x45e398];return _0x4b05a3;};const _0x1efc65=a1135_0x4b05;'use strict';const _=require(_0x1efc65('0x5')),rp=require(_0x1efc65('0x22')),md5=require(_0x1efc65('0x3f')),util=require(_0x1efc65('0x3d')),intUtil=require(_0x1efc65('0x20')),logger=require(_0x1efc65('0x8'))(_0x1efc65('0x4d')),{getSocket}=require(_0x1efc65('0x2b')),io=getSocket();function emit(_0x5e6008,_0x25d02f,_0x1a76fd){const _0x88d7cd=_0x1efc65;io['to'](_0x5e6008)[_0x88d7cd('0x3e')](_0x25d02f,_0x1a76fd);}const headers={'Content-Type':_0x1efc65('0x18'),'Accept':_0x1efc65('0x18')};function getUser(_0xde193c,_0x34523e){const _0x535d77=_0x1efc65;return intUtil[_0x535d77('0x24')](_0x34523e)===!![]?(logger[_0x535d77('0x34')]('Getting\x20agent\x20with\x20email',_0x34523e),request({'uri':util[_0x535d77('0x66')]('%s/users',_0xde193c[_0x535d77('0x4b')])})[_0x535d77('0x5e')](function(_0x5d0f27){const _0x7680c9=_0x535d77;if(_0x5d0f27[_0x7680c9('0x53')])return _[_0x7680c9('0x3')](_0x5d0f27[_0x7680c9('0x4')][_0x7680c9('0x6')],{'email':_0x34523e});})[_0x535d77('0x5e')](function(_0x5e8507){const _0x5a8661=_0x535d77;return _0x5e8507?(logger['info']('Agent\x20found!'),_0x5e8507):(logger[_0x5a8661('0x34')](_0x5a8661('0x1a')),request({'uri':util[_0x5a8661('0x66')](_0x5a8661('0x4a'),_0xde193c['remoteUri'])}));})):(logger['info']('Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!'),request({'uri':util[_0x535d77('0x66')](_0x535d77('0x4a'),_0xde193c[_0x535d77('0x4b')])}));}function request(_0xdc64ed){const _0x33ba82=_0x1efc65;return logger[_0x33ba82('0x46')](_0x33ba82('0x25'),JSON['stringify'](_0xdc64ed)),rp(_['defaults'](_0xdc64ed,{'method':_0x33ba82('0x58'),'headers':headers,'json':!![]}));}function getAuth(_0x2dff4a){const _0x435414=_0x1efc65;let _0x2a6523;if(_0x2dff4a[_0x435414('0x2d')]===_0x435414('0x19'))_0x2a6523=util[_0x435414('0x66')]('%s:%s',_0x2dff4a[_0x435414('0x36')],_0x2dff4a[_0x435414('0x9')]);else _0x2dff4a['authType']===_0x435414('0x56')&&(_0x2a6523=util['format'](_0x435414('0x1f'),_0x2dff4a[_0x435414('0x36')],_0x2dff4a[_0x435414('0x56')]));return util[_0x435414('0x66')](_0x435414('0x40'),Buffer[_0x435414('0x64')](_0x2a6523)[_0x435414('0x26')](_0x435414('0x2e')));}function getEndUser(_0xca8d7d,_0x2093d1,_0xb76306){const _0x3ec6d3=_0x1efc65;return logger[_0x3ec6d3('0x34')](_0x3ec6d3('0x4f'),_0xb76306),request({'uri':util[_0x3ec6d3('0x66')]('%s/customers/search',_0xca8d7d[_0x3ec6d3('0x4b')]),'qs':{'phone':_0xb76306}})[_0x3ec6d3('0x5e')](function(_0x484efd){const _0x35c4e8=_0x3ec6d3;return _0x484efd['total_entries']?(logger[_0x35c4e8('0x34')](_0x35c4e8('0x44')),_0x484efd[_0x35c4e8('0x4')][_0x35c4e8('0x6')][0x0]):(logger['info']('Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!'),request({'uri':util[_0x35c4e8('0x66')](_0x35c4e8('0x1c'),_0xca8d7d['remoteUri']),'method':_0x35c4e8('0xc'),'body':{'first_name':_0x2093d1,'title':_0x2093d1,'phone_numbers':[{'type':_0x35c4e8('0x50'),'value':_0xb76306}]}}));});}function getTags(_0x39a620,_0x5387df){let _0x26f20a=[];return _0x39a620&&_0x39a620['length']>0x0&&(_0x26f20a=_['map'](_0x39a620,_0x5387df)),_0x26f20a;}function getCustomFields(_0x29a0c6,_0x18bdf6){const _0x1b9ae8=_0x1efc65,_0x59b47a={'custom_fields':{}};return _0x29a0c6[_0x1b9ae8('0x21')]>0x0&&_['forEach'](_0x29a0c6,function(_0x7b83db){const _0xef64ac=_0x1b9ae8;if(_0x7b83db['type']===_0xef64ac('0x48')&&_0x7b83db[_0xef64ac('0x23')]&&!_0x7b83db['customField'])_0x59b47a[_0x7b83db[_0xef64ac('0x23')][_0xef64ac('0xd')]()]=_0x7b83db[_0xef64ac('0x11')][_0xef64ac('0x26')]();else{if(_0x7b83db[_0xef64ac('0x5d')]===_0xef64ac('0x48')&&_0x7b83db[_0xef64ac('0x2c')]&&_0x7b83db[_0xef64ac('0x5c')])_0x59b47a[_0xef64ac('0x15')][_0x7b83db['idField']]=_0x7b83db['content'][_0xef64ac('0x26')]();else{if(_0x7b83db[_0xef64ac('0x5d')]===_0xef64ac('0x41')&&_0x7b83db[_0xef64ac('0x3c')]&&_0x7b83db['idField']&&_0x7b83db[_0xef64ac('0x5c')])_0x18bdf6[_0x7b83db[_0xef64ac('0x3c')]]&&(_0x59b47a[_0xef64ac('0x15')][_0x7b83db[_0xef64ac('0x2c')]]=_0x18bdf6[_0x7b83db['variableName']][_0xef64ac('0x26')]());else _0x7b83db[_0xef64ac('0x5d')]===_0xef64ac('0x10')&&_0x7b83db[_0xef64ac('0x39')]&&_0x7b83db[_0xef64ac('0x39')][_0xef64ac('0x3b')]&&_0x7b83db[_0xef64ac('0x2c')]&&_0x7b83db[_0xef64ac('0x5c')]&&(_0x18bdf6[_0x7b83db[_0xef64ac('0x39')][_0xef64ac('0x3b')][_0xef64ac('0xd')]()]&&(_0x59b47a[_0xef64ac('0x15')][_0x7b83db['idField']]=_0x18bdf6[_0x7b83db[_0xef64ac('0x39')][_0xef64ac('0x3b')][_0xef64ac('0xd')]()][_0xef64ac('0x26')]()));}}}),_0x59b47a[_0x1b9ae8('0x17')]&&(_0x59b47a[_0x1b9ae8('0x17')]=Number(_0x59b47a[_0x1b9ae8('0x17')])),_0x59b47a;}function getTicket(_0x3e633e,_0x538115,_0x4bd2c3,_0x1bea07,_0x1259b6){const _0x246861=_0x1efc65;logger[_0x246861('0x34')](_0x246861('0xa'));const _0x6be76d=_['merge']({'subject':intUtil[_0x246861('0x27')](_0x1bea07[_0x246861('0x42')],_0x1259b6,'\x20'),'type':_0x246861('0x5a'),'status':_0x246861('0x0'),'labels':[_0x246861('0x61'),'Motion',_0x246861('0x31')][_0x246861('0x12')](getTags(_0x1bea07[_0x246861('0x60')],_0x246861('0x3b'))),'description':intUtil[_0x246861('0x27')](_0x1bea07[_0x246861('0x49')],_0x1259b6,'\x0a'),'_links':{'assigned_user':{'href':util[_0x246861('0x66')](_0x246861('0x62'),_0x538115['id']),'class':_0x246861('0x51')}},'message':{'direction':'in','body':intUtil['getString'](_0x1bea07[_0x246861('0x49')],_0x1259b6,'\x0a')}},getCustomFields(_0x1bea07[_0x246861('0x67')],_0x1259b6));return request({'uri':util[_0x246861('0x66')](_0x246861('0xf'),_0x3e633e[_0x246861('0x4b')],_0x4bd2c3['id']),'method':_0x246861('0xc'),'body':_0x6be76d});}exports['queue']=function(_0x43b6bb,_0x14e818,_0x53ebf4,_0x5122cf,_0x41fb01,_0x46179e){const _0x32dc29=_0x1efc65;let _0xdc0fc7,_0x2180e8;return _0x46179e['remoteUri']=intUtil['stripTrailingSlash'](_0x46179e[_0x32dc29('0x4b')]),_0x46179e[_0x32dc29('0x4c')]=intUtil[_0x32dc29('0x1b')](_0x46179e[_0x32dc29('0x4c')]),_0x41fb01&&_0x41fb01[_0x32dc29('0x5f')]?(logger[_0x32dc29('0x34')](_0x32dc29('0x28'),_0x32dc29('0x55')),_0x14e818['recordingURL']=util['format'](_0x32dc29('0x5b'),_0x46179e[_0x32dc29('0x4c')],_0x14e818[_0x32dc29('0xe')],md5(_0x14e818[_0x32dc29('0xe')]))):_0x14e818[_0x32dc29('0x14')]='',headers[_0x32dc29('0x13')]=getAuth(_0x46179e),getUser(_0x46179e,_0x5122cf[_0x32dc29('0x1')])['then'](function(_0x4f0c5e){const _0x1d256c=_0x32dc29;return _0xdc0fc7=_0x4f0c5e,logger[_0x1d256c('0x34')](_0x1d256c('0x4e'),_0xdc0fc7['id']),getEndUser(_0x46179e,_0x14e818[_0x1d256c('0x29')],_0x14e818[_0x1d256c('0x30')]);})[_0x32dc29('0x5e')](function(_0x13cfc8){const _0xec7f9a=_0x32dc29;return _0x2180e8=_0x13cfc8,logger[_0xec7f9a('0x34')]('The\x20Enduser\x20ID\x20is:',_0x2180e8['id']),getTicket(_0x46179e,_0xdc0fc7,_0x2180e8,_0x53ebf4,_0x14e818);})['then'](function(_0x4fbff0){const _0x8eabd4=_0x32dc29;if(_0x4fbff0){logger[_0x8eabd4('0x34')](_0x8eabd4('0x1e')),logger['info']('The\x20Ticket\x20ID\x20is:',_0x4fbff0['id']);switch(_0x46179e[_0x8eabd4('0x5d')]){case _0x8eabd4('0x33'):logger[_0x8eabd4('0x34')]('[QUEUE]','Displaying\x20ticket\x20to\x20agent...'),emit(util[_0x8eabd4('0x66')](_0x8eabd4('0x16'),_0x5122cf[_0x8eabd4('0x3b')]),_0x8eabd4('0x59'),{'recordId':_0x4fbff0['id']});break;case _0x8eabd4('0x38'):logger[_0x8eabd4('0x34')](_0x8eabd4('0x28'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...'),emit(util[_0x8eabd4('0x66')](_0x8eabd4('0x16'),_0x5122cf[_0x8eabd4('0x3b')]),_0x8eabd4('0x65'),{'uri':util[_0x8eabd4('0x66')](_0x8eabd4('0x35'),_0x4fbff0['id'])});break;default:logger[_0x8eabd4('0x57')](_0x8eabd4('0x28'),_0x8eabd4('0x2f'));}}})[_0x32dc29('0x32')](function(_0x5bcfc5){const _0x516f02=_0x32dc29;console['error'](_0x5bcfc5),logger[_0x516f02('0x57')](_0x516f02('0x28'),JSON[_0x516f02('0x37')](_0x5bcfc5));});},exports[_0x1efc65('0xb')]=function(_0x374ee5,_0x42c235,_0x78a909,_0x24957b,_0x242b7c,_0x213d0f,_0xdcfc75){const _0x2ed0e5=_0x1efc65;let _0x3ac481,_0x1c838c;return _0x213d0f[_0x2ed0e5('0x4b')]=intUtil[_0x2ed0e5('0x1b')](_0x213d0f[_0x2ed0e5('0x4b')]),_0x213d0f[_0x2ed0e5('0x4c')]=intUtil[_0x2ed0e5('0x1b')](_0x213d0f['serverUrl']),_0xdcfc75&&_0xdcfc75[_0x2ed0e5('0x43')]&&_0xdcfc75[_0x2ed0e5('0x43')]!=='none'&&(logger[_0x2ed0e5('0x34')](_0x2ed0e5('0x47'),_0x2ed0e5('0x55')),_0x42c235[_0x2ed0e5('0x14')]=util[_0x2ed0e5('0x66')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x213d0f[_0x2ed0e5('0x4c')],_0x42c235[_0x2ed0e5('0xe')],md5(_0x42c235[_0x2ed0e5('0xe')]))),headers[_0x2ed0e5('0x13')]=getAuth(_0x213d0f),getUser(_0x213d0f,_0x24957b[_0x2ed0e5('0x1')])[_0x2ed0e5('0x5e')](function(_0x41998e){const _0x46838b=_0x2ed0e5;return _0x3ac481=_0x41998e,logger[_0x46838b('0x34')](_0x46838b('0x4e'),_0x3ac481['id']),getEndUser(_0x213d0f,_0x42c235[_0x46838b('0x2a')],_0x42c235[_0x46838b('0x1d')]);})[_0x2ed0e5('0x5e')](function(_0x5e32ac){const _0x4b5917=_0x2ed0e5;return _0x1c838c=_0x5e32ac,logger[_0x4b5917('0x34')](_0x4b5917('0x63'),_0x1c838c['id']),getTicket(_0x213d0f,_0x3ac481,_0x1c838c,_0x78a909,_0x42c235);})['then'](function(_0x288c02){const _0x4faf2d=_0x2ed0e5;if(_0x288c02){logger[_0x4faf2d('0x34')](_0x4faf2d('0x1e')),logger[_0x4faf2d('0x34')](_0x4faf2d('0x45'),_0x288c02['id']);switch(_0x213d0f[_0x4faf2d('0x5d')]){case _0x4faf2d('0x33'):logger[_0x4faf2d('0x34')](_0x4faf2d('0x28'),_0x4faf2d('0x52')),emit(util[_0x4faf2d('0x66')](_0x4faf2d('0x16'),_0x24957b['name']),'trigger:desk:display',{'recordId':_0x288c02['id']});break;case _0x4faf2d('0x38'):logger['info'](_0x4faf2d('0x28'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...'),emit(util[_0x4faf2d('0x66')](_0x4faf2d('0x16'),_0x24957b[_0x4faf2d('0x3b')]),_0x4faf2d('0x65'),{'uri':util['format'](_0x4faf2d('0x54'),_0x213d0f[_0x4faf2d('0x4b')],_0x288c02['id'])});break;default:logger[_0x4faf2d('0x57')](_0x4faf2d('0x47'),_0x4faf2d('0x2f'));}}})[_0x2ed0e5('0x32')](function(_0x4b4f7a){const _0x1ec884=_0x2ed0e5;logger[_0x1ec884('0x57')](_0x1ec884('0x47'),JSON[_0x1ec884('0x37')](_0x4b4f7a));});},exports[_0x1efc65('0x2')]=function(_0x1639a3,_0x2f8c6f,_0x1043a1,_0x1c77ff,_0x2d0ceb,_0x376f2d){const _0x3f51df=_0x1efc65;let _0x59e576,_0x3e64dd;return _0x376f2d[_0x3f51df('0x4b')]=intUtil[_0x3f51df('0x1b')](_0x376f2d[_0x3f51df('0x4b')]),_0x376f2d[_0x3f51df('0x4c')]=intUtil['stripTrailingSlash'](_0x376f2d[_0x3f51df('0x4c')]),_0x2d0ceb&&_0x2d0ceb[_0x3f51df('0x5f')]?(logger[_0x3f51df('0x34')]('['+_0x2f8c6f['lastevent'][_0x3f51df('0x3a')]()+']','Recording\x20is\x20enabled!'),_0x2f8c6f[_0x3f51df('0x14')]=util[_0x3f51df('0x66')](_0x3f51df('0x5b'),_0x376f2d['serverUrl'],_0x2f8c6f['uniqueid'],md5(_0x2f8c6f[_0x3f51df('0xe')]))):_0x2f8c6f['recordingURL']='',headers[_0x3f51df('0x13')]=getAuth(_0x376f2d),getUser(_0x376f2d)[_0x3f51df('0x5e')](function(_0x2b93ff){const _0x2b6e99=_0x3f51df;return _0x59e576=_0x2b93ff,logger[_0x2b6e99('0x34')](_0x2b6e99('0x4e'),_0x59e576['id']),getEndUser(_0x376f2d,_0x2f8c6f[_0x2b6e99('0x29')],_0x2f8c6f['calleridnum']);})[_0x3f51df('0x5e')](function(_0x36ade6){const _0x1c6ff1=_0x3f51df;return _0x3e64dd=_0x36ade6,logger[_0x1c6ff1('0x34')](_0x1c6ff1('0x63'),_0x3e64dd['id']),getTicket(_0x376f2d,_0x59e576,_0x3e64dd,_0x1043a1,_0x2f8c6f);})[_0x3f51df('0x5e')](function(_0x5b7ad0){const _0xb95fd9=_0x3f51df;_0x5b7ad0&&(logger[_0xb95fd9('0x34')](_0xb95fd9('0x1e')),logger[_0xb95fd9('0x34')](_0xb95fd9('0x45'),_0x5b7ad0['id']));})['catch'](function(_0x16affb){const _0x2a35f5=_0x3f51df;logger[_0x2a35f5('0x57')]('['+_0x2f8c6f[_0x2a35f5('0x7')][_0x2a35f5('0x3a')]()+']',JSON['stringify'](_0x16affb));});};