cb7ad9836260af313378427e9a0ff1871b8f17f3
[motion2.git] / server / services / ami / trigger / integration / freshdesk.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 _0x3f98=['md5','util','./util','../../../../config/environment','../../../../config/logger','freshdesk','redis','defaults','localhost','application/json','emit','merge','format','calleridname','calleridnum','getString','Subjects','Descriptions','Tags','name','Fields','mapValues','value','%s/api/v2/tickets','remoteUri','POST','then','info','validateEmail','%s/api/v2/agents','length','Agent\x20found!','find','contact','email','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/api/v2/agents/me','Admin\x20account\x20selected.','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','Searching\x20enduser\x20with\x20phone','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','%s/api/v2/contacts','Enduser\x20created.','[HTTP]','GET','map','includes','string','picklist','type','customField','toLowerCase','content','toString','idField','custom_fields','push','variable','variableName','customVariable','Variable','queue','serverUrl','stripTrailingSlash','monitor_format','[QUEUE]','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','Authorization','The\x20User\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','user:%s','%s/helpdesk/tickets/%d','catch','error','outbound','recordingFormat','none','[OUTBOUND]','Recording\x20is\x20enabled!','destcalleridname','destcalleridnum','The\x20Enduser\x20ID\x20is:','Ticket\x20created!','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','stringify','unmanaged','lastevent','toUpperCase','lodash','request-promise'];(function(_0x5547c6,_0x5d611c){var _0x1f3fbc=function(_0x161797){while(--_0x161797){_0x5547c6['push'](_0x5547c6['shift']());}};_0x1f3fbc(++_0x5d611c);}(_0x3f98,0x172));var _0x83f9=function(_0x3a70d9,_0x310f4){_0x3a70d9=_0x3a70d9-0x0;var _0x3d0d0a=_0x3f98[_0x3a70d9];return _0x3d0d0a;};'use strict';var _=require(_0x83f9('0x0'));var rp=require(_0x83f9('0x1'));var md5=require(_0x83f9('0x2'));var util=require(_0x83f9('0x3'));var Redis=require('ioredis');var intUtil=require(_0x83f9('0x4'));var config=require(_0x83f9('0x5'));var logger=require(_0x83f9('0x6'))(_0x83f9('0x7'));config[_0x83f9('0x8')]=_[_0x83f9('0x9')](config[_0x83f9('0x8')],{'host':_0x83f9('0xa'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x83f9('0x8')]));var headers={'Content-Type':_0x83f9('0xb'),'Accept':_0x83f9('0xb')};function emit(_0x156dc4,_0x17b4c1,_0x38aa26){io['to'](_0x156dc4)[_0x83f9('0xc')](_0x17b4c1,_0x38aa26);}function getTicket(_0x543bf0,_0xe73f87,_0x386320,_0x23ce5b,_0xccfd7a){logger['info']('Creating\x20new\x20ticket...');var _0x1cdf2c=_[_0x83f9('0xd')]({'responder_id':_0xe73f87['id'],'requester_id':_0x386320['id'],'name':util[_0x83f9('0xe')]('Caller\x20-\x20%s',_0xccfd7a[_0x83f9('0xf')]),'phone':_0xccfd7a[_0x83f9('0x10')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0x83f9('0x11')](_0x23ce5b[_0x83f9('0x12')],_0xccfd7a,'\x20'),'description':intUtil[_0x83f9('0x11')](_0x23ce5b[_0x83f9('0x13')],_0xccfd7a,'<br>'),'tags':getTags(_0x23ce5b[_0x83f9('0x14')],_0x83f9('0x15'))},getCustomFields(_0x23ce5b[_0x83f9('0x16')],_0xccfd7a));_0x1cdf2c['custom_fields']=_[_0x83f9('0x17')](_['keyBy'](_0x1cdf2c['custom_fields'],'id'),_0x83f9('0x18'));return request({'uri':util['format'](_0x83f9('0x19'),_0x543bf0[_0x83f9('0x1a')]),'method':_0x83f9('0x1b'),'body':_0x1cdf2c})[_0x83f9('0x1c')](function(_0x5f3003){logger[_0x83f9('0x1d')]('Ticket\x20created!');return _0x5f3003;});}function getUser(_0x4bf6f7,_0x313333){if(intUtil[_0x83f9('0x1e')](_0x313333)){return request({'uri':util[_0x83f9('0xe')](_0x83f9('0x1f'),_0x4bf6f7[_0x83f9('0x1a')]),'qs':{'email':_0x313333}})['then'](function(_0x1d8824){if(_0x1d8824[_0x83f9('0x20')]){logger['info'](_0x83f9('0x21'));return _[_0x83f9('0x22')](_0x1d8824,function(_0xa31d45){return _0xa31d45[_0x83f9('0x23')][_0x83f9('0x24')]===_0x313333;});}else{logger[_0x83f9('0x1d')](_0x83f9('0x25'));return request({'uri':util['format'](_0x83f9('0x26'),_0x4bf6f7[_0x83f9('0x1a')])})[_0x83f9('0x1c')](function(_0x5821bd){logger[_0x83f9('0x1d')](_0x83f9('0x27'));return _0x5821bd;});}});}else{logger['info'](_0x83f9('0x28'));return request({'uri':util[_0x83f9('0xe')](_0x83f9('0x26'),_0x4bf6f7['remoteUri'])})[_0x83f9('0x1c')](function(_0x6f9ab5){logger[_0x83f9('0x1d')](_0x83f9('0x27'));return _0x6f9ab5;});}}function getEndUser(_0x2b1269,_0x2d9f59,_0x371ed8){logger[_0x83f9('0x1d')](_0x83f9('0x29'),_0x371ed8);return request({'uri':util[_0x83f9('0xe')]('%s/api/v2/contacts',_0x2b1269[_0x83f9('0x1a')]),'qs':{'phone':_0x371ed8}})['then'](function(_0x259d70){if(_0x259d70[_0x83f9('0x20')]){logger[_0x83f9('0x1d')](_0x83f9('0x2a'));return _[_0x83f9('0x22')](_0x259d70,{'phone':_0x371ed8});}else{logger[_0x83f9('0x1d')](_0x83f9('0x2b'));return request({'uri':util[_0x83f9('0xe')](_0x83f9('0x2c'),_0x2b1269[_0x83f9('0x1a')]),'method':'POST','body':{'name':_0x2d9f59,'phone':_0x371ed8}})['then'](function(_0x14bde5){logger[_0x83f9('0x1d')](_0x83f9('0x2d'));return _0x14bde5;});}});}function getAuth(_0x58f477){return util[_0x83f9('0xe')]('Basic\x20%s',new Buffer(_0x58f477['apiKey']+':X')['toString']('base64'));}function request(_0x34144d){logger['debug'](_0x83f9('0x2e'),JSON['stringify'](_0x34144d));return rp(_['defaults'](_0x34144d,{'method':_0x83f9('0x2f'),'headers':headers,'json':!![]}));}function getTags(_0x1c8f62,_0x1dd35c){var _0x357923=[];if(_0x1c8f62&&_0x1c8f62[_0x83f9('0x20')]>0x0){_0x357923=_[_0x83f9('0x30')](_0x1c8f62,_0x1dd35c);}return _0x357923;}function getCustomFields(_0x289e89,_0x4cdd32){var _0x594e93=[];var _0x260951={'custom_fields':[]};if(_0x289e89[_0x83f9('0x20')]>0x0){_['forEach'](_0x289e89,function(_0x56b6d6){if(_[_0x83f9('0x31')]([_0x83f9('0x32'),_0x83f9('0x33')],_0x56b6d6[_0x83f9('0x34')])&&_0x56b6d6['nameField']&&!_0x56b6d6[_0x83f9('0x35')]){_0x260951[_0x56b6d6['nameField'][_0x83f9('0x36')]()]=_0x56b6d6[_0x83f9('0x37')][_0x83f9('0x38')]();}else if(_[_0x83f9('0x31')]([_0x83f9('0x32'),_0x83f9('0x33')],_0x56b6d6[_0x83f9('0x34')])&&_0x56b6d6[_0x83f9('0x39')]&&_0x56b6d6[_0x83f9('0x35')]){_0x260951[_0x83f9('0x3a')][_0x83f9('0x3b')]({'id':_0x56b6d6[_0x83f9('0x39')],'value':_0x56b6d6[_0x83f9('0x37')][_0x83f9('0x38')]()});}else if(_0x56b6d6['type']===_0x83f9('0x3c')&&_0x56b6d6[_0x83f9('0x3d')]&&_0x56b6d6[_0x83f9('0x39')]&&_0x56b6d6['customField']){_0x4cdd32[_0x56b6d6[_0x83f9('0x3d')]]&&_0x260951['custom_fields'][_0x83f9('0x3b')]({'id':_0x56b6d6[_0x83f9('0x39')],'value':_0x4cdd32[_0x56b6d6['variableName']]['toString']()});}else if(_0x56b6d6[_0x83f9('0x34')]===_0x83f9('0x3e')&&_0x56b6d6[_0x83f9('0x3f')]&&_0x56b6d6[_0x83f9('0x3f')]['name']&&_0x56b6d6[_0x83f9('0x39')]&&_0x56b6d6[_0x83f9('0x35')]){_0x4cdd32[_0x56b6d6['Variable'][_0x83f9('0x15')][_0x83f9('0x36')]()]&&_0x260951[_0x83f9('0x3a')][_0x83f9('0x3b')]({'id':_0x56b6d6[_0x83f9('0x39')],'value':_0x4cdd32[_0x56b6d6['Variable'][_0x83f9('0x15')][_0x83f9('0x36')]()][_0x83f9('0x38')]()});}});}return _0x260951;}exports[_0x83f9('0x40')]=function(_0x5def6f,_0x1ef1c3,_0x3b559d,_0x31a4f9,_0x28364d,_0x4f7fa5){var _0x11023d,_0x31ad0e;_0x4f7fa5[_0x83f9('0x1a')]=intUtil['stripTrailingSlash'](_0x4f7fa5[_0x83f9('0x1a')]);_0x4f7fa5[_0x83f9('0x41')]=intUtil[_0x83f9('0x42')](_0x4f7fa5['serverUrl']);if(_0x28364d&&_0x28364d[_0x83f9('0x43')]){logger['info'](_0x83f9('0x44'),'Recording\x20is\x20enabled!');_0x1ef1c3[_0x83f9('0x45')]=util[_0x83f9('0xe')](_0x83f9('0x46'),_0x4f7fa5[_0x83f9('0x41')],_0x1ef1c3[_0x83f9('0x47')],md5(_0x1ef1c3['uniqueid']));}else{_0x1ef1c3['recordingURL']='';}headers[_0x83f9('0x48')]=getAuth(_0x4f7fa5);return getUser(_0x4f7fa5,_0x31a4f9[_0x83f9('0x24')])[_0x83f9('0x1c')](function(_0xc9eb6a){_0x11023d=_0xc9eb6a;logger[_0x83f9('0x1d')](_0x83f9('0x49'),_0x11023d['id']);return getEndUser(_0x4f7fa5,_0x1ef1c3[_0x83f9('0xf')],_0x1ef1c3['calleridnum']);})['then'](function(_0x17bf63){_0x31ad0e=_0x17bf63;logger[_0x83f9('0x1d')]('The\x20Enduser\x20ID\x20is:',_0x31ad0e['id']);return getTicket(_0x4f7fa5,_0x11023d,_0x31ad0e,_0x3b559d,_0x1ef1c3);})[_0x83f9('0x1c')](function(_0x2a789c){if(_0x2a789c&&_0x2a789c['id']){logger[_0x83f9('0x1d')]('Ticket\x20created!');logger[_0x83f9('0x1d')](_0x83f9('0x4a'),_0x2a789c['id']);logger[_0x83f9('0x1d')]('[QUEUE]','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0x83f9('0xe')](_0x83f9('0x4b'),_0x31a4f9[_0x83f9('0x15')]),'trigger:browser:url',{'uri':util[_0x83f9('0xe')](_0x83f9('0x4c'),_0x4f7fa5[_0x83f9('0x1a')],_0x2a789c['id'])});}})[_0x83f9('0x4d')](function(_0x4682f8){logger[_0x83f9('0x4e')](_0x83f9('0x44'),JSON['stringify'](_0x4682f8));});};exports[_0x83f9('0x4f')]=function(_0x4d44fb,_0x536336,_0x2d04a7,_0x707653,_0x223c81,_0x2bf068,_0x280973){var _0x4e36c0,_0x482cfa;_0x2bf068[_0x83f9('0x1a')]=intUtil[_0x83f9('0x42')](_0x2bf068[_0x83f9('0x1a')]);_0x2bf068[_0x83f9('0x41')]=intUtil[_0x83f9('0x42')](_0x2bf068[_0x83f9('0x41')]);if(_0x280973&&_0x280973[_0x83f9('0x50')]&&_0x280973[_0x83f9('0x50')]!==_0x83f9('0x51')){logger['info'](_0x83f9('0x52'),_0x83f9('0x53'));_0x536336[_0x83f9('0x45')]=util[_0x83f9('0xe')](_0x83f9('0x46'),_0x2bf068['serverUrl'],_0x536336[_0x83f9('0x47')],md5(_0x536336[_0x83f9('0x47')]));}headers[_0x83f9('0x48')]=getAuth(_0x2bf068);return getUser(_0x2bf068,_0x707653[_0x83f9('0x24')])[_0x83f9('0x1c')](function(_0x3c4647){_0x4e36c0=_0x3c4647;logger[_0x83f9('0x1d')](_0x83f9('0x49'),_0x4e36c0['id']);return getEndUser(_0x2bf068,_0x536336[_0x83f9('0x54')],_0x536336[_0x83f9('0x55')]);})['then'](function(_0x4e633c){_0x482cfa=_0x4e633c;logger[_0x83f9('0x1d')](_0x83f9('0x56'),_0x482cfa['id']);return getTicket(_0x2bf068,_0x4e36c0,_0x482cfa,_0x2d04a7,_0x536336);})[_0x83f9('0x1c')](function(_0x1e34a1){if(_0x1e34a1&&_0x1e34a1['id']){logger['info'](_0x83f9('0x57'));logger[_0x83f9('0x1d')](_0x83f9('0x4a'),_0x1e34a1['id']);logger[_0x83f9('0x1d')](_0x83f9('0x52'),_0x83f9('0x58'));emit(util[_0x83f9('0xe')]('user:%s',_0x707653[_0x83f9('0x15')]),'trigger:browser:url',{'uri':util[_0x83f9('0xe')]('%s/helpdesk/tickets/%d',_0x2bf068[_0x83f9('0x1a')],_0x1e34a1['id'])});}})['catch'](function(_0x27f28b){logger[_0x83f9('0x4e')](_0x83f9('0x52'),JSON[_0x83f9('0x59')](_0x27f28b));});};exports[_0x83f9('0x5a')]=function(_0x40cc7e,_0x5107db,_0x33ff9d,_0x12e14d,_0x2e3090,_0x58fc8d){var _0x1ae68a,_0x9a7cdb;_0x58fc8d['remoteUri']=intUtil[_0x83f9('0x42')](_0x58fc8d[_0x83f9('0x1a')]);_0x58fc8d['serverUrl']=intUtil[_0x83f9('0x42')](_0x58fc8d[_0x83f9('0x41')]);if(_0x2e3090&&_0x2e3090['monitor_format']){logger[_0x83f9('0x1d')]('['+_0x5107db[_0x83f9('0x5b')][_0x83f9('0x5c')]()+']',_0x83f9('0x53'));_0x5107db[_0x83f9('0x45')]=util[_0x83f9('0xe')](_0x83f9('0x46'),_0x58fc8d['serverUrl'],_0x5107db[_0x83f9('0x47')],md5(_0x5107db[_0x83f9('0x47')]));}else{_0x5107db[_0x83f9('0x45')]='';}headers[_0x83f9('0x48')]=getAuth(_0x58fc8d);return getUser(_0x58fc8d,'')[_0x83f9('0x1c')](function(_0x25feec){_0x1ae68a=_0x25feec;logger[_0x83f9('0x1d')](_0x83f9('0x49'),_0x1ae68a['id']);return getEndUser(_0x58fc8d,_0x5107db[_0x83f9('0xf')],_0x5107db[_0x83f9('0x10')]);})[_0x83f9('0x1c')](function(_0x44bd0d){_0x9a7cdb=_0x44bd0d;logger['info']('The\x20Enduser\x20ID\x20is:',_0x9a7cdb['id']);return getTicket(_0x58fc8d,_0x1ae68a,_0x9a7cdb,_0x33ff9d,_0x5107db);})[_0x83f9('0x1c')](function(_0x5f3c87){if(_0x5f3c87){logger[_0x83f9('0x1d')](_0x83f9('0x57'));logger[_0x83f9('0x1d')](_0x83f9('0x4a'),_0x5f3c87['id']);}})['catch'](function(_0x136d38){logger[_0x83f9('0x4e')]('['+_0x5107db[_0x83f9('0x5b')][_0x83f9('0x5c')]()+']',JSON[_0x83f9('0x59')](_0x136d38));});};