a1e798d8da757eca1a903ad2522d8b07c2379aca
[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 _0x1682=['customField','content','includes','variable','variableName','idField','custom_fields','push','customVariable','Variable','toLowerCase','stripTrailingSlash','serverUrl','monitor_format','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','Authorization','The\x20User\x20ID\x20is:','calleridname','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','integrationTab','[QUEUE]','Displaying\x20ticket\x20to\x20agent...','newTab','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','trigger:browser:url','%s/agent/tickets/%s','/api/v2','error','catch','[QUEUE]\x20Error\x20running\x20Zendesk\x20integration','inspect','outbound','recordingFormat','[OUTBOUND]','email','destcalleridnum','replace','Zendesk\x20account\x20type\x20not\x20recognized','[OUTBOUND]\x20Error\x20running\x20Zendesk\x20integration','unmanaged','lastevent','calleridnum','toUpperCase','\x20Error\x20running\x20Zendesk\x20integration','lodash','request-promise','ioredis','./util','../../../../config/environment','zendesk','redis','defaults','localhost','application/json','info','format','%s/tickets.json','POST','merge','Tags','name','getString','Subjects','Descriptions','Fields','then','Ticket\x20created!','ticket','validateEmail','Getting\x20agent\x20with\x20email','remoteUri','type:user\x20role:agent\x20email:%s','count','find','results','agent','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','Admin\x20account\x20selected.','user','%s/users/me.json','Searching\x20enduser\x20with\x20phone','%s/search.json','type:user\x20role:end-user\x20phone:%s','length','filter','end-user','first','%s/users.json','Caller\x20-\x20%s','authType','password','username','toString','base64','debug','stringify','GET','map','forEach','string','picklist','type','nameField'];(function(_0x109a8b,_0x291bce){var _0x5d5a7f=function(_0x8ef75a){while(--_0x8ef75a){_0x109a8b['push'](_0x109a8b['shift']());}};_0x5d5a7f(++_0x291bce);}(_0x1682,0x1e1));var _0x2168=function(_0x320dc2,_0x129a13){_0x320dc2=_0x320dc2-0x0;var _0x5abef5=_0x1682[_0x320dc2];return _0x5abef5;};'use strict';var _=require(_0x2168('0x0'));var rp=require(_0x2168('0x1'));var md5=require('md5');var util=require('util');var Redis=require(_0x2168('0x2'));var intUtil=require(_0x2168('0x3'));var config=require(_0x2168('0x4'));var logger=require('../../../../config/logger')(_0x2168('0x5'));config[_0x2168('0x6')]=_[_0x2168('0x7')](config['redis'],{'host':_0x2168('0x8'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x2168('0x6')]));function emit(_0x211ec1,_0x319ecb,_0x5dfe0e){io['to'](_0x211ec1)['emit'](_0x319ecb,_0x5dfe0e);}var headers={'Content-Type':_0x2168('0x9'),'Accept':_0x2168('0x9')};function getTicket(_0x55d100,_0x295caf,_0x1d5554,_0x5157e7,_0x34b234){logger[_0x2168('0xa')]('Creating\x20new\x20ticket...');return request({'uri':util[_0x2168('0xb')](_0x2168('0xc'),_0x55d100['remoteUri']),'method':_0x2168('0xd'),'body':{'ticket':_[_0x2168('0xe')]({'requester_id':_0x1d5554['id'],'submitter_id':_0x295caf?_0x295caf['id']:undefined,'assignee_id':_0x295caf?_0x295caf['id']:undefined,'tags':getTags(_0x5157e7[_0x2168('0xf')],_0x2168('0x10')),'subject':intUtil[_0x2168('0x11')](_0x5157e7[_0x2168('0x12')],_0x34b234,'\x20'),'description':intUtil[_0x2168('0x11')](_0x5157e7[_0x2168('0x13')],_0x34b234,'\x0a'),'comment':{'public':![],'body':intUtil[_0x2168('0x11')](_0x5157e7['Descriptions'],_0x34b234,'\x0a')}},getCustomFields(_0x5157e7[_0x2168('0x14')],_0x34b234))}})[_0x2168('0x15')](function(_0x3060eb){logger['info'](_0x2168('0x16'));return _0x3060eb[_0x2168('0x17')];});}function getUser(_0x157c97,_0x1a21e4){if(intUtil[_0x2168('0x18')](_0x1a21e4)){logger[_0x2168('0xa')](_0x2168('0x19'),_0x1a21e4);return request({'uri':util['format']('%s/search.json',_0x157c97[_0x2168('0x1a')]),'qs':{'query':util[_0x2168('0xb')](_0x2168('0x1b'),_0x1a21e4)}})[_0x2168('0x15')](function(_0x1ad7f0){if(_0x1ad7f0[_0x2168('0x1c')]){logger['info']('Agent\x20found!');return _[_0x2168('0x1d')](_0x1ad7f0[_0x2168('0x1e')],{'email':_0x1a21e4,'role':_0x2168('0x1f')});}else{logger[_0x2168('0xa')](_0x2168('0x20'));return request({'uri':util['format']('%s/users/me.json',_0x157c97[_0x2168('0x1a')])})['then'](function(_0x2942ac){logger['info'](_0x2168('0x21'));return _0x2942ac[_0x2168('0x22')];});}});}else{logger[_0x2168('0xa')](_0x2168('0x20'));return request({'uri':util['format'](_0x2168('0x23'),_0x157c97['remoteUri'])})[_0x2168('0x15')](function(_0x4b3856){logger[_0x2168('0xa')](_0x2168('0x21'));return _0x4b3856[_0x2168('0x22')];});}}function getEndUser(_0x74c3dd,_0x28b6cd,_0x4559cc){logger[_0x2168('0xa')](_0x2168('0x24'),_0x4559cc);return request({'uri':util['format'](_0x2168('0x25'),_0x74c3dd['remoteUri']),'qs':{'query':util[_0x2168('0xb')](_0x2168('0x26'),_0x4559cc)}})['then'](function(_0x125a1c){var _0x531cb7;if(_0x125a1c[_0x2168('0x1e')][_0x2168('0x27')]){_0x125a1c[_0x2168('0x1e')]=_[_0x2168('0x28')](_0x125a1c[_0x2168('0x1e')],{'suspended':![],'role':_0x2168('0x29')});if(_0x125a1c[_0x2168('0x1e')][_0x2168('0x27')]){_0x531cb7=_[_0x2168('0x2a')](_0x125a1c['results']);}}if(_0x531cb7){logger['info']('Enduser\x20found!');return _0x531cb7;}else{logger[_0x2168('0xa')]('Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!');return request({'uri':util[_0x2168('0xb')](_0x2168('0x2b'),_0x74c3dd[_0x2168('0x1a')]),'method':_0x2168('0xd'),'body':{'user':{'role':_0x2168('0x29'),'name':util[_0x2168('0xb')](_0x2168('0x2c'),_0x28b6cd||_0x4559cc),'phone':_0x4559cc}}})['then'](function(_0x19b6e6){logger[_0x2168('0xa')]('Enduser\x20created.');return _0x19b6e6[_0x2168('0x22')];});}});}function getAuth(_0x70d39c){var _0x480954;if(_0x70d39c[_0x2168('0x2d')]===_0x2168('0x2e')){_0x480954=util['format']('%s:%s',_0x70d39c[_0x2168('0x2f')],_0x70d39c[_0x2168('0x2e')]);}else if(_0x70d39c[_0x2168('0x2d')]==='token'){_0x480954=util[_0x2168('0xb')]('%s/token:%s',_0x70d39c[_0x2168('0x2f')],_0x70d39c['token']);}return util['format']('Basic\x20%s',new Buffer(_0x480954)[_0x2168('0x30')](_0x2168('0x31')));}function request(_0x4e0361){logger[_0x2168('0x32')]('[HTTP]',JSON[_0x2168('0x33')](_0x4e0361));return rp(_[_0x2168('0x7')](_0x4e0361,{'method':_0x2168('0x34'),'headers':headers,'json':!![]}));}function getTags(_0x4b9391,_0x3595a6){var _0xa8e7af=[];if(_0x4b9391&&_0x4b9391['length']>0x0){_0xa8e7af=_[_0x2168('0x35')](_0x4b9391,_0x3595a6);}return _0xa8e7af;}function getCustomFields(_0x474b66,_0x283b05){var _0x10edd=[];var _0x43cbc0={'custom_fields':[]};if(_0x474b66[_0x2168('0x27')]>0x0){_[_0x2168('0x36')](_0x474b66,function(_0x4d00b6){if(_['includes']([_0x2168('0x37'),_0x2168('0x38')],_0x4d00b6[_0x2168('0x39')])&&_0x4d00b6[_0x2168('0x3a')]&&!_0x4d00b6[_0x2168('0x3b')]){_0x43cbc0[_0x4d00b6['nameField']['toLowerCase']()]=_0x4d00b6[_0x2168('0x3c')][_0x2168('0x30')]();}else if(_[_0x2168('0x3d')]([_0x2168('0x37'),_0x2168('0x38')],_0x4d00b6[_0x2168('0x39')])&&_0x4d00b6['idField']&&_0x4d00b6[_0x2168('0x3b')]){_0x43cbc0['custom_fields']['push']({'id':_0x4d00b6['idField'],'value':_0x4d00b6['content'][_0x2168('0x30')]()});}else if(_0x4d00b6[_0x2168('0x39')]===_0x2168('0x3e')&&_0x4d00b6[_0x2168('0x3f')]&&_0x4d00b6[_0x2168('0x40')]&&_0x4d00b6[_0x2168('0x3b')]){_0x283b05[_0x4d00b6['variableName']]&&_0x43cbc0[_0x2168('0x41')][_0x2168('0x42')]({'id':_0x4d00b6[_0x2168('0x40')],'value':_0x283b05[_0x4d00b6[_0x2168('0x3f')]]['toString']()});}else if(_0x4d00b6['type']===_0x2168('0x43')&&_0x4d00b6['Variable']&&_0x4d00b6[_0x2168('0x44')][_0x2168('0x10')]&&_0x4d00b6[_0x2168('0x40')]&&_0x4d00b6[_0x2168('0x3b')]){_0x283b05[_0x4d00b6[_0x2168('0x44')][_0x2168('0x10')][_0x2168('0x45')]()]&&_0x43cbc0[_0x2168('0x41')][_0x2168('0x42')]({'id':_0x4d00b6[_0x2168('0x40')],'value':_0x283b05[_0x4d00b6['Variable']['name']['toLowerCase']()][_0x2168('0x30')]()});}});}return _0x43cbc0;}exports['queue']=function(_0x176c11,_0x5761d4,_0xee2b45,_0x249a8d,_0x1d2c13,_0x526fe6){var _0x1c2743,_0x1fd932;_0x526fe6[_0x2168('0x1a')]=intUtil[_0x2168('0x46')](_0x526fe6[_0x2168('0x1a')]);_0x526fe6[_0x2168('0x47')]=intUtil[_0x2168('0x46')](_0x526fe6[_0x2168('0x47')]);if(_0x1d2c13&&_0x1d2c13[_0x2168('0x48')]){logger[_0x2168('0xa')]('[QUEUE]',_0x2168('0x49'));_0x5761d4[_0x2168('0x4a')]=util[_0x2168('0xb')](_0x2168('0x4b'),_0x526fe6[_0x2168('0x47')],_0x5761d4[_0x2168('0x4c')],md5(_0x5761d4['uniqueid']));}else{_0x5761d4[_0x2168('0x4a')]='';}headers[_0x2168('0x4d')]=getAuth(_0x526fe6);return getUser(_0x526fe6,_0x249a8d['email'])[_0x2168('0x15')](function(_0xf8b5d){_0x1c2743=_0xf8b5d;logger[_0x2168('0xa')](_0x2168('0x4e'),_0x1c2743['id']);return getEndUser(_0x526fe6,_0x5761d4[_0x2168('0x4f')],_0x5761d4['calleridnum']);})[_0x2168('0x15')](function(_0x3eb2e2){_0x1fd932=_0x3eb2e2;logger[_0x2168('0xa')](_0x2168('0x50'),_0x1fd932['id']);return getTicket(_0x526fe6,_0x1c2743,_0x1fd932,_0xee2b45,_0x5761d4);})[_0x2168('0x15')](function(_0x1a0d35){if(_0x1a0d35){logger[_0x2168('0xa')](_0x2168('0x51'),_0x1a0d35['id']);switch(_0x526fe6[_0x2168('0x39')]){case _0x2168('0x52'):logger[_0x2168('0xa')](_0x2168('0x53'),_0x2168('0x54'));return request({'uri':util['format']('%s/channels/voice/agents/%d/tickets/%d/display.json',_0x526fe6[_0x2168('0x1a')],_0x1c2743['id'],_0x1a0d35['id']),'method':_0x2168('0xd')});case _0x2168('0x55'):logger[_0x2168('0xa')](_0x2168('0x53'),_0x2168('0x56'));emit(util['format'](_0x2168('0x57'),_0x249a8d[_0x2168('0x10')]),_0x2168('0x58'),{'uri':util[_0x2168('0xb')](_0x2168('0x59'),_0x526fe6['remoteUri']['replace'](_0x2168('0x5a'),''),_0x1a0d35['id'])});break;default:logger[_0x2168('0x5b')](_0x2168('0x53'),'Zendesk\x20account\x20type\x20not\x20recognized');}}})[_0x2168('0x5c')](function(_0x40fae5){logger['error'](_0x2168('0x5d'),util[_0x2168('0x5e')](_0x40fae5,{'showHidden':!![],'depth':null}));});};exports[_0x2168('0x5f')]=function(_0x4fd431,_0x5aa94a,_0x12db02,_0x423259,_0x24fabc,_0x308c6f,_0x13b260){var _0x5b8d38,_0x228490;_0x308c6f[_0x2168('0x1a')]=intUtil['stripTrailingSlash'](_0x308c6f[_0x2168('0x1a')]);_0x308c6f[_0x2168('0x47')]=intUtil[_0x2168('0x46')](_0x308c6f[_0x2168('0x47')]);if(_0x13b260&&_0x13b260[_0x2168('0x60')]&&_0x13b260[_0x2168('0x60')]!=='none'){logger[_0x2168('0xa')](_0x2168('0x61'),_0x2168('0x49'));_0x5aa94a['recordingURL']=util['format'](_0x2168('0x4b'),_0x308c6f[_0x2168('0x47')],_0x5aa94a[_0x2168('0x4c')],md5(_0x5aa94a['uniqueid']));}headers[_0x2168('0x4d')]=getAuth(_0x308c6f);return getUser(_0x308c6f,_0x423259[_0x2168('0x62')])[_0x2168('0x15')](function(_0x2a2520){_0x5b8d38=_0x2a2520;logger[_0x2168('0xa')](_0x2168('0x4e'),_0x5b8d38['id']);return getEndUser(_0x308c6f,_0x5aa94a['destcalleridname'],_0x5aa94a[_0x2168('0x63')]);})[_0x2168('0x15')](function(_0x5c9359){_0x228490=_0x5c9359;logger[_0x2168('0xa')]('The\x20Enduser\x20ID\x20is:',_0x228490['id']);return getTicket(_0x308c6f,_0x5b8d38,_0x228490,_0x12db02,_0x5aa94a);})[_0x2168('0x15')](function(_0x1a8b7d){if(_0x1a8b7d){logger['info'](_0x2168('0x51'),_0x1a8b7d['id']);switch(_0x308c6f[_0x2168('0x39')]){case _0x2168('0x52'):logger[_0x2168('0xa')](_0x2168('0x61'),_0x2168('0x54'));return request({'uri':util[_0x2168('0xb')]('%s/channels/voice/agents/%d/tickets/%d/display.json',_0x308c6f[_0x2168('0x1a')],_0x5b8d38['id'],_0x1a8b7d['id']),'method':_0x2168('0xd')});case _0x2168('0x55'):logger[_0x2168('0xa')]('[OUTBOUND]',_0x2168('0x56'));emit(util[_0x2168('0xb')]('user:%s',_0x423259[_0x2168('0x10')]),_0x2168('0x58'),{'uri':util[_0x2168('0xb')](_0x2168('0x59'),_0x308c6f[_0x2168('0x1a')][_0x2168('0x64')]('/api/v2',''),_0x1a8b7d['id'])});break;default:logger[_0x2168('0x5b')](_0x2168('0x61'),_0x2168('0x65'));}}})['catch'](function(_0x46a10e){logger[_0x2168('0x5b')](_0x2168('0x66'),util[_0x2168('0x5e')](_0x46a10e,{'showHidden':!![],'depth':null}));});};exports[_0x2168('0x67')]=function(_0x1554d4,_0x2697dc,_0x4c2845,_0x4a3e92,_0x14e2c7,_0x4923e2){var _0x3916f5,_0xbbbd33;_0x4923e2[_0x2168('0x1a')]=intUtil[_0x2168('0x46')](_0x4923e2[_0x2168('0x1a')]);_0x4923e2[_0x2168('0x47')]=intUtil['stripTrailingSlash'](_0x4923e2[_0x2168('0x47')]);if(_0x14e2c7&&_0x14e2c7['monitor_format']){logger['info']('['+_0x2697dc[_0x2168('0x68')]['toUpperCase']()+']',_0x2168('0x49'));_0x2697dc[_0x2168('0x4a')]=util[_0x2168('0xb')](_0x2168('0x4b'),_0x4923e2['serverUrl'],_0x2697dc['uniqueid'],md5(_0x2697dc['uniqueid']));}else{_0x2697dc['recordingURL']='';}headers[_0x2168('0x4d')]=getAuth(_0x4923e2);return getUser(_0x4923e2)[_0x2168('0x15')](function(_0x4cb213){_0x3916f5=_0x4cb213;logger['info'](_0x2168('0x4e'),_0x3916f5['id']);return getEndUser(_0x4923e2,_0x2697dc[_0x2168('0x4f')],_0x2697dc[_0x2168('0x69')]);})[_0x2168('0x15')](function(_0x2dda33){_0xbbbd33=_0x2dda33;logger[_0x2168('0xa')]('The\x20Enduser\x20ID\x20is:',_0xbbbd33['id']);return getTicket(_0x4923e2,_0x3916f5,_0xbbbd33,_0x4c2845,_0x2697dc);})[_0x2168('0x15')](function(_0xa402b9){if(_0xa402b9){logger['info'](_0x2168('0x51'),_0xa402b9['id']);}})[_0x2168('0x5c')](function(_0x5acd04){logger['error']('['+_0x2697dc['lastevent'][_0x2168('0x6a')]()+']'+_0x2168('0x6b'),util['inspect'](_0x5acd04,{'showHidden':!![],'depth':null}));});};