Built motion from commit 092f8824.|2.5.49
[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 _0xb295=['Admin\x20account\x20selected.','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','Searching\x20enduser\x20with\x20phone','%s/api/v2/contacts','remoteUri','Enduser\x20found!','find','Enduser\x20created.','Basic\x20%s','apiKey','toString','debug','[HTTP]','stringify','map','length','forEach','string','picklist','nameField','customField','toLowerCase','includes','type','idField','custom_fields','push','content','variableName','Variable','name','stripTrailingSlash','serverUrl','monitor_format','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','Authorization','The\x20User\x20ID\x20is:','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','[QUEUE]','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','%s/helpdesk/tickets/%d','catch','outbound','recordingFormat','none','[OUTBOUND]','destcalleridname','destcalleridnum','Ticket\x20created!','error','unmanaged','lastevent','toUpperCase','lodash','md5','util','ioredis','./util','freshdesk','redis','defaults','application/json','emit','info','Creating\x20new\x20ticket...','merge','Caller\x20-\x20%s','calleridname','calleridnum','getString','Subjects','Descriptions','<br>','Fields','mapValues','value','format','%s/api/v2/tickets','POST','then','validateEmail','%s/api/v2/agents','contact','email','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/api/v2/agents/me'];(function(_0x109e1d,_0x4474f0){var _0x549e93=function(_0x214d66){while(--_0x214d66){_0x109e1d['push'](_0x109e1d['shift']());}};_0x549e93(++_0x4474f0);}(_0xb295,0x147));var _0x5b29=function(_0x3e5086,_0x48f4a2){_0x3e5086=_0x3e5086-0x0;var _0xfeb102=_0xb295[_0x3e5086];return _0xfeb102;};'use strict';var _=require(_0x5b29('0x0'));var rp=require('request-promise');var md5=require(_0x5b29('0x1'));var util=require(_0x5b29('0x2'));var Redis=require(_0x5b29('0x3'));var intUtil=require(_0x5b29('0x4'));var config=require('../../../../config/environment');var logger=require('../../../../config/logger')(_0x5b29('0x5'));config[_0x5b29('0x6')]=_[_0x5b29('0x7')](config[_0x5b29('0x6')],{'host':'localhost','port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x5b29('0x6')]));var headers={'Content-Type':'application/json','Accept':_0x5b29('0x8')};function emit(_0x174bb4,_0x4f08b4,_0xdf75bd){io['to'](_0x174bb4)[_0x5b29('0x9')](_0x4f08b4,_0xdf75bd);}function getTicket(_0x51845a,_0x2ec23c,_0x25418b,_0x4dae41,_0x2ee933){logger[_0x5b29('0xa')](_0x5b29('0xb'));var _0x1d4ff8=_[_0x5b29('0xc')]({'responder_id':_0x2ec23c['id'],'requester_id':_0x25418b['id'],'name':util['format'](_0x5b29('0xd'),_0x2ee933[_0x5b29('0xe')]),'phone':_0x2ee933[_0x5b29('0xf')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0x5b29('0x10')](_0x4dae41[_0x5b29('0x11')],_0x2ee933,'\x20'),'description':intUtil[_0x5b29('0x10')](_0x4dae41[_0x5b29('0x12')],_0x2ee933,_0x5b29('0x13')),'tags':getTags(_0x4dae41['Tags'],'name')},getCustomFields(_0x4dae41[_0x5b29('0x14')],_0x2ee933));_0x1d4ff8['custom_fields']=_[_0x5b29('0x15')](_['keyBy'](_0x1d4ff8['custom_fields'],'id'),_0x5b29('0x16'));return request({'uri':util[_0x5b29('0x17')](_0x5b29('0x18'),_0x51845a['remoteUri']),'method':_0x5b29('0x19'),'body':_0x1d4ff8})[_0x5b29('0x1a')](function(_0x539c25){logger[_0x5b29('0xa')]('Ticket\x20created!');return _0x539c25;});}function getUser(_0x177f19,_0x5aaf12){if(intUtil[_0x5b29('0x1b')](_0x5aaf12)){return request({'uri':util['format'](_0x5b29('0x1c'),_0x177f19['remoteUri']),'qs':{'email':_0x5aaf12}})[_0x5b29('0x1a')](function(_0x3fb737){if(_0x3fb737['length']){logger['info']('Agent\x20found!');return _['find'](_0x3fb737,function(_0x484fac){return _0x484fac[_0x5b29('0x1d')][_0x5b29('0x1e')]===_0x5aaf12;});}else{logger['info'](_0x5b29('0x1f'));return request({'uri':util['format'](_0x5b29('0x20'),_0x177f19['remoteUri'])})[_0x5b29('0x1a')](function(_0x43e464){logger[_0x5b29('0xa')](_0x5b29('0x21'));return _0x43e464;});}});}else{logger[_0x5b29('0xa')](_0x5b29('0x22'));return request({'uri':util[_0x5b29('0x17')](_0x5b29('0x20'),_0x177f19['remoteUri'])})[_0x5b29('0x1a')](function(_0x3f893f){logger[_0x5b29('0xa')](_0x5b29('0x21'));return _0x3f893f;});}}function getEndUser(_0x114bb4,_0x29e78d,_0x58ece6){logger['info'](_0x5b29('0x23'),_0x58ece6);return request({'uri':util[_0x5b29('0x17')](_0x5b29('0x24'),_0x114bb4[_0x5b29('0x25')]),'qs':{'phone':_0x58ece6}})[_0x5b29('0x1a')](function(_0x3c4a74){if(_0x3c4a74['length']){logger[_0x5b29('0xa')](_0x5b29('0x26'));return _[_0x5b29('0x27')](_0x3c4a74,{'phone':_0x58ece6});}else{logger[_0x5b29('0xa')]('Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!');return request({'uri':util[_0x5b29('0x17')]('%s/api/v2/contacts',_0x114bb4[_0x5b29('0x25')]),'method':_0x5b29('0x19'),'body':{'name':_0x29e78d,'phone':_0x58ece6}})['then'](function(_0x3d4a6b){logger['info'](_0x5b29('0x28'));return _0x3d4a6b;});}});}function getAuth(_0x3eb797){return util[_0x5b29('0x17')](_0x5b29('0x29'),new Buffer(_0x3eb797[_0x5b29('0x2a')]+':X')[_0x5b29('0x2b')]('base64'));}function request(_0x327c89){logger[_0x5b29('0x2c')](_0x5b29('0x2d'),JSON[_0x5b29('0x2e')](_0x327c89));return rp(_['defaults'](_0x327c89,{'method':'GET','headers':headers,'json':!![]}));}function getTags(_0x167940,_0xc11cb9){var _0x4a80f4=[];if(_0x167940&&_0x167940['length']>0x0){_0x4a80f4=_[_0x5b29('0x2f')](_0x167940,_0xc11cb9);}return _0x4a80f4;}function getCustomFields(_0x3993de,_0x28e05d){var _0x30aad1=[];var _0x14f7be={'custom_fields':[]};if(_0x3993de[_0x5b29('0x30')]>0x0){_[_0x5b29('0x31')](_0x3993de,function(_0x3ba548){if(_['includes']([_0x5b29('0x32'),_0x5b29('0x33')],_0x3ba548['type'])&&_0x3ba548[_0x5b29('0x34')]&&!_0x3ba548[_0x5b29('0x35')]){_0x14f7be[_0x3ba548[_0x5b29('0x34')][_0x5b29('0x36')]()]=_0x3ba548['content'][_0x5b29('0x2b')]();}else if(_[_0x5b29('0x37')]([_0x5b29('0x32'),_0x5b29('0x33')],_0x3ba548[_0x5b29('0x38')])&&_0x3ba548[_0x5b29('0x39')]&&_0x3ba548['customField']){_0x14f7be[_0x5b29('0x3a')][_0x5b29('0x3b')]({'id':_0x3ba548[_0x5b29('0x39')],'value':_0x3ba548[_0x5b29('0x3c')][_0x5b29('0x2b')]()});}else if(_0x3ba548[_0x5b29('0x38')]==='variable'&&_0x3ba548[_0x5b29('0x3d')]&&_0x3ba548[_0x5b29('0x39')]&&_0x3ba548[_0x5b29('0x35')]){_0x28e05d[_0x3ba548[_0x5b29('0x3d')]]&&_0x14f7be['custom_fields'][_0x5b29('0x3b')]({'id':_0x3ba548['idField'],'value':_0x28e05d[_0x3ba548[_0x5b29('0x3d')]][_0x5b29('0x2b')]()});}else if(_0x3ba548[_0x5b29('0x38')]==='customVariable'&&_0x3ba548[_0x5b29('0x3e')]&&_0x3ba548['Variable']['name']&&_0x3ba548['idField']&&_0x3ba548['customField']){_0x28e05d[_0x3ba548[_0x5b29('0x3e')][_0x5b29('0x3f')][_0x5b29('0x36')]()]&&_0x14f7be['custom_fields']['push']({'id':_0x3ba548[_0x5b29('0x39')],'value':_0x28e05d[_0x3ba548['Variable'][_0x5b29('0x3f')]['toLowerCase']()][_0x5b29('0x2b')]()});}});}return _0x14f7be;}exports['queue']=function(_0x312a42,_0x57e554,_0x2d1b0d,_0x30f305,_0x4cb7b3,_0x4b9f85){var _0x43285b,_0x537fa6;_0x4b9f85['remoteUri']=intUtil[_0x5b29('0x40')](_0x4b9f85[_0x5b29('0x25')]);_0x4b9f85[_0x5b29('0x41')]=intUtil['stripTrailingSlash'](_0x4b9f85[_0x5b29('0x41')]);if(_0x4cb7b3&&_0x4cb7b3[_0x5b29('0x42')]){logger[_0x5b29('0xa')]('[QUEUE]','Recording\x20is\x20enabled!');_0x57e554[_0x5b29('0x43')]=util['format'](_0x5b29('0x44'),_0x4b9f85[_0x5b29('0x41')],_0x57e554['uniqueid'],md5(_0x57e554[_0x5b29('0x45')]));}else{_0x57e554[_0x5b29('0x43')]='';}headers[_0x5b29('0x46')]=getAuth(_0x4b9f85);return getUser(_0x4b9f85,_0x30f305[_0x5b29('0x1e')])[_0x5b29('0x1a')](function(_0x18c12f){_0x43285b=_0x18c12f;logger[_0x5b29('0xa')](_0x5b29('0x47'),_0x43285b['id']);return getEndUser(_0x4b9f85,_0x57e554['calleridname'],_0x57e554['calleridnum']);})[_0x5b29('0x1a')](function(_0x4ca19d){_0x537fa6=_0x4ca19d;logger[_0x5b29('0xa')](_0x5b29('0x48'),_0x537fa6['id']);return getTicket(_0x4b9f85,_0x43285b,_0x537fa6,_0x2d1b0d,_0x57e554);})['then'](function(_0x1ec70c){if(_0x1ec70c&&_0x1ec70c['id']){logger['info']('Ticket\x20created!');logger[_0x5b29('0xa')](_0x5b29('0x49'),_0x1ec70c['id']);logger[_0x5b29('0xa')](_0x5b29('0x4a'),_0x5b29('0x4b'));emit(util[_0x5b29('0x17')](_0x5b29('0x4c'),_0x30f305[_0x5b29('0x3f')]),'trigger:browser:url',{'uri':util['format'](_0x5b29('0x4d'),_0x4b9f85[_0x5b29('0x25')],_0x1ec70c['id'])});}})[_0x5b29('0x4e')](function(_0x29400c){logger['error'](_0x5b29('0x4a'),JSON['stringify'](_0x29400c));});};exports[_0x5b29('0x4f')]=function(_0x15660e,_0x3efd2f,_0x230d2a,_0x86e8cd,_0x499a5e,_0x1671b8,_0x199b10){var _0x34c927,_0x35a92c;_0x1671b8[_0x5b29('0x25')]=intUtil['stripTrailingSlash'](_0x1671b8[_0x5b29('0x25')]);_0x1671b8[_0x5b29('0x41')]=intUtil[_0x5b29('0x40')](_0x1671b8['serverUrl']);if(_0x199b10&&_0x199b10[_0x5b29('0x50')]&&_0x199b10[_0x5b29('0x50')]!==_0x5b29('0x51')){logger[_0x5b29('0xa')](_0x5b29('0x52'),'Recording\x20is\x20enabled!');_0x3efd2f[_0x5b29('0x43')]=util[_0x5b29('0x17')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x1671b8[_0x5b29('0x41')],_0x3efd2f['uniqueid'],md5(_0x3efd2f[_0x5b29('0x45')]));}headers[_0x5b29('0x46')]=getAuth(_0x1671b8);return getUser(_0x1671b8,_0x86e8cd[_0x5b29('0x1e')])[_0x5b29('0x1a')](function(_0x185998){_0x34c927=_0x185998;logger['info'](_0x5b29('0x47'),_0x34c927['id']);return getEndUser(_0x1671b8,_0x3efd2f[_0x5b29('0x53')],_0x3efd2f[_0x5b29('0x54')]);})[_0x5b29('0x1a')](function(_0x364a73){_0x35a92c=_0x364a73;logger['info']('The\x20Enduser\x20ID\x20is:',_0x35a92c['id']);return getTicket(_0x1671b8,_0x34c927,_0x35a92c,_0x230d2a,_0x3efd2f);})['then'](function(_0x47c775){if(_0x47c775&&_0x47c775['id']){logger[_0x5b29('0xa')](_0x5b29('0x55'));logger[_0x5b29('0xa')](_0x5b29('0x49'),_0x47c775['id']);logger['info']('[OUTBOUND]',_0x5b29('0x4b'));emit(util[_0x5b29('0x17')]('user:%s',_0x86e8cd[_0x5b29('0x3f')]),'trigger:browser:url',{'uri':util[_0x5b29('0x17')](_0x5b29('0x4d'),_0x1671b8[_0x5b29('0x25')],_0x47c775['id'])});}})[_0x5b29('0x4e')](function(_0x391570){logger[_0x5b29('0x56')](_0x5b29('0x52'),JSON[_0x5b29('0x2e')](_0x391570));});};exports[_0x5b29('0x57')]=function(_0x7b6d1,_0x22b6e3,_0xc71522,_0x1fa06b,_0x28e794,_0x5d8827){var _0x3b9061,_0x2a4d9e;_0x5d8827[_0x5b29('0x25')]=intUtil[_0x5b29('0x40')](_0x5d8827[_0x5b29('0x25')]);_0x5d8827['serverUrl']=intUtil[_0x5b29('0x40')](_0x5d8827['serverUrl']);if(_0x28e794&&_0x28e794[_0x5b29('0x42')]){logger[_0x5b29('0xa')]('['+_0x22b6e3['lastevent']['toUpperCase']()+']','Recording\x20is\x20enabled!');_0x22b6e3[_0x5b29('0x43')]=util[_0x5b29('0x17')](_0x5b29('0x44'),_0x5d8827[_0x5b29('0x41')],_0x22b6e3[_0x5b29('0x45')],md5(_0x22b6e3[_0x5b29('0x45')]));}else{_0x22b6e3[_0x5b29('0x43')]='';}headers[_0x5b29('0x46')]=getAuth(_0x5d8827);return getUser(_0x5d8827,'')[_0x5b29('0x1a')](function(_0x2476fc){_0x3b9061=_0x2476fc;logger[_0x5b29('0xa')](_0x5b29('0x47'),_0x3b9061['id']);return getEndUser(_0x5d8827,_0x22b6e3[_0x5b29('0xe')],_0x22b6e3[_0x5b29('0xf')]);})[_0x5b29('0x1a')](function(_0x5473fd){_0x2a4d9e=_0x5473fd;logger['info'](_0x5b29('0x48'),_0x2a4d9e['id']);return getTicket(_0x5d8827,_0x3b9061,_0x2a4d9e,_0xc71522,_0x22b6e3);})[_0x5b29('0x1a')](function(_0x71c051){if(_0x71c051){logger[_0x5b29('0xa')]('Ticket\x20created!');logger[_0x5b29('0xa')]('The\x20Ticket\x20ID\x20is:',_0x71c051['id']);}})[_0x5b29('0x4e')](function(_0x16aa17){logger[_0x5b29('0x56')]('['+_0x22b6e3[_0x5b29('0x58')][_0x5b29('0x59')]()+']',JSON[_0x5b29('0x2e')](_0x16aa17));});};