cd7a23ef8c852009fbf4381a8e70a3c42e6e406f
[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 _0xadf8=['token','Basic\x20%s','toString','base64','debug','[HTTP]','defaults','length','map','forEach','includes','string','picklist','type','nameField','toLowerCase','idField','customField','custom_fields','push','content','variable','variableName','customVariable','Variable','queue','stripTrailingSlash','serverUrl','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','Authorization','The\x20User\x20ID\x20is:','calleridname','calleridnum','The\x20Enduser\x20ID\x20is:','integrationTab','Displaying\x20ticket\x20to\x20agent...','%s/channels/voice/agents/%d/tickets/%d/display.json','newTab','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','name','trigger:browser:url','%s/agent/tickets/%s','replace','/api/v2','error','Zendesk\x20account\x20type\x20not\x20recognized','catch','[QUEUE]\x20Error\x20running\x20Zendesk\x20integration','inspect','recordingFormat','none','[OUTBOUND]','destcalleridname','destcalleridnum','[OUTBOUND]\x20Error\x20running\x20Zendesk\x20integration','unmanaged','lastevent','toUpperCase','The\x20Ticket\x20ID\x20is:','lodash','request-promise','ioredis','./util','../../../../config/environment','../../../../config/logger','zendesk','redis','localhost','socket.io-emitter','emit','application/json','info','%s/tickets.json','remoteUri','POST','Tags','getString','Subjects','Descriptions','Fields','then','Ticket\x20created!','ticket','format','type:user\x20role:agent\x20email:%s','count','Agent\x20found!','find','results','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','Admin\x20account\x20selected.','user','Searching\x20enduser\x20with\x20phone','type:user\x20role:end-user\x20phone:%s','filter','end-user','first','Enduser\x20found!','%s/users.json','Caller\x20-\x20%s','%s:%s','username','password'];(function(_0x1c6ed5,_0x30afe1){var _0x792592=function(_0x17e1c0){while(--_0x17e1c0){_0x1c6ed5['push'](_0x1c6ed5['shift']());}};_0x792592(++_0x30afe1);}(_0xadf8,0xac));var _0x8adf=function(_0x330141,_0x1dd290){_0x330141=_0x330141-0x0;var _0x1b68a2=_0xadf8[_0x330141];return _0x1b68a2;};'use strict';var _=require(_0x8adf('0x0'));var rp=require(_0x8adf('0x1'));var md5=require('md5');var util=require('util');var Redis=require(_0x8adf('0x2'));var intUtil=require(_0x8adf('0x3'));var config=require(_0x8adf('0x4'));var logger=require(_0x8adf('0x5'))(_0x8adf('0x6'));config[_0x8adf('0x7')]=_['defaults'](config['redis'],{'host':_0x8adf('0x8'),'port':0x18eb});var io=require(_0x8adf('0x9'))(new Redis(config[_0x8adf('0x7')]));function emit(_0x53a39b,_0x49637c,_0x161722){io['to'](_0x53a39b)[_0x8adf('0xa')](_0x49637c,_0x161722);}var headers={'Content-Type':_0x8adf('0xb'),'Accept':_0x8adf('0xb')};function getTicket(_0x522296,_0x55d9dd,_0x2cf3b0,_0x2ec48b,_0x5ec789){logger[_0x8adf('0xc')]('Creating\x20new\x20ticket...');return request({'uri':util['format'](_0x8adf('0xd'),_0x522296[_0x8adf('0xe')]),'method':_0x8adf('0xf'),'body':{'ticket':_['merge']({'requester_id':_0x2cf3b0['id'],'submitter_id':_0x55d9dd?_0x55d9dd['id']:undefined,'assignee_id':_0x55d9dd?_0x55d9dd['id']:undefined,'tags':getTags(_0x2ec48b[_0x8adf('0x10')],'name'),'subject':intUtil[_0x8adf('0x11')](_0x2ec48b[_0x8adf('0x12')],_0x5ec789,'\x20'),'description':intUtil[_0x8adf('0x11')](_0x2ec48b['Descriptions'],_0x5ec789,'\x0a'),'comment':{'public':![],'body':intUtil[_0x8adf('0x11')](_0x2ec48b[_0x8adf('0x13')],_0x5ec789,'\x0a')}},getCustomFields(_0x2ec48b[_0x8adf('0x14')],_0x5ec789))}})[_0x8adf('0x15')](function(_0x1145e7){logger[_0x8adf('0xc')](_0x8adf('0x16'));return _0x1145e7[_0x8adf('0x17')];});}function getUser(_0x36ec3f,_0x479ef4){if(intUtil['validateEmail'](_0x479ef4)){logger[_0x8adf('0xc')]('Getting\x20agent\x20with\x20email',_0x479ef4);return request({'uri':util[_0x8adf('0x18')]('%s/search.json',_0x36ec3f[_0x8adf('0xe')]),'qs':{'query':util['format'](_0x8adf('0x19'),_0x479ef4)}})[_0x8adf('0x15')](function(_0x1d7d8c){if(_0x1d7d8c[_0x8adf('0x1a')]){logger[_0x8adf('0xc')](_0x8adf('0x1b'));return _[_0x8adf('0x1c')](_0x1d7d8c[_0x8adf('0x1d')],{'email':_0x479ef4,'role':'agent'});}else{logger[_0x8adf('0xc')](_0x8adf('0x1e'));return request({'uri':util['format']('%s/users/me.json',_0x36ec3f[_0x8adf('0xe')])})[_0x8adf('0x15')](function(_0x3e68eb){logger['info'](_0x8adf('0x1f'));return _0x3e68eb[_0x8adf('0x20')];});}});}else{logger[_0x8adf('0xc')](_0x8adf('0x1e'));return request({'uri':util['format']('%s/users/me.json',_0x36ec3f[_0x8adf('0xe')])})['then'](function(_0x1220dd){logger[_0x8adf('0xc')](_0x8adf('0x1f'));return _0x1220dd[_0x8adf('0x20')];});}}function getEndUser(_0x2e7898,_0x4f9c17,_0x17d0e9){logger[_0x8adf('0xc')](_0x8adf('0x21'),_0x17d0e9);return request({'uri':util[_0x8adf('0x18')]('%s/search.json',_0x2e7898['remoteUri']),'qs':{'query':util[_0x8adf('0x18')](_0x8adf('0x22'),_0x17d0e9)}})[_0x8adf('0x15')](function(_0x1fef2a){var _0x7525a0;if(_0x1fef2a['results']['length']){_0x1fef2a['results']=_[_0x8adf('0x23')](_0x1fef2a[_0x8adf('0x1d')],{'suspended':![],'role':_0x8adf('0x24')});if(_0x1fef2a[_0x8adf('0x1d')]['length']){_0x7525a0=_[_0x8adf('0x25')](_0x1fef2a[_0x8adf('0x1d')]);}}if(_0x7525a0){logger[_0x8adf('0xc')](_0x8adf('0x26'));return _0x7525a0;}else{logger['info']('Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!');return request({'uri':util[_0x8adf('0x18')](_0x8adf('0x27'),_0x2e7898[_0x8adf('0xe')]),'method':_0x8adf('0xf'),'body':{'user':{'role':'end-user','name':util[_0x8adf('0x18')](_0x8adf('0x28'),_0x4f9c17||_0x17d0e9),'phone':_0x17d0e9}}})[_0x8adf('0x15')](function(_0x465834){logger['info']('Enduser\x20created.');return _0x465834[_0x8adf('0x20')];});}});}function getAuth(_0x597969){var _0x12e475;if(_0x597969['authType']==='password'){_0x12e475=util['format'](_0x8adf('0x29'),_0x597969[_0x8adf('0x2a')],_0x597969[_0x8adf('0x2b')]);}else if(_0x597969['authType']==='token'){_0x12e475=util[_0x8adf('0x18')]('%s/token:%s',_0x597969[_0x8adf('0x2a')],_0x597969[_0x8adf('0x2c')]);}return util[_0x8adf('0x18')](_0x8adf('0x2d'),new Buffer(_0x12e475)[_0x8adf('0x2e')](_0x8adf('0x2f')));}function request(_0xb9dc7a){logger[_0x8adf('0x30')](_0x8adf('0x31'),JSON['stringify'](_0xb9dc7a));return rp(_[_0x8adf('0x32')](_0xb9dc7a,{'method':'GET','headers':headers,'json':!![]}));}function getTags(_0x3ea70e,_0x2e0bd4){var _0x211d81=[];if(_0x3ea70e&&_0x3ea70e[_0x8adf('0x33')]>0x0){_0x211d81=_[_0x8adf('0x34')](_0x3ea70e,_0x2e0bd4);}return _0x211d81;}function getCustomFields(_0x339910,_0x45b306){var _0x1d9770=[];var _0x1d8cb9={'custom_fields':[]};if(_0x339910[_0x8adf('0x33')]>0x0){_[_0x8adf('0x35')](_0x339910,function(_0x1f1279){if(_[_0x8adf('0x36')]([_0x8adf('0x37'),_0x8adf('0x38')],_0x1f1279[_0x8adf('0x39')])&&_0x1f1279[_0x8adf('0x3a')]&&!_0x1f1279['customField']){_0x1d8cb9[_0x1f1279[_0x8adf('0x3a')][_0x8adf('0x3b')]()]=_0x1f1279['content']['toString']();}else if(_[_0x8adf('0x36')]([_0x8adf('0x37'),'picklist'],_0x1f1279[_0x8adf('0x39')])&&_0x1f1279[_0x8adf('0x3c')]&&_0x1f1279[_0x8adf('0x3d')]){_0x1d8cb9[_0x8adf('0x3e')][_0x8adf('0x3f')]({'id':_0x1f1279[_0x8adf('0x3c')],'value':_0x1f1279[_0x8adf('0x40')][_0x8adf('0x2e')]()});}else if(_0x1f1279[_0x8adf('0x39')]===_0x8adf('0x41')&&_0x1f1279[_0x8adf('0x42')]&&_0x1f1279[_0x8adf('0x3c')]&&_0x1f1279[_0x8adf('0x3d')]){_0x45b306[_0x1f1279[_0x8adf('0x42')]]&&_0x1d8cb9['custom_fields']['push']({'id':_0x1f1279['idField'],'value':_0x45b306[_0x1f1279[_0x8adf('0x42')]]['toString']()});}else if(_0x1f1279[_0x8adf('0x39')]===_0x8adf('0x43')&&_0x1f1279[_0x8adf('0x44')]&&_0x1f1279[_0x8adf('0x44')]['name']&&_0x1f1279[_0x8adf('0x3c')]&&_0x1f1279['customField']){_0x45b306[_0x1f1279[_0x8adf('0x44')]['name'][_0x8adf('0x3b')]()]&&_0x1d8cb9[_0x8adf('0x3e')][_0x8adf('0x3f')]({'id':_0x1f1279[_0x8adf('0x3c')],'value':_0x45b306[_0x1f1279[_0x8adf('0x44')]['name'][_0x8adf('0x3b')]()]['toString']()});}});}return _0x1d8cb9;}exports[_0x8adf('0x45')]=function(_0x47f1a3,_0x4875e4,_0x4c6ad3,_0x40ec4b,_0x141c5e,_0x4d0626){var _0x3b2a2d,_0x15ae90;_0x4d0626[_0x8adf('0xe')]=intUtil[_0x8adf('0x46')](_0x4d0626[_0x8adf('0xe')]);_0x4d0626[_0x8adf('0x47')]=intUtil['stripTrailingSlash'](_0x4d0626[_0x8adf('0x47')]);if(_0x141c5e&&_0x141c5e[_0x8adf('0x48')]){logger[_0x8adf('0xc')](_0x8adf('0x49'),_0x8adf('0x4a'));_0x4875e4[_0x8adf('0x4b')]=util[_0x8adf('0x18')](_0x8adf('0x4c'),_0x4d0626[_0x8adf('0x47')],_0x4875e4[_0x8adf('0x4d')],md5(_0x4875e4['uniqueid']));}else{_0x4875e4[_0x8adf('0x4b')]='';}headers[_0x8adf('0x4e')]=getAuth(_0x4d0626);return getUser(_0x4d0626,_0x40ec4b['email'])[_0x8adf('0x15')](function(_0x4db8e9){_0x3b2a2d=_0x4db8e9;logger['info'](_0x8adf('0x4f'),_0x3b2a2d['id']);return getEndUser(_0x4d0626,_0x4875e4[_0x8adf('0x50')],_0x4875e4[_0x8adf('0x51')]);})[_0x8adf('0x15')](function(_0x3b2309){_0x15ae90=_0x3b2309;logger['info'](_0x8adf('0x52'),_0x15ae90['id']);return getTicket(_0x4d0626,_0x3b2a2d,_0x15ae90,_0x4c6ad3,_0x4875e4);})['then'](function(_0x590149){if(_0x590149){logger[_0x8adf('0xc')]('The\x20Ticket\x20ID\x20is:',_0x590149['id']);switch(_0x4d0626[_0x8adf('0x39')]){case _0x8adf('0x53'):logger[_0x8adf('0xc')](_0x8adf('0x49'),_0x8adf('0x54'));return request({'uri':util[_0x8adf('0x18')](_0x8adf('0x55'),_0x4d0626['remoteUri'],_0x3b2a2d['id'],_0x590149['id']),'method':_0x8adf('0xf')});case _0x8adf('0x56'):logger[_0x8adf('0xc')]('[QUEUE]',_0x8adf('0x57'));emit(util[_0x8adf('0x18')]('user:%s',_0x40ec4b[_0x8adf('0x58')]),_0x8adf('0x59'),{'uri':util[_0x8adf('0x18')](_0x8adf('0x5a'),_0x4d0626[_0x8adf('0xe')][_0x8adf('0x5b')](_0x8adf('0x5c'),''),_0x590149['id'])});break;default:logger[_0x8adf('0x5d')](_0x8adf('0x49'),_0x8adf('0x5e'));}}})[_0x8adf('0x5f')](function(_0x577b4c){logger[_0x8adf('0x5d')](_0x8adf('0x60'),util[_0x8adf('0x61')](_0x577b4c,{'showHidden':!![],'depth':null}));});};exports['outbound']=function(_0x14196e,_0x3960fe,_0x332d1c,_0x1f2b96,_0x51c2c1,_0x293f30,_0x3614cc){var _0x2ddd5a,_0x3f8474;_0x293f30[_0x8adf('0xe')]=intUtil[_0x8adf('0x46')](_0x293f30[_0x8adf('0xe')]);_0x293f30[_0x8adf('0x47')]=intUtil[_0x8adf('0x46')](_0x293f30['serverUrl']);if(_0x3614cc&&_0x3614cc[_0x8adf('0x62')]&&_0x3614cc[_0x8adf('0x62')]!==_0x8adf('0x63')){logger[_0x8adf('0xc')](_0x8adf('0x64'),_0x8adf('0x4a'));_0x3960fe[_0x8adf('0x4b')]=util[_0x8adf('0x18')](_0x8adf('0x4c'),_0x293f30[_0x8adf('0x47')],_0x3960fe[_0x8adf('0x4d')],md5(_0x3960fe[_0x8adf('0x4d')]));}headers[_0x8adf('0x4e')]=getAuth(_0x293f30);return getUser(_0x293f30,_0x1f2b96['email'])[_0x8adf('0x15')](function(_0x408dc5){_0x2ddd5a=_0x408dc5;logger[_0x8adf('0xc')](_0x8adf('0x4f'),_0x2ddd5a['id']);return getEndUser(_0x293f30,_0x3960fe[_0x8adf('0x65')],_0x3960fe[_0x8adf('0x66')]);})['then'](function(_0x275ec9){_0x3f8474=_0x275ec9;logger[_0x8adf('0xc')](_0x8adf('0x52'),_0x3f8474['id']);return getTicket(_0x293f30,_0x2ddd5a,_0x3f8474,_0x332d1c,_0x3960fe);})['then'](function(_0x4701e7){if(_0x4701e7){logger[_0x8adf('0xc')]('The\x20Ticket\x20ID\x20is:',_0x4701e7['id']);switch(_0x293f30['type']){case'integrationTab':logger[_0x8adf('0xc')](_0x8adf('0x64'),'Displaying\x20ticket\x20to\x20agent...');return request({'uri':util[_0x8adf('0x18')](_0x8adf('0x55'),_0x293f30[_0x8adf('0xe')],_0x2ddd5a['id'],_0x4701e7['id']),'method':_0x8adf('0xf')});case _0x8adf('0x56'):logger[_0x8adf('0xc')](_0x8adf('0x64'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util['format']('user:%s',_0x1f2b96[_0x8adf('0x58')]),_0x8adf('0x59'),{'uri':util['format'](_0x8adf('0x5a'),_0x293f30['remoteUri'][_0x8adf('0x5b')](_0x8adf('0x5c'),''),_0x4701e7['id'])});break;default:logger[_0x8adf('0x5d')]('[OUTBOUND]',_0x8adf('0x5e'));}}})[_0x8adf('0x5f')](function(_0x3bdfd8){logger[_0x8adf('0x5d')](_0x8adf('0x67'),util['inspect'](_0x3bdfd8,{'showHidden':!![],'depth':null}));});};exports[_0x8adf('0x68')]=function(_0x3d36d3,_0x597729,_0x1cc9fd,_0x4046c9,_0x520d60,_0x14789d){var _0x3e4140,_0x135d02;_0x14789d[_0x8adf('0xe')]=intUtil['stripTrailingSlash'](_0x14789d[_0x8adf('0xe')]);_0x14789d[_0x8adf('0x47')]=intUtil[_0x8adf('0x46')](_0x14789d[_0x8adf('0x47')]);if(_0x520d60&&_0x520d60[_0x8adf('0x48')]){logger['info']('['+_0x597729[_0x8adf('0x69')][_0x8adf('0x6a')]()+']','Recording\x20is\x20enabled!');_0x597729[_0x8adf('0x4b')]=util[_0x8adf('0x18')](_0x8adf('0x4c'),_0x14789d[_0x8adf('0x47')],_0x597729['uniqueid'],md5(_0x597729['uniqueid']));}else{_0x597729['recordingURL']='';}headers[_0x8adf('0x4e')]=getAuth(_0x14789d);return getUser(_0x14789d)[_0x8adf('0x15')](function(_0x514b35){_0x3e4140=_0x514b35;logger[_0x8adf('0xc')](_0x8adf('0x4f'),_0x3e4140['id']);return getEndUser(_0x14789d,_0x597729[_0x8adf('0x50')],_0x597729['calleridnum']);})[_0x8adf('0x15')](function(_0x47d553){_0x135d02=_0x47d553;logger[_0x8adf('0xc')](_0x8adf('0x52'),_0x135d02['id']);return getTicket(_0x14789d,_0x3e4140,_0x135d02,_0x1cc9fd,_0x597729);})['then'](function(_0x1132f8){if(_0x1132f8){logger['info'](_0x8adf('0x6b'),_0x1132f8['id']);}})[_0x8adf('0x5f')](function(_0x4743ba){logger[_0x8adf('0x5d')]('['+_0x597729[_0x8adf('0x69')][_0x8adf('0x6a')]()+']'+'\x20Error\x20running\x20Zendesk\x20integration',util[_0x8adf('0x61')](_0x4743ba,{'showHidden':!![],'depth':null}));});};