Built motion from commit efa66e19.|2.6.22
[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 _0x443c=['The\x20User\x20ID\x20is:','calleridnum','The\x20Enduser\x20ID\x20is:','user:%s','trigger:browser:url','%s/helpdesk/tickets/%d','catch','error','outbound','recordingFormat','none','[OUTBOUND]','destcalleridname','destcalleridnum','The\x20Ticket\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','lastevent','toUpperCase','lodash','request-promise','md5','util','ioredis','./util','../../../../config/logger','redis','defaults','localhost','socket.io-emitter','application/json','info','merge','format','Caller\x20-\x20%s','calleridname','getString','Descriptions','Tags','Fields','custom_fields','mapValues','%s/api/v2/tickets','POST','then','Ticket\x20created!','validateEmail','Agent\x20found!','contact','email','%s/api/v2/agents/me','remoteUri','Admin\x20account\x20selected.','Searching\x20enduser\x20with\x20phone','%s/api/v2/contacts','length','Enduser\x20found!','find','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Enduser\x20created.','Basic\x20%s','apiKey','toString','base64','debug','[HTTP]','stringify','GET','map','forEach','includes','string','picklist','nameField','customField','type','idField','push','variable','variableName','customVariable','Variable','name','toLowerCase','queue','stripTrailingSlash','serverUrl','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','Authorization'];(function(_0x1fa841,_0x37a70c){var _0x384b67=function(_0x248c40){while(--_0x248c40){_0x1fa841['push'](_0x1fa841['shift']());}};_0x384b67(++_0x37a70c);}(_0x443c,0xcc));var _0xc443=function(_0x362ce4,_0x5cfa56){_0x362ce4=_0x362ce4-0x0;var _0x18dcb0=_0x443c[_0x362ce4];return _0x18dcb0;};'use strict';var _=require(_0xc443('0x0'));var rp=require(_0xc443('0x1'));var md5=require(_0xc443('0x2'));var util=require(_0xc443('0x3'));var Redis=require(_0xc443('0x4'));var intUtil=require(_0xc443('0x5'));var config=require('../../../../config/environment');var logger=require(_0xc443('0x6'))('freshdesk');config[_0xc443('0x7')]=_[_0xc443('0x8')](config[_0xc443('0x7')],{'host':_0xc443('0x9'),'port':0x18eb});var io=require(_0xc443('0xa'))(new Redis(config['redis']));var headers={'Content-Type':_0xc443('0xb'),'Accept':_0xc443('0xb')};function emit(_0xd70def,_0x16dae9,_0x469d2e){io['to'](_0xd70def)['emit'](_0x16dae9,_0x469d2e);}function getTicket(_0xb07850,_0x437114,_0x39af77,_0x1f77c1,_0x43d1a5){logger[_0xc443('0xc')]('Creating\x20new\x20ticket...');var _0x15679d=_[_0xc443('0xd')]({'responder_id':_0x437114['id'],'requester_id':_0x39af77['id'],'name':util[_0xc443('0xe')](_0xc443('0xf'),_0x43d1a5[_0xc443('0x10')]),'phone':_0x43d1a5['calleridnum'],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0xc443('0x11')](_0x1f77c1['Subjects'],_0x43d1a5,'\x20'),'description':intUtil[_0xc443('0x11')](_0x1f77c1[_0xc443('0x12')],_0x43d1a5,'<br>'),'tags':getTags(_0x1f77c1[_0xc443('0x13')],'name')},getCustomFields(_0x1f77c1[_0xc443('0x14')],_0x43d1a5));_0x15679d[_0xc443('0x15')]=_[_0xc443('0x16')](_['keyBy'](_0x15679d[_0xc443('0x15')],'id'),'value');return request({'uri':util[_0xc443('0xe')](_0xc443('0x17'),_0xb07850['remoteUri']),'method':_0xc443('0x18'),'body':_0x15679d})[_0xc443('0x19')](function(_0x38eda6){logger[_0xc443('0xc')](_0xc443('0x1a'));return _0x38eda6;});}function getUser(_0xb576c,_0x2e56d5){if(intUtil[_0xc443('0x1b')](_0x2e56d5)){return request({'uri':util['format']('%s/api/v2/agents',_0xb576c['remoteUri']),'qs':{'email':_0x2e56d5}})[_0xc443('0x19')](function(_0x2f537d){if(_0x2f537d['length']){logger[_0xc443('0xc')](_0xc443('0x1c'));return _['find'](_0x2f537d,function(_0x32b48c){return _0x32b48c[_0xc443('0x1d')][_0xc443('0x1e')]===_0x2e56d5;});}else{logger[_0xc443('0xc')]('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');return request({'uri':util[_0xc443('0xe')](_0xc443('0x1f'),_0xb576c[_0xc443('0x20')])})[_0xc443('0x19')](function(_0x19f1ab){logger['info']('Admin\x20account\x20selected.');return _0x19f1ab;});}});}else{logger[_0xc443('0xc')]('Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!');return request({'uri':util[_0xc443('0xe')](_0xc443('0x1f'),_0xb576c[_0xc443('0x20')])})[_0xc443('0x19')](function(_0x3e5d7e){logger['info'](_0xc443('0x21'));return _0x3e5d7e;});}}function getEndUser(_0x39ad1d,_0xfe7c8b,_0x50061e){logger['info'](_0xc443('0x22'),_0x50061e);return request({'uri':util[_0xc443('0xe')](_0xc443('0x23'),_0x39ad1d[_0xc443('0x20')]),'qs':{'phone':_0x50061e}})[_0xc443('0x19')](function(_0x254b78){if(_0x254b78[_0xc443('0x24')]){logger['info'](_0xc443('0x25'));return _[_0xc443('0x26')](_0x254b78,{'phone':_0x50061e});}else{logger[_0xc443('0xc')](_0xc443('0x27'));return request({'uri':util[_0xc443('0xe')](_0xc443('0x23'),_0x39ad1d[_0xc443('0x20')]),'method':_0xc443('0x18'),'body':{'name':_0xfe7c8b,'phone':_0x50061e}})['then'](function(_0x44f7c5){logger[_0xc443('0xc')](_0xc443('0x28'));return _0x44f7c5;});}});}function getAuth(_0x4c9d3d){return util['format'](_0xc443('0x29'),new Buffer(_0x4c9d3d[_0xc443('0x2a')]+':X')[_0xc443('0x2b')](_0xc443('0x2c')));}function request(_0x371803){logger[_0xc443('0x2d')](_0xc443('0x2e'),JSON[_0xc443('0x2f')](_0x371803));return rp(_[_0xc443('0x8')](_0x371803,{'method':_0xc443('0x30'),'headers':headers,'json':!![]}));}function getTags(_0x52469d,_0x12e1ed){var _0x5b0aba=[];if(_0x52469d&&_0x52469d[_0xc443('0x24')]>0x0){_0x5b0aba=_[_0xc443('0x31')](_0x52469d,_0x12e1ed);}return _0x5b0aba;}function getCustomFields(_0x584ea5,_0x30367f){var _0x5b755f=[];var _0x375b73={'custom_fields':[]};if(_0x584ea5['length']>0x0){_[_0xc443('0x32')](_0x584ea5,function(_0x374cb8){if(_[_0xc443('0x33')]([_0xc443('0x34'),_0xc443('0x35')],_0x374cb8['type'])&&_0x374cb8[_0xc443('0x36')]&&!_0x374cb8[_0xc443('0x37')]){_0x375b73[_0x374cb8['nameField']['toLowerCase']()]=_0x374cb8['content']['toString']();}else if(_[_0xc443('0x33')](['string','picklist'],_0x374cb8[_0xc443('0x38')])&&_0x374cb8[_0xc443('0x39')]&&_0x374cb8[_0xc443('0x37')]){_0x375b73[_0xc443('0x15')][_0xc443('0x3a')]({'id':_0x374cb8[_0xc443('0x39')],'value':_0x374cb8['content'][_0xc443('0x2b')]()});}else if(_0x374cb8['type']===_0xc443('0x3b')&&_0x374cb8['variableName']&&_0x374cb8[_0xc443('0x39')]&&_0x374cb8[_0xc443('0x37')]){_0x30367f[_0x374cb8[_0xc443('0x3c')]]&&_0x375b73[_0xc443('0x15')]['push']({'id':_0x374cb8[_0xc443('0x39')],'value':_0x30367f[_0x374cb8['variableName']][_0xc443('0x2b')]()});}else if(_0x374cb8[_0xc443('0x38')]===_0xc443('0x3d')&&_0x374cb8[_0xc443('0x3e')]&&_0x374cb8['Variable'][_0xc443('0x3f')]&&_0x374cb8[_0xc443('0x39')]&&_0x374cb8['customField']){_0x30367f[_0x374cb8[_0xc443('0x3e')][_0xc443('0x3f')][_0xc443('0x40')]()]&&_0x375b73['custom_fields'][_0xc443('0x3a')]({'id':_0x374cb8[_0xc443('0x39')],'value':_0x30367f[_0x374cb8[_0xc443('0x3e')]['name'][_0xc443('0x40')]()]['toString']()});}});}return _0x375b73;}exports[_0xc443('0x41')]=function(_0x5a25de,_0x4e8cf9,_0x151f6e,_0x1a5ec4,_0x5a4ec7,_0x5c7394){var _0x21539d,_0xf37cf4;_0x5c7394[_0xc443('0x20')]=intUtil[_0xc443('0x42')](_0x5c7394[_0xc443('0x20')]);_0x5c7394[_0xc443('0x43')]=intUtil[_0xc443('0x42')](_0x5c7394['serverUrl']);if(_0x5a4ec7&&_0x5a4ec7[_0xc443('0x44')]){logger[_0xc443('0xc')](_0xc443('0x45'),_0xc443('0x46'));_0x4e8cf9[_0xc443('0x47')]=util['format'](_0xc443('0x48'),_0x5c7394[_0xc443('0x43')],_0x4e8cf9['uniqueid'],md5(_0x4e8cf9[_0xc443('0x49')]));}else{_0x4e8cf9[_0xc443('0x47')]='';}headers[_0xc443('0x4a')]=getAuth(_0x5c7394);return getUser(_0x5c7394,_0x1a5ec4[_0xc443('0x1e')])[_0xc443('0x19')](function(_0x26f189){_0x21539d=_0x26f189;logger['info'](_0xc443('0x4b'),_0x21539d['id']);return getEndUser(_0x5c7394,_0x4e8cf9[_0xc443('0x10')],_0x4e8cf9[_0xc443('0x4c')]);})[_0xc443('0x19')](function(_0x1e19ec){_0xf37cf4=_0x1e19ec;logger[_0xc443('0xc')](_0xc443('0x4d'),_0xf37cf4['id']);return getTicket(_0x5c7394,_0x21539d,_0xf37cf4,_0x151f6e,_0x4e8cf9);})[_0xc443('0x19')](function(_0x4a323b){if(_0x4a323b&&_0x4a323b['id']){logger[_0xc443('0xc')](_0xc443('0x1a'));logger[_0xc443('0xc')]('The\x20Ticket\x20ID\x20is:',_0x4a323b['id']);logger[_0xc443('0xc')](_0xc443('0x45'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0xc443('0xe')](_0xc443('0x4e'),_0x1a5ec4[_0xc443('0x3f')]),_0xc443('0x4f'),{'uri':util[_0xc443('0xe')](_0xc443('0x50'),_0x5c7394[_0xc443('0x20')],_0x4a323b['id'])});}})[_0xc443('0x51')](function(_0x138a4e){logger[_0xc443('0x52')]('[QUEUE]',JSON[_0xc443('0x2f')](_0x138a4e));});};exports[_0xc443('0x53')]=function(_0x183648,_0x3ba131,_0x13e1df,_0x5e051c,_0x1f6be5,_0x204502,_0x1d719b){var _0x201a87,_0x35318a;_0x204502[_0xc443('0x20')]=intUtil['stripTrailingSlash'](_0x204502[_0xc443('0x20')]);_0x204502[_0xc443('0x43')]=intUtil[_0xc443('0x42')](_0x204502[_0xc443('0x43')]);if(_0x1d719b&&_0x1d719b['recordingFormat']&&_0x1d719b[_0xc443('0x54')]!==_0xc443('0x55')){logger[_0xc443('0xc')](_0xc443('0x56'),_0xc443('0x46'));_0x3ba131[_0xc443('0x47')]=util[_0xc443('0xe')](_0xc443('0x48'),_0x204502[_0xc443('0x43')],_0x3ba131[_0xc443('0x49')],md5(_0x3ba131[_0xc443('0x49')]));}headers[_0xc443('0x4a')]=getAuth(_0x204502);return getUser(_0x204502,_0x5e051c[_0xc443('0x1e')])[_0xc443('0x19')](function(_0xc14721){_0x201a87=_0xc14721;logger['info'](_0xc443('0x4b'),_0x201a87['id']);return getEndUser(_0x204502,_0x3ba131[_0xc443('0x57')],_0x3ba131[_0xc443('0x58')]);})[_0xc443('0x19')](function(_0xb7209){_0x35318a=_0xb7209;logger[_0xc443('0xc')](_0xc443('0x4d'),_0x35318a['id']);return getTicket(_0x204502,_0x201a87,_0x35318a,_0x13e1df,_0x3ba131);})[_0xc443('0x19')](function(_0x20c6c3){if(_0x20c6c3&&_0x20c6c3['id']){logger['info']('Ticket\x20created!');logger['info'](_0xc443('0x59'),_0x20c6c3['id']);logger[_0xc443('0xc')](_0xc443('0x56'),_0xc443('0x5a'));emit(util[_0xc443('0xe')](_0xc443('0x4e'),_0x5e051c['name']),'trigger:browser:url',{'uri':util[_0xc443('0xe')](_0xc443('0x50'),_0x204502[_0xc443('0x20')],_0x20c6c3['id'])});}})[_0xc443('0x51')](function(_0x30efaf){logger['error']('[OUTBOUND]',JSON[_0xc443('0x2f')](_0x30efaf));});};exports['unmanaged']=function(_0x5cefe1,_0xd447a0,_0x54dbe7,_0x27fcd6,_0x4db1e2,_0x534b76){var _0x17ef37,_0x32009b;_0x534b76[_0xc443('0x20')]=intUtil[_0xc443('0x42')](_0x534b76['remoteUri']);_0x534b76['serverUrl']=intUtil[_0xc443('0x42')](_0x534b76[_0xc443('0x43')]);if(_0x4db1e2&&_0x4db1e2['monitor_format']){logger[_0xc443('0xc')]('['+_0xd447a0[_0xc443('0x5b')][_0xc443('0x5c')]()+']','Recording\x20is\x20enabled!');_0xd447a0[_0xc443('0x47')]=util[_0xc443('0xe')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x534b76['serverUrl'],_0xd447a0[_0xc443('0x49')],md5(_0xd447a0[_0xc443('0x49')]));}else{_0xd447a0['recordingURL']='';}headers[_0xc443('0x4a')]=getAuth(_0x534b76);return getUser(_0x534b76,'')[_0xc443('0x19')](function(_0x568843){_0x17ef37=_0x568843;logger[_0xc443('0xc')](_0xc443('0x4b'),_0x17ef37['id']);return getEndUser(_0x534b76,_0xd447a0['calleridname'],_0xd447a0[_0xc443('0x4c')]);})[_0xc443('0x19')](function(_0x5e8cbf){_0x32009b=_0x5e8cbf;logger[_0xc443('0xc')](_0xc443('0x4d'),_0x32009b['id']);return getTicket(_0x534b76,_0x17ef37,_0x32009b,_0x54dbe7,_0xd447a0);})[_0xc443('0x19')](function(_0x27fee8){if(_0x27fee8){logger[_0xc443('0xc')](_0xc443('0x1a'));logger[_0xc443('0xc')]('The\x20Ticket\x20ID\x20is:',_0x27fee8['id']);}})[_0xc443('0x51')](function(_0x5f54e5){logger[_0xc443('0x52')]('['+_0xd447a0[_0xc443('0x5b')][_0xc443('0x5c')]()+']',JSON['stringify'](_0x5f54e5));});};