66916f0970a550da6704eb4f711261a1c089ec3b
[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 _0x820c=['monitor_format','lastevent','toUpperCase','request-promise','md5','util','./util','../../../../config/environment','../../../../config/logger','freshdesk','redis','defaults','socket.io-emitter','application/json','emit','merge','format','Caller\x20-\x20%s','Subjects','getString','<br>','Tags','name','Fields','mapValues','keyBy','custom_fields','%s/api/v2/tickets','remoteUri','then','info','validateEmail','%s/api/v2/agents','Agent\x20found!','find','contact','email','%s/api/v2/agents/me','Admin\x20account\x20selected.','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','Searching\x20enduser\x20with\x20phone','%s/api/v2/contacts','length','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Enduser\x20created.','Basic\x20%s','toString','base64','debug','[HTTP]','map','forEach','type','nameField','toLowerCase','content','string','idField','customField','push','variable','variableName','Variable','queue','stripTrailingSlash','serverUrl','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','uniqueid','Authorization','The\x20User\x20ID\x20is:','calleridname','calleridnum','The\x20Enduser\x20ID\x20is:','Ticket\x20created!','The\x20Ticket\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','trigger:browser:url','catch','error','stringify','outbound','recordingFormat','[OUTBOUND]','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','destcalleridname','destcalleridnum','%s/helpdesk/tickets/%d'];(function(_0xdbce8,_0x550d11){var _0x4e8866=function(_0x570546){while(--_0x570546){_0xdbce8['push'](_0xdbce8['shift']());}};_0x4e8866(++_0x550d11);}(_0x820c,0x1ca));var _0xc820=function(_0x54da48,_0x56ddff){_0x54da48=_0x54da48-0x0;var _0x1f3848=_0x820c[_0x54da48];return _0x1f3848;};'use strict';var _=require('lodash');var rp=require(_0xc820('0x0'));var md5=require(_0xc820('0x1'));var util=require(_0xc820('0x2'));var Redis=require('ioredis');var intUtil=require(_0xc820('0x3'));var config=require(_0xc820('0x4'));var logger=require(_0xc820('0x5'))(_0xc820('0x6'));config[_0xc820('0x7')]=_[_0xc820('0x8')](config[_0xc820('0x7')],{'host':'localhost','port':0x18eb});var io=require(_0xc820('0x9'))(new Redis(config[_0xc820('0x7')]));var headers={'Content-Type':_0xc820('0xa'),'Accept':_0xc820('0xa')};function emit(_0x2fa24f,_0x3d452f,_0x359a30){io['to'](_0x2fa24f)[_0xc820('0xb')](_0x3d452f,_0x359a30);}function getTicket(_0x111450,_0x30858f,_0x42dac0,_0x328c0c,_0x59b510){logger['info']('Creating\x20new\x20ticket...');var _0x80ea0d=_[_0xc820('0xc')]({'responder_id':_0x30858f['id'],'requester_id':_0x42dac0['id'],'name':util[_0xc820('0xd')](_0xc820('0xe'),_0x59b510['calleridname']),'phone':_0x59b510['calleridnum'],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil['getString'](_0x328c0c[_0xc820('0xf')],_0x59b510,'\x20'),'description':intUtil[_0xc820('0x10')](_0x328c0c['Descriptions'],_0x59b510,_0xc820('0x11')),'tags':getTags(_0x328c0c[_0xc820('0x12')],_0xc820('0x13'))},getCustomFields(_0x328c0c[_0xc820('0x14')],_0x59b510));_0x80ea0d['custom_fields']=_[_0xc820('0x15')](_[_0xc820('0x16')](_0x80ea0d[_0xc820('0x17')],'id'),'value');return request({'uri':util[_0xc820('0xd')](_0xc820('0x18'),_0x111450[_0xc820('0x19')]),'method':'POST','body':_0x80ea0d})[_0xc820('0x1a')](function(_0x445505){logger[_0xc820('0x1b')]('Ticket\x20created!');return _0x445505;});}function getUser(_0x7a896b,_0x51a8a5){if(intUtil[_0xc820('0x1c')](_0x51a8a5)){return request({'uri':util[_0xc820('0xd')](_0xc820('0x1d'),_0x7a896b[_0xc820('0x19')]),'qs':{'email':_0x51a8a5}})[_0xc820('0x1a')](function(_0xdb8328){if(_0xdb8328['length']){logger[_0xc820('0x1b')](_0xc820('0x1e'));return _[_0xc820('0x1f')](_0xdb8328,function(_0x37eae7){return _0x37eae7[_0xc820('0x20')][_0xc820('0x21')]===_0x51a8a5;});}else{logger[_0xc820('0x1b')]('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');return request({'uri':util[_0xc820('0xd')](_0xc820('0x22'),_0x7a896b['remoteUri'])})[_0xc820('0x1a')](function(_0x287102){logger[_0xc820('0x1b')](_0xc820('0x23'));return _0x287102;});}});}else{logger[_0xc820('0x1b')](_0xc820('0x24'));return request({'uri':util['format'](_0xc820('0x22'),_0x7a896b[_0xc820('0x19')])})['then'](function(_0x2a10bc){logger[_0xc820('0x1b')]('Admin\x20account\x20selected.');return _0x2a10bc;});}}function getEndUser(_0x268c7f,_0x3054bf,_0x330f11){logger['info'](_0xc820('0x25'),_0x330f11);return request({'uri':util[_0xc820('0xd')](_0xc820('0x26'),_0x268c7f[_0xc820('0x19')]),'qs':{'phone':_0x330f11}})[_0xc820('0x1a')](function(_0x14d933){if(_0x14d933[_0xc820('0x27')]){logger[_0xc820('0x1b')](_0xc820('0x28'));return _['find'](_0x14d933,{'phone':_0x330f11});}else{logger[_0xc820('0x1b')](_0xc820('0x29'));return request({'uri':util[_0xc820('0xd')](_0xc820('0x26'),_0x268c7f[_0xc820('0x19')]),'method':'POST','body':{'name':_0x3054bf,'phone':_0x330f11}})[_0xc820('0x1a')](function(_0x5cc9a9){logger['info'](_0xc820('0x2a'));return _0x5cc9a9;});}});}function getAuth(_0x458fd2){return util[_0xc820('0xd')](_0xc820('0x2b'),new Buffer(_0x458fd2['apiKey']+':X')[_0xc820('0x2c')](_0xc820('0x2d')));}function request(_0x983b03){logger[_0xc820('0x2e')](_0xc820('0x2f'),JSON['stringify'](_0x983b03));return rp(_[_0xc820('0x8')](_0x983b03,{'method':'GET','headers':headers,'json':!![]}));}function getTags(_0x3a6019,_0x35c906){var _0x59e857=[];if(_0x3a6019&&_0x3a6019[_0xc820('0x27')]>0x0){_0x59e857=_[_0xc820('0x30')](_0x3a6019,_0x35c906);}return _0x59e857;}function getCustomFields(_0x6f74a4,_0xf01507){var _0x15f8c8=[];var _0x10774c={'custom_fields':[]};if(_0x6f74a4[_0xc820('0x27')]>0x0){_[_0xc820('0x31')](_0x6f74a4,function(_0x48387f){if(_0x48387f[_0xc820('0x32')]==='string'&&_0x48387f[_0xc820('0x33')]&&!_0x48387f['customField']){_0x10774c[_0x48387f[_0xc820('0x33')][_0xc820('0x34')]()]=_0x48387f[_0xc820('0x35')][_0xc820('0x2c')]();}else if(_0x48387f[_0xc820('0x32')]===_0xc820('0x36')&&_0x48387f[_0xc820('0x37')]&&_0x48387f[_0xc820('0x38')]){_0x10774c[_0xc820('0x17')][_0xc820('0x39')]({'id':_0x48387f['idField'],'value':_0x48387f['content']['toString']()});}else if(_0x48387f['type']===_0xc820('0x3a')&&_0x48387f['variableName']&&_0x48387f[_0xc820('0x37')]&&_0x48387f[_0xc820('0x38')]){_0xf01507[_0x48387f[_0xc820('0x3b')]]&&_0x10774c['custom_fields'][_0xc820('0x39')]({'id':_0x48387f[_0xc820('0x37')],'value':_0xf01507[_0x48387f['variableName']][_0xc820('0x2c')]()});}else if(_0x48387f[_0xc820('0x32')]==='customVariable'&&_0x48387f[_0xc820('0x3c')]&&_0x48387f['Variable'][_0xc820('0x13')]&&_0x48387f[_0xc820('0x37')]&&_0x48387f['customField']){_0xf01507[_0x48387f[_0xc820('0x3c')][_0xc820('0x13')][_0xc820('0x34')]()]&&_0x10774c[_0xc820('0x17')][_0xc820('0x39')]({'id':_0x48387f[_0xc820('0x37')],'value':_0xf01507[_0x48387f[_0xc820('0x3c')][_0xc820('0x13')][_0xc820('0x34')]()][_0xc820('0x2c')]()});}});}return _0x10774c;}exports[_0xc820('0x3d')]=function(_0x5860be,_0x11240c,_0x412be1,_0x5ccfa7,_0x11d690,_0x4be361){var _0x3815cc,_0x1e0319;_0x4be361['remoteUri']=intUtil[_0xc820('0x3e')](_0x4be361[_0xc820('0x19')]);_0x4be361[_0xc820('0x3f')]=intUtil[_0xc820('0x3e')](_0x4be361[_0xc820('0x3f')]);if(_0x11d690&&_0x11d690['monitor_format']){logger['info'](_0xc820('0x40'),_0xc820('0x41'));_0x11240c[_0xc820('0x42')]=util[_0xc820('0xd')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x4be361['serverUrl'],_0x11240c[_0xc820('0x43')],md5(_0x11240c[_0xc820('0x43')]));}else{_0x11240c[_0xc820('0x42')]='';}headers[_0xc820('0x44')]=getAuth(_0x4be361);return getUser(_0x4be361,_0x5ccfa7['email'])[_0xc820('0x1a')](function(_0x25bc0e){_0x3815cc=_0x25bc0e;logger[_0xc820('0x1b')](_0xc820('0x45'),_0x3815cc['id']);return getEndUser(_0x4be361,_0x11240c[_0xc820('0x46')],_0x11240c[_0xc820('0x47')]);})[_0xc820('0x1a')](function(_0x4f69bf){_0x1e0319=_0x4f69bf;logger[_0xc820('0x1b')](_0xc820('0x48'),_0x1e0319['id']);return getTicket(_0x4be361,_0x3815cc,_0x1e0319,_0x412be1,_0x11240c);})['then'](function(_0xbde262){if(_0xbde262&&_0xbde262['id']){logger[_0xc820('0x1b')](_0xc820('0x49'));logger[_0xc820('0x1b')](_0xc820('0x4a'),_0xbde262['id']);logger[_0xc820('0x1b')](_0xc820('0x40'),_0xc820('0x4b'));emit(util[_0xc820('0xd')](_0xc820('0x4c'),_0x5ccfa7[_0xc820('0x13')]),_0xc820('0x4d'),{'uri':util[_0xc820('0xd')]('%s/helpdesk/tickets/%d',_0x4be361[_0xc820('0x19')],_0xbde262['id'])});}})[_0xc820('0x4e')](function(_0x1a559c){logger[_0xc820('0x4f')]('[QUEUE]',JSON[_0xc820('0x50')](_0x1a559c));});};exports[_0xc820('0x51')]=function(_0x398fda,_0x5b6c44,_0x1b3bca,_0x540f7d,_0x2b3e84,_0x23a288,_0x25ef9b){var _0x2665d4,_0x4d23a4;_0x23a288[_0xc820('0x19')]=intUtil['stripTrailingSlash'](_0x23a288['remoteUri']);_0x23a288[_0xc820('0x3f')]=intUtil[_0xc820('0x3e')](_0x23a288[_0xc820('0x3f')]);if(_0x25ef9b&&_0x25ef9b[_0xc820('0x52')]&&_0x25ef9b[_0xc820('0x52')]!=='none'){logger[_0xc820('0x1b')](_0xc820('0x53'),'Recording\x20is\x20enabled!');_0x5b6c44['recordingURL']=util[_0xc820('0xd')](_0xc820('0x54'),_0x23a288[_0xc820('0x3f')],_0x5b6c44[_0xc820('0x43')],md5(_0x5b6c44['uniqueid']));}headers[_0xc820('0x44')]=getAuth(_0x23a288);return getUser(_0x23a288,_0x540f7d['email'])[_0xc820('0x1a')](function(_0x2fa443){_0x2665d4=_0x2fa443;logger[_0xc820('0x1b')](_0xc820('0x45'),_0x2665d4['id']);return getEndUser(_0x23a288,_0x5b6c44[_0xc820('0x55')],_0x5b6c44[_0xc820('0x56')]);})[_0xc820('0x1a')](function(_0x1fdc13){_0x4d23a4=_0x1fdc13;logger[_0xc820('0x1b')](_0xc820('0x48'),_0x4d23a4['id']);return getTicket(_0x23a288,_0x2665d4,_0x4d23a4,_0x1b3bca,_0x5b6c44);})[_0xc820('0x1a')](function(_0x18f7fe){if(_0x18f7fe&&_0x18f7fe['id']){logger['info'](_0xc820('0x49'));logger['info']('The\x20Ticket\x20ID\x20is:',_0x18f7fe['id']);logger[_0xc820('0x1b')](_0xc820('0x53'),_0xc820('0x4b'));emit(util[_0xc820('0xd')](_0xc820('0x4c'),_0x540f7d[_0xc820('0x13')]),_0xc820('0x4d'),{'uri':util['format'](_0xc820('0x57'),_0x23a288[_0xc820('0x19')],_0x18f7fe['id'])});}})[_0xc820('0x4e')](function(_0x3a2a25){logger[_0xc820('0x4f')](_0xc820('0x53'),JSON[_0xc820('0x50')](_0x3a2a25));});};exports['unmanaged']=function(_0x3be616,_0x515e41,_0x346911,_0x452fc6,_0x1d283e,_0x206e34){var _0x4276d4,_0x18bc4a;_0x206e34[_0xc820('0x19')]=intUtil[_0xc820('0x3e')](_0x206e34[_0xc820('0x19')]);_0x206e34[_0xc820('0x3f')]=intUtil[_0xc820('0x3e')](_0x206e34[_0xc820('0x3f')]);if(_0x1d283e&&_0x1d283e[_0xc820('0x58')]){logger[_0xc820('0x1b')]('['+_0x515e41[_0xc820('0x59')][_0xc820('0x5a')]()+']',_0xc820('0x41'));_0x515e41[_0xc820('0x42')]=util['format'](_0xc820('0x54'),_0x206e34[_0xc820('0x3f')],_0x515e41['uniqueid'],md5(_0x515e41[_0xc820('0x43')]));}else{_0x515e41[_0xc820('0x42')]='';}headers['Authorization']=getAuth(_0x206e34);return getUser(_0x206e34,'')[_0xc820('0x1a')](function(_0x95a952){_0x4276d4=_0x95a952;logger[_0xc820('0x1b')](_0xc820('0x45'),_0x4276d4['id']);return getEndUser(_0x206e34,_0x515e41[_0xc820('0x46')],_0x515e41['calleridnum']);})['then'](function(_0x9de47f){_0x18bc4a=_0x9de47f;logger[_0xc820('0x1b')](_0xc820('0x48'),_0x18bc4a['id']);return getTicket(_0x206e34,_0x4276d4,_0x18bc4a,_0x346911,_0x515e41);})['then'](function(_0x1622c7){if(_0x1622c7){logger['info'](_0xc820('0x49'));logger[_0xc820('0x1b')](_0xc820('0x4a'),_0x1622c7['id']);}})[_0xc820('0x4e')](function(_0x2307d7){logger[_0xc820('0x4f')]('['+_0x515e41[_0xc820('0x59')][_0xc820('0x5a')]()+']',JSON[_0xc820('0x50')](_0x2307d7));});};