Built motion from commit dbf3a7e2.|2.6.30
[motion2.git] / server / services / ami / trigger / integration / zendesk.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0xf890=['../../../../config/logger','zendesk','defaults','redis','localhost','socket.io-emitter','emit','application/json','info','Creating\x20new\x20ticket...','format','%s/tickets.json','remoteUri','POST','merge','name','getString','Subjects','Descriptions','Fields','then','ticket','validateEmail','Getting\x20agent\x20with\x20email','%s/search.json','type:user\x20role:agent\x20email:%s','count','Agent\x20found!','find','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/users/me.json','Admin\x20account\x20selected.','Searching\x20enduser\x20with\x20phone','length','results','filter','end-user','first','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','%s/users.json','Enduser\x20created.','user','authType','password','%s:%s','username','token','%s/token:%s','Basic\x20%s','debug','GET','forEach','includes','string','picklist','nameField','customField','content','toString','idField','custom_fields','push','type','variableName','Variable','toLowerCase','serverUrl','stripTrailingSlash','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:','The\x20Ticket\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent...','%s/channels/voice/agents/%d/tickets/%d/display.json','newTab','trigger:browser:url','%s/agent/tickets/%s','error','Zendesk\x20account\x20type\x20not\x20recognized','catch','inspect','outbound','recordingFormat','none','[OUTBOUND]','destcalleridnum','integrationTab','user:%s','replace','/api/v2','[OUTBOUND]\x20Error\x20running\x20Zendesk\x20integration','unmanaged','lastevent','toUpperCase','\x20Error\x20running\x20Zendesk\x20integration','lodash','request-promise','md5','ioredis','./util'];(function(_0x1f1f4c,_0x5da224){var _0x29112a=function(_0x3d63d7){while(--_0x3d63d7){_0x1f1f4c['push'](_0x1f1f4c['shift']());}};_0x29112a(++_0x5da224);}(_0xf890,0x69));var _0x0f89=function(_0x1e8462,_0x4c0443){_0x1e8462=_0x1e8462-0x0;var _0x34774f=_0xf890[_0x1e8462];return _0x34774f;};'use strict';var _=require(_0x0f89('0x0'));var rp=require(_0x0f89('0x1'));var md5=require(_0x0f89('0x2'));var util=require('util');var Redis=require(_0x0f89('0x3'));var intUtil=require(_0x0f89('0x4'));var config=require('../../../../config/environment');var logger=require(_0x0f89('0x5'))(_0x0f89('0x6'));config['redis']=_[_0x0f89('0x7')](config[_0x0f89('0x8')],{'host':_0x0f89('0x9'),'port':0x18eb});var io=require(_0x0f89('0xa'))(new Redis(config[_0x0f89('0x8')]));function emit(_0x8cc495,_0x56afe5,_0x53db62){io['to'](_0x8cc495)[_0x0f89('0xb')](_0x56afe5,_0x53db62);}var headers={'Content-Type':_0x0f89('0xc'),'Accept':_0x0f89('0xc')};function getTicket(_0x1029ac,_0x1d895e,_0x3db3c6,_0x350573,_0x27d407){logger[_0x0f89('0xd')](_0x0f89('0xe'));return request({'uri':util[_0x0f89('0xf')](_0x0f89('0x10'),_0x1029ac[_0x0f89('0x11')]),'method':_0x0f89('0x12'),'body':{'ticket':_[_0x0f89('0x13')]({'requester_id':_0x3db3c6['id'],'submitter_id':_0x1d895e?_0x1d895e['id']:undefined,'assignee_id':_0x1d895e?_0x1d895e['id']:undefined,'tags':getTags(_0x350573['Tags'],_0x0f89('0x14')),'subject':intUtil[_0x0f89('0x15')](_0x350573[_0x0f89('0x16')],_0x27d407,'\x20'),'description':intUtil[_0x0f89('0x15')](_0x350573[_0x0f89('0x17')],_0x27d407,'\x0a'),'comment':{'public':![],'body':intUtil[_0x0f89('0x15')](_0x350573[_0x0f89('0x17')],_0x27d407,'\x0a')}},getCustomFields(_0x350573[_0x0f89('0x18')],_0x27d407))}})[_0x0f89('0x19')](function(_0x5d56b5){logger[_0x0f89('0xd')]('Ticket\x20created!');return _0x5d56b5[_0x0f89('0x1a')];});}function getUser(_0x29e399,_0x20b7e9){if(intUtil[_0x0f89('0x1b')](_0x20b7e9)){logger[_0x0f89('0xd')](_0x0f89('0x1c'),_0x20b7e9);return request({'uri':util[_0x0f89('0xf')](_0x0f89('0x1d'),_0x29e399[_0x0f89('0x11')]),'qs':{'query':util[_0x0f89('0xf')](_0x0f89('0x1e'),_0x20b7e9)}})[_0x0f89('0x19')](function(_0x43a248){if(_0x43a248[_0x0f89('0x1f')]){logger[_0x0f89('0xd')](_0x0f89('0x20'));return _[_0x0f89('0x21')](_0x43a248['results'],{'email':_0x20b7e9,'role':'agent'});}else{logger['info'](_0x0f89('0x22'));return request({'uri':util[_0x0f89('0xf')](_0x0f89('0x23'),_0x29e399[_0x0f89('0x11')])})['then'](function(_0x18cb10){logger[_0x0f89('0xd')](_0x0f89('0x24'));return _0x18cb10['user'];});}});}else{logger['info']('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');return request({'uri':util[_0x0f89('0xf')]('%s/users/me.json',_0x29e399[_0x0f89('0x11')])})[_0x0f89('0x19')](function(_0x3f1b9d){logger[_0x0f89('0xd')](_0x0f89('0x24'));return _0x3f1b9d['user'];});}}function getEndUser(_0x366c04,_0x4c25d1,_0x3b0775){logger[_0x0f89('0xd')](_0x0f89('0x25'),_0x3b0775);return request({'uri':util[_0x0f89('0xf')]('%s/search.json',_0x366c04[_0x0f89('0x11')]),'qs':{'query':util[_0x0f89('0xf')]('type:user\x20role:end-user\x20phone:%s',_0x3b0775)}})['then'](function(_0x32f064){var _0x3f6ff5;if(_0x32f064['results'][_0x0f89('0x26')]){_0x32f064[_0x0f89('0x27')]=_[_0x0f89('0x28')](_0x32f064['results'],{'suspended':![],'role':_0x0f89('0x29')});if(_0x32f064['results'][_0x0f89('0x26')]){_0x3f6ff5=_[_0x0f89('0x2a')](_0x32f064[_0x0f89('0x27')]);}}if(_0x3f6ff5){logger[_0x0f89('0xd')](_0x0f89('0x2b'));return _0x3f6ff5;}else{logger['info'](_0x0f89('0x2c'));return request({'uri':util[_0x0f89('0xf')](_0x0f89('0x2d'),_0x366c04[_0x0f89('0x11')]),'method':_0x0f89('0x12'),'body':{'user':{'role':_0x0f89('0x29'),'name':util[_0x0f89('0xf')]('Caller\x20-\x20%s',_0x4c25d1||_0x3b0775),'phone':_0x3b0775}}})[_0x0f89('0x19')](function(_0x5c917e){logger[_0x0f89('0xd')](_0x0f89('0x2e'));return _0x5c917e[_0x0f89('0x2f')];});}});}function getAuth(_0x86e880){var _0x59d29f;if(_0x86e880[_0x0f89('0x30')]===_0x0f89('0x31')){_0x59d29f=util['format'](_0x0f89('0x32'),_0x86e880[_0x0f89('0x33')],_0x86e880[_0x0f89('0x31')]);}else if(_0x86e880[_0x0f89('0x30')]===_0x0f89('0x34')){_0x59d29f=util[_0x0f89('0xf')](_0x0f89('0x35'),_0x86e880[_0x0f89('0x33')],_0x86e880['token']);}return util['format'](_0x0f89('0x36'),new Buffer(_0x59d29f)['toString']('base64'));}function request(_0x1621cb){logger[_0x0f89('0x37')]('[HTTP]',JSON['stringify'](_0x1621cb));return rp(_[_0x0f89('0x7')](_0x1621cb,{'method':_0x0f89('0x38'),'headers':headers,'json':!![]}));}function getTags(_0xefca94,_0x5bb1a0){var _0x373853=[];if(_0xefca94&&_0xefca94[_0x0f89('0x26')]>0x0){_0x373853=_['map'](_0xefca94,_0x5bb1a0);}return _0x373853;}function getCustomFields(_0x23f604,_0x2f6e3c){var _0x2a1a03=[];var _0x33e5c2={'custom_fields':[]};if(_0x23f604[_0x0f89('0x26')]>0x0){_[_0x0f89('0x39')](_0x23f604,function(_0x377231){if(_[_0x0f89('0x3a')]([_0x0f89('0x3b'),_0x0f89('0x3c')],_0x377231['type'])&&_0x377231[_0x0f89('0x3d')]&&!_0x377231[_0x0f89('0x3e')]){_0x33e5c2[_0x377231['nameField']['toLowerCase']()]=_0x377231[_0x0f89('0x3f')][_0x0f89('0x40')]();}else if(_['includes']([_0x0f89('0x3b'),_0x0f89('0x3c')],_0x377231['type'])&&_0x377231[_0x0f89('0x41')]&&_0x377231[_0x0f89('0x3e')]){_0x33e5c2[_0x0f89('0x42')][_0x0f89('0x43')]({'id':_0x377231['idField'],'value':_0x377231[_0x0f89('0x3f')][_0x0f89('0x40')]()});}else if(_0x377231[_0x0f89('0x44')]==='variable'&&_0x377231['variableName']&&_0x377231[_0x0f89('0x41')]&&_0x377231[_0x0f89('0x3e')]){_0x2f6e3c[_0x377231['variableName']]&&_0x33e5c2['custom_fields']['push']({'id':_0x377231['idField'],'value':_0x2f6e3c[_0x377231[_0x0f89('0x45')]][_0x0f89('0x40')]()});}else if(_0x377231['type']==='customVariable'&&_0x377231[_0x0f89('0x46')]&&_0x377231[_0x0f89('0x46')][_0x0f89('0x14')]&&_0x377231[_0x0f89('0x41')]&&_0x377231[_0x0f89('0x3e')]){_0x2f6e3c[_0x377231[_0x0f89('0x46')][_0x0f89('0x14')][_0x0f89('0x47')]()]&&_0x33e5c2[_0x0f89('0x42')][_0x0f89('0x43')]({'id':_0x377231[_0x0f89('0x41')],'value':_0x2f6e3c[_0x377231[_0x0f89('0x46')][_0x0f89('0x14')][_0x0f89('0x47')]()][_0x0f89('0x40')]()});}});}return _0x33e5c2;}exports['queue']=function(_0x37622e,_0x60cddd,_0x20807c,_0x170c9f,_0x247650,_0x25da74){var _0x549ea0,_0x308325;_0x25da74['remoteUri']=intUtil['stripTrailingSlash'](_0x25da74[_0x0f89('0x11')]);_0x25da74[_0x0f89('0x48')]=intUtil[_0x0f89('0x49')](_0x25da74[_0x0f89('0x48')]);if(_0x247650&&_0x247650[_0x0f89('0x4a')]){logger[_0x0f89('0xd')](_0x0f89('0x4b'),_0x0f89('0x4c'));_0x60cddd[_0x0f89('0x4d')]=util[_0x0f89('0xf')](_0x0f89('0x4e'),_0x25da74[_0x0f89('0x48')],_0x60cddd[_0x0f89('0x4f')],md5(_0x60cddd[_0x0f89('0x4f')]));}else{_0x60cddd[_0x0f89('0x4d')]='';}headers[_0x0f89('0x50')]=getAuth(_0x25da74);return getUser(_0x25da74,_0x170c9f[_0x0f89('0x51')])[_0x0f89('0x19')](function(_0x12ea92){_0x549ea0=_0x12ea92;logger[_0x0f89('0xd')](_0x0f89('0x52'),_0x549ea0['id']);return getEndUser(_0x25da74,_0x60cddd[_0x0f89('0x53')],_0x60cddd[_0x0f89('0x54')]);})[_0x0f89('0x19')](function(_0x53fba1){_0x308325=_0x53fba1;logger[_0x0f89('0xd')](_0x0f89('0x55'),_0x308325['id']);return getTicket(_0x25da74,_0x549ea0,_0x308325,_0x20807c,_0x60cddd);})['then'](function(_0x55bd9e){if(_0x55bd9e){logger[_0x0f89('0xd')](_0x0f89('0x56'),_0x55bd9e['id']);switch(_0x25da74['type']){case'integrationTab':logger[_0x0f89('0xd')](_0x0f89('0x4b'),_0x0f89('0x57'));return request({'uri':util['format'](_0x0f89('0x58'),_0x25da74[_0x0f89('0x11')],_0x549ea0['id'],_0x55bd9e['id']),'method':_0x0f89('0x12')});case _0x0f89('0x59'):logger['info'](_0x0f89('0x4b'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util['format']('user:%s',_0x170c9f['name']),_0x0f89('0x5a'),{'uri':util['format'](_0x0f89('0x5b'),_0x25da74['remoteUri']['replace']('/api/v2',''),_0x55bd9e['id'])});break;default:logger[_0x0f89('0x5c')](_0x0f89('0x4b'),_0x0f89('0x5d'));}}})[_0x0f89('0x5e')](function(_0x2dcc92){logger[_0x0f89('0x5c')]('[QUEUE]\x20Error\x20running\x20Zendesk\x20integration',util[_0x0f89('0x5f')](_0x2dcc92,{'showHidden':!![],'depth':null}));});};exports[_0x0f89('0x60')]=function(_0x19354a,_0x286051,_0x2f7c1f,_0x1a58d4,_0x5db78f,_0x434bac,_0x49f144){var _0x3fa4e9,_0x3ee6b1;_0x434bac['remoteUri']=intUtil[_0x0f89('0x49')](_0x434bac[_0x0f89('0x11')]);_0x434bac[_0x0f89('0x48')]=intUtil[_0x0f89('0x49')](_0x434bac[_0x0f89('0x48')]);if(_0x49f144&&_0x49f144[_0x0f89('0x61')]&&_0x49f144['recordingFormat']!==_0x0f89('0x62')){logger[_0x0f89('0xd')](_0x0f89('0x63'),'Recording\x20is\x20enabled!');_0x286051[_0x0f89('0x4d')]=util[_0x0f89('0xf')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x434bac[_0x0f89('0x48')],_0x286051[_0x0f89('0x4f')],md5(_0x286051[_0x0f89('0x4f')]));}headers[_0x0f89('0x50')]=getAuth(_0x434bac);return getUser(_0x434bac,_0x1a58d4[_0x0f89('0x51')])['then'](function(_0x19b5be){_0x3fa4e9=_0x19b5be;logger[_0x0f89('0xd')]('The\x20User\x20ID\x20is:',_0x3fa4e9['id']);return getEndUser(_0x434bac,_0x286051['destcalleridname'],_0x286051[_0x0f89('0x64')]);})['then'](function(_0x5fbdea){_0x3ee6b1=_0x5fbdea;logger[_0x0f89('0xd')](_0x0f89('0x55'),_0x3ee6b1['id']);return getTicket(_0x434bac,_0x3fa4e9,_0x3ee6b1,_0x2f7c1f,_0x286051);})[_0x0f89('0x19')](function(_0x5de462){if(_0x5de462){logger['info']('The\x20Ticket\x20ID\x20is:',_0x5de462['id']);switch(_0x434bac[_0x0f89('0x44')]){case _0x0f89('0x65'):logger['info'](_0x0f89('0x63'),_0x0f89('0x57'));return request({'uri':util[_0x0f89('0xf')](_0x0f89('0x58'),_0x434bac[_0x0f89('0x11')],_0x3fa4e9['id'],_0x5de462['id']),'method':_0x0f89('0x12')});case _0x0f89('0x59'):logger[_0x0f89('0xd')](_0x0f89('0x63'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0x0f89('0xf')](_0x0f89('0x66'),_0x1a58d4[_0x0f89('0x14')]),_0x0f89('0x5a'),{'uri':util[_0x0f89('0xf')](_0x0f89('0x5b'),_0x434bac[_0x0f89('0x11')][_0x0f89('0x67')](_0x0f89('0x68'),''),_0x5de462['id'])});break;default:logger[_0x0f89('0x5c')](_0x0f89('0x63'),_0x0f89('0x5d'));}}})[_0x0f89('0x5e')](function(_0x136b4d){logger['error'](_0x0f89('0x69'),util[_0x0f89('0x5f')](_0x136b4d,{'showHidden':!![],'depth':null}));});};exports[_0x0f89('0x6a')]=function(_0x1b5589,_0x492944,_0xe554bf,_0x1c3283,_0x551a74,_0xb58e58){var _0x251369,_0x4d998f;_0xb58e58['remoteUri']=intUtil[_0x0f89('0x49')](_0xb58e58['remoteUri']);_0xb58e58[_0x0f89('0x48')]=intUtil[_0x0f89('0x49')](_0xb58e58[_0x0f89('0x48')]);if(_0x551a74&&_0x551a74[_0x0f89('0x4a')]){logger[_0x0f89('0xd')]('['+_0x492944[_0x0f89('0x6b')][_0x0f89('0x6c')]()+']',_0x0f89('0x4c'));_0x492944[_0x0f89('0x4d')]=util['format'](_0x0f89('0x4e'),_0xb58e58[_0x0f89('0x48')],_0x492944[_0x0f89('0x4f')],md5(_0x492944[_0x0f89('0x4f')]));}else{_0x492944[_0x0f89('0x4d')]='';}headers[_0x0f89('0x50')]=getAuth(_0xb58e58);return getUser(_0xb58e58)[_0x0f89('0x19')](function(_0x365ff6){_0x251369=_0x365ff6;logger[_0x0f89('0xd')](_0x0f89('0x52'),_0x251369['id']);return getEndUser(_0xb58e58,_0x492944[_0x0f89('0x53')],_0x492944[_0x0f89('0x54')]);})[_0x0f89('0x19')](function(_0x2bd4e7){_0x4d998f=_0x2bd4e7;logger[_0x0f89('0xd')](_0x0f89('0x55'),_0x4d998f['id']);return getTicket(_0xb58e58,_0x251369,_0x4d998f,_0xe554bf,_0x492944);})[_0x0f89('0x19')](function(_0x444907){if(_0x444907){logger[_0x0f89('0xd')]('The\x20Ticket\x20ID\x20is:',_0x444907['id']);}})[_0x0f89('0x5e')](function(_0x2de7fc){logger[_0x0f89('0x5c')]('['+_0x492944['lastevent']['toUpperCase']()+']'+_0x0f89('0x6d'),util[_0x0f89('0x5f')](_0x2de7fc,{'showHidden':!![],'depth':null}));});};