26e0f617bd00172d6eeb187966b4eb3ecc89e14c
[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 _0x885f=['calleridname','calleridnum','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','integrationTab','%s/channels/voice/agents/%d/tickets/%d/display.json','user:%s','trigger:browser:url','%s/agent/tickets/%s','replace','/api/v2','catch','[QUEUE]\x20Error\x20running\x20Zendesk\x20integration','inspect','outbound','recordingFormat','none','[OUTBOUND]','Recording\x20is\x20enabled!','destcalleridname','Displaying\x20ticket\x20to\x20agent...','newTab','error','Zendesk\x20account\x20type\x20not\x20recognized','[OUTBOUND]\x20Error\x20running\x20Zendesk\x20integration','unmanaged','lastevent','toUpperCase','\x20Error\x20running\x20Zendesk\x20integration','md5','util','./util','../../../../config/environment','../../../../config/logger','zendesk','redis','localhost','socket.io-emitter','emit','application/json','Creating\x20new\x20ticket...','format','%s/tickets.json','POST','merge','Tags','name','getString','Subjects','Descriptions','then','info','Ticket\x20created!','validateEmail','Getting\x20agent\x20with\x20email','%s/search.json','type:user\x20role:agent\x20email:%s','Agent\x20found!','results','agent','%s/users/me.json','remoteUri','Admin\x20account\x20selected.','user','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','Searching\x20enduser\x20with\x20phone','type:user\x20role:end-user\x20phone:%s','length','end-user','first','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','%s/users.json','Caller\x20-\x20%s','Enduser\x20created.','authType','password','username','%s/token:%s','token','Basic\x20%s','base64','[HTTP]','stringify','defaults','map','type','string','nameField','customField','content','toString','idField','custom_fields','variable','variableName','push','customVariable','Variable','toLowerCase','stripTrailingSlash','serverUrl','monitor_format','[QUEUE]','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','recordingURL','Authorization','email','The\x20User\x20ID\x20is:'];(function(_0x2a3a4,_0x53edfd){var _0x5590d3=function(_0x55847a){while(--_0x55847a){_0x2a3a4['push'](_0x2a3a4['shift']());}};_0x5590d3(++_0x53edfd);}(_0x885f,0x8b));var _0xf885=function(_0x39c0fe,_0x285b08){_0x39c0fe=_0x39c0fe-0x0;var _0x381a92=_0x885f[_0x39c0fe];return _0x381a92;};'use strict';var _=require('lodash');var rp=require('request-promise');var md5=require(_0xf885('0x0'));var util=require(_0xf885('0x1'));var Redis=require('ioredis');var intUtil=require(_0xf885('0x2'));var config=require(_0xf885('0x3'));var logger=require(_0xf885('0x4'))(_0xf885('0x5'));config[_0xf885('0x6')]=_['defaults'](config[_0xf885('0x6')],{'host':_0xf885('0x7'),'port':0x18eb});var io=require(_0xf885('0x8'))(new Redis(config[_0xf885('0x6')]));function emit(_0x2383db,_0x3aef5e,_0xbc79b9){io['to'](_0x2383db)[_0xf885('0x9')](_0x3aef5e,_0xbc79b9);}var headers={'Content-Type':_0xf885('0xa'),'Accept':_0xf885('0xa')};function getTicket(_0x46f931,_0x13f9ef,_0x16d9c2,_0xfb8cd9,_0x3a5060){logger['info'](_0xf885('0xb'));return request({'uri':util[_0xf885('0xc')](_0xf885('0xd'),_0x46f931['remoteUri']),'method':_0xf885('0xe'),'body':{'ticket':_[_0xf885('0xf')]({'requester_id':_0x16d9c2['id'],'submitter_id':_0x13f9ef?_0x13f9ef['id']:undefined,'assignee_id':_0x13f9ef?_0x13f9ef['id']:undefined,'tags':getTags(_0xfb8cd9[_0xf885('0x10')],_0xf885('0x11')),'subject':intUtil[_0xf885('0x12')](_0xfb8cd9[_0xf885('0x13')],_0x3a5060,'\x20'),'description':intUtil[_0xf885('0x12')](_0xfb8cd9[_0xf885('0x14')],_0x3a5060,'\x0a'),'comment':{'public':![],'body':intUtil['getString'](_0xfb8cd9[_0xf885('0x14')],_0x3a5060,'\x0a')}},getCustomFields(_0xfb8cd9['Fields'],_0x3a5060))}})[_0xf885('0x15')](function(_0x116d93){logger[_0xf885('0x16')](_0xf885('0x17'));return _0x116d93['ticket'];});}function getUser(_0x1410f4,_0x1a6916){if(intUtil[_0xf885('0x18')](_0x1a6916)){logger[_0xf885('0x16')](_0xf885('0x19'),_0x1a6916);return request({'uri':util[_0xf885('0xc')](_0xf885('0x1a'),_0x1410f4['remoteUri']),'qs':{'query':util[_0xf885('0xc')](_0xf885('0x1b'),_0x1a6916)}})['then'](function(_0x36afff){if(_0x36afff['count']){logger[_0xf885('0x16')](_0xf885('0x1c'));return _['find'](_0x36afff[_0xf885('0x1d')],{'email':_0x1a6916,'role':_0xf885('0x1e')});}else{logger['info']('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');return request({'uri':util['format'](_0xf885('0x1f'),_0x1410f4[_0xf885('0x20')])})[_0xf885('0x15')](function(_0x9b55cf){logger[_0xf885('0x16')](_0xf885('0x21'));return _0x9b55cf[_0xf885('0x22')];});}});}else{logger[_0xf885('0x16')](_0xf885('0x23'));return request({'uri':util[_0xf885('0xc')](_0xf885('0x1f'),_0x1410f4[_0xf885('0x20')])})[_0xf885('0x15')](function(_0x4c22cf){logger[_0xf885('0x16')](_0xf885('0x21'));return _0x4c22cf[_0xf885('0x22')];});}}function getEndUser(_0x3fb886,_0xacd9c0,_0x2121f8){logger[_0xf885('0x16')](_0xf885('0x24'),_0x2121f8);return request({'uri':util[_0xf885('0xc')](_0xf885('0x1a'),_0x3fb886['remoteUri']),'qs':{'query':util[_0xf885('0xc')](_0xf885('0x25'),_0x2121f8)}})[_0xf885('0x15')](function(_0x14c3b9){var _0x3d2359;if(_0x14c3b9[_0xf885('0x1d')][_0xf885('0x26')]){_0x14c3b9['results']=_['filter'](_0x14c3b9[_0xf885('0x1d')],{'suspended':![],'role':_0xf885('0x27')});if(_0x14c3b9['results'][_0xf885('0x26')]){_0x3d2359=_[_0xf885('0x28')](_0x14c3b9[_0xf885('0x1d')]);}}if(_0x3d2359){logger['info'](_0xf885('0x29'));return _0x3d2359;}else{logger[_0xf885('0x16')](_0xf885('0x2a'));return request({'uri':util['format'](_0xf885('0x2b'),_0x3fb886[_0xf885('0x20')]),'method':_0xf885('0xe'),'body':{'user':{'role':_0xf885('0x27'),'name':util[_0xf885('0xc')](_0xf885('0x2c'),_0xacd9c0||_0x2121f8),'phone':_0x2121f8}}})[_0xf885('0x15')](function(_0x3bbe9a){logger[_0xf885('0x16')](_0xf885('0x2d'));return _0x3bbe9a[_0xf885('0x22')];});}});}function getAuth(_0x5d9473){var _0x1d6710;if(_0x5d9473[_0xf885('0x2e')]===_0xf885('0x2f')){_0x1d6710=util[_0xf885('0xc')]('%s:%s',_0x5d9473[_0xf885('0x30')],_0x5d9473['password']);}else if(_0x5d9473[_0xf885('0x2e')]==='token'){_0x1d6710=util['format'](_0xf885('0x31'),_0x5d9473['username'],_0x5d9473[_0xf885('0x32')]);}return util[_0xf885('0xc')](_0xf885('0x33'),new Buffer(_0x1d6710)['toString'](_0xf885('0x34')));}function request(_0x401a70){logger['debug'](_0xf885('0x35'),JSON[_0xf885('0x36')](_0x401a70));return rp(_[_0xf885('0x37')](_0x401a70,{'method':'GET','headers':headers,'json':!![]}));}function getTags(_0x471141,_0x331848){var _0x388fa7=[];if(_0x471141&&_0x471141['length']>0x0){_0x388fa7=_[_0xf885('0x38')](_0x471141,_0x331848);}return _0x388fa7;}function getCustomFields(_0x4daf90,_0x396f6c){var _0x3591b3=[];var _0x5729d5={'custom_fields':[]};if(_0x4daf90['length']>0x0){_['forEach'](_0x4daf90,function(_0x2aea14){if(_0x2aea14[_0xf885('0x39')]===_0xf885('0x3a')&&_0x2aea14[_0xf885('0x3b')]&&!_0x2aea14[_0xf885('0x3c')]){_0x5729d5[_0x2aea14['nameField']['toLowerCase']()]=_0x2aea14[_0xf885('0x3d')][_0xf885('0x3e')]();}else if(_0x2aea14[_0xf885('0x39')]==='string'&&_0x2aea14[_0xf885('0x3f')]&&_0x2aea14[_0xf885('0x3c')]){_0x5729d5[_0xf885('0x40')]['push']({'id':_0x2aea14[_0xf885('0x3f')],'value':_0x2aea14['content'][_0xf885('0x3e')]()});}else if(_0x2aea14[_0xf885('0x39')]===_0xf885('0x41')&&_0x2aea14[_0xf885('0x42')]&&_0x2aea14['idField']&&_0x2aea14[_0xf885('0x3c')]){_0x396f6c[_0x2aea14['variableName']]&&_0x5729d5[_0xf885('0x40')][_0xf885('0x43')]({'id':_0x2aea14[_0xf885('0x3f')],'value':_0x396f6c[_0x2aea14[_0xf885('0x42')]]['toString']()});}else if(_0x2aea14[_0xf885('0x39')]===_0xf885('0x44')&&_0x2aea14[_0xf885('0x45')]&&_0x2aea14[_0xf885('0x45')][_0xf885('0x11')]&&_0x2aea14[_0xf885('0x3f')]&&_0x2aea14[_0xf885('0x3c')]){_0x396f6c[_0x2aea14[_0xf885('0x45')][_0xf885('0x11')][_0xf885('0x46')]()]&&_0x5729d5[_0xf885('0x40')][_0xf885('0x43')]({'id':_0x2aea14['idField'],'value':_0x396f6c[_0x2aea14['Variable'][_0xf885('0x11')][_0xf885('0x46')]()][_0xf885('0x3e')]()});}});}return _0x5729d5;}exports['queue']=function(_0x2afb49,_0x27078b,_0x2a6f5b,_0x150062,_0x1a0e50,_0x54c4f2){var _0x590fa2,_0x236bd6;_0x54c4f2[_0xf885('0x20')]=intUtil[_0xf885('0x47')](_0x54c4f2[_0xf885('0x20')]);_0x54c4f2[_0xf885('0x48')]=intUtil[_0xf885('0x47')](_0x54c4f2[_0xf885('0x48')]);if(_0x1a0e50&&_0x1a0e50[_0xf885('0x49')]){logger['info'](_0xf885('0x4a'),'Recording\x20is\x20enabled!');_0x27078b['recordingURL']=util[_0xf885('0xc')](_0xf885('0x4b'),_0x54c4f2['serverUrl'],_0x27078b[_0xf885('0x4c')],md5(_0x27078b[_0xf885('0x4c')]));}else{_0x27078b[_0xf885('0x4d')]='';}headers[_0xf885('0x4e')]=getAuth(_0x54c4f2);return getUser(_0x54c4f2,_0x150062[_0xf885('0x4f')])[_0xf885('0x15')](function(_0x1e8b73){_0x590fa2=_0x1e8b73;logger[_0xf885('0x16')](_0xf885('0x50'),_0x590fa2['id']);return getEndUser(_0x54c4f2,_0x27078b[_0xf885('0x51')],_0x27078b[_0xf885('0x52')]);})[_0xf885('0x15')](function(_0x5b574c){_0x236bd6=_0x5b574c;logger[_0xf885('0x16')](_0xf885('0x53'),_0x236bd6['id']);return getTicket(_0x54c4f2,_0x590fa2,_0x236bd6,_0x2a6f5b,_0x27078b);})[_0xf885('0x15')](function(_0x5547e5){if(_0x5547e5){logger[_0xf885('0x16')](_0xf885('0x54'),_0x5547e5['id']);switch(_0x54c4f2[_0xf885('0x39')]){case _0xf885('0x55'):logger['info'](_0xf885('0x4a'),'Displaying\x20ticket\x20to\x20agent...');return request({'uri':util[_0xf885('0xc')](_0xf885('0x56'),_0x54c4f2[_0xf885('0x20')],_0x590fa2['id'],_0x5547e5['id']),'method':'POST'});case'newTab':logger[_0xf885('0x16')](_0xf885('0x4a'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0xf885('0xc')](_0xf885('0x57'),_0x150062[_0xf885('0x11')]),_0xf885('0x58'),{'uri':util[_0xf885('0xc')](_0xf885('0x59'),_0x54c4f2[_0xf885('0x20')][_0xf885('0x5a')](_0xf885('0x5b'),''),_0x5547e5['id'])});break;default:logger['error'](_0xf885('0x4a'),'Zendesk\x20account\x20type\x20not\x20recognized');}}})[_0xf885('0x5c')](function(_0x90553f){logger['error'](_0xf885('0x5d'),util[_0xf885('0x5e')](_0x90553f,{'showHidden':!![],'depth':null}));});};exports[_0xf885('0x5f')]=function(_0x1cd403,_0x472a00,_0x23da53,_0x414952,_0x5cc7bb,_0x4f0c55,_0x311c11){var _0x294066,_0x34c313;_0x4f0c55['remoteUri']=intUtil[_0xf885('0x47')](_0x4f0c55[_0xf885('0x20')]);_0x4f0c55[_0xf885('0x48')]=intUtil[_0xf885('0x47')](_0x4f0c55[_0xf885('0x48')]);if(_0x311c11&&_0x311c11[_0xf885('0x60')]&&_0x311c11['recordingFormat']!==_0xf885('0x61')){logger['info'](_0xf885('0x62'),_0xf885('0x63'));_0x472a00[_0xf885('0x4d')]=util[_0xf885('0xc')](_0xf885('0x4b'),_0x4f0c55[_0xf885('0x48')],_0x472a00[_0xf885('0x4c')],md5(_0x472a00[_0xf885('0x4c')]));}headers['Authorization']=getAuth(_0x4f0c55);return getUser(_0x4f0c55,_0x414952[_0xf885('0x4f')])['then'](function(_0x33edd0){_0x294066=_0x33edd0;logger[_0xf885('0x16')](_0xf885('0x50'),_0x294066['id']);return getEndUser(_0x4f0c55,_0x472a00[_0xf885('0x64')],_0x472a00['destcalleridnum']);})['then'](function(_0x1e3072){_0x34c313=_0x1e3072;logger[_0xf885('0x16')]('The\x20Enduser\x20ID\x20is:',_0x34c313['id']);return getTicket(_0x4f0c55,_0x294066,_0x34c313,_0x23da53,_0x472a00);})['then'](function(_0x1512c5){if(_0x1512c5){logger[_0xf885('0x16')](_0xf885('0x54'),_0x1512c5['id']);switch(_0x4f0c55[_0xf885('0x39')]){case _0xf885('0x55'):logger[_0xf885('0x16')]('[OUTBOUND]',_0xf885('0x65'));return request({'uri':util[_0xf885('0xc')](_0xf885('0x56'),_0x4f0c55[_0xf885('0x20')],_0x294066['id'],_0x1512c5['id']),'method':_0xf885('0xe')});case _0xf885('0x66'):logger['info']('[OUTBOUND]','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0xf885('0xc')](_0xf885('0x57'),_0x414952['name']),_0xf885('0x58'),{'uri':util[_0xf885('0xc')](_0xf885('0x59'),_0x4f0c55[_0xf885('0x20')][_0xf885('0x5a')](_0xf885('0x5b'),''),_0x1512c5['id'])});break;default:logger[_0xf885('0x67')](_0xf885('0x62'),_0xf885('0x68'));}}})[_0xf885('0x5c')](function(_0x18f3e9){logger[_0xf885('0x67')](_0xf885('0x69'),util['inspect'](_0x18f3e9,{'showHidden':!![],'depth':null}));});};exports[_0xf885('0x6a')]=function(_0x5ae12b,_0x39188f,_0x7f0ee,_0x2558d6,_0x3766c3,_0x5dcb28){var _0x3acd40,_0x3d9200;_0x5dcb28[_0xf885('0x20')]=intUtil[_0xf885('0x47')](_0x5dcb28[_0xf885('0x20')]);_0x5dcb28[_0xf885('0x48')]=intUtil['stripTrailingSlash'](_0x5dcb28[_0xf885('0x48')]);if(_0x3766c3&&_0x3766c3[_0xf885('0x49')]){logger['info']('['+_0x39188f[_0xf885('0x6b')]['toUpperCase']()+']',_0xf885('0x63'));_0x39188f['recordingURL']=util[_0xf885('0xc')](_0xf885('0x4b'),_0x5dcb28[_0xf885('0x48')],_0x39188f[_0xf885('0x4c')],md5(_0x39188f['uniqueid']));}else{_0x39188f[_0xf885('0x4d')]='';}headers['Authorization']=getAuth(_0x5dcb28);return getUser(_0x5dcb28)[_0xf885('0x15')](function(_0xd4d588){_0x3acd40=_0xd4d588;logger[_0xf885('0x16')](_0xf885('0x50'),_0x3acd40['id']);return getEndUser(_0x5dcb28,_0x39188f['calleridname'],_0x39188f[_0xf885('0x52')]);})['then'](function(_0x102560){_0x3d9200=_0x102560;logger['info'](_0xf885('0x53'),_0x3d9200['id']);return getTicket(_0x5dcb28,_0x3acd40,_0x3d9200,_0x7f0ee,_0x39188f);})[_0xf885('0x15')](function(_0x2bb856){if(_0x2bb856){logger['info']('The\x20Ticket\x20ID\x20is:',_0x2bb856['id']);}})['catch'](function(_0x3f2b08){logger[_0xf885('0x67')]('['+_0x39188f[_0xf885('0x6b')][_0xf885('0x6c')]()+']'+_0xf885('0x6d'),util[_0xf885('0x5e')](_0x3f2b08,{'showHidden':!![],'depth':null}));});};