8ec87253144e7a055d4d42ece874a5054d7e4391
[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 _0xf8f3=['util','ioredis','./util','../../../../config/environment','freshdesk','redis','defaults','localhost','socket.io-emitter','application/json','emit','Creating\x20new\x20ticket...','merge','format','Caller\x20-\x20%s','calleridnum','getString','Descriptions','<br>','Tags','Fields','custom_fields','value','remoteUri','info','validateEmail','%s/api/v2/agents','Agent\x20found!','find','email','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/api/v2/agents/me','Admin\x20account\x20selected.','then','%s/api/v2/contacts','length','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','POST','Enduser\x20created.','base64','debug','[HTTP]','stringify','GET','map','forEach','includes','string','picklist','type','nameField','customField','toLowerCase','content','idField','toString','variable','variableName','customVariable','Variable','name','push','queue','stripTrailingSlash','serverUrl','monitor_format','Recording\x20is\x20enabled!','uniqueid','Authorization','The\x20User\x20ID\x20is:','The\x20Enduser\x20ID\x20is:','Ticket\x20created!','The\x20Ticket\x20ID\x20is:','[QUEUE]','%s/helpdesk/tickets/%d','catch','error','recordingFormat','none','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','destcalleridnum','[OUTBOUND]','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','toUpperCase','recordingURL','calleridname','lodash','request-promise','md5'];(function(_0x26df48,_0x22aa50){var _0x209047=function(_0x1d068b){while(--_0x1d068b){_0x26df48['push'](_0x26df48['shift']());}};_0x209047(++_0x22aa50);}(_0xf8f3,0xb1));var _0x3f8f=function(_0x4f22c8,_0x5c5252){_0x4f22c8=_0x4f22c8-0x0;var _0x516ef5=_0xf8f3[_0x4f22c8];return _0x516ef5;};'use strict';var _=require(_0x3f8f('0x0'));var rp=require(_0x3f8f('0x1'));var md5=require(_0x3f8f('0x2'));var util=require(_0x3f8f('0x3'));var Redis=require(_0x3f8f('0x4'));var intUtil=require(_0x3f8f('0x5'));var config=require(_0x3f8f('0x6'));var logger=require('../../../../config/logger')(_0x3f8f('0x7'));config[_0x3f8f('0x8')]=_[_0x3f8f('0x9')](config[_0x3f8f('0x8')],{'host':_0x3f8f('0xa'),'port':0x18eb});var io=require(_0x3f8f('0xb'))(new Redis(config['redis']));var headers={'Content-Type':'application/json','Accept':_0x3f8f('0xc')};function emit(_0x4e6225,_0xec1da7,_0x26c036){io['to'](_0x4e6225)[_0x3f8f('0xd')](_0xec1da7,_0x26c036);}function getTicket(_0x277dd0,_0xb964e7,_0x4be6ff,_0x4621c4,_0x168920){logger['info'](_0x3f8f('0xe'));var _0x56d77c=_[_0x3f8f('0xf')]({'responder_id':_0xb964e7['id'],'requester_id':_0x4be6ff['id'],'name':util[_0x3f8f('0x10')](_0x3f8f('0x11'),_0x168920['calleridname']),'phone':_0x168920[_0x3f8f('0x12')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil['getString'](_0x4621c4['Subjects'],_0x168920,'\x20'),'description':intUtil[_0x3f8f('0x13')](_0x4621c4[_0x3f8f('0x14')],_0x168920,_0x3f8f('0x15')),'tags':getTags(_0x4621c4[_0x3f8f('0x16')],'name')},getCustomFields(_0x4621c4[_0x3f8f('0x17')],_0x168920));_0x56d77c[_0x3f8f('0x18')]=_['mapValues'](_['keyBy'](_0x56d77c['custom_fields'],'id'),_0x3f8f('0x19'));return request({'uri':util[_0x3f8f('0x10')]('%s/api/v2/tickets',_0x277dd0[_0x3f8f('0x1a')]),'method':'POST','body':_0x56d77c})['then'](function(_0x22cb18){logger[_0x3f8f('0x1b')]('Ticket\x20created!');return _0x22cb18;});}function getUser(_0x8ee1f6,_0x259515){if(intUtil[_0x3f8f('0x1c')](_0x259515)){return request({'uri':util[_0x3f8f('0x10')](_0x3f8f('0x1d'),_0x8ee1f6['remoteUri']),'qs':{'email':_0x259515}})['then'](function(_0x349e9c){if(_0x349e9c['length']){logger[_0x3f8f('0x1b')](_0x3f8f('0x1e'));return _[_0x3f8f('0x1f')](_0x349e9c,function(_0x35cc15){return _0x35cc15['contact'][_0x3f8f('0x20')]===_0x259515;});}else{logger[_0x3f8f('0x1b')](_0x3f8f('0x21'));return request({'uri':util[_0x3f8f('0x10')](_0x3f8f('0x22'),_0x8ee1f6['remoteUri'])})['then'](function(_0x561a88){logger[_0x3f8f('0x1b')](_0x3f8f('0x23'));return _0x561a88;});}});}else{logger[_0x3f8f('0x1b')]('Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!');return request({'uri':util['format'](_0x3f8f('0x22'),_0x8ee1f6[_0x3f8f('0x1a')])})[_0x3f8f('0x24')](function(_0x13c261){logger[_0x3f8f('0x1b')](_0x3f8f('0x23'));return _0x13c261;});}}function getEndUser(_0x569f42,_0x524876,_0x527953){logger['info']('Searching\x20enduser\x20with\x20phone',_0x527953);return request({'uri':util[_0x3f8f('0x10')](_0x3f8f('0x25'),_0x569f42['remoteUri']),'qs':{'phone':_0x527953}})['then'](function(_0x352b32){if(_0x352b32[_0x3f8f('0x26')]){logger[_0x3f8f('0x1b')](_0x3f8f('0x27'));return _['find'](_0x352b32,{'phone':_0x527953});}else{logger['info'](_0x3f8f('0x28'));return request({'uri':util[_0x3f8f('0x10')]('%s/api/v2/contacts',_0x569f42[_0x3f8f('0x1a')]),'method':_0x3f8f('0x29'),'body':{'name':_0x524876,'phone':_0x527953}})[_0x3f8f('0x24')](function(_0x3b7779){logger[_0x3f8f('0x1b')](_0x3f8f('0x2a'));return _0x3b7779;});}});}function getAuth(_0x42f27d){return util['format']('Basic\x20%s',new Buffer(_0x42f27d['apiKey']+':X')['toString'](_0x3f8f('0x2b')));}function request(_0x10f151){logger[_0x3f8f('0x2c')](_0x3f8f('0x2d'),JSON[_0x3f8f('0x2e')](_0x10f151));return rp(_[_0x3f8f('0x9')](_0x10f151,{'method':_0x3f8f('0x2f'),'headers':headers,'json':!![]}));}function getTags(_0x27ce0d,_0x1f54f5){var _0x2567ce=[];if(_0x27ce0d&&_0x27ce0d[_0x3f8f('0x26')]>0x0){_0x2567ce=_[_0x3f8f('0x30')](_0x27ce0d,_0x1f54f5);}return _0x2567ce;}function getCustomFields(_0x9ffd73,_0x16f8cc){var _0x1b0d68=[];var _0x2d5da5={'custom_fields':[]};if(_0x9ffd73[_0x3f8f('0x26')]>0x0){_[_0x3f8f('0x31')](_0x9ffd73,function(_0x46ab89){if(_[_0x3f8f('0x32')]([_0x3f8f('0x33'),_0x3f8f('0x34')],_0x46ab89[_0x3f8f('0x35')])&&_0x46ab89[_0x3f8f('0x36')]&&!_0x46ab89[_0x3f8f('0x37')]){_0x2d5da5[_0x46ab89[_0x3f8f('0x36')][_0x3f8f('0x38')]()]=_0x46ab89[_0x3f8f('0x39')]['toString']();}else if(_[_0x3f8f('0x32')]([_0x3f8f('0x33'),_0x3f8f('0x34')],_0x46ab89[_0x3f8f('0x35')])&&_0x46ab89[_0x3f8f('0x3a')]&&_0x46ab89[_0x3f8f('0x37')]){_0x2d5da5['custom_fields']['push']({'id':_0x46ab89[_0x3f8f('0x3a')],'value':_0x46ab89[_0x3f8f('0x39')][_0x3f8f('0x3b')]()});}else if(_0x46ab89[_0x3f8f('0x35')]===_0x3f8f('0x3c')&&_0x46ab89[_0x3f8f('0x3d')]&&_0x46ab89[_0x3f8f('0x3a')]&&_0x46ab89['customField']){_0x16f8cc[_0x46ab89[_0x3f8f('0x3d')]]&&_0x2d5da5[_0x3f8f('0x18')]['push']({'id':_0x46ab89[_0x3f8f('0x3a')],'value':_0x16f8cc[_0x46ab89[_0x3f8f('0x3d')]][_0x3f8f('0x3b')]()});}else if(_0x46ab89[_0x3f8f('0x35')]===_0x3f8f('0x3e')&&_0x46ab89[_0x3f8f('0x3f')]&&_0x46ab89[_0x3f8f('0x3f')][_0x3f8f('0x40')]&&_0x46ab89['idField']&&_0x46ab89['customField']){_0x16f8cc[_0x46ab89[_0x3f8f('0x3f')][_0x3f8f('0x40')][_0x3f8f('0x38')]()]&&_0x2d5da5[_0x3f8f('0x18')][_0x3f8f('0x41')]({'id':_0x46ab89['idField'],'value':_0x16f8cc[_0x46ab89['Variable'][_0x3f8f('0x40')][_0x3f8f('0x38')]()][_0x3f8f('0x3b')]()});}});}return _0x2d5da5;}exports[_0x3f8f('0x42')]=function(_0x25c89f,_0x107518,_0x3eb467,_0x526450,_0x4634b2,_0x2205c7){var _0x27bdb6,_0x178bb5;_0x2205c7[_0x3f8f('0x1a')]=intUtil[_0x3f8f('0x43')](_0x2205c7[_0x3f8f('0x1a')]);_0x2205c7['serverUrl']=intUtil[_0x3f8f('0x43')](_0x2205c7[_0x3f8f('0x44')]);if(_0x4634b2&&_0x4634b2[_0x3f8f('0x45')]){logger[_0x3f8f('0x1b')]('[QUEUE]',_0x3f8f('0x46'));_0x107518['recordingURL']=util['format']('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x2205c7[_0x3f8f('0x44')],_0x107518[_0x3f8f('0x47')],md5(_0x107518[_0x3f8f('0x47')]));}else{_0x107518['recordingURL']='';}headers[_0x3f8f('0x48')]=getAuth(_0x2205c7);return getUser(_0x2205c7,_0x526450['email'])[_0x3f8f('0x24')](function(_0xc7ed3a){_0x27bdb6=_0xc7ed3a;logger[_0x3f8f('0x1b')](_0x3f8f('0x49'),_0x27bdb6['id']);return getEndUser(_0x2205c7,_0x107518['calleridname'],_0x107518['calleridnum']);})[_0x3f8f('0x24')](function(_0x260208){_0x178bb5=_0x260208;logger[_0x3f8f('0x1b')](_0x3f8f('0x4a'),_0x178bb5['id']);return getTicket(_0x2205c7,_0x27bdb6,_0x178bb5,_0x3eb467,_0x107518);})[_0x3f8f('0x24')](function(_0x362ef4){if(_0x362ef4&&_0x362ef4['id']){logger[_0x3f8f('0x1b')](_0x3f8f('0x4b'));logger['info'](_0x3f8f('0x4c'),_0x362ef4['id']);logger[_0x3f8f('0x1b')](_0x3f8f('0x4d'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util['format']('user:%s',_0x526450[_0x3f8f('0x40')]),'trigger:browser:url',{'uri':util[_0x3f8f('0x10')](_0x3f8f('0x4e'),_0x2205c7[_0x3f8f('0x1a')],_0x362ef4['id'])});}})[_0x3f8f('0x4f')](function(_0x38719b){logger[_0x3f8f('0x50')]('[QUEUE]',JSON[_0x3f8f('0x2e')](_0x38719b));});};exports['outbound']=function(_0xaeecbb,_0x46f5a9,_0x46870b,_0x20dbb8,_0x5d189a,_0x5b4293,_0x4467a9){var _0x1aeaf8,_0x2cbf93;_0x5b4293[_0x3f8f('0x1a')]=intUtil[_0x3f8f('0x43')](_0x5b4293[_0x3f8f('0x1a')]);_0x5b4293['serverUrl']=intUtil[_0x3f8f('0x43')](_0x5b4293[_0x3f8f('0x44')]);if(_0x4467a9&&_0x4467a9['recordingFormat']&&_0x4467a9[_0x3f8f('0x51')]!==_0x3f8f('0x52')){logger[_0x3f8f('0x1b')]('[OUTBOUND]',_0x3f8f('0x46'));_0x46f5a9['recordingURL']=util[_0x3f8f('0x10')](_0x3f8f('0x53'),_0x5b4293[_0x3f8f('0x44')],_0x46f5a9[_0x3f8f('0x47')],md5(_0x46f5a9[_0x3f8f('0x47')]));}headers[_0x3f8f('0x48')]=getAuth(_0x5b4293);return getUser(_0x5b4293,_0x20dbb8[_0x3f8f('0x20')])[_0x3f8f('0x24')](function(_0x460fda){_0x1aeaf8=_0x460fda;logger[_0x3f8f('0x1b')](_0x3f8f('0x49'),_0x1aeaf8['id']);return getEndUser(_0x5b4293,_0x46f5a9['destcalleridname'],_0x46f5a9[_0x3f8f('0x54')]);})[_0x3f8f('0x24')](function(_0x432999){_0x2cbf93=_0x432999;logger[_0x3f8f('0x1b')](_0x3f8f('0x4a'),_0x2cbf93['id']);return getTicket(_0x5b4293,_0x1aeaf8,_0x2cbf93,_0x46870b,_0x46f5a9);})[_0x3f8f('0x24')](function(_0x1eb6f9){if(_0x1eb6f9&&_0x1eb6f9['id']){logger['info'](_0x3f8f('0x4b'));logger['info'](_0x3f8f('0x4c'),_0x1eb6f9['id']);logger[_0x3f8f('0x1b')](_0x3f8f('0x55'),_0x3f8f('0x56'));emit(util[_0x3f8f('0x10')]('user:%s',_0x20dbb8[_0x3f8f('0x40')]),'trigger:browser:url',{'uri':util[_0x3f8f('0x10')](_0x3f8f('0x4e'),_0x5b4293[_0x3f8f('0x1a')],_0x1eb6f9['id'])});}})[_0x3f8f('0x4f')](function(_0x1aff67){logger[_0x3f8f('0x50')](_0x3f8f('0x55'),JSON[_0x3f8f('0x2e')](_0x1aff67));});};exports['unmanaged']=function(_0x5813d2,_0x31b52f,_0x207f22,_0x383dd0,_0x387b58,_0x403898){var _0x354385,_0x233d74;_0x403898[_0x3f8f('0x1a')]=intUtil[_0x3f8f('0x43')](_0x403898['remoteUri']);_0x403898['serverUrl']=intUtil[_0x3f8f('0x43')](_0x403898['serverUrl']);if(_0x387b58&&_0x387b58[_0x3f8f('0x45')]){logger[_0x3f8f('0x1b')]('['+_0x31b52f['lastevent'][_0x3f8f('0x57')]()+']',_0x3f8f('0x46'));_0x31b52f[_0x3f8f('0x58')]=util[_0x3f8f('0x10')](_0x3f8f('0x53'),_0x403898[_0x3f8f('0x44')],_0x31b52f['uniqueid'],md5(_0x31b52f[_0x3f8f('0x47')]));}else{_0x31b52f[_0x3f8f('0x58')]='';}headers[_0x3f8f('0x48')]=getAuth(_0x403898);return getUser(_0x403898,'')[_0x3f8f('0x24')](function(_0x1da657){_0x354385=_0x1da657;logger[_0x3f8f('0x1b')]('The\x20User\x20ID\x20is:',_0x354385['id']);return getEndUser(_0x403898,_0x31b52f[_0x3f8f('0x59')],_0x31b52f[_0x3f8f('0x12')]);})[_0x3f8f('0x24')](function(_0x3ed6a0){_0x233d74=_0x3ed6a0;logger[_0x3f8f('0x1b')](_0x3f8f('0x4a'),_0x233d74['id']);return getTicket(_0x403898,_0x354385,_0x233d74,_0x207f22,_0x31b52f);})[_0x3f8f('0x24')](function(_0x1297c5){if(_0x1297c5){logger[_0x3f8f('0x1b')](_0x3f8f('0x4b'));logger['info'](_0x3f8f('0x4c'),_0x1297c5['id']);}})[_0x3f8f('0x4f')](function(_0x377122){logger[_0x3f8f('0x50')]('['+_0x31b52f['lastevent'][_0x3f8f('0x57')]()+']',JSON[_0x3f8f('0x2e')](_0x377122));});};