Built motion from commit 497d3d92.|2.5.43
[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 _0x3214=['../../../../config/logger','freshdesk','redis','defaults','socket.io-emitter','application/json','emit','info','Creating\x20new\x20ticket...','merge','format','calleridname','calleridnum','Subjects','getString','<br>','Tags','mapValues','keyBy','custom_fields','%s/api/v2/tickets','remoteUri','POST','Ticket\x20created!','validateEmail','%s/api/v2/agents','then','length','Agent\x20found!','find','email','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/api/v2/agents/me','Admin\x20account\x20selected.','Searching\x20enduser\x20with\x20phone','%s/api/v2/contacts','Enduser\x20found!','Enduser\x20created.','Basic\x20%s','apiKey','base64','debug','[HTTP]','stringify','map','forEach','includes','string','picklist','nameField','customField','content','toString','type','idField','push','variable','variableName','customVariable','Variable','name','toLowerCase','queue','serverUrl','stripTrailingSlash','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','recordingURL','Authorization','The\x20User\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','%s/helpdesk/tickets/%d','catch','error','outbound','recordingFormat','[OUTBOUND]','destcalleridnum','The\x20Enduser\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','trigger:browser:url','unmanaged','lastevent','md5','util','ioredis'];(function(_0x17de2f,_0x3c5f03){var _0x45f1ac=function(_0x1f5800){while(--_0x1f5800){_0x17de2f['push'](_0x17de2f['shift']());}};_0x45f1ac(++_0x3c5f03);}(_0x3214,0x108));var _0x4321=function(_0x5dda44,_0x40b720){_0x5dda44=_0x5dda44-0x0;var _0x25506a=_0x3214[_0x5dda44];return _0x25506a;};'use strict';var _=require('lodash');var rp=require('request-promise');var md5=require(_0x4321('0x0'));var util=require(_0x4321('0x1'));var Redis=require(_0x4321('0x2'));var intUtil=require('./util');var config=require('../../../../config/environment');var logger=require(_0x4321('0x3'))(_0x4321('0x4'));config[_0x4321('0x5')]=_[_0x4321('0x6')](config[_0x4321('0x5')],{'host':'localhost','port':0x18eb});var io=require(_0x4321('0x7'))(new Redis(config[_0x4321('0x5')]));var headers={'Content-Type':_0x4321('0x8'),'Accept':_0x4321('0x8')};function emit(_0xfb0374,_0xeafbc3,_0x111cf6){io['to'](_0xfb0374)[_0x4321('0x9')](_0xeafbc3,_0x111cf6);}function getTicket(_0x94d95d,_0x20a6ec,_0x35d2d2,_0x390f02,_0x398d78){logger[_0x4321('0xa')](_0x4321('0xb'));var _0x15aa1a=_[_0x4321('0xc')]({'responder_id':_0x20a6ec['id'],'requester_id':_0x35d2d2['id'],'name':util[_0x4321('0xd')]('Caller\x20-\x20%s',_0x398d78[_0x4321('0xe')]),'phone':_0x398d78[_0x4321('0xf')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil['getString'](_0x390f02[_0x4321('0x10')],_0x398d78,'\x20'),'description':intUtil[_0x4321('0x11')](_0x390f02['Descriptions'],_0x398d78,_0x4321('0x12')),'tags':getTags(_0x390f02[_0x4321('0x13')],'name')},getCustomFields(_0x390f02['Fields'],_0x398d78));_0x15aa1a['custom_fields']=_[_0x4321('0x14')](_[_0x4321('0x15')](_0x15aa1a[_0x4321('0x16')],'id'),'value');return request({'uri':util[_0x4321('0xd')](_0x4321('0x17'),_0x94d95d[_0x4321('0x18')]),'method':_0x4321('0x19'),'body':_0x15aa1a})['then'](function(_0x476330){logger[_0x4321('0xa')](_0x4321('0x1a'));return _0x476330;});}function getUser(_0xe79d50,_0x21a81f){if(intUtil[_0x4321('0x1b')](_0x21a81f)){return request({'uri':util['format'](_0x4321('0x1c'),_0xe79d50[_0x4321('0x18')]),'qs':{'email':_0x21a81f}})[_0x4321('0x1d')](function(_0x4968ee){if(_0x4968ee[_0x4321('0x1e')]){logger[_0x4321('0xa')](_0x4321('0x1f'));return _[_0x4321('0x20')](_0x4968ee,function(_0x61e869){return _0x61e869['contact'][_0x4321('0x21')]===_0x21a81f;});}else{logger[_0x4321('0xa')](_0x4321('0x22'));return request({'uri':util[_0x4321('0xd')](_0x4321('0x23'),_0xe79d50[_0x4321('0x18')])})['then'](function(_0x305a5a){logger['info'](_0x4321('0x24'));return _0x305a5a;});}});}else{logger['info']('Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!');return request({'uri':util[_0x4321('0xd')](_0x4321('0x23'),_0xe79d50[_0x4321('0x18')])})[_0x4321('0x1d')](function(_0x4195c2){logger[_0x4321('0xa')](_0x4321('0x24'));return _0x4195c2;});}}function getEndUser(_0x3a2790,_0x5dbd7c,_0x4153be){logger[_0x4321('0xa')](_0x4321('0x25'),_0x4153be);return request({'uri':util[_0x4321('0xd')](_0x4321('0x26'),_0x3a2790[_0x4321('0x18')]),'qs':{'phone':_0x4153be}})[_0x4321('0x1d')](function(_0x504a39){if(_0x504a39[_0x4321('0x1e')]){logger[_0x4321('0xa')](_0x4321('0x27'));return _[_0x4321('0x20')](_0x504a39,{'phone':_0x4153be});}else{logger[_0x4321('0xa')]('Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!');return request({'uri':util['format'](_0x4321('0x26'),_0x3a2790[_0x4321('0x18')]),'method':_0x4321('0x19'),'body':{'name':_0x5dbd7c,'phone':_0x4153be}})['then'](function(_0x421e7e){logger[_0x4321('0xa')](_0x4321('0x28'));return _0x421e7e;});}});}function getAuth(_0x5d33c9){return util[_0x4321('0xd')](_0x4321('0x29'),new Buffer(_0x5d33c9[_0x4321('0x2a')]+':X')['toString'](_0x4321('0x2b')));}function request(_0x45ffba){logger[_0x4321('0x2c')](_0x4321('0x2d'),JSON[_0x4321('0x2e')](_0x45ffba));return rp(_['defaults'](_0x45ffba,{'method':'GET','headers':headers,'json':!![]}));}function getTags(_0x269b9b,_0x130e6c){var _0x200f40=[];if(_0x269b9b&&_0x269b9b['length']>0x0){_0x200f40=_[_0x4321('0x2f')](_0x269b9b,_0x130e6c);}return _0x200f40;}function getCustomFields(_0x225098,_0xa2a272){var _0x11ab5b=[];var _0x360c00={'custom_fields':[]};if(_0x225098[_0x4321('0x1e')]>0x0){_[_0x4321('0x30')](_0x225098,function(_0x56decd){if(_[_0x4321('0x31')]([_0x4321('0x32'),_0x4321('0x33')],_0x56decd['type'])&&_0x56decd[_0x4321('0x34')]&&!_0x56decd[_0x4321('0x35')]){_0x360c00[_0x56decd['nameField']['toLowerCase']()]=_0x56decd[_0x4321('0x36')][_0x4321('0x37')]();}else if(_[_0x4321('0x31')](['string',_0x4321('0x33')],_0x56decd[_0x4321('0x38')])&&_0x56decd[_0x4321('0x39')]&&_0x56decd[_0x4321('0x35')]){_0x360c00[_0x4321('0x16')][_0x4321('0x3a')]({'id':_0x56decd[_0x4321('0x39')],'value':_0x56decd[_0x4321('0x36')][_0x4321('0x37')]()});}else if(_0x56decd['type']===_0x4321('0x3b')&&_0x56decd['variableName']&&_0x56decd['idField']&&_0x56decd['customField']){_0xa2a272[_0x56decd[_0x4321('0x3c')]]&&_0x360c00[_0x4321('0x16')][_0x4321('0x3a')]({'id':_0x56decd[_0x4321('0x39')],'value':_0xa2a272[_0x56decd[_0x4321('0x3c')]][_0x4321('0x37')]()});}else if(_0x56decd['type']===_0x4321('0x3d')&&_0x56decd[_0x4321('0x3e')]&&_0x56decd[_0x4321('0x3e')]['name']&&_0x56decd[_0x4321('0x39')]&&_0x56decd[_0x4321('0x35')]){_0xa2a272[_0x56decd[_0x4321('0x3e')][_0x4321('0x3f')]['toLowerCase']()]&&_0x360c00[_0x4321('0x16')]['push']({'id':_0x56decd[_0x4321('0x39')],'value':_0xa2a272[_0x56decd[_0x4321('0x3e')][_0x4321('0x3f')][_0x4321('0x40')]()][_0x4321('0x37')]()});}});}return _0x360c00;}exports[_0x4321('0x41')]=function(_0x13531f,_0x2b0df3,_0x5d459c,_0x464c06,_0x50c0ad,_0x2288ee){var _0x3b781e,_0x54d5f0;_0x2288ee[_0x4321('0x18')]=intUtil['stripTrailingSlash'](_0x2288ee[_0x4321('0x18')]);_0x2288ee[_0x4321('0x42')]=intUtil[_0x4321('0x43')](_0x2288ee[_0x4321('0x42')]);if(_0x50c0ad&&_0x50c0ad[_0x4321('0x44')]){logger[_0x4321('0xa')](_0x4321('0x45'),_0x4321('0x46'));_0x2b0df3['recordingURL']=util[_0x4321('0xd')](_0x4321('0x47'),_0x2288ee[_0x4321('0x42')],_0x2b0df3[_0x4321('0x48')],md5(_0x2b0df3[_0x4321('0x48')]));}else{_0x2b0df3[_0x4321('0x49')]='';}headers[_0x4321('0x4a')]=getAuth(_0x2288ee);return getUser(_0x2288ee,_0x464c06['email'])[_0x4321('0x1d')](function(_0x2849af){_0x3b781e=_0x2849af;logger[_0x4321('0xa')](_0x4321('0x4b'),_0x3b781e['id']);return getEndUser(_0x2288ee,_0x2b0df3[_0x4321('0xe')],_0x2b0df3[_0x4321('0xf')]);})['then'](function(_0x2c5a52){_0x54d5f0=_0x2c5a52;logger[_0x4321('0xa')]('The\x20Enduser\x20ID\x20is:',_0x54d5f0['id']);return getTicket(_0x2288ee,_0x3b781e,_0x54d5f0,_0x5d459c,_0x2b0df3);})[_0x4321('0x1d')](function(_0x50b2af){if(_0x50b2af&&_0x50b2af['id']){logger['info'](_0x4321('0x1a'));logger[_0x4321('0xa')](_0x4321('0x4c'),_0x50b2af['id']);logger[_0x4321('0xa')](_0x4321('0x45'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util['format']('user:%s',_0x464c06['name']),'trigger:browser:url',{'uri':util['format'](_0x4321('0x4d'),_0x2288ee['remoteUri'],_0x50b2af['id'])});}})[_0x4321('0x4e')](function(_0x3ad705){logger[_0x4321('0x4f')](_0x4321('0x45'),JSON[_0x4321('0x2e')](_0x3ad705));});};exports[_0x4321('0x50')]=function(_0x5c2cfa,_0x4052e3,_0x279980,_0x1dbc86,_0xc1182a,_0x3b76cd,_0x2c6885){var _0x51e0a7,_0x348e06;_0x3b76cd['remoteUri']=intUtil['stripTrailingSlash'](_0x3b76cd[_0x4321('0x18')]);_0x3b76cd[_0x4321('0x42')]=intUtil[_0x4321('0x43')](_0x3b76cd['serverUrl']);if(_0x2c6885&&_0x2c6885['recordingFormat']&&_0x2c6885[_0x4321('0x51')]!=='none'){logger[_0x4321('0xa')](_0x4321('0x52'),_0x4321('0x46'));_0x4052e3[_0x4321('0x49')]=util[_0x4321('0xd')](_0x4321('0x47'),_0x3b76cd[_0x4321('0x42')],_0x4052e3['uniqueid'],md5(_0x4052e3[_0x4321('0x48')]));}headers[_0x4321('0x4a')]=getAuth(_0x3b76cd);return getUser(_0x3b76cd,_0x1dbc86[_0x4321('0x21')])['then'](function(_0x2dcc92){_0x51e0a7=_0x2dcc92;logger[_0x4321('0xa')](_0x4321('0x4b'),_0x51e0a7['id']);return getEndUser(_0x3b76cd,_0x4052e3['destcalleridname'],_0x4052e3[_0x4321('0x53')]);})[_0x4321('0x1d')](function(_0x35c3be){_0x348e06=_0x35c3be;logger[_0x4321('0xa')](_0x4321('0x54'),_0x348e06['id']);return getTicket(_0x3b76cd,_0x51e0a7,_0x348e06,_0x279980,_0x4052e3);})[_0x4321('0x1d')](function(_0x2e6562){if(_0x2e6562&&_0x2e6562['id']){logger[_0x4321('0xa')](_0x4321('0x1a'));logger[_0x4321('0xa')]('The\x20Ticket\x20ID\x20is:',_0x2e6562['id']);logger[_0x4321('0xa')](_0x4321('0x52'),_0x4321('0x55'));emit(util[_0x4321('0xd')]('user:%s',_0x1dbc86['name']),_0x4321('0x56'),{'uri':util[_0x4321('0xd')](_0x4321('0x4d'),_0x3b76cd[_0x4321('0x18')],_0x2e6562['id'])});}})[_0x4321('0x4e')](function(_0x351174){logger['error'](_0x4321('0x52'),JSON['stringify'](_0x351174));});};exports[_0x4321('0x57')]=function(_0x1d9c9f,_0x13132c,_0x5ae0bf,_0x302d0b,_0x4f9298,_0x6d476){var _0x497d2d,_0x54f779;_0x6d476['remoteUri']=intUtil['stripTrailingSlash'](_0x6d476[_0x4321('0x18')]);_0x6d476['serverUrl']=intUtil[_0x4321('0x43')](_0x6d476[_0x4321('0x42')]);if(_0x4f9298&&_0x4f9298[_0x4321('0x44')]){logger[_0x4321('0xa')]('['+_0x13132c[_0x4321('0x58')]['toUpperCase']()+']','Recording\x20is\x20enabled!');_0x13132c[_0x4321('0x49')]=util['format'](_0x4321('0x47'),_0x6d476[_0x4321('0x42')],_0x13132c[_0x4321('0x48')],md5(_0x13132c['uniqueid']));}else{_0x13132c[_0x4321('0x49')]='';}headers['Authorization']=getAuth(_0x6d476);return getUser(_0x6d476,'')[_0x4321('0x1d')](function(_0x921b42){_0x497d2d=_0x921b42;logger[_0x4321('0xa')](_0x4321('0x4b'),_0x497d2d['id']);return getEndUser(_0x6d476,_0x13132c[_0x4321('0xe')],_0x13132c[_0x4321('0xf')]);})['then'](function(_0x5c7c6b){_0x54f779=_0x5c7c6b;logger[_0x4321('0xa')](_0x4321('0x54'),_0x54f779['id']);return getTicket(_0x6d476,_0x497d2d,_0x54f779,_0x5ae0bf,_0x13132c);})[_0x4321('0x1d')](function(_0x10cfd6){if(_0x10cfd6){logger['info']('Ticket\x20created!');logger[_0x4321('0xa')](_0x4321('0x4c'),_0x10cfd6['id']);}})[_0x4321('0x4e')](function(_0x562d75){logger['error']('['+_0x13132c['lastevent']['toUpperCase']()+']',JSON['stringify'](_0x562d75));});};