Built motion from commit (unavailable).|2.5.6
[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 _0xf713=['stripTrailingSlash','serverUrl','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','uniqueid','Authorization','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','catch','error','outbound','recordingFormat','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','email','The\x20User\x20ID\x20is:','destcalleridname','trigger:browser:url','%s/helpdesk/tickets/%d','[OUTBOUND]','unmanaged','lastevent','toUpperCase','lodash','request-promise','md5','util','ioredis','../../../../config/environment','freshdesk','redis','defaults','localhost','socket.io-emitter','application/json','emit','info','Creating\x20new\x20ticket...','merge','format','calleridname','Subjects','getString','Descriptions','Tags','name','Fields','keyBy','custom_fields','value','%s/api/v2/tickets','remoteUri','POST','then','Ticket\x20created!','validateEmail','%s/api/v2/agents','length','contact','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','%s/api/v2/agents/me','Admin\x20account\x20selected.','Searching\x20enduser\x20with\x20phone','%s/api/v2/contacts','Enduser\x20found!','find','Enduser\x20created.','Basic\x20%s','apiKey','toString','debug','[HTTP]','stringify','type','customField','toLowerCase','string','idField','push','content','variable','variableName','customVariable','Variable'];(function(_0x50cd41,_0x4874a7){var _0x399475=function(_0x1946da){while(--_0x1946da){_0x50cd41['push'](_0x50cd41['shift']());}};_0x399475(++_0x4874a7);}(_0xf713,0x122));var _0x3f71=function(_0x2118ad,_0xe8e3fd){_0x2118ad=_0x2118ad-0x0;var _0x4b5715=_0xf713[_0x2118ad];return _0x4b5715;};'use strict';var _=require(_0x3f71('0x0'));var rp=require(_0x3f71('0x1'));var md5=require(_0x3f71('0x2'));var util=require(_0x3f71('0x3'));var Redis=require(_0x3f71('0x4'));var intUtil=require('./util');var config=require(_0x3f71('0x5'));var logger=require('../../../../config/logger')(_0x3f71('0x6'));config[_0x3f71('0x7')]=_[_0x3f71('0x8')](config[_0x3f71('0x7')],{'host':_0x3f71('0x9'),'port':0x18eb});var io=require(_0x3f71('0xa'))(new Redis(config['redis']));var headers={'Content-Type':'application/json','Accept':_0x3f71('0xb')};function emit(_0x1d427c,_0xaf18d1,_0x451d03){io['to'](_0x1d427c)[_0x3f71('0xc')](_0xaf18d1,_0x451d03);}function getTicket(_0xca1003,_0x57f956,_0x11f4fd,_0x21a3e4,_0xf865f5){logger[_0x3f71('0xd')](_0x3f71('0xe'));var _0x419c33=_[_0x3f71('0xf')]({'responder_id':_0x57f956['id'],'requester_id':_0x11f4fd['id'],'name':util[_0x3f71('0x10')]('Caller\x20-\x20%s',_0xf865f5[_0x3f71('0x11')]),'phone':_0xf865f5['calleridnum'],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil['getString'](_0x21a3e4[_0x3f71('0x12')],_0xf865f5,'\x20'),'description':intUtil[_0x3f71('0x13')](_0x21a3e4[_0x3f71('0x14')],_0xf865f5,'<br>'),'tags':getTags(_0x21a3e4[_0x3f71('0x15')],_0x3f71('0x16'))},getCustomFields(_0x21a3e4[_0x3f71('0x17')],_0xf865f5));_0x419c33['custom_fields']=_['mapValues'](_[_0x3f71('0x18')](_0x419c33[_0x3f71('0x19')],'id'),_0x3f71('0x1a'));return request({'uri':util[_0x3f71('0x10')](_0x3f71('0x1b'),_0xca1003[_0x3f71('0x1c')]),'method':_0x3f71('0x1d'),'body':_0x419c33})[_0x3f71('0x1e')](function(_0x23a45b){logger[_0x3f71('0xd')](_0x3f71('0x1f'));return _0x23a45b;});}function getUser(_0x7b6cf2,_0x1d9219){if(intUtil[_0x3f71('0x20')](_0x1d9219)){return request({'uri':util[_0x3f71('0x10')](_0x3f71('0x21'),_0x7b6cf2['remoteUri']),'qs':{'email':_0x1d9219}})['then'](function(_0xb98ed1){if(_0xb98ed1[_0x3f71('0x22')]){logger[_0x3f71('0xd')]('Agent\x20found!');return _['find'](_0xb98ed1,function(_0x1c5c47){return _0x1c5c47[_0x3f71('0x23')]['email']===_0x1d9219;});}else{logger[_0x3f71('0xd')](_0x3f71('0x24'));return request({'uri':util[_0x3f71('0x10')]('%s/api/v2/agents/me',_0x7b6cf2[_0x3f71('0x1c')])})[_0x3f71('0x1e')](function(_0x223170){logger[_0x3f71('0xd')]('Admin\x20account\x20selected.');return _0x223170;});}});}else{logger['info'](_0x3f71('0x25'));return request({'uri':util[_0x3f71('0x10')](_0x3f71('0x26'),_0x7b6cf2[_0x3f71('0x1c')])})[_0x3f71('0x1e')](function(_0x2dd860){logger[_0x3f71('0xd')](_0x3f71('0x27'));return _0x2dd860;});}}function getEndUser(_0x46fb02,_0x307239,_0x361aec){logger[_0x3f71('0xd')](_0x3f71('0x28'),_0x361aec);return request({'uri':util['format'](_0x3f71('0x29'),_0x46fb02[_0x3f71('0x1c')]),'qs':{'phone':_0x361aec}})[_0x3f71('0x1e')](function(_0x1e24d4){if(_0x1e24d4['length']){logger[_0x3f71('0xd')](_0x3f71('0x2a'));return _[_0x3f71('0x2b')](_0x1e24d4,{'phone':_0x361aec});}else{logger['info']('Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!');return request({'uri':util[_0x3f71('0x10')](_0x3f71('0x29'),_0x46fb02['remoteUri']),'method':_0x3f71('0x1d'),'body':{'name':_0x307239,'phone':_0x361aec}})[_0x3f71('0x1e')](function(_0x3c13b2){logger['info'](_0x3f71('0x2c'));return _0x3c13b2;});}});}function getAuth(_0x1e5087){return util[_0x3f71('0x10')](_0x3f71('0x2d'),new Buffer(_0x1e5087[_0x3f71('0x2e')]+':X')[_0x3f71('0x2f')]('base64'));}function request(_0x370440){logger[_0x3f71('0x30')](_0x3f71('0x31'),JSON[_0x3f71('0x32')](_0x370440));return rp(_[_0x3f71('0x8')](_0x370440,{'method':'GET','headers':headers,'json':!![]}));}function getTags(_0x18f81f,_0xa7f651){var _0x37a360=[];if(_0x18f81f&&_0x18f81f[_0x3f71('0x22')]>0x0){_0x37a360=_['map'](_0x18f81f,_0xa7f651);}return _0x37a360;}function getCustomFields(_0x227106,_0x3fa635){var _0x334a6f=[];var _0x36b125={'custom_fields':[]};if(_0x227106[_0x3f71('0x22')]>0x0){_['forEach'](_0x227106,function(_0x5f3364){if(_0x5f3364[_0x3f71('0x33')]==='string'&&_0x5f3364['nameField']&&!_0x5f3364[_0x3f71('0x34')]){_0x36b125[_0x5f3364['nameField'][_0x3f71('0x35')]()]=_0x5f3364['content']['toString']();}else if(_0x5f3364[_0x3f71('0x33')]===_0x3f71('0x36')&&_0x5f3364[_0x3f71('0x37')]&&_0x5f3364[_0x3f71('0x34')]){_0x36b125[_0x3f71('0x19')][_0x3f71('0x38')]({'id':_0x5f3364['idField'],'value':_0x5f3364[_0x3f71('0x39')][_0x3f71('0x2f')]()});}else if(_0x5f3364[_0x3f71('0x33')]===_0x3f71('0x3a')&&_0x5f3364['variableName']&&_0x5f3364[_0x3f71('0x37')]&&_0x5f3364['customField']){_0x3fa635[_0x5f3364[_0x3f71('0x3b')]]&&_0x36b125[_0x3f71('0x19')]['push']({'id':_0x5f3364[_0x3f71('0x37')],'value':_0x3fa635[_0x5f3364['variableName']][_0x3f71('0x2f')]()});}else if(_0x5f3364['type']===_0x3f71('0x3c')&&_0x5f3364['Variable']&&_0x5f3364[_0x3f71('0x3d')]['name']&&_0x5f3364[_0x3f71('0x37')]&&_0x5f3364[_0x3f71('0x34')]){_0x3fa635[_0x5f3364['Variable'][_0x3f71('0x16')][_0x3f71('0x35')]()]&&_0x36b125['custom_fields'][_0x3f71('0x38')]({'id':_0x5f3364[_0x3f71('0x37')],'value':_0x3fa635[_0x5f3364[_0x3f71('0x3d')]['name'][_0x3f71('0x35')]()][_0x3f71('0x2f')]()});}});}return _0x36b125;}exports['queue']=function(_0x500add,_0x58bb58,_0x2422d1,_0x4b0881,_0x39aab9,_0x445f8f){var _0x2e33ed,_0x1b2487;_0x445f8f[_0x3f71('0x1c')]=intUtil[_0x3f71('0x3e')](_0x445f8f['remoteUri']);_0x445f8f['serverUrl']=intUtil['stripTrailingSlash'](_0x445f8f[_0x3f71('0x3f')]);if(_0x39aab9&&_0x39aab9[_0x3f71('0x40')]){logger['info'](_0x3f71('0x41'),_0x3f71('0x42'));_0x58bb58[_0x3f71('0x43')]=util[_0x3f71('0x10')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x445f8f[_0x3f71('0x3f')],_0x58bb58[_0x3f71('0x44')],md5(_0x58bb58['uniqueid']));}else{_0x58bb58[_0x3f71('0x43')]='';}headers[_0x3f71('0x45')]=getAuth(_0x445f8f);return getUser(_0x445f8f,_0x4b0881['email'])['then'](function(_0x304199){_0x2e33ed=_0x304199;logger['info']('The\x20User\x20ID\x20is:',_0x2e33ed['id']);return getEndUser(_0x445f8f,_0x58bb58[_0x3f71('0x11')],_0x58bb58['calleridnum']);})[_0x3f71('0x1e')](function(_0x78a09a){_0x1b2487=_0x78a09a;logger[_0x3f71('0xd')](_0x3f71('0x46'),_0x1b2487['id']);return getTicket(_0x445f8f,_0x2e33ed,_0x1b2487,_0x2422d1,_0x58bb58);})[_0x3f71('0x1e')](function(_0x227076){if(_0x227076&&_0x227076['id']){logger[_0x3f71('0xd')]('Ticket\x20created!');logger[_0x3f71('0xd')](_0x3f71('0x47'),_0x227076['id']);logger['info'](_0x3f71('0x41'),_0x3f71('0x48'));emit(util[_0x3f71('0x10')](_0x3f71('0x49'),_0x4b0881[_0x3f71('0x16')]),'trigger:browser:url',{'uri':util['format']('%s/helpdesk/tickets/%d',_0x445f8f[_0x3f71('0x1c')],_0x227076['id'])});}})[_0x3f71('0x4a')](function(_0x3157c3){logger[_0x3f71('0x4b')](_0x3f71('0x41'),JSON[_0x3f71('0x32')](_0x3157c3));});};exports[_0x3f71('0x4c')]=function(_0x3b574e,_0x5b79a0,_0x7f09ae,_0x254af6,_0x24f645,_0xb48939,_0x311814){var _0x4d7a82,_0x25bc5a;_0xb48939[_0x3f71('0x1c')]=intUtil[_0x3f71('0x3e')](_0xb48939[_0x3f71('0x1c')]);_0xb48939['serverUrl']=intUtil['stripTrailingSlash'](_0xb48939[_0x3f71('0x3f')]);if(_0x311814&&_0x311814[_0x3f71('0x4d')]&&_0x311814[_0x3f71('0x4d')]!=='none'){logger[_0x3f71('0xd')]('[OUTBOUND]','Recording\x20is\x20enabled!');_0x5b79a0[_0x3f71('0x43')]=util[_0x3f71('0x10')](_0x3f71('0x4e'),_0xb48939[_0x3f71('0x3f')],_0x5b79a0[_0x3f71('0x44')],md5(_0x5b79a0[_0x3f71('0x44')]));}headers['Authorization']=getAuth(_0xb48939);return getUser(_0xb48939,_0x254af6[_0x3f71('0x4f')])['then'](function(_0x2a9f53){_0x4d7a82=_0x2a9f53;logger[_0x3f71('0xd')](_0x3f71('0x50'),_0x4d7a82['id']);return getEndUser(_0xb48939,_0x5b79a0[_0x3f71('0x51')],_0x5b79a0['destcalleridnum']);})['then'](function(_0x3fa75d){_0x25bc5a=_0x3fa75d;logger['info'](_0x3f71('0x46'),_0x25bc5a['id']);return getTicket(_0xb48939,_0x4d7a82,_0x25bc5a,_0x7f09ae,_0x5b79a0);})[_0x3f71('0x1e')](function(_0x48e066){if(_0x48e066&&_0x48e066['id']){logger[_0x3f71('0xd')](_0x3f71('0x1f'));logger[_0x3f71('0xd')](_0x3f71('0x47'),_0x48e066['id']);logger[_0x3f71('0xd')]('[OUTBOUND]',_0x3f71('0x48'));emit(util[_0x3f71('0x10')]('user:%s',_0x254af6[_0x3f71('0x16')]),_0x3f71('0x52'),{'uri':util['format'](_0x3f71('0x53'),_0xb48939['remoteUri'],_0x48e066['id'])});}})['catch'](function(_0x5a8d04){logger[_0x3f71('0x4b')](_0x3f71('0x54'),JSON[_0x3f71('0x32')](_0x5a8d04));});};exports[_0x3f71('0x55')]=function(_0x3038ee,_0x2c42cf,_0x1dc84a,_0x3f77e6,_0x270aa1,_0x33f00c){var _0x338b18,_0x238f7e;_0x33f00c[_0x3f71('0x1c')]=intUtil[_0x3f71('0x3e')](_0x33f00c[_0x3f71('0x1c')]);_0x33f00c['serverUrl']=intUtil[_0x3f71('0x3e')](_0x33f00c['serverUrl']);if(_0x270aa1&&_0x270aa1['monitor_format']){logger['info']('['+_0x2c42cf[_0x3f71('0x56')][_0x3f71('0x57')]()+']',_0x3f71('0x42'));_0x2c42cf[_0x3f71('0x43')]=util[_0x3f71('0x10')](_0x3f71('0x4e'),_0x33f00c[_0x3f71('0x3f')],_0x2c42cf[_0x3f71('0x44')],md5(_0x2c42cf[_0x3f71('0x44')]));}else{_0x2c42cf[_0x3f71('0x43')]='';}headers[_0x3f71('0x45')]=getAuth(_0x33f00c);return getUser(_0x33f00c,'')['then'](function(_0x1ac88){_0x338b18=_0x1ac88;logger[_0x3f71('0xd')]('The\x20User\x20ID\x20is:',_0x338b18['id']);return getEndUser(_0x33f00c,_0x2c42cf[_0x3f71('0x11')],_0x2c42cf['calleridnum']);})[_0x3f71('0x1e')](function(_0x17e8e0){_0x238f7e=_0x17e8e0;logger[_0x3f71('0xd')](_0x3f71('0x46'),_0x238f7e['id']);return getTicket(_0x33f00c,_0x338b18,_0x238f7e,_0x1dc84a,_0x2c42cf);})[_0x3f71('0x1e')](function(_0xa99390){if(_0xa99390){logger[_0x3f71('0xd')](_0x3f71('0x1f'));logger[_0x3f71('0xd')]('The\x20Ticket\x20ID\x20is:',_0xa99390['id']);}})[_0x3f71('0x4a')](function(_0x453bea){logger[_0x3f71('0x4b')]('['+_0x2c42cf['lastevent']['toUpperCase']()+']',JSON[_0x3f71('0x32')](_0x453bea));});};