Built motion from commit da617fac.|2.6.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 _0xc882=['includes','string','picklist','type','nameField','customField','idField','push','variableName','Variable','name','toLowerCase','queue','stripTrailingSlash','serverUrl','monitor_format','Recording\x20is\x20enabled!','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]','user:%s','%s/helpdesk/tickets/%d','catch','error','stringify','recordingFormat','none','[OUTBOUND]','destcalleridnum','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','trigger:browser:url','unmanaged','lastevent','toUpperCase','request-promise','md5','ioredis','../../../../config/environment','../../../../config/logger','freshdesk','redis','defaults','localhost','socket.io-emitter','application/json','Creating\x20new\x20ticket...','merge','format','Caller\x20-\x20%s','calleridname','calleridnum','getString','Subjects','Tags','Fields','custom_fields','mapValues','keyBy','value','%s/api/v2/tickets','then','info','Ticket\x20created!','validateEmail','%s/api/v2/agents','remoteUri','Agent\x20found!','find','contact','email','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/api/v2/agents/me','Admin\x20account\x20selected.','%s/api/v2/contacts','length','Enduser\x20found!','Enduser\x20created.','Basic\x20%s','apiKey','toString','debug','GET','forEach'];(function(_0x2dd02a,_0x47b9a9){var _0x42eda4=function(_0xdc0de6){while(--_0xdc0de6){_0x2dd02a['push'](_0x2dd02a['shift']());}};_0x42eda4(++_0x47b9a9);}(_0xc882,0x187));var _0x2c88=function(_0x502c7d,_0x2f1285){_0x502c7d=_0x502c7d-0x0;var _0x30b7ed=_0xc882[_0x502c7d];return _0x30b7ed;};'use strict';var _=require('lodash');var rp=require(_0x2c88('0x0'));var md5=require(_0x2c88('0x1'));var util=require('util');var Redis=require(_0x2c88('0x2'));var intUtil=require('./util');var config=require(_0x2c88('0x3'));var logger=require(_0x2c88('0x4'))(_0x2c88('0x5'));config[_0x2c88('0x6')]=_[_0x2c88('0x7')](config[_0x2c88('0x6')],{'host':_0x2c88('0x8'),'port':0x18eb});var io=require(_0x2c88('0x9'))(new Redis(config[_0x2c88('0x6')]));var headers={'Content-Type':_0x2c88('0xa'),'Accept':_0x2c88('0xa')};function emit(_0x530db4,_0x59cb12,_0x5a6f5d){io['to'](_0x530db4)['emit'](_0x59cb12,_0x5a6f5d);}function getTicket(_0x2332fc,_0x1eb9b6,_0x9f992f,_0x2bd807,_0x2a877b){logger['info'](_0x2c88('0xb'));var _0x33ae80=_[_0x2c88('0xc')]({'responder_id':_0x1eb9b6['id'],'requester_id':_0x9f992f['id'],'name':util[_0x2c88('0xd')](_0x2c88('0xe'),_0x2a877b[_0x2c88('0xf')]),'phone':_0x2a877b[_0x2c88('0x10')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0x2c88('0x11')](_0x2bd807[_0x2c88('0x12')],_0x2a877b,'\x20'),'description':intUtil['getString'](_0x2bd807['Descriptions'],_0x2a877b,'<br>'),'tags':getTags(_0x2bd807[_0x2c88('0x13')],'name')},getCustomFields(_0x2bd807[_0x2c88('0x14')],_0x2a877b));_0x33ae80[_0x2c88('0x15')]=_[_0x2c88('0x16')](_[_0x2c88('0x17')](_0x33ae80[_0x2c88('0x15')],'id'),_0x2c88('0x18'));return request({'uri':util[_0x2c88('0xd')](_0x2c88('0x19'),_0x2332fc['remoteUri']),'method':'POST','body':_0x33ae80})[_0x2c88('0x1a')](function(_0x459b43){logger[_0x2c88('0x1b')](_0x2c88('0x1c'));return _0x459b43;});}function getUser(_0x55b1d8,_0x266410){if(intUtil[_0x2c88('0x1d')](_0x266410)){return request({'uri':util[_0x2c88('0xd')](_0x2c88('0x1e'),_0x55b1d8[_0x2c88('0x1f')]),'qs':{'email':_0x266410}})['then'](function(_0x1fe08c){if(_0x1fe08c['length']){logger['info'](_0x2c88('0x20'));return _[_0x2c88('0x21')](_0x1fe08c,function(_0x2fc842){return _0x2fc842[_0x2c88('0x22')][_0x2c88('0x23')]===_0x266410;});}else{logger[_0x2c88('0x1b')](_0x2c88('0x24'));return request({'uri':util['format'](_0x2c88('0x25'),_0x55b1d8[_0x2c88('0x1f')])})[_0x2c88('0x1a')](function(_0x117bf9){logger[_0x2c88('0x1b')](_0x2c88('0x26'));return _0x117bf9;});}});}else{logger['info']('Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!');return request({'uri':util[_0x2c88('0xd')](_0x2c88('0x25'),_0x55b1d8['remoteUri'])})[_0x2c88('0x1a')](function(_0x45a824){logger[_0x2c88('0x1b')](_0x2c88('0x26'));return _0x45a824;});}}function getEndUser(_0x413130,_0x5b5aa5,_0x2517db){logger[_0x2c88('0x1b')]('Searching\x20enduser\x20with\x20phone',_0x2517db);return request({'uri':util['format'](_0x2c88('0x27'),_0x413130[_0x2c88('0x1f')]),'qs':{'phone':_0x2517db}})[_0x2c88('0x1a')](function(_0x12a449){if(_0x12a449[_0x2c88('0x28')]){logger[_0x2c88('0x1b')](_0x2c88('0x29'));return _[_0x2c88('0x21')](_0x12a449,{'phone':_0x2517db});}else{logger[_0x2c88('0x1b')]('Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!');return request({'uri':util[_0x2c88('0xd')](_0x2c88('0x27'),_0x413130[_0x2c88('0x1f')]),'method':'POST','body':{'name':_0x5b5aa5,'phone':_0x2517db}})['then'](function(_0x51594d){logger[_0x2c88('0x1b')](_0x2c88('0x2a'));return _0x51594d;});}});}function getAuth(_0x2853c3){return util['format'](_0x2c88('0x2b'),new Buffer(_0x2853c3[_0x2c88('0x2c')]+':X')[_0x2c88('0x2d')]('base64'));}function request(_0x25350b){logger[_0x2c88('0x2e')]('[HTTP]',JSON['stringify'](_0x25350b));return rp(_['defaults'](_0x25350b,{'method':_0x2c88('0x2f'),'headers':headers,'json':!![]}));}function getTags(_0x51bab5,_0x2e07f7){var _0x3304c3=[];if(_0x51bab5&&_0x51bab5[_0x2c88('0x28')]>0x0){_0x3304c3=_['map'](_0x51bab5,_0x2e07f7);}return _0x3304c3;}function getCustomFields(_0x420f1a,_0x2ed002){var _0x5595a2=[];var _0x4d1a37={'custom_fields':[]};if(_0x420f1a[_0x2c88('0x28')]>0x0){_[_0x2c88('0x30')](_0x420f1a,function(_0x866138){if(_[_0x2c88('0x31')]([_0x2c88('0x32'),_0x2c88('0x33')],_0x866138[_0x2c88('0x34')])&&_0x866138[_0x2c88('0x35')]&&!_0x866138[_0x2c88('0x36')]){_0x4d1a37[_0x866138[_0x2c88('0x35')]['toLowerCase']()]=_0x866138['content'][_0x2c88('0x2d')]();}else if(_['includes']([_0x2c88('0x32'),'picklist'],_0x866138[_0x2c88('0x34')])&&_0x866138[_0x2c88('0x37')]&&_0x866138[_0x2c88('0x36')]){_0x4d1a37[_0x2c88('0x15')][_0x2c88('0x38')]({'id':_0x866138[_0x2c88('0x37')],'value':_0x866138['content'][_0x2c88('0x2d')]()});}else if(_0x866138[_0x2c88('0x34')]==='variable'&&_0x866138[_0x2c88('0x39')]&&_0x866138[_0x2c88('0x37')]&&_0x866138[_0x2c88('0x36')]){_0x2ed002[_0x866138[_0x2c88('0x39')]]&&_0x4d1a37[_0x2c88('0x15')]['push']({'id':_0x866138['idField'],'value':_0x2ed002[_0x866138[_0x2c88('0x39')]][_0x2c88('0x2d')]()});}else if(_0x866138[_0x2c88('0x34')]==='customVariable'&&_0x866138['Variable']&&_0x866138[_0x2c88('0x3a')][_0x2c88('0x3b')]&&_0x866138[_0x2c88('0x37')]&&_0x866138[_0x2c88('0x36')]){_0x2ed002[_0x866138[_0x2c88('0x3a')]['name'][_0x2c88('0x3c')]()]&&_0x4d1a37[_0x2c88('0x15')][_0x2c88('0x38')]({'id':_0x866138['idField'],'value':_0x2ed002[_0x866138[_0x2c88('0x3a')][_0x2c88('0x3b')][_0x2c88('0x3c')]()][_0x2c88('0x2d')]()});}});}return _0x4d1a37;}exports[_0x2c88('0x3d')]=function(_0x11ccc7,_0x214c72,_0x1c72c8,_0x4729e4,_0x3b1726,_0xf2595c){var _0x21ba51,_0x85a39e;_0xf2595c[_0x2c88('0x1f')]=intUtil[_0x2c88('0x3e')](_0xf2595c[_0x2c88('0x1f')]);_0xf2595c[_0x2c88('0x3f')]=intUtil[_0x2c88('0x3e')](_0xf2595c[_0x2c88('0x3f')]);if(_0x3b1726&&_0x3b1726[_0x2c88('0x40')]){logger['info']('[QUEUE]',_0x2c88('0x41'));_0x214c72[_0x2c88('0x42')]=util[_0x2c88('0xd')](_0x2c88('0x43'),_0xf2595c['serverUrl'],_0x214c72[_0x2c88('0x44')],md5(_0x214c72[_0x2c88('0x44')]));}else{_0x214c72[_0x2c88('0x42')]='';}headers[_0x2c88('0x45')]=getAuth(_0xf2595c);return getUser(_0xf2595c,_0x4729e4[_0x2c88('0x23')])['then'](function(_0x1e431f){_0x21ba51=_0x1e431f;logger['info'](_0x2c88('0x46'),_0x21ba51['id']);return getEndUser(_0xf2595c,_0x214c72[_0x2c88('0xf')],_0x214c72[_0x2c88('0x10')]);})['then'](function(_0x195423){_0x85a39e=_0x195423;logger[_0x2c88('0x1b')](_0x2c88('0x47'),_0x85a39e['id']);return getTicket(_0xf2595c,_0x21ba51,_0x85a39e,_0x1c72c8,_0x214c72);})['then'](function(_0x450415){if(_0x450415&&_0x450415['id']){logger['info'](_0x2c88('0x1c'));logger[_0x2c88('0x1b')](_0x2c88('0x48'),_0x450415['id']);logger[_0x2c88('0x1b')](_0x2c88('0x49'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0x2c88('0xd')](_0x2c88('0x4a'),_0x4729e4[_0x2c88('0x3b')]),'trigger:browser:url',{'uri':util[_0x2c88('0xd')](_0x2c88('0x4b'),_0xf2595c[_0x2c88('0x1f')],_0x450415['id'])});}})[_0x2c88('0x4c')](function(_0x188d90){logger[_0x2c88('0x4d')](_0x2c88('0x49'),JSON[_0x2c88('0x4e')](_0x188d90));});};exports['outbound']=function(_0x3923ee,_0x193a01,_0x5bedb9,_0x496352,_0x54c9de,_0x5400ff,_0xb5ff97){var _0x19b868,_0x287df4;_0x5400ff['remoteUri']=intUtil[_0x2c88('0x3e')](_0x5400ff[_0x2c88('0x1f')]);_0x5400ff['serverUrl']=intUtil['stripTrailingSlash'](_0x5400ff[_0x2c88('0x3f')]);if(_0xb5ff97&&_0xb5ff97[_0x2c88('0x4f')]&&_0xb5ff97[_0x2c88('0x4f')]!==_0x2c88('0x50')){logger[_0x2c88('0x1b')](_0x2c88('0x51'),_0x2c88('0x41'));_0x193a01[_0x2c88('0x42')]=util[_0x2c88('0xd')](_0x2c88('0x43'),_0x5400ff[_0x2c88('0x3f')],_0x193a01[_0x2c88('0x44')],md5(_0x193a01[_0x2c88('0x44')]));}headers[_0x2c88('0x45')]=getAuth(_0x5400ff);return getUser(_0x5400ff,_0x496352[_0x2c88('0x23')])[_0x2c88('0x1a')](function(_0x3c0a39){_0x19b868=_0x3c0a39;logger['info']('The\x20User\x20ID\x20is:',_0x19b868['id']);return getEndUser(_0x5400ff,_0x193a01['destcalleridname'],_0x193a01[_0x2c88('0x52')]);})[_0x2c88('0x1a')](function(_0x469bb9){_0x287df4=_0x469bb9;logger[_0x2c88('0x1b')](_0x2c88('0x47'),_0x287df4['id']);return getTicket(_0x5400ff,_0x19b868,_0x287df4,_0x5bedb9,_0x193a01);})['then'](function(_0x1cd748){if(_0x1cd748&&_0x1cd748['id']){logger[_0x2c88('0x1b')](_0x2c88('0x1c'));logger[_0x2c88('0x1b')]('The\x20Ticket\x20ID\x20is:',_0x1cd748['id']);logger[_0x2c88('0x1b')]('[OUTBOUND]',_0x2c88('0x53'));emit(util[_0x2c88('0xd')]('user:%s',_0x496352[_0x2c88('0x3b')]),_0x2c88('0x54'),{'uri':util['format'](_0x2c88('0x4b'),_0x5400ff[_0x2c88('0x1f')],_0x1cd748['id'])});}})[_0x2c88('0x4c')](function(_0x7b70dd){logger[_0x2c88('0x4d')](_0x2c88('0x51'),JSON[_0x2c88('0x4e')](_0x7b70dd));});};exports[_0x2c88('0x55')]=function(_0x128f86,_0x14478e,_0x442ec9,_0x2d51aa,_0xb0fa56,_0x2c6204){var _0x5038ff,_0x11a3a4;_0x2c6204['remoteUri']=intUtil['stripTrailingSlash'](_0x2c6204[_0x2c88('0x1f')]);_0x2c6204[_0x2c88('0x3f')]=intUtil[_0x2c88('0x3e')](_0x2c6204[_0x2c88('0x3f')]);if(_0xb0fa56&&_0xb0fa56['monitor_format']){logger['info']('['+_0x14478e[_0x2c88('0x56')][_0x2c88('0x57')]()+']',_0x2c88('0x41'));_0x14478e['recordingURL']=util[_0x2c88('0xd')](_0x2c88('0x43'),_0x2c6204['serverUrl'],_0x14478e['uniqueid'],md5(_0x14478e[_0x2c88('0x44')]));}else{_0x14478e['recordingURL']='';}headers[_0x2c88('0x45')]=getAuth(_0x2c6204);return getUser(_0x2c6204,'')[_0x2c88('0x1a')](function(_0x1f8120){_0x5038ff=_0x1f8120;logger['info']('The\x20User\x20ID\x20is:',_0x5038ff['id']);return getEndUser(_0x2c6204,_0x14478e[_0x2c88('0xf')],_0x14478e[_0x2c88('0x10')]);})[_0x2c88('0x1a')](function(_0x29274f){_0x11a3a4=_0x29274f;logger[_0x2c88('0x1b')](_0x2c88('0x47'),_0x11a3a4['id']);return getTicket(_0x2c6204,_0x5038ff,_0x11a3a4,_0x442ec9,_0x14478e);})[_0x2c88('0x1a')](function(_0x1a9324){if(_0x1a9324){logger[_0x2c88('0x1b')]('Ticket\x20created!');logger[_0x2c88('0x1b')](_0x2c88('0x48'),_0x1a9324['id']);}})[_0x2c88('0x4c')](function(_0x153c52){logger[_0x2c88('0x4d')]('['+_0x14478e['lastevent']['toUpperCase']()+']',JSON['stringify'](_0x153c52));});};