Built motion from commit 3c2ce842.|2.6.31
[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 _0x214f=['idField','customField','custom_fields','push','variable','variableName','customVariable','Variable','queue','stripTrailingSlash','serverUrl','monitor_format','Recording\x20is\x20enabled!','recordingURL','uniqueid','The\x20User\x20ID\x20is:','calleridnum','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','[QUEUE]','Displaying\x20ticket\x20to\x20agent...','%s/channels/voice/agents/%d/tickets/%d/display.json','newTab','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','trigger:browser:url','%s/agent/tickets/%s','replace','error','Zendesk\x20account\x20type\x20not\x20recognized','catch','inspect','outbound','recordingFormat','none','[OUTBOUND]','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','Authorization','email','destcalleridname','destcalleridnum','user:%s','/api/v2','lastevent','toUpperCase','calleridname','\x20Error\x20running\x20Zendesk\x20integration','lodash','request-promise','md5','util','ioredis','./util','../../../../config/environment','../../../../config/logger','zendesk','redis','defaults','socket.io-emitter','emit','application/json','info','Creating\x20new\x20ticket...','format','%s/tickets.json','remoteUri','POST','merge','Tags','name','getString','Descriptions','Fields','then','Ticket\x20created!','validateEmail','%s/search.json','count','find','results','agent','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/users/me.json','Admin\x20account\x20selected.','user','Searching\x20enduser\x20with\x20phone','type:user\x20role:end-user\x20phone:%s','length','filter','end-user','first','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','%s/users.json','Caller\x20-\x20%s','Enduser\x20created.','authType','password','token','username','Basic\x20%s','toString','base64','debug','stringify','GET','forEach','includes','string','picklist','type','nameField','toLowerCase','content'];(function(_0x42dd6c,_0x4ea28c){var _0x3346a2=function(_0x17877b){while(--_0x17877b){_0x42dd6c['push'](_0x42dd6c['shift']());}};_0x3346a2(++_0x4ea28c);}(_0x214f,0x181));var _0xf214=function(_0x2bc919,_0x1a22c0){_0x2bc919=_0x2bc919-0x0;var _0x274fbf=_0x214f[_0x2bc919];return _0x274fbf;};'use strict';var _=require(_0xf214('0x0'));var rp=require(_0xf214('0x1'));var md5=require(_0xf214('0x2'));var util=require(_0xf214('0x3'));var Redis=require(_0xf214('0x4'));var intUtil=require(_0xf214('0x5'));var config=require(_0xf214('0x6'));var logger=require(_0xf214('0x7'))(_0xf214('0x8'));config[_0xf214('0x9')]=_[_0xf214('0xa')](config[_0xf214('0x9')],{'host':'localhost','port':0x18eb});var io=require(_0xf214('0xb'))(new Redis(config[_0xf214('0x9')]));function emit(_0x211054,_0x734e2a,_0x3f8657){io['to'](_0x211054)[_0xf214('0xc')](_0x734e2a,_0x3f8657);}var headers={'Content-Type':_0xf214('0xd'),'Accept':'application/json'};function getTicket(_0x47156b,_0x4c6e35,_0xe4a442,_0x4612fb,_0x3c4756){logger[_0xf214('0xe')](_0xf214('0xf'));return request({'uri':util[_0xf214('0x10')](_0xf214('0x11'),_0x47156b[_0xf214('0x12')]),'method':_0xf214('0x13'),'body':{'ticket':_[_0xf214('0x14')]({'requester_id':_0xe4a442['id'],'submitter_id':_0x4c6e35?_0x4c6e35['id']:undefined,'assignee_id':_0x4c6e35?_0x4c6e35['id']:undefined,'tags':getTags(_0x4612fb[_0xf214('0x15')],_0xf214('0x16')),'subject':intUtil['getString'](_0x4612fb['Subjects'],_0x3c4756,'\x20'),'description':intUtil[_0xf214('0x17')](_0x4612fb[_0xf214('0x18')],_0x3c4756,'\x0a'),'comment':{'public':![],'body':intUtil[_0xf214('0x17')](_0x4612fb['Descriptions'],_0x3c4756,'\x0a')}},getCustomFields(_0x4612fb[_0xf214('0x19')],_0x3c4756))}})[_0xf214('0x1a')](function(_0x42fc64){logger[_0xf214('0xe')](_0xf214('0x1b'));return _0x42fc64['ticket'];});}function getUser(_0x23ba17,_0x514a89){if(intUtil[_0xf214('0x1c')](_0x514a89)){logger['info']('Getting\x20agent\x20with\x20email',_0x514a89);return request({'uri':util[_0xf214('0x10')](_0xf214('0x1d'),_0x23ba17[_0xf214('0x12')]),'qs':{'query':util[_0xf214('0x10')]('type:user\x20role:agent\x20email:%s',_0x514a89)}})[_0xf214('0x1a')](function(_0x30df34){if(_0x30df34[_0xf214('0x1e')]){logger['info']('Agent\x20found!');return _[_0xf214('0x1f')](_0x30df34[_0xf214('0x20')],{'email':_0x514a89,'role':_0xf214('0x21')});}else{logger[_0xf214('0xe')](_0xf214('0x22'));return request({'uri':util['format'](_0xf214('0x23'),_0x23ba17[_0xf214('0x12')])})[_0xf214('0x1a')](function(_0xfc92b8){logger[_0xf214('0xe')](_0xf214('0x24'));return _0xfc92b8[_0xf214('0x25')];});}});}else{logger[_0xf214('0xe')]('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');return request({'uri':util[_0xf214('0x10')](_0xf214('0x23'),_0x23ba17[_0xf214('0x12')])})['then'](function(_0x54fe5a){logger[_0xf214('0xe')](_0xf214('0x24'));return _0x54fe5a[_0xf214('0x25')];});}}function getEndUser(_0x51844f,_0x16f425,_0x52df8e){logger[_0xf214('0xe')](_0xf214('0x26'),_0x52df8e);return request({'uri':util[_0xf214('0x10')](_0xf214('0x1d'),_0x51844f[_0xf214('0x12')]),'qs':{'query':util[_0xf214('0x10')](_0xf214('0x27'),_0x52df8e)}})[_0xf214('0x1a')](function(_0x478526){var _0x3d5963;if(_0x478526[_0xf214('0x20')][_0xf214('0x28')]){_0x478526[_0xf214('0x20')]=_[_0xf214('0x29')](_0x478526[_0xf214('0x20')],{'suspended':![],'role':_0xf214('0x2a')});if(_0x478526['results'][_0xf214('0x28')]){_0x3d5963=_[_0xf214('0x2b')](_0x478526['results']);}}if(_0x3d5963){logger[_0xf214('0xe')](_0xf214('0x2c'));return _0x3d5963;}else{logger['info'](_0xf214('0x2d'));return request({'uri':util[_0xf214('0x10')](_0xf214('0x2e'),_0x51844f[_0xf214('0x12')]),'method':_0xf214('0x13'),'body':{'user':{'role':_0xf214('0x2a'),'name':util[_0xf214('0x10')](_0xf214('0x2f'),_0x16f425||_0x52df8e),'phone':_0x52df8e}}})['then'](function(_0x1ca9fb){logger['info'](_0xf214('0x30'));return _0x1ca9fb[_0xf214('0x25')];});}});}function getAuth(_0x523677){var _0x5d5f53;if(_0x523677[_0xf214('0x31')]===_0xf214('0x32')){_0x5d5f53=util['format']('%s:%s',_0x523677['username'],_0x523677[_0xf214('0x32')]);}else if(_0x523677[_0xf214('0x31')]===_0xf214('0x33')){_0x5d5f53=util['format']('%s/token:%s',_0x523677[_0xf214('0x34')],_0x523677[_0xf214('0x33')]);}return util[_0xf214('0x10')](_0xf214('0x35'),new Buffer(_0x5d5f53)[_0xf214('0x36')](_0xf214('0x37')));}function request(_0x5c2f8c){logger[_0xf214('0x38')]('[HTTP]',JSON[_0xf214('0x39')](_0x5c2f8c));return rp(_[_0xf214('0xa')](_0x5c2f8c,{'method':_0xf214('0x3a'),'headers':headers,'json':!![]}));}function getTags(_0x461211,_0xe1a1f7){var _0xd07b4f=[];if(_0x461211&&_0x461211['length']>0x0){_0xd07b4f=_['map'](_0x461211,_0xe1a1f7);}return _0xd07b4f;}function getCustomFields(_0x36fda8,_0x21e71e){var _0xf0ba3=[];var _0x267371={'custom_fields':[]};if(_0x36fda8[_0xf214('0x28')]>0x0){_[_0xf214('0x3b')](_0x36fda8,function(_0x4803e7){if(_[_0xf214('0x3c')]([_0xf214('0x3d'),_0xf214('0x3e')],_0x4803e7[_0xf214('0x3f')])&&_0x4803e7[_0xf214('0x40')]&&!_0x4803e7['customField']){_0x267371[_0x4803e7['nameField'][_0xf214('0x41')]()]=_0x4803e7[_0xf214('0x42')][_0xf214('0x36')]();}else if(_[_0xf214('0x3c')](['string',_0xf214('0x3e')],_0x4803e7['type'])&&_0x4803e7[_0xf214('0x43')]&&_0x4803e7[_0xf214('0x44')]){_0x267371[_0xf214('0x45')][_0xf214('0x46')]({'id':_0x4803e7[_0xf214('0x43')],'value':_0x4803e7[_0xf214('0x42')][_0xf214('0x36')]()});}else if(_0x4803e7[_0xf214('0x3f')]===_0xf214('0x47')&&_0x4803e7['variableName']&&_0x4803e7[_0xf214('0x43')]&&_0x4803e7[_0xf214('0x44')]){_0x21e71e[_0x4803e7[_0xf214('0x48')]]&&_0x267371[_0xf214('0x45')]['push']({'id':_0x4803e7[_0xf214('0x43')],'value':_0x21e71e[_0x4803e7[_0xf214('0x48')]]['toString']()});}else if(_0x4803e7[_0xf214('0x3f')]===_0xf214('0x49')&&_0x4803e7[_0xf214('0x4a')]&&_0x4803e7[_0xf214('0x4a')]['name']&&_0x4803e7[_0xf214('0x43')]&&_0x4803e7['customField']){_0x21e71e[_0x4803e7['Variable']['name'][_0xf214('0x41')]()]&&_0x267371[_0xf214('0x45')][_0xf214('0x46')]({'id':_0x4803e7[_0xf214('0x43')],'value':_0x21e71e[_0x4803e7[_0xf214('0x4a')][_0xf214('0x16')][_0xf214('0x41')]()][_0xf214('0x36')]()});}});}return _0x267371;}exports[_0xf214('0x4b')]=function(_0x32e686,_0x5afdcf,_0x4a9c2,_0x54e468,_0x35eb62,_0x58ee26){var _0x28051c,_0x589d4d;_0x58ee26[_0xf214('0x12')]=intUtil[_0xf214('0x4c')](_0x58ee26[_0xf214('0x12')]);_0x58ee26['serverUrl']=intUtil[_0xf214('0x4c')](_0x58ee26[_0xf214('0x4d')]);if(_0x35eb62&&_0x35eb62[_0xf214('0x4e')]){logger[_0xf214('0xe')]('[QUEUE]',_0xf214('0x4f'));_0x5afdcf[_0xf214('0x50')]=util[_0xf214('0x10')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x58ee26[_0xf214('0x4d')],_0x5afdcf[_0xf214('0x51')],md5(_0x5afdcf[_0xf214('0x51')]));}else{_0x5afdcf['recordingURL']='';}headers['Authorization']=getAuth(_0x58ee26);return getUser(_0x58ee26,_0x54e468['email'])['then'](function(_0x963261){_0x28051c=_0x963261;logger[_0xf214('0xe')](_0xf214('0x52'),_0x28051c['id']);return getEndUser(_0x58ee26,_0x5afdcf['calleridname'],_0x5afdcf[_0xf214('0x53')]);})[_0xf214('0x1a')](function(_0x2ac5ea){_0x589d4d=_0x2ac5ea;logger[_0xf214('0xe')](_0xf214('0x54'),_0x589d4d['id']);return getTicket(_0x58ee26,_0x28051c,_0x589d4d,_0x4a9c2,_0x5afdcf);})[_0xf214('0x1a')](function(_0x31a5ce){if(_0x31a5ce){logger[_0xf214('0xe')](_0xf214('0x55'),_0x31a5ce['id']);switch(_0x58ee26['type']){case'integrationTab':logger[_0xf214('0xe')](_0xf214('0x56'),_0xf214('0x57'));return request({'uri':util[_0xf214('0x10')](_0xf214('0x58'),_0x58ee26['remoteUri'],_0x28051c['id'],_0x31a5ce['id']),'method':_0xf214('0x13')});case _0xf214('0x59'):logger[_0xf214('0xe')]('[QUEUE]',_0xf214('0x5a'));emit(util[_0xf214('0x10')]('user:%s',_0x54e468[_0xf214('0x16')]),_0xf214('0x5b'),{'uri':util[_0xf214('0x10')](_0xf214('0x5c'),_0x58ee26[_0xf214('0x12')][_0xf214('0x5d')]('/api/v2',''),_0x31a5ce['id'])});break;default:logger[_0xf214('0x5e')]('[QUEUE]',_0xf214('0x5f'));}}})[_0xf214('0x60')](function(_0x37b5dc){logger[_0xf214('0x5e')]('[QUEUE]\x20Error\x20running\x20Zendesk\x20integration',util[_0xf214('0x61')](_0x37b5dc,{'showHidden':!![],'depth':null}));});};exports[_0xf214('0x62')]=function(_0x556e99,_0x4b153e,_0x13d20f,_0x1059d6,_0x41d38a,_0xb62034,_0x3a7bb5){var _0xe0cfca,_0x35bd0b;_0xb62034[_0xf214('0x12')]=intUtil[_0xf214('0x4c')](_0xb62034[_0xf214('0x12')]);_0xb62034[_0xf214('0x4d')]=intUtil[_0xf214('0x4c')](_0xb62034[_0xf214('0x4d')]);if(_0x3a7bb5&&_0x3a7bb5[_0xf214('0x63')]&&_0x3a7bb5[_0xf214('0x63')]!==_0xf214('0x64')){logger[_0xf214('0xe')](_0xf214('0x65'),'Recording\x20is\x20enabled!');_0x4b153e['recordingURL']=util[_0xf214('0x10')](_0xf214('0x66'),_0xb62034[_0xf214('0x4d')],_0x4b153e[_0xf214('0x51')],md5(_0x4b153e[_0xf214('0x51')]));}headers[_0xf214('0x67')]=getAuth(_0xb62034);return getUser(_0xb62034,_0x1059d6[_0xf214('0x68')])[_0xf214('0x1a')](function(_0x3b6d7b){_0xe0cfca=_0x3b6d7b;logger['info'](_0xf214('0x52'),_0xe0cfca['id']);return getEndUser(_0xb62034,_0x4b153e[_0xf214('0x69')],_0x4b153e[_0xf214('0x6a')]);})[_0xf214('0x1a')](function(_0x37c902){_0x35bd0b=_0x37c902;logger[_0xf214('0xe')](_0xf214('0x54'),_0x35bd0b['id']);return getTicket(_0xb62034,_0xe0cfca,_0x35bd0b,_0x13d20f,_0x4b153e);})[_0xf214('0x1a')](function(_0x224336){if(_0x224336){logger[_0xf214('0xe')](_0xf214('0x55'),_0x224336['id']);switch(_0xb62034[_0xf214('0x3f')]){case'integrationTab':logger[_0xf214('0xe')](_0xf214('0x65'),'Displaying\x20ticket\x20to\x20agent...');return request({'uri':util[_0xf214('0x10')]('%s/channels/voice/agents/%d/tickets/%d/display.json',_0xb62034[_0xf214('0x12')],_0xe0cfca['id'],_0x224336['id']),'method':_0xf214('0x13')});case'newTab':logger[_0xf214('0xe')](_0xf214('0x65'),_0xf214('0x5a'));emit(util[_0xf214('0x10')](_0xf214('0x6b'),_0x1059d6[_0xf214('0x16')]),_0xf214('0x5b'),{'uri':util['format'](_0xf214('0x5c'),_0xb62034[_0xf214('0x12')][_0xf214('0x5d')](_0xf214('0x6c'),''),_0x224336['id'])});break;default:logger[_0xf214('0x5e')](_0xf214('0x65'),'Zendesk\x20account\x20type\x20not\x20recognized');}}})['catch'](function(_0x32c632){logger[_0xf214('0x5e')]('[OUTBOUND]\x20Error\x20running\x20Zendesk\x20integration',util['inspect'](_0x32c632,{'showHidden':!![],'depth':null}));});};exports['unmanaged']=function(_0x42608f,_0x23c585,_0x3f8810,_0x463724,_0x58fe48,_0x251ae1){var _0xaa8520,_0x43502b;_0x251ae1[_0xf214('0x12')]=intUtil[_0xf214('0x4c')](_0x251ae1[_0xf214('0x12')]);_0x251ae1[_0xf214('0x4d')]=intUtil[_0xf214('0x4c')](_0x251ae1[_0xf214('0x4d')]);if(_0x58fe48&&_0x58fe48['monitor_format']){logger['info']('['+_0x23c585[_0xf214('0x6d')][_0xf214('0x6e')]()+']','Recording\x20is\x20enabled!');_0x23c585[_0xf214('0x50')]=util[_0xf214('0x10')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x251ae1[_0xf214('0x4d')],_0x23c585[_0xf214('0x51')],md5(_0x23c585['uniqueid']));}else{_0x23c585[_0xf214('0x50')]='';}headers[_0xf214('0x67')]=getAuth(_0x251ae1);return getUser(_0x251ae1)[_0xf214('0x1a')](function(_0xd91042){_0xaa8520=_0xd91042;logger['info'](_0xf214('0x52'),_0xaa8520['id']);return getEndUser(_0x251ae1,_0x23c585[_0xf214('0x6f')],_0x23c585[_0xf214('0x53')]);})[_0xf214('0x1a')](function(_0x55a60e){_0x43502b=_0x55a60e;logger['info'](_0xf214('0x54'),_0x43502b['id']);return getTicket(_0x251ae1,_0xaa8520,_0x43502b,_0x3f8810,_0x23c585);})[_0xf214('0x1a')](function(_0x3a0325){if(_0x3a0325){logger[_0xf214('0xe')](_0xf214('0x55'),_0x3a0325['id']);}})['catch'](function(_0xd608ea){logger[_0xf214('0x5e')]('['+_0x23c585[_0xf214('0x6d')][_0xf214('0x6e')]()+']'+_0xf214('0x70'),util[_0xf214('0x61')](_0xd608ea,{'showHidden':!![],'depth':null}));});};