d705d2df5d5fb01316931bf6127ca21f56009c88
[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 _0x910f=['destcalleridname','The\x20Ticket\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent...','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','[OUTBOUND]\x20Error\x20running\x20Zendesk\x20integration','toUpperCase','The\x20User\x20ID\x20is:','calleridname','\x20Error\x20running\x20Zendesk\x20integration','lodash','md5','util','./util','../../../../config/environment','../../../../config/logger','redis','localhost','socket.io-emitter','application/json','info','format','%s/tickets.json','remoteUri','POST','merge','Tags','name','getString','Subjects','Descriptions','then','Ticket\x20created!','ticket','validateEmail','Getting\x20agent\x20with\x20email','%s/search.json','type:user\x20role:agent\x20email:%s','Agent\x20found!','results','agent','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/users/me.json','user','Searching\x20enduser\x20with\x20phone','type:user\x20role:end-user\x20phone:%s','length','filter','end-user','first','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Enduser\x20created.','authType','%s:%s','username','password','token','%s/token:%s','Basic\x20%s','base64','[HTTP]','stringify','defaults','map','forEach','includes','string','picklist','type','nameField','customField','toLowerCase','content','custom_fields','push','idField','toString','variableName','customVariable','Variable','queue','stripTrailingSlash','serverUrl','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','uniqueid','Authorization','email','calleridnum','The\x20Enduser\x20ID\x20is:','integrationTab','%s/channels/voice/agents/%d/tickets/%d/display.json','newTab','user:%s','trigger:browser:url','%s/agent/tickets/%s','replace','/api/v2','error','Zendesk\x20account\x20type\x20not\x20recognized','catch','inspect','recordingFormat','none','[OUTBOUND]','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s'];(function(_0x548ee0,_0x4817f2){var _0x434d96=function(_0x141527){while(--_0x141527){_0x548ee0['push'](_0x548ee0['shift']());}};_0x434d96(++_0x4817f2);}(_0x910f,0x74));var _0xf910=function(_0x28d6b9,_0xbe9964){_0x28d6b9=_0x28d6b9-0x0;var _0x1e2bee=_0x910f[_0x28d6b9];return _0x1e2bee;};'use strict';var _=require(_0xf910('0x0'));var rp=require('request-promise');var md5=require(_0xf910('0x1'));var util=require(_0xf910('0x2'));var Redis=require('ioredis');var intUtil=require(_0xf910('0x3'));var config=require(_0xf910('0x4'));var logger=require(_0xf910('0x5'))('zendesk');config[_0xf910('0x6')]=_['defaults'](config[_0xf910('0x6')],{'host':_0xf910('0x7'),'port':0x18eb});var io=require(_0xf910('0x8'))(new Redis(config[_0xf910('0x6')]));function emit(_0x5e7f61,_0x3664f8,_0x5e909a){io['to'](_0x5e7f61)['emit'](_0x3664f8,_0x5e909a);}var headers={'Content-Type':_0xf910('0x9'),'Accept':_0xf910('0x9')};function getTicket(_0x1fcfe2,_0x3ebc5d,_0x3f7736,_0x10f395,_0x23c2ea){logger[_0xf910('0xa')]('Creating\x20new\x20ticket...');return request({'uri':util[_0xf910('0xb')](_0xf910('0xc'),_0x1fcfe2[_0xf910('0xd')]),'method':_0xf910('0xe'),'body':{'ticket':_[_0xf910('0xf')]({'requester_id':_0x3f7736['id'],'submitter_id':_0x3ebc5d?_0x3ebc5d['id']:undefined,'assignee_id':_0x3ebc5d?_0x3ebc5d['id']:undefined,'tags':getTags(_0x10f395[_0xf910('0x10')],_0xf910('0x11')),'subject':intUtil[_0xf910('0x12')](_0x10f395[_0xf910('0x13')],_0x23c2ea,'\x20'),'description':intUtil['getString'](_0x10f395[_0xf910('0x14')],_0x23c2ea,'\x0a'),'comment':{'public':![],'body':intUtil[_0xf910('0x12')](_0x10f395[_0xf910('0x14')],_0x23c2ea,'\x0a')}},getCustomFields(_0x10f395['Fields'],_0x23c2ea))}})[_0xf910('0x15')](function(_0x267ad9){logger[_0xf910('0xa')](_0xf910('0x16'));return _0x267ad9[_0xf910('0x17')];});}function getUser(_0x3b53d6,_0x4d0166){if(intUtil[_0xf910('0x18')](_0x4d0166)){logger[_0xf910('0xa')](_0xf910('0x19'),_0x4d0166);return request({'uri':util['format'](_0xf910('0x1a'),_0x3b53d6[_0xf910('0xd')]),'qs':{'query':util[_0xf910('0xb')](_0xf910('0x1b'),_0x4d0166)}})['then'](function(_0x25c45d){if(_0x25c45d['count']){logger['info'](_0xf910('0x1c'));return _['find'](_0x25c45d[_0xf910('0x1d')],{'email':_0x4d0166,'role':_0xf910('0x1e')});}else{logger[_0xf910('0xa')](_0xf910('0x1f'));return request({'uri':util[_0xf910('0xb')](_0xf910('0x20'),_0x3b53d6['remoteUri'])})[_0xf910('0x15')](function(_0x3fa4d6){logger['info']('Admin\x20account\x20selected.');return _0x3fa4d6['user'];});}});}else{logger[_0xf910('0xa')](_0xf910('0x1f'));return request({'uri':util[_0xf910('0xb')](_0xf910('0x20'),_0x3b53d6[_0xf910('0xd')])})['then'](function(_0x1c6f31){logger[_0xf910('0xa')]('Admin\x20account\x20selected.');return _0x1c6f31[_0xf910('0x21')];});}}function getEndUser(_0x335b7c,_0x417b9c,_0x463ed8){logger[_0xf910('0xa')](_0xf910('0x22'),_0x463ed8);return request({'uri':util[_0xf910('0xb')](_0xf910('0x1a'),_0x335b7c[_0xf910('0xd')]),'qs':{'query':util['format'](_0xf910('0x23'),_0x463ed8)}})[_0xf910('0x15')](function(_0x4bd739){var _0x280fb8;if(_0x4bd739[_0xf910('0x1d')][_0xf910('0x24')]){_0x4bd739[_0xf910('0x1d')]=_[_0xf910('0x25')](_0x4bd739[_0xf910('0x1d')],{'suspended':![],'role':_0xf910('0x26')});if(_0x4bd739[_0xf910('0x1d')][_0xf910('0x24')]){_0x280fb8=_[_0xf910('0x27')](_0x4bd739[_0xf910('0x1d')]);}}if(_0x280fb8){logger[_0xf910('0xa')]('Enduser\x20found!');return _0x280fb8;}else{logger[_0xf910('0xa')](_0xf910('0x28'));return request({'uri':util['format']('%s/users.json',_0x335b7c[_0xf910('0xd')]),'method':_0xf910('0xe'),'body':{'user':{'role':_0xf910('0x26'),'name':util['format']('Caller\x20-\x20%s',_0x417b9c||_0x463ed8),'phone':_0x463ed8}}})[_0xf910('0x15')](function(_0x49d1){logger[_0xf910('0xa')](_0xf910('0x29'));return _0x49d1[_0xf910('0x21')];});}});}function getAuth(_0x246a30){var _0x491859;if(_0x246a30[_0xf910('0x2a')]==='password'){_0x491859=util[_0xf910('0xb')](_0xf910('0x2b'),_0x246a30[_0xf910('0x2c')],_0x246a30[_0xf910('0x2d')]);}else if(_0x246a30[_0xf910('0x2a')]===_0xf910('0x2e')){_0x491859=util['format'](_0xf910('0x2f'),_0x246a30['username'],_0x246a30[_0xf910('0x2e')]);}return util[_0xf910('0xb')](_0xf910('0x30'),new Buffer(_0x491859)['toString'](_0xf910('0x31')));}function request(_0x2bfcf1){logger['debug'](_0xf910('0x32'),JSON[_0xf910('0x33')](_0x2bfcf1));return rp(_[_0xf910('0x34')](_0x2bfcf1,{'method':'GET','headers':headers,'json':!![]}));}function getTags(_0x472453,_0x2a8ded){var _0x4b0e35=[];if(_0x472453&&_0x472453[_0xf910('0x24')]>0x0){_0x4b0e35=_[_0xf910('0x35')](_0x472453,_0x2a8ded);}return _0x4b0e35;}function getCustomFields(_0xe187cf,_0x629350){var _0x3e7735=[];var _0x38f958={'custom_fields':[]};if(_0xe187cf['length']>0x0){_[_0xf910('0x36')](_0xe187cf,function(_0x25ec15){if(_[_0xf910('0x37')]([_0xf910('0x38'),_0xf910('0x39')],_0x25ec15[_0xf910('0x3a')])&&_0x25ec15[_0xf910('0x3b')]&&!_0x25ec15[_0xf910('0x3c')]){_0x38f958[_0x25ec15[_0xf910('0x3b')][_0xf910('0x3d')]()]=_0x25ec15[_0xf910('0x3e')]['toString']();}else if(_[_0xf910('0x37')]([_0xf910('0x38'),_0xf910('0x39')],_0x25ec15[_0xf910('0x3a')])&&_0x25ec15['idField']&&_0x25ec15[_0xf910('0x3c')]){_0x38f958[_0xf910('0x3f')][_0xf910('0x40')]({'id':_0x25ec15[_0xf910('0x41')],'value':_0x25ec15['content'][_0xf910('0x42')]()});}else if(_0x25ec15[_0xf910('0x3a')]==='variable'&&_0x25ec15[_0xf910('0x43')]&&_0x25ec15[_0xf910('0x41')]&&_0x25ec15['customField']){_0x629350[_0x25ec15[_0xf910('0x43')]]&&_0x38f958[_0xf910('0x3f')][_0xf910('0x40')]({'id':_0x25ec15[_0xf910('0x41')],'value':_0x629350[_0x25ec15['variableName']][_0xf910('0x42')]()});}else if(_0x25ec15[_0xf910('0x3a')]===_0xf910('0x44')&&_0x25ec15['Variable']&&_0x25ec15[_0xf910('0x45')][_0xf910('0x11')]&&_0x25ec15[_0xf910('0x41')]&&_0x25ec15[_0xf910('0x3c')]){_0x629350[_0x25ec15[_0xf910('0x45')][_0xf910('0x11')][_0xf910('0x3d')]()]&&_0x38f958[_0xf910('0x3f')][_0xf910('0x40')]({'id':_0x25ec15['idField'],'value':_0x629350[_0x25ec15['Variable'][_0xf910('0x11')]['toLowerCase']()][_0xf910('0x42')]()});}});}return _0x38f958;}exports[_0xf910('0x46')]=function(_0x4cf406,_0x380ca8,_0x168c2b,_0x5988bc,_0x435847,_0x15c3b2){var _0x1f42aa,_0x4f96a7;_0x15c3b2[_0xf910('0xd')]=intUtil[_0xf910('0x47')](_0x15c3b2[_0xf910('0xd')]);_0x15c3b2[_0xf910('0x48')]=intUtil[_0xf910('0x47')](_0x15c3b2[_0xf910('0x48')]);if(_0x435847&&_0x435847[_0xf910('0x49')]){logger['info'](_0xf910('0x4a'),_0xf910('0x4b'));_0x380ca8[_0xf910('0x4c')]=util[_0xf910('0xb')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x15c3b2['serverUrl'],_0x380ca8[_0xf910('0x4d')],md5(_0x380ca8[_0xf910('0x4d')]));}else{_0x380ca8['recordingURL']='';}headers[_0xf910('0x4e')]=getAuth(_0x15c3b2);return getUser(_0x15c3b2,_0x5988bc[_0xf910('0x4f')])[_0xf910('0x15')](function(_0x586bfb){_0x1f42aa=_0x586bfb;logger['info']('The\x20User\x20ID\x20is:',_0x1f42aa['id']);return getEndUser(_0x15c3b2,_0x380ca8['calleridname'],_0x380ca8[_0xf910('0x50')]);})[_0xf910('0x15')](function(_0x2977bd){_0x4f96a7=_0x2977bd;logger[_0xf910('0xa')](_0xf910('0x51'),_0x4f96a7['id']);return getTicket(_0x15c3b2,_0x1f42aa,_0x4f96a7,_0x168c2b,_0x380ca8);})['then'](function(_0x70c946){if(_0x70c946){logger['info']('The\x20Ticket\x20ID\x20is:',_0x70c946['id']);switch(_0x15c3b2[_0xf910('0x3a')]){case _0xf910('0x52'):logger['info'](_0xf910('0x4a'),'Displaying\x20ticket\x20to\x20agent...');return request({'uri':util[_0xf910('0xb')](_0xf910('0x53'),_0x15c3b2[_0xf910('0xd')],_0x1f42aa['id'],_0x70c946['id']),'method':_0xf910('0xe')});case _0xf910('0x54'):logger[_0xf910('0xa')]('[QUEUE]','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0xf910('0xb')](_0xf910('0x55'),_0x5988bc['name']),_0xf910('0x56'),{'uri':util['format'](_0xf910('0x57'),_0x15c3b2[_0xf910('0xd')][_0xf910('0x58')](_0xf910('0x59'),''),_0x70c946['id'])});break;default:logger[_0xf910('0x5a')](_0xf910('0x4a'),_0xf910('0x5b'));}}})[_0xf910('0x5c')](function(_0x470f34){logger['error']('[QUEUE]\x20Error\x20running\x20Zendesk\x20integration',util[_0xf910('0x5d')](_0x470f34,{'showHidden':!![],'depth':null}));});};exports['outbound']=function(_0x229542,_0x2bd50c,_0xb1d71c,_0x41d549,_0x54a1f6,_0x295062,_0x511a1f){var _0x59f8d6,_0x5a77cc;_0x295062[_0xf910('0xd')]=intUtil['stripTrailingSlash'](_0x295062['remoteUri']);_0x295062[_0xf910('0x48')]=intUtil[_0xf910('0x47')](_0x295062[_0xf910('0x48')]);if(_0x511a1f&&_0x511a1f[_0xf910('0x5e')]&&_0x511a1f['recordingFormat']!==_0xf910('0x5f')){logger['info'](_0xf910('0x60'),_0xf910('0x4b'));_0x2bd50c[_0xf910('0x4c')]=util[_0xf910('0xb')](_0xf910('0x61'),_0x295062[_0xf910('0x48')],_0x2bd50c['uniqueid'],md5(_0x2bd50c[_0xf910('0x4d')]));}headers[_0xf910('0x4e')]=getAuth(_0x295062);return getUser(_0x295062,_0x41d549[_0xf910('0x4f')])[_0xf910('0x15')](function(_0x277b80){_0x59f8d6=_0x277b80;logger[_0xf910('0xa')]('The\x20User\x20ID\x20is:',_0x59f8d6['id']);return getEndUser(_0x295062,_0x2bd50c[_0xf910('0x62')],_0x2bd50c['destcalleridnum']);})['then'](function(_0x4986c2){_0x5a77cc=_0x4986c2;logger['info'](_0xf910('0x51'),_0x5a77cc['id']);return getTicket(_0x295062,_0x59f8d6,_0x5a77cc,_0xb1d71c,_0x2bd50c);})['then'](function(_0x3e2a51){if(_0x3e2a51){logger['info'](_0xf910('0x63'),_0x3e2a51['id']);switch(_0x295062[_0xf910('0x3a')]){case _0xf910('0x52'):logger[_0xf910('0xa')](_0xf910('0x60'),_0xf910('0x64'));return request({'uri':util[_0xf910('0xb')](_0xf910('0x53'),_0x295062[_0xf910('0xd')],_0x59f8d6['id'],_0x3e2a51['id']),'method':'POST'});case _0xf910('0x54'):logger[_0xf910('0xa')](_0xf910('0x60'),_0xf910('0x65'));emit(util[_0xf910('0xb')](_0xf910('0x55'),_0x41d549[_0xf910('0x11')]),'trigger:browser:url',{'uri':util['format'](_0xf910('0x57'),_0x295062[_0xf910('0xd')]['replace']('/api/v2',''),_0x3e2a51['id'])});break;default:logger['error'](_0xf910('0x60'),_0xf910('0x5b'));}}})['catch'](function(_0x52d1e5){logger[_0xf910('0x5a')](_0xf910('0x66'),util[_0xf910('0x5d')](_0x52d1e5,{'showHidden':!![],'depth':null}));});};exports['unmanaged']=function(_0x470944,_0x1ff360,_0x16ffee,_0x52db2a,_0x4fbf7b,_0x3a9c89){var _0x7c96cc,_0x2e7ff6;_0x3a9c89[_0xf910('0xd')]=intUtil[_0xf910('0x47')](_0x3a9c89[_0xf910('0xd')]);_0x3a9c89['serverUrl']=intUtil[_0xf910('0x47')](_0x3a9c89[_0xf910('0x48')]);if(_0x4fbf7b&&_0x4fbf7b[_0xf910('0x49')]){logger[_0xf910('0xa')]('['+_0x1ff360['lastevent'][_0xf910('0x67')]()+']',_0xf910('0x4b'));_0x1ff360[_0xf910('0x4c')]=util['format'](_0xf910('0x61'),_0x3a9c89[_0xf910('0x48')],_0x1ff360[_0xf910('0x4d')],md5(_0x1ff360[_0xf910('0x4d')]));}else{_0x1ff360[_0xf910('0x4c')]='';}headers[_0xf910('0x4e')]=getAuth(_0x3a9c89);return getUser(_0x3a9c89)[_0xf910('0x15')](function(_0x39463d){_0x7c96cc=_0x39463d;logger[_0xf910('0xa')](_0xf910('0x68'),_0x7c96cc['id']);return getEndUser(_0x3a9c89,_0x1ff360[_0xf910('0x69')],_0x1ff360['calleridnum']);})[_0xf910('0x15')](function(_0xc83801){_0x2e7ff6=_0xc83801;logger['info'](_0xf910('0x51'),_0x2e7ff6['id']);return getTicket(_0x3a9c89,_0x7c96cc,_0x2e7ff6,_0x16ffee,_0x1ff360);})[_0xf910('0x15')](function(_0x41e14e){if(_0x41e14e){logger['info']('The\x20Ticket\x20ID\x20is:',_0x41e14e['id']);}})[_0xf910('0x5c')](function(_0x12c31e){logger[_0xf910('0x5a')]('['+_0x1ff360['lastevent']['toUpperCase']()+']'+_0xf910('0x6a'),util[_0xf910('0x5d')](_0x12c31e,{'showHidden':!![],'depth':null}));});};