0676b727798137caef6c763ee141fe2d120a1bcc
[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 _0xb983=['GET','map','includes','string','picklist','type','nameField','toLowerCase','content','toString','push','idField','variable','variableName','customField','customVariable','Variable','queue','serverUrl','stripTrailingSlash','monitor_format','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','Authorization','The\x20Enduser\x20ID\x20is:','Ticket\x20created!','[QUEUE]','trigger:browser:url','%s/helpdesk/tickets/%d','catch','recordingFormat','none','[OUTBOUND]','destcalleridname','destcalleridnum','The\x20Ticket\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','lastevent','toUpperCase','The\x20User\x20ID\x20is:','lodash','request-promise','util','ioredis','./util','../../../../config/logger','defaults','redis','localhost','application/json','info','Creating\x20new\x20ticket...','merge','Caller\x20-\x20%s','calleridname','calleridnum','getString','Subjects','Descriptions','<br>','Tags','name','Fields','custom_fields','keyBy','value','format','%s/api/v2/tickets','POST','then','validateEmail','remoteUri','length','Agent\x20found!','contact','email','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/api/v2/agents/me','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','Admin\x20account\x20selected.','Searching\x20enduser\x20with\x20phone','%s/api/v2/contacts','find','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Basic\x20%s','apiKey','debug','[HTTP]','stringify'];(function(_0x42735c,_0x3f4b00){var _0x5a519a=function(_0x582073){while(--_0x582073){_0x42735c['push'](_0x42735c['shift']());}};_0x5a519a(++_0x3f4b00);}(_0xb983,0x87));var _0x3b98=function(_0x43f881,_0xd6e5a2){_0x43f881=_0x43f881-0x0;var _0x365c14=_0xb983[_0x43f881];return _0x365c14;};'use strict';var _=require(_0x3b98('0x0'));var rp=require(_0x3b98('0x1'));var md5=require('md5');var util=require(_0x3b98('0x2'));var Redis=require(_0x3b98('0x3'));var intUtil=require(_0x3b98('0x4'));var config=require('../../../../config/environment');var logger=require(_0x3b98('0x5'))('freshdesk');config['redis']=_[_0x3b98('0x6')](config[_0x3b98('0x7')],{'host':_0x3b98('0x8'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x3b98('0x7')]));var headers={'Content-Type':'application/json','Accept':_0x3b98('0x9')};function emit(_0x4a3700,_0x2a7d55,_0x4fb51b){io['to'](_0x4a3700)['emit'](_0x2a7d55,_0x4fb51b);}function getTicket(_0x2b5be6,_0x230d71,_0x3ccaf9,_0x6100c6,_0x2eaec6){logger[_0x3b98('0xa')](_0x3b98('0xb'));var _0x3d2265=_[_0x3b98('0xc')]({'responder_id':_0x230d71['id'],'requester_id':_0x3ccaf9['id'],'name':util['format'](_0x3b98('0xd'),_0x2eaec6[_0x3b98('0xe')]),'phone':_0x2eaec6[_0x3b98('0xf')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0x3b98('0x10')](_0x6100c6[_0x3b98('0x11')],_0x2eaec6,'\x20'),'description':intUtil[_0x3b98('0x10')](_0x6100c6[_0x3b98('0x12')],_0x2eaec6,_0x3b98('0x13')),'tags':getTags(_0x6100c6[_0x3b98('0x14')],_0x3b98('0x15'))},getCustomFields(_0x6100c6[_0x3b98('0x16')],_0x2eaec6));_0x3d2265[_0x3b98('0x17')]=_['mapValues'](_[_0x3b98('0x18')](_0x3d2265['custom_fields'],'id'),_0x3b98('0x19'));return request({'uri':util[_0x3b98('0x1a')](_0x3b98('0x1b'),_0x2b5be6['remoteUri']),'method':_0x3b98('0x1c'),'body':_0x3d2265})[_0x3b98('0x1d')](function(_0x46ac95){logger[_0x3b98('0xa')]('Ticket\x20created!');return _0x46ac95;});}function getUser(_0x1d34cf,_0x5ed639){if(intUtil[_0x3b98('0x1e')](_0x5ed639)){return request({'uri':util[_0x3b98('0x1a')]('%s/api/v2/agents',_0x1d34cf[_0x3b98('0x1f')]),'qs':{'email':_0x5ed639}})[_0x3b98('0x1d')](function(_0x2309b4){if(_0x2309b4[_0x3b98('0x20')]){logger[_0x3b98('0xa')](_0x3b98('0x21'));return _['find'](_0x2309b4,function(_0x278f4c){return _0x278f4c[_0x3b98('0x22')][_0x3b98('0x23')]===_0x5ed639;});}else{logger[_0x3b98('0xa')](_0x3b98('0x24'));return request({'uri':util['format'](_0x3b98('0x25'),_0x1d34cf[_0x3b98('0x1f')])})['then'](function(_0x4f6709){logger[_0x3b98('0xa')]('Admin\x20account\x20selected.');return _0x4f6709;});}});}else{logger['info'](_0x3b98('0x26'));return request({'uri':util[_0x3b98('0x1a')](_0x3b98('0x25'),_0x1d34cf[_0x3b98('0x1f')])})[_0x3b98('0x1d')](function(_0x5bc84f){logger[_0x3b98('0xa')](_0x3b98('0x27'));return _0x5bc84f;});}}function getEndUser(_0x10e25c,_0x3c8b69,_0x939d2f){logger[_0x3b98('0xa')](_0x3b98('0x28'),_0x939d2f);return request({'uri':util['format'](_0x3b98('0x29'),_0x10e25c['remoteUri']),'qs':{'phone':_0x939d2f}})[_0x3b98('0x1d')](function(_0x3b5253){if(_0x3b5253[_0x3b98('0x20')]){logger[_0x3b98('0xa')]('Enduser\x20found!');return _[_0x3b98('0x2a')](_0x3b5253,{'phone':_0x939d2f});}else{logger[_0x3b98('0xa')](_0x3b98('0x2b'));return request({'uri':util[_0x3b98('0x1a')](_0x3b98('0x29'),_0x10e25c[_0x3b98('0x1f')]),'method':'POST','body':{'name':_0x3c8b69,'phone':_0x939d2f}})[_0x3b98('0x1d')](function(_0x3a7580){logger[_0x3b98('0xa')]('Enduser\x20created.');return _0x3a7580;});}});}function getAuth(_0x50c8f8){return util[_0x3b98('0x1a')](_0x3b98('0x2c'),new Buffer(_0x50c8f8[_0x3b98('0x2d')]+':X')['toString']('base64'));}function request(_0x3b047d){logger[_0x3b98('0x2e')](_0x3b98('0x2f'),JSON[_0x3b98('0x30')](_0x3b047d));return rp(_[_0x3b98('0x6')](_0x3b047d,{'method':_0x3b98('0x31'),'headers':headers,'json':!![]}));}function getTags(_0x201524,_0xe2ddc6){var _0x446df7=[];if(_0x201524&&_0x201524['length']>0x0){_0x446df7=_[_0x3b98('0x32')](_0x201524,_0xe2ddc6);}return _0x446df7;}function getCustomFields(_0x5e5aef,_0x43a5a){var _0x21d52e=[];var _0x193b7e={'custom_fields':[]};if(_0x5e5aef[_0x3b98('0x20')]>0x0){_['forEach'](_0x5e5aef,function(_0x12e779){if(_[_0x3b98('0x33')]([_0x3b98('0x34'),_0x3b98('0x35')],_0x12e779[_0x3b98('0x36')])&&_0x12e779['nameField']&&!_0x12e779['customField']){_0x193b7e[_0x12e779[_0x3b98('0x37')][_0x3b98('0x38')]()]=_0x12e779[_0x3b98('0x39')][_0x3b98('0x3a')]();}else if(_[_0x3b98('0x33')](['string',_0x3b98('0x35')],_0x12e779['type'])&&_0x12e779['idField']&&_0x12e779['customField']){_0x193b7e[_0x3b98('0x17')][_0x3b98('0x3b')]({'id':_0x12e779[_0x3b98('0x3c')],'value':_0x12e779[_0x3b98('0x39')][_0x3b98('0x3a')]()});}else if(_0x12e779['type']===_0x3b98('0x3d')&&_0x12e779[_0x3b98('0x3e')]&&_0x12e779[_0x3b98('0x3c')]&&_0x12e779[_0x3b98('0x3f')]){_0x43a5a[_0x12e779[_0x3b98('0x3e')]]&&_0x193b7e[_0x3b98('0x17')]['push']({'id':_0x12e779[_0x3b98('0x3c')],'value':_0x43a5a[_0x12e779[_0x3b98('0x3e')]][_0x3b98('0x3a')]()});}else if(_0x12e779[_0x3b98('0x36')]===_0x3b98('0x40')&&_0x12e779[_0x3b98('0x41')]&&_0x12e779['Variable'][_0x3b98('0x15')]&&_0x12e779[_0x3b98('0x3c')]&&_0x12e779[_0x3b98('0x3f')]){_0x43a5a[_0x12e779[_0x3b98('0x41')][_0x3b98('0x15')][_0x3b98('0x38')]()]&&_0x193b7e[_0x3b98('0x17')]['push']({'id':_0x12e779[_0x3b98('0x3c')],'value':_0x43a5a[_0x12e779['Variable'][_0x3b98('0x15')]['toLowerCase']()][_0x3b98('0x3a')]()});}});}return _0x193b7e;}exports[_0x3b98('0x42')]=function(_0x211925,_0x71235f,_0x28ff63,_0x29b18d,_0x32f6a3,_0x507136){var _0x569d48,_0x2518e0;_0x507136['remoteUri']=intUtil['stripTrailingSlash'](_0x507136[_0x3b98('0x1f')]);_0x507136[_0x3b98('0x43')]=intUtil[_0x3b98('0x44')](_0x507136['serverUrl']);if(_0x32f6a3&&_0x32f6a3[_0x3b98('0x45')]){logger[_0x3b98('0xa')]('[QUEUE]',_0x3b98('0x46'));_0x71235f[_0x3b98('0x47')]=util['format'](_0x3b98('0x48'),_0x507136['serverUrl'],_0x71235f[_0x3b98('0x49')],md5(_0x71235f[_0x3b98('0x49')]));}else{_0x71235f[_0x3b98('0x47')]='';}headers[_0x3b98('0x4a')]=getAuth(_0x507136);return getUser(_0x507136,_0x29b18d[_0x3b98('0x23')])[_0x3b98('0x1d')](function(_0x3924d4){_0x569d48=_0x3924d4;logger[_0x3b98('0xa')]('The\x20User\x20ID\x20is:',_0x569d48['id']);return getEndUser(_0x507136,_0x71235f[_0x3b98('0xe')],_0x71235f[_0x3b98('0xf')]);})[_0x3b98('0x1d')](function(_0x3e6c8d){_0x2518e0=_0x3e6c8d;logger[_0x3b98('0xa')](_0x3b98('0x4b'),_0x2518e0['id']);return getTicket(_0x507136,_0x569d48,_0x2518e0,_0x28ff63,_0x71235f);})['then'](function(_0x58618e){if(_0x58618e&&_0x58618e['id']){logger[_0x3b98('0xa')](_0x3b98('0x4c'));logger[_0x3b98('0xa')]('The\x20Ticket\x20ID\x20is:',_0x58618e['id']);logger[_0x3b98('0xa')](_0x3b98('0x4d'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0x3b98('0x1a')]('user:%s',_0x29b18d[_0x3b98('0x15')]),_0x3b98('0x4e'),{'uri':util[_0x3b98('0x1a')](_0x3b98('0x4f'),_0x507136[_0x3b98('0x1f')],_0x58618e['id'])});}})[_0x3b98('0x50')](function(_0x4b3595){logger['error'](_0x3b98('0x4d'),JSON[_0x3b98('0x30')](_0x4b3595));});};exports['outbound']=function(_0x31325d,_0x581720,_0x408ac0,_0x533ccc,_0x46e485,_0x25e078,_0x14faba){var _0x8aafa2,_0x4d832e;_0x25e078['remoteUri']=intUtil['stripTrailingSlash'](_0x25e078[_0x3b98('0x1f')]);_0x25e078[_0x3b98('0x43')]=intUtil[_0x3b98('0x44')](_0x25e078[_0x3b98('0x43')]);if(_0x14faba&&_0x14faba[_0x3b98('0x51')]&&_0x14faba[_0x3b98('0x51')]!==_0x3b98('0x52')){logger[_0x3b98('0xa')](_0x3b98('0x53'),'Recording\x20is\x20enabled!');_0x581720[_0x3b98('0x47')]=util[_0x3b98('0x1a')](_0x3b98('0x48'),_0x25e078[_0x3b98('0x43')],_0x581720[_0x3b98('0x49')],md5(_0x581720[_0x3b98('0x49')]));}headers['Authorization']=getAuth(_0x25e078);return getUser(_0x25e078,_0x533ccc[_0x3b98('0x23')])[_0x3b98('0x1d')](function(_0x464084){_0x8aafa2=_0x464084;logger[_0x3b98('0xa')]('The\x20User\x20ID\x20is:',_0x8aafa2['id']);return getEndUser(_0x25e078,_0x581720[_0x3b98('0x54')],_0x581720[_0x3b98('0x55')]);})[_0x3b98('0x1d')](function(_0x379b73){_0x4d832e=_0x379b73;logger[_0x3b98('0xa')]('The\x20Enduser\x20ID\x20is:',_0x4d832e['id']);return getTicket(_0x25e078,_0x8aafa2,_0x4d832e,_0x408ac0,_0x581720);})['then'](function(_0x584a06){if(_0x584a06&&_0x584a06['id']){logger['info'](_0x3b98('0x4c'));logger[_0x3b98('0xa')](_0x3b98('0x56'),_0x584a06['id']);logger['info'](_0x3b98('0x53'),_0x3b98('0x57'));emit(util[_0x3b98('0x1a')](_0x3b98('0x58'),_0x533ccc[_0x3b98('0x15')]),_0x3b98('0x4e'),{'uri':util[_0x3b98('0x1a')](_0x3b98('0x4f'),_0x25e078[_0x3b98('0x1f')],_0x584a06['id'])});}})['catch'](function(_0x23b218){logger['error']('[OUTBOUND]',JSON['stringify'](_0x23b218));});};exports['unmanaged']=function(_0x2b8042,_0x4a2371,_0x2709f2,_0x246adf,_0x5e7f61,_0x53faba){var _0x4e3b47,_0x80ecbe;_0x53faba[_0x3b98('0x1f')]=intUtil[_0x3b98('0x44')](_0x53faba[_0x3b98('0x1f')]);_0x53faba[_0x3b98('0x43')]=intUtil[_0x3b98('0x44')](_0x53faba[_0x3b98('0x43')]);if(_0x5e7f61&&_0x5e7f61['monitor_format']){logger['info']('['+_0x4a2371[_0x3b98('0x59')][_0x3b98('0x5a')]()+']',_0x3b98('0x46'));_0x4a2371[_0x3b98('0x47')]=util['format'](_0x3b98('0x48'),_0x53faba['serverUrl'],_0x4a2371['uniqueid'],md5(_0x4a2371['uniqueid']));}else{_0x4a2371['recordingURL']='';}headers['Authorization']=getAuth(_0x53faba);return getUser(_0x53faba,'')[_0x3b98('0x1d')](function(_0xb8bd73){_0x4e3b47=_0xb8bd73;logger['info'](_0x3b98('0x5b'),_0x4e3b47['id']);return getEndUser(_0x53faba,_0x4a2371['calleridname'],_0x4a2371[_0x3b98('0xf')]);})[_0x3b98('0x1d')](function(_0x401cc0){_0x80ecbe=_0x401cc0;logger['info'](_0x3b98('0x4b'),_0x80ecbe['id']);return getTicket(_0x53faba,_0x4e3b47,_0x80ecbe,_0x2709f2,_0x4a2371);})[_0x3b98('0x1d')](function(_0x1c0e38){if(_0x1c0e38){logger['info'](_0x3b98('0x4c'));logger[_0x3b98('0xa')](_0x3b98('0x56'),_0x1c0e38['id']);}})[_0x3b98('0x50')](function(_0x37614d){logger['error']('['+_0x4a2371[_0x3b98('0x59')][_0x3b98('0x5a')]()+']',JSON['stringify'](_0x37614d));});};