Built motion from commit aab208717af8d15f4fa224cdb6a202877019af9b. Version 3.0.0...
[motion-next.git] / server / services / ami / trigger / integration / desk.js
1 const a1136_0x1f04=['password','application/json','nameField','emit','%s/agent/tickets/%s','idField','error','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','token','desk','%s/customers/%d/cases','base64','Motion','Creating\x20new\x20ticket...','from','username','../../../../config/logger','length','serverUrl','variableName','%s/users','stripTrailingSlash','Agent\x20found!','The\x20Ticket\x20ID\x20is:','stringify','The\x20Enduser\x20ID\x20is:','none','calleridname','uniqueid','getString','customVariable','trigger:browser:url','CTI','calleridnum','merge','new','email','forEach','https://xcally.desk.com/web/agent/case/%d','recordingFormat','destcalleridnum','catch','xCALLY','info','priority','then','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','user','content','./util','integrationTab','variable','[HTTP]','work','Displaying\x20ticket\x20to\x20agent...','lastevent','debug','customField','POST','newTab','entries','%s/customers/search','unmanaged','queue','[OUTBOUND]','trigger:desk:display','_embedded','concat','toString','toUpperCase','Descriptions','Authorization','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','Recording\x20is\x20enabled!','destcalleridname','Fields','string','basic','The\x20User\x20ID\x20is:','custom_fields','Searching\x20enduser\x20with\x20phone','authType','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','total_entries','lodash','monitor_format','recordingURL','%s/token:%s','/api/v2/users/%d','[QUEUE]','Getting\x20agent\x20with\x20email','Basic\x20%s','GET','user:%s','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','Desk\x20account\x20type\x20not\x20recognized','request-promise-native','type','Tags','util','find','name','format','phone','Variable','remoteUri','Ticket\x20created!','map'];(function(_0x1862c4,_0x1f0475){const _0x1560d8=function(_0x150580){while(--_0x150580){_0x1862c4['push'](_0x1862c4['shift']());}};_0x1560d8(++_0x1f0475);}(a1136_0x1f04,0x88));const a1136_0x1560=function(_0x1862c4,_0x1f0475){_0x1862c4=_0x1862c4-0x0;let _0x1560d8=a1136_0x1f04[_0x1862c4];return _0x1560d8;};const _0x355e9e=a1136_0x1560;'use strict';const _=require(_0x355e9e('0x38')),rp=require(_0x355e9e('0x44')),md5=require('md5'),util=require(_0x355e9e('0x47')),intUtil=require(_0x355e9e('0x15')),logger=require(_0x355e9e('0x60'))(_0x355e9e('0x59')),{getSocket}=require('../../../../rediscache'),io=getSocket();function emit(_0x150580,_0x3ad00e,_0x5e2049){const _0x9d45f1=_0x355e9e;io['to'](_0x150580)[_0x9d45f1('0x53')](_0x3ad00e,_0x5e2049);}const headers={'Content-Type':'application/json','Accept':_0x355e9e('0x51')};function getUser(_0x2facdc,_0x27579c){const _0x546c2d=_0x355e9e;return intUtil['validateEmail'](_0x27579c)===!![]?(logger[_0x546c2d('0xf')](_0x546c2d('0x3e'),_0x27579c),request({'uri':util[_0x546c2d('0x4a')](_0x546c2d('0x64'),_0x2facdc[_0x546c2d('0x4d')])})[_0x546c2d('0x11')](function(_0x3c51d1){const _0x474839=_0x546c2d;if(_0x3c51d1[_0x474839('0x37')])return _[_0x474839('0x48')](_0x3c51d1[_0x474839('0x26')][_0x474839('0x20')],{'email':_0x27579c});})[_0x546c2d('0x11')](function(_0x308fc9){const _0x58ba2c=_0x546c2d;return _0x308fc9?(logger['info'](_0x58ba2c('0x66')),_0x308fc9):(logger[_0x58ba2c('0xf')](_0x58ba2c('0x12')),request({'uri':util[_0x58ba2c('0x4a')]('%s/users/me',_0x2facdc[_0x58ba2c('0x4d')])}));})):(logger[_0x546c2d('0xf')](_0x546c2d('0x57')),request({'uri':util[_0x546c2d('0x4a')]('%s/users/me',_0x2facdc[_0x546c2d('0x4d')])}));}function request(_0x172b78){const _0x3673e6=_0x355e9e;return logger[_0x3673e6('0x1c')](_0x3673e6('0x18'),JSON[_0x3673e6('0x68')](_0x172b78)),rp(_['defaults'](_0x172b78,{'method':_0x3673e6('0x40'),'headers':headers,'json':!![]}));}function getAuth(_0xeeb305){const _0x1bdfa8=_0x355e9e;let _0x34ef1d;if(_0xeeb305[_0x1bdfa8('0x35')]===_0x1bdfa8('0x31'))_0x34ef1d=util[_0x1bdfa8('0x4a')]('%s:%s',_0xeeb305[_0x1bdfa8('0x5f')],_0xeeb305[_0x1bdfa8('0x50')]);else _0xeeb305[_0x1bdfa8('0x35')]===_0x1bdfa8('0x58')&&(_0x34ef1d=util[_0x1bdfa8('0x4a')](_0x1bdfa8('0x3b'),_0xeeb305[_0x1bdfa8('0x5f')],_0xeeb305['token']));return util['format'](_0x1bdfa8('0x3f'),Buffer[_0x1bdfa8('0x5e')](_0x34ef1d)[_0x1bdfa8('0x28')](_0x1bdfa8('0x5b')));}function getEndUser(_0x5245fc,_0x15f7ef,_0x11f04e){const _0x49610e=_0x355e9e;return logger[_0x49610e('0xf')](_0x49610e('0x34'),_0x11f04e),request({'uri':util[_0x49610e('0x4a')](_0x49610e('0x21'),_0x5245fc[_0x49610e('0x4d')]),'qs':{'phone':_0x11f04e}})[_0x49610e('0x11')](function(_0x926c60){const _0x56d475=_0x49610e;return _0x926c60['total_entries']?(logger[_0x56d475('0xf')]('Enduser\x20found!'),_0x926c60[_0x56d475('0x26')][_0x56d475('0x20')][0x0]):(logger[_0x56d475('0xf')](_0x56d475('0x36')),request({'uri':util[_0x56d475('0x4a')]('%s/customers',_0x5245fc['remoteUri']),'method':'POST','body':{'first_name':_0x15f7ef,'title':_0x15f7ef,'phone_numbers':[{'type':_0x56d475('0x19'),'value':_0x11f04e}]}}));});}function getTags(_0x5371a1,_0x1a5ab5){const _0x5c52c6=_0x355e9e;let _0x231cad=[];return _0x5371a1&&_0x5371a1[_0x5c52c6('0x61')]>0x0&&(_0x231cad=_[_0x5c52c6('0x4f')](_0x5371a1,_0x1a5ab5)),_0x231cad;}function getCustomFields(_0x14c56c,_0x22b288){const _0x22d0bb=_0x355e9e,_0xfe2f95={'custom_fields':{}};return _0x14c56c['length']>0x0&&_[_0x22d0bb('0x9')](_0x14c56c,function(_0x573a35){const _0x41dfab=_0x22d0bb;if(_0x573a35['type']==='string'&&_0x573a35[_0x41dfab('0x52')]&&!_0x573a35[_0x41dfab('0x1d')])_0xfe2f95[_0x573a35['nameField']['toLowerCase']()]=_0x573a35[_0x41dfab('0x14')][_0x41dfab('0x28')]();else{if(_0x573a35[_0x41dfab('0x45')]===_0x41dfab('0x30')&&_0x573a35['idField']&&_0x573a35[_0x41dfab('0x1d')])_0xfe2f95[_0x41dfab('0x33')][_0x573a35['idField']]=_0x573a35['content'][_0x41dfab('0x28')]();else{if(_0x573a35[_0x41dfab('0x45')]===_0x41dfab('0x17')&&_0x573a35[_0x41dfab('0x63')]&&_0x573a35[_0x41dfab('0x55')]&&_0x573a35['customField'])_0x22b288[_0x573a35[_0x41dfab('0x63')]]&&(_0xfe2f95[_0x41dfab('0x33')][_0x573a35[_0x41dfab('0x55')]]=_0x22b288[_0x573a35[_0x41dfab('0x63')]][_0x41dfab('0x28')]());else _0x573a35[_0x41dfab('0x45')]===_0x41dfab('0x2')&&_0x573a35['Variable']&&_0x573a35[_0x41dfab('0x4c')][_0x41dfab('0x49')]&&_0x573a35[_0x41dfab('0x55')]&&_0x573a35[_0x41dfab('0x1d')]&&(_0x22b288[_0x573a35[_0x41dfab('0x4c')][_0x41dfab('0x49')]['toLowerCase']()]&&(_0xfe2f95['custom_fields'][_0x573a35[_0x41dfab('0x55')]]=_0x22b288[_0x573a35[_0x41dfab('0x4c')]['name']['toLowerCase']()]['toString']()));}}}),_0xfe2f95[_0x22d0bb('0x10')]&&(_0xfe2f95[_0x22d0bb('0x10')]=Number(_0xfe2f95[_0x22d0bb('0x10')])),_0xfe2f95;}function getTicket(_0x248d99,_0x3e7f22,_0x422379,_0x41a1c9,_0x406394){const _0x5318f9=_0x355e9e;logger[_0x5318f9('0xf')](_0x5318f9('0x5d'));const _0x2a7074=_[_0x5318f9('0x6')]({'subject':intUtil[_0x5318f9('0x1')](_0x41a1c9['Subjects'],_0x406394,'\x20'),'type':_0x5318f9('0x4b'),'status':_0x5318f9('0x7'),'labels':[_0x5318f9('0xe'),_0x5318f9('0x5c'),_0x5318f9('0x4')][_0x5318f9('0x27')](getTags(_0x41a1c9[_0x5318f9('0x46')],_0x5318f9('0x49'))),'description':intUtil[_0x5318f9('0x1')](_0x41a1c9[_0x5318f9('0x2a')],_0x406394,'\x0a'),'_links':{'assigned_user':{'href':util[_0x5318f9('0x4a')](_0x5318f9('0x3c'),_0x3e7f22['id']),'class':_0x5318f9('0x13')}},'message':{'direction':'in','body':intUtil[_0x5318f9('0x1')](_0x41a1c9[_0x5318f9('0x2a')],_0x406394,'\x0a')}},getCustomFields(_0x41a1c9[_0x5318f9('0x2f')],_0x406394));return request({'uri':util[_0x5318f9('0x4a')](_0x5318f9('0x5a'),_0x248d99[_0x5318f9('0x4d')],_0x422379['id']),'method':_0x5318f9('0x1e'),'body':_0x2a7074});}exports[_0x355e9e('0x23')]=function(_0x305a2b,_0xb44b9f,_0x483f8c,_0x4e15c8,_0x3c46dc,_0x8d8c25){const _0x495f7f=_0x355e9e;let _0x48f70d,_0x49e65d;return _0x8d8c25[_0x495f7f('0x4d')]=intUtil[_0x495f7f('0x65')](_0x8d8c25[_0x495f7f('0x4d')]),_0x8d8c25[_0x495f7f('0x62')]=intUtil[_0x495f7f('0x65')](_0x8d8c25[_0x495f7f('0x62')]),_0x3c46dc&&_0x3c46dc[_0x495f7f('0x39')]?(logger[_0x495f7f('0xf')](_0x495f7f('0x3d'),_0x495f7f('0x2d')),_0xb44b9f[_0x495f7f('0x3a')]=util['format'](_0x495f7f('0x2c'),_0x8d8c25[_0x495f7f('0x62')],_0xb44b9f[_0x495f7f('0x0')],md5(_0xb44b9f[_0x495f7f('0x0')]))):_0xb44b9f[_0x495f7f('0x3a')]='',headers[_0x495f7f('0x2b')]=getAuth(_0x8d8c25),getUser(_0x8d8c25,_0x4e15c8['email'])[_0x495f7f('0x11')](function(_0x3e0362){const _0x3089ec=_0x495f7f;return _0x48f70d=_0x3e0362,logger[_0x3089ec('0xf')](_0x3089ec('0x32'),_0x48f70d['id']),getEndUser(_0x8d8c25,_0xb44b9f[_0x3089ec('0x6b')],_0xb44b9f[_0x3089ec('0x5')]);})[_0x495f7f('0x11')](function(_0x143f24){const _0x518a78=_0x495f7f;return _0x49e65d=_0x143f24,logger[_0x518a78('0xf')](_0x518a78('0x69'),_0x49e65d['id']),getTicket(_0x8d8c25,_0x48f70d,_0x49e65d,_0x483f8c,_0xb44b9f);})[_0x495f7f('0x11')](function(_0x276a00){const _0x282e00=_0x495f7f;if(_0x276a00){logger['info'](_0x282e00('0x4e')),logger[_0x282e00('0xf')]('The\x20Ticket\x20ID\x20is:',_0x276a00['id']);switch(_0x8d8c25[_0x282e00('0x45')]){case _0x282e00('0x16'):logger['info'](_0x282e00('0x3d'),_0x282e00('0x1a')),emit(util[_0x282e00('0x4a')]('user:%s',_0x4e15c8['name']),_0x282e00('0x25'),{'recordId':_0x276a00['id']});break;case _0x282e00('0x1f'):logger[_0x282e00('0xf')](_0x282e00('0x3d'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...'),emit(util[_0x282e00('0x4a')](_0x282e00('0x41'),_0x4e15c8[_0x282e00('0x49')]),_0x282e00('0x3'),{'uri':util[_0x282e00('0x4a')](_0x282e00('0xa'),_0x276a00['id'])});break;default:logger[_0x282e00('0x56')](_0x282e00('0x3d'),'Desk\x20account\x20type\x20not\x20recognized');}}})[_0x495f7f('0xd')](function(_0x4631b3){const _0x11561e=_0x495f7f;console[_0x11561e('0x56')](_0x4631b3),logger[_0x11561e('0x56')](_0x11561e('0x3d'),JSON[_0x11561e('0x68')](_0x4631b3));});},exports['outbound']=function(_0x39242b,_0x1412d4,_0x2ef5f6,_0x31ad88,_0x5c2068,_0x47ac41,_0x437957){const _0xb59f8c=_0x355e9e;let _0xf68d03,_0x148e8d;return _0x47ac41[_0xb59f8c('0x4d')]=intUtil['stripTrailingSlash'](_0x47ac41[_0xb59f8c('0x4d')]),_0x47ac41[_0xb59f8c('0x62')]=intUtil[_0xb59f8c('0x65')](_0x47ac41['serverUrl']),_0x437957&&_0x437957[_0xb59f8c('0xb')]&&_0x437957[_0xb59f8c('0xb')]!==_0xb59f8c('0x6a')&&(logger[_0xb59f8c('0xf')]('[OUTBOUND]',_0xb59f8c('0x2d')),_0x1412d4[_0xb59f8c('0x3a')]=util[_0xb59f8c('0x4a')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x47ac41[_0xb59f8c('0x62')],_0x1412d4[_0xb59f8c('0x0')],md5(_0x1412d4[_0xb59f8c('0x0')]))),headers[_0xb59f8c('0x2b')]=getAuth(_0x47ac41),getUser(_0x47ac41,_0x31ad88[_0xb59f8c('0x8')])[_0xb59f8c('0x11')](function(_0x129da9){const _0x3dcc75=_0xb59f8c;return _0xf68d03=_0x129da9,logger[_0x3dcc75('0xf')](_0x3dcc75('0x32'),_0xf68d03['id']),getEndUser(_0x47ac41,_0x1412d4[_0x3dcc75('0x2e')],_0x1412d4[_0x3dcc75('0xc')]);})['then'](function(_0x3e4e9d){const _0x5413af=_0xb59f8c;return _0x148e8d=_0x3e4e9d,logger['info'](_0x5413af('0x69'),_0x148e8d['id']),getTicket(_0x47ac41,_0xf68d03,_0x148e8d,_0x2ef5f6,_0x1412d4);})[_0xb59f8c('0x11')](function(_0x298cd3){const _0x4c2a4b=_0xb59f8c;if(_0x298cd3){logger[_0x4c2a4b('0xf')](_0x4c2a4b('0x4e')),logger[_0x4c2a4b('0xf')](_0x4c2a4b('0x67'),_0x298cd3['id']);switch(_0x47ac41['type']){case _0x4c2a4b('0x16'):logger[_0x4c2a4b('0xf')](_0x4c2a4b('0x3d'),'Displaying\x20ticket\x20to\x20agent...'),emit(util[_0x4c2a4b('0x4a')]('user:%s',_0x31ad88[_0x4c2a4b('0x49')]),_0x4c2a4b('0x25'),{'recordId':_0x298cd3['id']});break;case _0x4c2a4b('0x1f'):logger['info'](_0x4c2a4b('0x3d'),_0x4c2a4b('0x42')),emit(util[_0x4c2a4b('0x4a')](_0x4c2a4b('0x41'),_0x31ad88[_0x4c2a4b('0x49')]),_0x4c2a4b('0x3'),{'uri':util[_0x4c2a4b('0x4a')](_0x4c2a4b('0x54'),_0x47ac41['remoteUri'],_0x298cd3['id'])});break;default:logger['error']('[OUTBOUND]',_0x4c2a4b('0x43'));}}})[_0xb59f8c('0xd')](function(_0x1d11df){const _0x5c3ee6=_0xb59f8c;logger[_0x5c3ee6('0x56')](_0x5c3ee6('0x24'),JSON[_0x5c3ee6('0x68')](_0x1d11df));});},exports[_0x355e9e('0x22')]=function(_0x11092a,_0x42cf8a,_0x9a5f27,_0x1cb813,_0x239ae5,_0x135b12){const _0x548e8a=_0x355e9e;let _0x2610af,_0x2543d4;return _0x135b12['remoteUri']=intUtil[_0x548e8a('0x65')](_0x135b12[_0x548e8a('0x4d')]),_0x135b12[_0x548e8a('0x62')]=intUtil[_0x548e8a('0x65')](_0x135b12[_0x548e8a('0x62')]),_0x239ae5&&_0x239ae5[_0x548e8a('0x39')]?(logger[_0x548e8a('0xf')]('['+_0x42cf8a[_0x548e8a('0x1b')][_0x548e8a('0x29')]()+']',_0x548e8a('0x2d')),_0x42cf8a[_0x548e8a('0x3a')]=util['format']('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x135b12[_0x548e8a('0x62')],_0x42cf8a['uniqueid'],md5(_0x42cf8a[_0x548e8a('0x0')]))):_0x42cf8a['recordingURL']='',headers[_0x548e8a('0x2b')]=getAuth(_0x135b12),getUser(_0x135b12)[_0x548e8a('0x11')](function(_0xd01129){const _0x54dd69=_0x548e8a;return _0x2610af=_0xd01129,logger[_0x54dd69('0xf')](_0x54dd69('0x32'),_0x2610af['id']),getEndUser(_0x135b12,_0x42cf8a[_0x54dd69('0x6b')],_0x42cf8a[_0x54dd69('0x5')]);})[_0x548e8a('0x11')](function(_0x4edf83){const _0x1422ab=_0x548e8a;return _0x2543d4=_0x4edf83,logger[_0x1422ab('0xf')](_0x1422ab('0x69'),_0x2543d4['id']),getTicket(_0x135b12,_0x2610af,_0x2543d4,_0x9a5f27,_0x42cf8a);})['then'](function(_0x4c65d1){const _0x1b3aa7=_0x548e8a;_0x4c65d1&&(logger[_0x1b3aa7('0xf')](_0x1b3aa7('0x4e')),logger[_0x1b3aa7('0xf')](_0x1b3aa7('0x67'),_0x4c65d1['id']));})[_0x548e8a('0xd')](function(_0x59042c){const _0x1003ba=_0x548e8a;logger[_0x1003ba('0x56')]('['+_0x42cf8a['lastevent']['toUpperCase']()+']',JSON[_0x1003ba('0x68')](_0x59042c));});};