ad4aa49b65871a98f138b4dabf00eef02a2daab6
[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 _0x34ff=['push','toString','variable','variableName','customVariable','Variable','queue','stripTrailingSlash','serverUrl','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','The\x20User\x20ID\x20is:','calleridnum','The\x20Enduser\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','trigger:browser:url','%s/helpdesk/tickets/%d','catch','outbound','recordingFormat','none','[OUTBOUND]','Authorization','destcalleridname','The\x20Ticket\x20ID\x20is:','unmanaged','monitor_format','lastevent','error','toUpperCase','lodash','request-promise','util','./util','freshdesk','defaults','redis','localhost','application/json','emit','info','Creating\x20new\x20ticket...','format','Caller\x20-\x20%s','calleridname','getString','Descriptions','<br>','Tags','name','Fields','custom_fields','keyBy','value','%s/api/v2/tickets','remoteUri','then','Ticket\x20created!','%s/api/v2/agents','length','Agent\x20found!','find','contact','email','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','Admin\x20account\x20selected.','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','%s/api/v2/agents/me','Searching\x20enduser\x20with\x20phone','%s/api/v2/contacts','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','POST','Basic\x20%s','apiKey','debug','stringify','GET','forEach','string','picklist','nameField','customField','toLowerCase','content','includes','type','idField'];(function(_0x1ad30b,_0x34fcee){var _0x54e155=function(_0x5b3074){while(--_0x5b3074){_0x1ad30b['push'](_0x1ad30b['shift']());}};_0x54e155(++_0x34fcee);}(_0x34ff,0x1e9));var _0xf34f=function(_0x2daa75,_0x13b3ca){_0x2daa75=_0x2daa75-0x0;var _0x18fad1=_0x34ff[_0x2daa75];return _0x18fad1;};'use strict';var _=require(_0xf34f('0x0'));var rp=require(_0xf34f('0x1'));var md5=require('md5');var util=require(_0xf34f('0x2'));var Redis=require('ioredis');var intUtil=require(_0xf34f('0x3'));var config=require('../../../../config/environment');var logger=require('../../../../config/logger')(_0xf34f('0x4'));config['redis']=_[_0xf34f('0x5')](config[_0xf34f('0x6')],{'host':_0xf34f('0x7'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config['redis']));var headers={'Content-Type':_0xf34f('0x8'),'Accept':_0xf34f('0x8')};function emit(_0xcc40a4,_0x4daefe,_0x366972){io['to'](_0xcc40a4)[_0xf34f('0x9')](_0x4daefe,_0x366972);}function getTicket(_0x17c0fc,_0x2dc0c4,_0x1f5c46,_0x1f7004,_0x34d390){logger[_0xf34f('0xa')](_0xf34f('0xb'));var _0x3b02fa=_['merge']({'responder_id':_0x2dc0c4['id'],'requester_id':_0x1f5c46['id'],'name':util[_0xf34f('0xc')](_0xf34f('0xd'),_0x34d390[_0xf34f('0xe')]),'phone':_0x34d390['calleridnum'],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0xf34f('0xf')](_0x1f7004['Subjects'],_0x34d390,'\x20'),'description':intUtil[_0xf34f('0xf')](_0x1f7004[_0xf34f('0x10')],_0x34d390,_0xf34f('0x11')),'tags':getTags(_0x1f7004[_0xf34f('0x12')],_0xf34f('0x13'))},getCustomFields(_0x1f7004[_0xf34f('0x14')],_0x34d390));_0x3b02fa[_0xf34f('0x15')]=_['mapValues'](_[_0xf34f('0x16')](_0x3b02fa[_0xf34f('0x15')],'id'),_0xf34f('0x17'));return request({'uri':util['format'](_0xf34f('0x18'),_0x17c0fc[_0xf34f('0x19')]),'method':'POST','body':_0x3b02fa})[_0xf34f('0x1a')](function(_0x5502d1){logger[_0xf34f('0xa')](_0xf34f('0x1b'));return _0x5502d1;});}function getUser(_0x35ad7e,_0x5d90bc){if(intUtil['validateEmail'](_0x5d90bc)){return request({'uri':util['format'](_0xf34f('0x1c'),_0x35ad7e[_0xf34f('0x19')]),'qs':{'email':_0x5d90bc}})[_0xf34f('0x1a')](function(_0x17e1f6){if(_0x17e1f6[_0xf34f('0x1d')]){logger[_0xf34f('0xa')](_0xf34f('0x1e'));return _[_0xf34f('0x1f')](_0x17e1f6,function(_0x251353){return _0x251353[_0xf34f('0x20')][_0xf34f('0x21')]===_0x5d90bc;});}else{logger[_0xf34f('0xa')](_0xf34f('0x22'));return request({'uri':util['format']('%s/api/v2/agents/me',_0x35ad7e[_0xf34f('0x19')])})[_0xf34f('0x1a')](function(_0x2f1d8a){logger[_0xf34f('0xa')](_0xf34f('0x23'));return _0x2f1d8a;});}});}else{logger[_0xf34f('0xa')](_0xf34f('0x24'));return request({'uri':util[_0xf34f('0xc')](_0xf34f('0x25'),_0x35ad7e[_0xf34f('0x19')])})['then'](function(_0x160a87){logger[_0xf34f('0xa')](_0xf34f('0x23'));return _0x160a87;});}}function getEndUser(_0x17e744,_0x2bf8d7,_0x265247){logger[_0xf34f('0xa')](_0xf34f('0x26'),_0x265247);return request({'uri':util[_0xf34f('0xc')](_0xf34f('0x27'),_0x17e744['remoteUri']),'qs':{'phone':_0x265247}})['then'](function(_0x576de3){if(_0x576de3[_0xf34f('0x1d')]){logger['info']('Enduser\x20found!');return _['find'](_0x576de3,{'phone':_0x265247});}else{logger['info'](_0xf34f('0x28'));return request({'uri':util[_0xf34f('0xc')](_0xf34f('0x27'),_0x17e744[_0xf34f('0x19')]),'method':_0xf34f('0x29'),'body':{'name':_0x2bf8d7,'phone':_0x265247}})[_0xf34f('0x1a')](function(_0x3faea7){logger[_0xf34f('0xa')]('Enduser\x20created.');return _0x3faea7;});}});}function getAuth(_0x1ce547){return util[_0xf34f('0xc')](_0xf34f('0x2a'),new Buffer(_0x1ce547[_0xf34f('0x2b')]+':X')['toString']('base64'));}function request(_0x457426){logger[_0xf34f('0x2c')]('[HTTP]',JSON[_0xf34f('0x2d')](_0x457426));return rp(_[_0xf34f('0x5')](_0x457426,{'method':_0xf34f('0x2e'),'headers':headers,'json':!![]}));}function getTags(_0xec8cb8,_0x909cbe){var _0xd4df92=[];if(_0xec8cb8&&_0xec8cb8[_0xf34f('0x1d')]>0x0){_0xd4df92=_['map'](_0xec8cb8,_0x909cbe);}return _0xd4df92;}function getCustomFields(_0x536ab9,_0x4d3fbd){var _0x47c4f4=[];var _0x32047d={'custom_fields':[]};if(_0x536ab9['length']>0x0){_[_0xf34f('0x2f')](_0x536ab9,function(_0x1dc475){if(_['includes']([_0xf34f('0x30'),_0xf34f('0x31')],_0x1dc475['type'])&&_0x1dc475[_0xf34f('0x32')]&&!_0x1dc475[_0xf34f('0x33')]){_0x32047d[_0x1dc475[_0xf34f('0x32')][_0xf34f('0x34')]()]=_0x1dc475[_0xf34f('0x35')]['toString']();}else if(_[_0xf34f('0x36')]([_0xf34f('0x30'),_0xf34f('0x31')],_0x1dc475[_0xf34f('0x37')])&&_0x1dc475[_0xf34f('0x38')]&&_0x1dc475[_0xf34f('0x33')]){_0x32047d['custom_fields'][_0xf34f('0x39')]({'id':_0x1dc475[_0xf34f('0x38')],'value':_0x1dc475[_0xf34f('0x35')][_0xf34f('0x3a')]()});}else if(_0x1dc475[_0xf34f('0x37')]===_0xf34f('0x3b')&&_0x1dc475[_0xf34f('0x3c')]&&_0x1dc475['idField']&&_0x1dc475[_0xf34f('0x33')]){_0x4d3fbd[_0x1dc475[_0xf34f('0x3c')]]&&_0x32047d['custom_fields']['push']({'id':_0x1dc475[_0xf34f('0x38')],'value':_0x4d3fbd[_0x1dc475[_0xf34f('0x3c')]][_0xf34f('0x3a')]()});}else if(_0x1dc475[_0xf34f('0x37')]===_0xf34f('0x3d')&&_0x1dc475[_0xf34f('0x3e')]&&_0x1dc475['Variable'][_0xf34f('0x13')]&&_0x1dc475[_0xf34f('0x38')]&&_0x1dc475['customField']){_0x4d3fbd[_0x1dc475[_0xf34f('0x3e')][_0xf34f('0x13')]['toLowerCase']()]&&_0x32047d[_0xf34f('0x15')][_0xf34f('0x39')]({'id':_0x1dc475[_0xf34f('0x38')],'value':_0x4d3fbd[_0x1dc475[_0xf34f('0x3e')][_0xf34f('0x13')]['toLowerCase']()][_0xf34f('0x3a')]()});}});}return _0x32047d;}exports[_0xf34f('0x3f')]=function(_0x2091f9,_0x3b7683,_0x40d0f1,_0x2bca06,_0x45d0d1,_0x3e179c){var _0x1503d3,_0x508a71;_0x3e179c[_0xf34f('0x19')]=intUtil[_0xf34f('0x40')](_0x3e179c[_0xf34f('0x19')]);_0x3e179c[_0xf34f('0x41')]=intUtil[_0xf34f('0x40')](_0x3e179c['serverUrl']);if(_0x45d0d1&&_0x45d0d1['monitor_format']){logger[_0xf34f('0xa')](_0xf34f('0x42'),_0xf34f('0x43'));_0x3b7683[_0xf34f('0x44')]=util[_0xf34f('0xc')](_0xf34f('0x45'),_0x3e179c[_0xf34f('0x41')],_0x3b7683[_0xf34f('0x46')],md5(_0x3b7683[_0xf34f('0x46')]));}else{_0x3b7683[_0xf34f('0x44')]='';}headers['Authorization']=getAuth(_0x3e179c);return getUser(_0x3e179c,_0x2bca06[_0xf34f('0x21')])[_0xf34f('0x1a')](function(_0x14ba12){_0x1503d3=_0x14ba12;logger[_0xf34f('0xa')](_0xf34f('0x47'),_0x1503d3['id']);return getEndUser(_0x3e179c,_0x3b7683['calleridname'],_0x3b7683[_0xf34f('0x48')]);})[_0xf34f('0x1a')](function(_0x11b2d3){_0x508a71=_0x11b2d3;logger[_0xf34f('0xa')](_0xf34f('0x49'),_0x508a71['id']);return getTicket(_0x3e179c,_0x1503d3,_0x508a71,_0x40d0f1,_0x3b7683);})[_0xf34f('0x1a')](function(_0x18a21d){if(_0x18a21d&&_0x18a21d['id']){logger[_0xf34f('0xa')](_0xf34f('0x1b'));logger[_0xf34f('0xa')]('The\x20Ticket\x20ID\x20is:',_0x18a21d['id']);logger[_0xf34f('0xa')](_0xf34f('0x42'),_0xf34f('0x4a'));emit(util[_0xf34f('0xc')](_0xf34f('0x4b'),_0x2bca06[_0xf34f('0x13')]),_0xf34f('0x4c'),{'uri':util[_0xf34f('0xc')](_0xf34f('0x4d'),_0x3e179c[_0xf34f('0x19')],_0x18a21d['id'])});}})[_0xf34f('0x4e')](function(_0x8f43a6){logger['error'](_0xf34f('0x42'),JSON[_0xf34f('0x2d')](_0x8f43a6));});};exports[_0xf34f('0x4f')]=function(_0x437162,_0x206e9e,_0x3ed716,_0x4d22c3,_0x54744c,_0x5e81f0,_0x80efbd){var _0x4538b0,_0x3072a6;_0x5e81f0[_0xf34f('0x19')]=intUtil[_0xf34f('0x40')](_0x5e81f0[_0xf34f('0x19')]);_0x5e81f0['serverUrl']=intUtil[_0xf34f('0x40')](_0x5e81f0[_0xf34f('0x41')]);if(_0x80efbd&&_0x80efbd[_0xf34f('0x50')]&&_0x80efbd['recordingFormat']!==_0xf34f('0x51')){logger['info'](_0xf34f('0x52'),_0xf34f('0x43'));_0x206e9e[_0xf34f('0x44')]=util[_0xf34f('0xc')](_0xf34f('0x45'),_0x5e81f0[_0xf34f('0x41')],_0x206e9e[_0xf34f('0x46')],md5(_0x206e9e[_0xf34f('0x46')]));}headers[_0xf34f('0x53')]=getAuth(_0x5e81f0);return getUser(_0x5e81f0,_0x4d22c3[_0xf34f('0x21')])[_0xf34f('0x1a')](function(_0x4eebf3){_0x4538b0=_0x4eebf3;logger[_0xf34f('0xa')](_0xf34f('0x47'),_0x4538b0['id']);return getEndUser(_0x5e81f0,_0x206e9e[_0xf34f('0x54')],_0x206e9e['destcalleridnum']);})[_0xf34f('0x1a')](function(_0x55c5fd){_0x3072a6=_0x55c5fd;logger[_0xf34f('0xa')](_0xf34f('0x49'),_0x3072a6['id']);return getTicket(_0x5e81f0,_0x4538b0,_0x3072a6,_0x3ed716,_0x206e9e);})[_0xf34f('0x1a')](function(_0x3871db){if(_0x3871db&&_0x3871db['id']){logger[_0xf34f('0xa')](_0xf34f('0x1b'));logger[_0xf34f('0xa')](_0xf34f('0x55'),_0x3871db['id']);logger[_0xf34f('0xa')]('[OUTBOUND]','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0xf34f('0xc')](_0xf34f('0x4b'),_0x4d22c3['name']),'trigger:browser:url',{'uri':util[_0xf34f('0xc')](_0xf34f('0x4d'),_0x5e81f0[_0xf34f('0x19')],_0x3871db['id'])});}})['catch'](function(_0x1762f1){logger['error'](_0xf34f('0x52'),JSON[_0xf34f('0x2d')](_0x1762f1));});};exports[_0xf34f('0x56')]=function(_0x150e11,_0x2781b9,_0x35381e,_0x3a6872,_0x4aebbd,_0xea6de){var _0x49478f,_0x2cb124;_0xea6de['remoteUri']=intUtil['stripTrailingSlash'](_0xea6de[_0xf34f('0x19')]);_0xea6de[_0xf34f('0x41')]=intUtil[_0xf34f('0x40')](_0xea6de[_0xf34f('0x41')]);if(_0x4aebbd&&_0x4aebbd[_0xf34f('0x57')]){logger[_0xf34f('0xa')]('['+_0x2781b9[_0xf34f('0x58')]['toUpperCase']()+']','Recording\x20is\x20enabled!');_0x2781b9[_0xf34f('0x44')]=util[_0xf34f('0xc')](_0xf34f('0x45'),_0xea6de[_0xf34f('0x41')],_0x2781b9[_0xf34f('0x46')],md5(_0x2781b9['uniqueid']));}else{_0x2781b9[_0xf34f('0x44')]='';}headers[_0xf34f('0x53')]=getAuth(_0xea6de);return getUser(_0xea6de,'')['then'](function(_0xbd6f1c){_0x49478f=_0xbd6f1c;logger[_0xf34f('0xa')](_0xf34f('0x47'),_0x49478f['id']);return getEndUser(_0xea6de,_0x2781b9[_0xf34f('0xe')],_0x2781b9['calleridnum']);})[_0xf34f('0x1a')](function(_0x487ad0){_0x2cb124=_0x487ad0;logger[_0xf34f('0xa')](_0xf34f('0x49'),_0x2cb124['id']);return getTicket(_0xea6de,_0x49478f,_0x2cb124,_0x35381e,_0x2781b9);})[_0xf34f('0x1a')](function(_0x166bb4){if(_0x166bb4){logger[_0xf34f('0xa')](_0xf34f('0x1b'));logger[_0xf34f('0xa')](_0xf34f('0x55'),_0x166bb4['id']);}})[_0xf34f('0x4e')](function(_0xcfd711){logger[_0xf34f('0x59')]('['+_0x2781b9[_0xf34f('0x58')][_0xf34f('0x5a')]()+']',JSON['stringify'](_0xcfd711));});};