Built motion from commit 0900f080.|2.5.45
[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 _0xa405=['outbound','recordingFormat','[OUTBOUND]','The\x20User\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','trigger:browser:url','unmanaged','toUpperCase','catch','lastevent','lodash','md5','./util','../../../../config/environment','../../../../config/logger','freshdesk','redis','defaults','socket.io-emitter','application/json','emit','info','Creating\x20new\x20ticket...','format','Caller\x20-\x20%s','calleridname','calleridnum','getString','Subjects','Descriptions','<br>','name','Fields','custom_fields','keyBy','%s/api/v2/tickets','POST','then','Ticket\x20created!','validateEmail','%s/api/v2/agents','length','find','email','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/api/v2/agents/me','remoteUri','Admin\x20account\x20selected.','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','Searching\x20enduser\x20with\x20phone','%s/api/v2/contacts','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Enduser\x20created.','Basic\x20%s','apiKey','toString','base64','debug','[HTTP]','stringify','forEach','includes','type','nameField','customField','string','picklist','idField','push','content','variable','variableName','customVariable','Variable','toLowerCase','queue','stripTrailingSlash','serverUrl','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','recordingURL','Authorization','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','user:%s','%s/helpdesk/tickets/%d','error'];(function(_0x3e8a35,_0x5e441f){var _0x12ac4a=function(_0x46c8ae){while(--_0x46c8ae){_0x3e8a35['push'](_0x3e8a35['shift']());}};_0x12ac4a(++_0x5e441f);}(_0xa405,0xc0));var _0x5a40=function(_0x2641f9,_0x26715f){_0x2641f9=_0x2641f9-0x0;var _0x585741=_0xa405[_0x2641f9];return _0x585741;};'use strict';var _=require(_0x5a40('0x0'));var rp=require('request-promise');var md5=require(_0x5a40('0x1'));var util=require('util');var Redis=require('ioredis');var intUtil=require(_0x5a40('0x2'));var config=require(_0x5a40('0x3'));var logger=require(_0x5a40('0x4'))(_0x5a40('0x5'));config[_0x5a40('0x6')]=_[_0x5a40('0x7')](config[_0x5a40('0x6')],{'host':'localhost','port':0x18eb});var io=require(_0x5a40('0x8'))(new Redis(config[_0x5a40('0x6')]));var headers={'Content-Type':_0x5a40('0x9'),'Accept':_0x5a40('0x9')};function emit(_0x5b5578,_0x341915,_0x5c5b1a){io['to'](_0x5b5578)[_0x5a40('0xa')](_0x341915,_0x5c5b1a);}function getTicket(_0x5cab16,_0x1bb22f,_0x367005,_0x116983,_0x26648a){logger[_0x5a40('0xb')](_0x5a40('0xc'));var _0x962a1=_['merge']({'responder_id':_0x1bb22f['id'],'requester_id':_0x367005['id'],'name':util[_0x5a40('0xd')](_0x5a40('0xe'),_0x26648a[_0x5a40('0xf')]),'phone':_0x26648a[_0x5a40('0x10')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0x5a40('0x11')](_0x116983[_0x5a40('0x12')],_0x26648a,'\x20'),'description':intUtil['getString'](_0x116983[_0x5a40('0x13')],_0x26648a,_0x5a40('0x14')),'tags':getTags(_0x116983['Tags'],_0x5a40('0x15'))},getCustomFields(_0x116983[_0x5a40('0x16')],_0x26648a));_0x962a1[_0x5a40('0x17')]=_['mapValues'](_[_0x5a40('0x18')](_0x962a1[_0x5a40('0x17')],'id'),'value');return request({'uri':util[_0x5a40('0xd')](_0x5a40('0x19'),_0x5cab16['remoteUri']),'method':_0x5a40('0x1a'),'body':_0x962a1})[_0x5a40('0x1b')](function(_0x4348de){logger[_0x5a40('0xb')](_0x5a40('0x1c'));return _0x4348de;});}function getUser(_0x509271,_0xc4cc01){if(intUtil[_0x5a40('0x1d')](_0xc4cc01)){return request({'uri':util['format'](_0x5a40('0x1e'),_0x509271['remoteUri']),'qs':{'email':_0xc4cc01}})['then'](function(_0xbae659){if(_0xbae659[_0x5a40('0x1f')]){logger[_0x5a40('0xb')]('Agent\x20found!');return _[_0x5a40('0x20')](_0xbae659,function(_0x2dbe5f){return _0x2dbe5f['contact'][_0x5a40('0x21')]===_0xc4cc01;});}else{logger[_0x5a40('0xb')](_0x5a40('0x22'));return request({'uri':util[_0x5a40('0xd')](_0x5a40('0x23'),_0x509271[_0x5a40('0x24')])})[_0x5a40('0x1b')](function(_0x17cc80){logger[_0x5a40('0xb')](_0x5a40('0x25'));return _0x17cc80;});}});}else{logger[_0x5a40('0xb')](_0x5a40('0x26'));return request({'uri':util[_0x5a40('0xd')](_0x5a40('0x23'),_0x509271[_0x5a40('0x24')])})[_0x5a40('0x1b')](function(_0x1bafbc){logger[_0x5a40('0xb')](_0x5a40('0x25'));return _0x1bafbc;});}}function getEndUser(_0x334eb4,_0x4921ba,_0x4c05c6){logger[_0x5a40('0xb')](_0x5a40('0x27'),_0x4c05c6);return request({'uri':util[_0x5a40('0xd')](_0x5a40('0x28'),_0x334eb4[_0x5a40('0x24')]),'qs':{'phone':_0x4c05c6}})[_0x5a40('0x1b')](function(_0x364ac6){if(_0x364ac6[_0x5a40('0x1f')]){logger[_0x5a40('0xb')](_0x5a40('0x29'));return _[_0x5a40('0x20')](_0x364ac6,{'phone':_0x4c05c6});}else{logger[_0x5a40('0xb')](_0x5a40('0x2a'));return request({'uri':util[_0x5a40('0xd')](_0x5a40('0x28'),_0x334eb4['remoteUri']),'method':_0x5a40('0x1a'),'body':{'name':_0x4921ba,'phone':_0x4c05c6}})[_0x5a40('0x1b')](function(_0x5978a3){logger[_0x5a40('0xb')](_0x5a40('0x2b'));return _0x5978a3;});}});}function getAuth(_0x5cb77e){return util[_0x5a40('0xd')](_0x5a40('0x2c'),new Buffer(_0x5cb77e[_0x5a40('0x2d')]+':X')[_0x5a40('0x2e')](_0x5a40('0x2f')));}function request(_0x54adb2){logger[_0x5a40('0x30')](_0x5a40('0x31'),JSON[_0x5a40('0x32')](_0x54adb2));return rp(_[_0x5a40('0x7')](_0x54adb2,{'method':'GET','headers':headers,'json':!![]}));}function getTags(_0x58eb9f,_0x29c6e8){var _0x1fc533=[];if(_0x58eb9f&&_0x58eb9f[_0x5a40('0x1f')]>0x0){_0x1fc533=_['map'](_0x58eb9f,_0x29c6e8);}return _0x1fc533;}function getCustomFields(_0x9166ed,_0x96d982){var _0x14276c=[];var _0x396303={'custom_fields':[]};if(_0x9166ed[_0x5a40('0x1f')]>0x0){_[_0x5a40('0x33')](_0x9166ed,function(_0x3009b8){if(_[_0x5a40('0x34')](['string','picklist'],_0x3009b8[_0x5a40('0x35')])&&_0x3009b8[_0x5a40('0x36')]&&!_0x3009b8[_0x5a40('0x37')]){_0x396303[_0x3009b8['nameField']['toLowerCase']()]=_0x3009b8['content']['toString']();}else if(_[_0x5a40('0x34')]([_0x5a40('0x38'),_0x5a40('0x39')],_0x3009b8[_0x5a40('0x35')])&&_0x3009b8[_0x5a40('0x3a')]&&_0x3009b8[_0x5a40('0x37')]){_0x396303[_0x5a40('0x17')][_0x5a40('0x3b')]({'id':_0x3009b8[_0x5a40('0x3a')],'value':_0x3009b8[_0x5a40('0x3c')][_0x5a40('0x2e')]()});}else if(_0x3009b8[_0x5a40('0x35')]===_0x5a40('0x3d')&&_0x3009b8[_0x5a40('0x3e')]&&_0x3009b8['idField']&&_0x3009b8[_0x5a40('0x37')]){_0x96d982[_0x3009b8[_0x5a40('0x3e')]]&&_0x396303['custom_fields'][_0x5a40('0x3b')]({'id':_0x3009b8['idField'],'value':_0x96d982[_0x3009b8[_0x5a40('0x3e')]][_0x5a40('0x2e')]()});}else if(_0x3009b8[_0x5a40('0x35')]===_0x5a40('0x3f')&&_0x3009b8[_0x5a40('0x40')]&&_0x3009b8['Variable']['name']&&_0x3009b8[_0x5a40('0x3a')]&&_0x3009b8[_0x5a40('0x37')]){_0x96d982[_0x3009b8[_0x5a40('0x40')]['name'][_0x5a40('0x41')]()]&&_0x396303[_0x5a40('0x17')][_0x5a40('0x3b')]({'id':_0x3009b8[_0x5a40('0x3a')],'value':_0x96d982[_0x3009b8['Variable'][_0x5a40('0x15')][_0x5a40('0x41')]()][_0x5a40('0x2e')]()});}});}return _0x396303;}exports[_0x5a40('0x42')]=function(_0x55e68b,_0x156e85,_0x435f1,_0x21f814,_0x10ef68,_0x29e19e){var _0x30f373,_0x17b1d8;_0x29e19e[_0x5a40('0x24')]=intUtil[_0x5a40('0x43')](_0x29e19e['remoteUri']);_0x29e19e[_0x5a40('0x44')]=intUtil[_0x5a40('0x43')](_0x29e19e[_0x5a40('0x44')]);if(_0x10ef68&&_0x10ef68[_0x5a40('0x45')]){logger[_0x5a40('0xb')](_0x5a40('0x46'),_0x5a40('0x47'));_0x156e85['recordingURL']=util[_0x5a40('0xd')](_0x5a40('0x48'),_0x29e19e['serverUrl'],_0x156e85[_0x5a40('0x49')],md5(_0x156e85[_0x5a40('0x49')]));}else{_0x156e85[_0x5a40('0x4a')]='';}headers[_0x5a40('0x4b')]=getAuth(_0x29e19e);return getUser(_0x29e19e,_0x21f814[_0x5a40('0x21')])[_0x5a40('0x1b')](function(_0x2f1562){_0x30f373=_0x2f1562;logger['info']('The\x20User\x20ID\x20is:',_0x30f373['id']);return getEndUser(_0x29e19e,_0x156e85[_0x5a40('0xf')],_0x156e85[_0x5a40('0x10')]);})[_0x5a40('0x1b')](function(_0x1ee8e0){_0x17b1d8=_0x1ee8e0;logger['info'](_0x5a40('0x4c'),_0x17b1d8['id']);return getTicket(_0x29e19e,_0x30f373,_0x17b1d8,_0x435f1,_0x156e85);})[_0x5a40('0x1b')](function(_0x480a5c){if(_0x480a5c&&_0x480a5c['id']){logger['info'](_0x5a40('0x1c'));logger[_0x5a40('0xb')](_0x5a40('0x4d'),_0x480a5c['id']);logger[_0x5a40('0xb')](_0x5a40('0x46'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util['format'](_0x5a40('0x4e'),_0x21f814[_0x5a40('0x15')]),'trigger:browser:url',{'uri':util['format'](_0x5a40('0x4f'),_0x29e19e['remoteUri'],_0x480a5c['id'])});}})['catch'](function(_0x31372a){logger[_0x5a40('0x50')](_0x5a40('0x46'),JSON[_0x5a40('0x32')](_0x31372a));});};exports[_0x5a40('0x51')]=function(_0x5773bb,_0x4c105b,_0x55b744,_0x111525,_0x253dda,_0x4fbf4d,_0x5f3be7){var _0x2acfd4,_0x2eab66;_0x4fbf4d[_0x5a40('0x24')]=intUtil[_0x5a40('0x43')](_0x4fbf4d[_0x5a40('0x24')]);_0x4fbf4d[_0x5a40('0x44')]=intUtil[_0x5a40('0x43')](_0x4fbf4d[_0x5a40('0x44')]);if(_0x5f3be7&&_0x5f3be7[_0x5a40('0x52')]&&_0x5f3be7[_0x5a40('0x52')]!=='none'){logger[_0x5a40('0xb')](_0x5a40('0x53'),_0x5a40('0x47'));_0x4c105b['recordingURL']=util[_0x5a40('0xd')](_0x5a40('0x48'),_0x4fbf4d[_0x5a40('0x44')],_0x4c105b[_0x5a40('0x49')],md5(_0x4c105b[_0x5a40('0x49')]));}headers[_0x5a40('0x4b')]=getAuth(_0x4fbf4d);return getUser(_0x4fbf4d,_0x111525[_0x5a40('0x21')])['then'](function(_0x17b6b3){_0x2acfd4=_0x17b6b3;logger[_0x5a40('0xb')](_0x5a40('0x54'),_0x2acfd4['id']);return getEndUser(_0x4fbf4d,_0x4c105b['destcalleridname'],_0x4c105b['destcalleridnum']);})[_0x5a40('0x1b')](function(_0x417519){_0x2eab66=_0x417519;logger[_0x5a40('0xb')](_0x5a40('0x4c'),_0x2eab66['id']);return getTicket(_0x4fbf4d,_0x2acfd4,_0x2eab66,_0x55b744,_0x4c105b);})['then'](function(_0x5bdf2d){if(_0x5bdf2d&&_0x5bdf2d['id']){logger[_0x5a40('0xb')](_0x5a40('0x1c'));logger['info'](_0x5a40('0x4d'),_0x5bdf2d['id']);logger[_0x5a40('0xb')](_0x5a40('0x53'),_0x5a40('0x55'));emit(util[_0x5a40('0xd')](_0x5a40('0x4e'),_0x111525['name']),_0x5a40('0x56'),{'uri':util[_0x5a40('0xd')](_0x5a40('0x4f'),_0x4fbf4d[_0x5a40('0x24')],_0x5bdf2d['id'])});}})['catch'](function(_0x2b1f7b){logger[_0x5a40('0x50')](_0x5a40('0x53'),JSON[_0x5a40('0x32')](_0x2b1f7b));});};exports[_0x5a40('0x57')]=function(_0x225b01,_0xf11670,_0x3e0873,_0x34a105,_0x5dd3ca,_0x5a8b8b){var _0x367a12,_0x3816ec;_0x5a8b8b['remoteUri']=intUtil['stripTrailingSlash'](_0x5a8b8b[_0x5a40('0x24')]);_0x5a8b8b[_0x5a40('0x44')]=intUtil[_0x5a40('0x43')](_0x5a8b8b[_0x5a40('0x44')]);if(_0x5dd3ca&&_0x5dd3ca[_0x5a40('0x45')]){logger[_0x5a40('0xb')]('['+_0xf11670['lastevent'][_0x5a40('0x58')]()+']',_0x5a40('0x47'));_0xf11670['recordingURL']=util[_0x5a40('0xd')](_0x5a40('0x48'),_0x5a8b8b['serverUrl'],_0xf11670['uniqueid'],md5(_0xf11670['uniqueid']));}else{_0xf11670[_0x5a40('0x4a')]='';}headers[_0x5a40('0x4b')]=getAuth(_0x5a8b8b);return getUser(_0x5a8b8b,'')[_0x5a40('0x1b')](function(_0x20c0df){_0x367a12=_0x20c0df;logger[_0x5a40('0xb')](_0x5a40('0x54'),_0x367a12['id']);return getEndUser(_0x5a8b8b,_0xf11670[_0x5a40('0xf')],_0xf11670[_0x5a40('0x10')]);})[_0x5a40('0x1b')](function(_0x18e49d){_0x3816ec=_0x18e49d;logger[_0x5a40('0xb')](_0x5a40('0x4c'),_0x3816ec['id']);return getTicket(_0x5a8b8b,_0x367a12,_0x3816ec,_0x3e0873,_0xf11670);})[_0x5a40('0x1b')](function(_0x1a7941){if(_0x1a7941){logger[_0x5a40('0xb')]('Ticket\x20created!');logger[_0x5a40('0xb')](_0x5a40('0x4d'),_0x1a7941['id']);}})[_0x5a40('0x59')](function(_0x5e18b2){logger[_0x5a40('0x50')]('['+_0xf11670[_0x5a40('0x5a')][_0x5a40('0x58')]()+']',JSON[_0x5a40('0x32')](_0x5e18b2));});};