Built motion from commit (unavailable).|2.4.4
[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 _0xceb8=['%s/api/v2/agents/me','Admin\x20account\x20selected.','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','%s/api/v2/contacts','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Enduser\x20created.','Basic\x20%s','apiKey','toString','base64','debug','[HTTP]','stringify','GET','forEach','string','customField','nameField','toLowerCase','content','type','idField','variableName','customVariable','Variable','name','push','queue','serverUrl','stripTrailingSlash','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','email','The\x20User\x20ID\x20is:','calleridnum','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','trigger:browser:url','%s/helpdesk/tickets/%d','error','recordingFormat','none','[OUTBOUND]','Authorization','destcalleridnum','catch','[ABANDONED]','request-promise','md5','util','ioredis','./util','../../../../config/logger','freshdesk','redis','defaults','localhost','application/json','emit','info','Creating\x20new\x20ticket...','format','Caller\x20-\x20%s','calleridname','getString','Subjects','<br>','Tags','Fields','custom_fields','mapValues','keyBy','value','%s/api/v2/tickets','POST','then','Ticket\x20created!','validateEmail','%s/api/v2/agents','remoteUri','length','Agent\x20found!','find','contact','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!'];(function(_0x13ac10,_0x46e79e){var _0x28eda2=function(_0x169ef0){while(--_0x169ef0){_0x13ac10['push'](_0x13ac10['shift']());}};_0x28eda2(++_0x46e79e);}(_0xceb8,0x92));var _0x8ceb=function(_0x14add3,_0x13ce8d){_0x14add3=_0x14add3-0x0;var _0x4a174f=_0xceb8[_0x14add3];return _0x4a174f;};'use strict';var _=require('lodash');var rp=require(_0x8ceb('0x0'));var md5=require(_0x8ceb('0x1'));var util=require(_0x8ceb('0x2'));var Redis=require(_0x8ceb('0x3'));var intUtil=require(_0x8ceb('0x4'));var config=require('../../../../config/environment');var logger=require(_0x8ceb('0x5'))(_0x8ceb('0x6'));config[_0x8ceb('0x7')]=_[_0x8ceb('0x8')](config['redis'],{'host':_0x8ceb('0x9'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x8ceb('0x7')]));var headers={'Content-Type':_0x8ceb('0xa'),'Accept':_0x8ceb('0xa')};function emit(_0xb46443,_0x2b467b,_0x2bc389){io['to'](_0xb46443)[_0x8ceb('0xb')](_0x2b467b,_0x2bc389);}function getTicket(_0x42b090,_0x398361,_0x38c5dc,_0x1c32d9,_0xd50c0f){logger[_0x8ceb('0xc')](_0x8ceb('0xd'));var _0x55ba44=_['merge']({'responder_id':_0x398361['id'],'requester_id':_0x38c5dc['id'],'name':util[_0x8ceb('0xe')](_0x8ceb('0xf'),_0xd50c0f[_0x8ceb('0x10')]),'phone':_0xd50c0f['calleridnum'],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0x8ceb('0x11')](_0x1c32d9[_0x8ceb('0x12')],_0xd50c0f,'\x20'),'description':intUtil[_0x8ceb('0x11')](_0x1c32d9['Descriptions'],_0xd50c0f,_0x8ceb('0x13')),'tags':getTags(_0x1c32d9[_0x8ceb('0x14')],'name')},getCustomFields(_0x1c32d9[_0x8ceb('0x15')],_0xd50c0f));_0x55ba44[_0x8ceb('0x16')]=_[_0x8ceb('0x17')](_[_0x8ceb('0x18')](_0x55ba44['custom_fields'],'id'),_0x8ceb('0x19'));return request({'uri':util[_0x8ceb('0xe')](_0x8ceb('0x1a'),_0x42b090['remoteUri']),'method':_0x8ceb('0x1b'),'body':_0x55ba44})[_0x8ceb('0x1c')](function(_0x46612f){logger[_0x8ceb('0xc')](_0x8ceb('0x1d'));return _0x46612f;});}function getUser(_0x146937,_0x4bea57){if(intUtil[_0x8ceb('0x1e')](_0x4bea57)){return request({'uri':util[_0x8ceb('0xe')](_0x8ceb('0x1f'),_0x146937[_0x8ceb('0x20')]),'qs':{'email':_0x4bea57}})[_0x8ceb('0x1c')](function(_0x59c7bf){if(_0x59c7bf[_0x8ceb('0x21')]){logger[_0x8ceb('0xc')](_0x8ceb('0x22'));return _[_0x8ceb('0x23')](_0x59c7bf,function(_0x3a297f){return _0x3a297f[_0x8ceb('0x24')]['email']===_0x4bea57;});}else{logger[_0x8ceb('0xc')](_0x8ceb('0x25'));return request({'uri':util[_0x8ceb('0xe')](_0x8ceb('0x26'),_0x146937[_0x8ceb('0x20')])})[_0x8ceb('0x1c')](function(_0x324986){logger[_0x8ceb('0xc')](_0x8ceb('0x27'));return _0x324986;});}});}else{logger[_0x8ceb('0xc')](_0x8ceb('0x28'));return request({'uri':util[_0x8ceb('0xe')](_0x8ceb('0x26'),_0x146937[_0x8ceb('0x20')])})['then'](function(_0x5d188b){logger[_0x8ceb('0xc')]('Admin\x20account\x20selected.');return _0x5d188b;});}}function getEndUser(_0x41e01d,_0x31509d,_0x26cae4){logger['info']('Searching\x20enduser\x20with\x20phone',_0x26cae4);return request({'uri':util[_0x8ceb('0xe')](_0x8ceb('0x29'),_0x41e01d['remoteUri']),'qs':{'phone':_0x26cae4}})[_0x8ceb('0x1c')](function(_0x4f718e){if(_0x4f718e['length']){logger[_0x8ceb('0xc')](_0x8ceb('0x2a'));return _[_0x8ceb('0x23')](_0x4f718e,{'phone':_0x26cae4});}else{logger[_0x8ceb('0xc')](_0x8ceb('0x2b'));return request({'uri':util[_0x8ceb('0xe')](_0x8ceb('0x29'),_0x41e01d[_0x8ceb('0x20')]),'method':_0x8ceb('0x1b'),'body':{'name':_0x31509d,'phone':_0x26cae4}})['then'](function(_0x3d5137){logger['info'](_0x8ceb('0x2c'));return _0x3d5137;});}});}function getAuth(_0x5939a2){return util[_0x8ceb('0xe')](_0x8ceb('0x2d'),new Buffer(_0x5939a2[_0x8ceb('0x2e')]+':X')[_0x8ceb('0x2f')](_0x8ceb('0x30')));}function request(_0x19fdfc){logger[_0x8ceb('0x31')](_0x8ceb('0x32'),JSON[_0x8ceb('0x33')](_0x19fdfc));return rp(_[_0x8ceb('0x8')](_0x19fdfc,{'method':_0x8ceb('0x34'),'headers':headers,'json':!![]}));}function getTags(_0x571dd2,_0x7fc7f7){var _0x29d6da=[];if(_0x571dd2&&_0x571dd2['length']>0x0){_0x29d6da=_['map'](_0x571dd2,_0x7fc7f7);}return _0x29d6da;}function getCustomFields(_0x52a059,_0x2fc20b){var _0x1e1571=[];var _0x3564b9={'custom_fields':[]};if(_0x52a059[_0x8ceb('0x21')]>0x0){_[_0x8ceb('0x35')](_0x52a059,function(_0x5875a2){if(_0x5875a2['type']===_0x8ceb('0x36')&&_0x5875a2['nameField']&&!_0x5875a2[_0x8ceb('0x37')]){_0x3564b9[_0x5875a2[_0x8ceb('0x38')][_0x8ceb('0x39')]()]=_0x5875a2[_0x8ceb('0x3a')][_0x8ceb('0x2f')]();}else if(_0x5875a2[_0x8ceb('0x3b')]===_0x8ceb('0x36')&&_0x5875a2[_0x8ceb('0x3c')]&&_0x5875a2[_0x8ceb('0x37')]){_0x3564b9[_0x8ceb('0x16')]['push']({'id':_0x5875a2[_0x8ceb('0x3c')],'value':_0x5875a2['content']['toString']()});}else if(_0x5875a2['type']==='variable'&&_0x5875a2['variableName']&&_0x5875a2[_0x8ceb('0x3c')]&&_0x5875a2[_0x8ceb('0x37')]){_0x2fc20b[_0x5875a2['variableName']]&&_0x3564b9['custom_fields']['push']({'id':_0x5875a2[_0x8ceb('0x3c')],'value':_0x2fc20b[_0x5875a2[_0x8ceb('0x3d')]]['toString']()});}else if(_0x5875a2[_0x8ceb('0x3b')]===_0x8ceb('0x3e')&&_0x5875a2['Variable']&&_0x5875a2[_0x8ceb('0x3f')][_0x8ceb('0x40')]&&_0x5875a2[_0x8ceb('0x3c')]&&_0x5875a2['customField']){_0x2fc20b[_0x5875a2[_0x8ceb('0x3f')][_0x8ceb('0x40')]['toLowerCase']()]&&_0x3564b9[_0x8ceb('0x16')][_0x8ceb('0x41')]({'id':_0x5875a2['idField'],'value':_0x2fc20b[_0x5875a2[_0x8ceb('0x3f')][_0x8ceb('0x40')]['toLowerCase']()][_0x8ceb('0x2f')]()});}});}return _0x3564b9;}exports[_0x8ceb('0x42')]=function(_0x37960d,_0xd3fc05,_0x18ff9f,_0x5648eb,_0x4e9651,_0x1ddca1){var _0x358c46,_0x559283;_0x1ddca1[_0x8ceb('0x20')]=intUtil['stripTrailingSlash'](_0x1ddca1[_0x8ceb('0x20')]);_0x1ddca1[_0x8ceb('0x43')]=intUtil[_0x8ceb('0x44')](_0x1ddca1['serverUrl']);if(_0x4e9651&&_0x4e9651[_0x8ceb('0x45')]){logger[_0x8ceb('0xc')](_0x8ceb('0x46'),_0x8ceb('0x47'));_0xd3fc05[_0x8ceb('0x48')]=util[_0x8ceb('0xe')](_0x8ceb('0x49'),_0x1ddca1[_0x8ceb('0x43')],_0xd3fc05[_0x8ceb('0x4a')],md5(_0xd3fc05['uniqueid']));}else{_0xd3fc05[_0x8ceb('0x48')]='';}headers['Authorization']=getAuth(_0x1ddca1);return getUser(_0x1ddca1,_0x5648eb[_0x8ceb('0x4b')])[_0x8ceb('0x1c')](function(_0x5e0f48){_0x358c46=_0x5e0f48;logger[_0x8ceb('0xc')](_0x8ceb('0x4c'),_0x358c46['id']);return getEndUser(_0x1ddca1,_0xd3fc05['calleridname'],_0xd3fc05[_0x8ceb('0x4d')]);})['then'](function(_0x24713d){_0x559283=_0x24713d;logger[_0x8ceb('0xc')](_0x8ceb('0x4e'),_0x559283['id']);return getTicket(_0x1ddca1,_0x358c46,_0x559283,_0x18ff9f,_0xd3fc05);})['then'](function(_0x4a8033){if(_0x4a8033&&_0x4a8033['id']){logger[_0x8ceb('0xc')]('Ticket\x20created!');logger[_0x8ceb('0xc')](_0x8ceb('0x4f'),_0x4a8033['id']);logger[_0x8ceb('0xc')](_0x8ceb('0x46'),_0x8ceb('0x50'));emit(util[_0x8ceb('0xe')](_0x8ceb('0x51'),_0x5648eb[_0x8ceb('0x40')]),_0x8ceb('0x52'),{'uri':util[_0x8ceb('0xe')](_0x8ceb('0x53'),_0x1ddca1[_0x8ceb('0x20')],_0x4a8033['id'])});}})['catch'](function(_0x21844c){logger[_0x8ceb('0x54')](_0x8ceb('0x46'),JSON[_0x8ceb('0x33')](_0x21844c));});};exports['outbound']=function(_0x458912,_0x14ca3a,_0x45b639,_0x35bcfa,_0x3929de,_0x1a41b5,_0x591edc){var _0x66aaa,_0x3fe884;_0x1a41b5['remoteUri']=intUtil[_0x8ceb('0x44')](_0x1a41b5['remoteUri']);_0x1a41b5[_0x8ceb('0x43')]=intUtil[_0x8ceb('0x44')](_0x1a41b5[_0x8ceb('0x43')]);if(_0x591edc&&_0x591edc[_0x8ceb('0x55')]&&_0x591edc[_0x8ceb('0x55')]!==_0x8ceb('0x56')){logger[_0x8ceb('0xc')](_0x8ceb('0x57'),_0x8ceb('0x47'));_0x14ca3a['recordingURL']=util[_0x8ceb('0xe')](_0x8ceb('0x49'),_0x1a41b5['serverUrl'],_0x14ca3a['uniqueid'],md5(_0x14ca3a[_0x8ceb('0x4a')]));}headers[_0x8ceb('0x58')]=getAuth(_0x1a41b5);return getUser(_0x1a41b5,_0x35bcfa[_0x8ceb('0x4b')])[_0x8ceb('0x1c')](function(_0x3cd14b){_0x66aaa=_0x3cd14b;logger[_0x8ceb('0xc')](_0x8ceb('0x4c'),_0x66aaa['id']);return getEndUser(_0x1a41b5,_0x14ca3a['destcalleridname'],_0x14ca3a[_0x8ceb('0x59')]);})[_0x8ceb('0x1c')](function(_0xece414){_0x3fe884=_0xece414;logger[_0x8ceb('0xc')](_0x8ceb('0x4e'),_0x3fe884['id']);return getTicket(_0x1a41b5,_0x66aaa,_0x3fe884,_0x45b639,_0x14ca3a);})[_0x8ceb('0x1c')](function(_0x2e7030){if(_0x2e7030&&_0x2e7030['id']){logger['info'](_0x8ceb('0x1d'));logger['info'](_0x8ceb('0x4f'),_0x2e7030['id']);logger['info'](_0x8ceb('0x57'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0x8ceb('0xe')](_0x8ceb('0x51'),_0x35bcfa[_0x8ceb('0x40')]),'trigger:browser:url',{'uri':util[_0x8ceb('0xe')]('%s/helpdesk/tickets/%d',_0x1a41b5['remoteUri'],_0x2e7030['id'])});}})[_0x8ceb('0x5a')](function(_0x1cb0d5){logger[_0x8ceb('0x54')](_0x8ceb('0x57'),JSON[_0x8ceb('0x33')](_0x1cb0d5));});};exports['abandoned']=function(_0x1df14f,_0xa1abc6,_0x2df8e2,_0x310637,_0x34a386,_0x50fab6){var _0x221ee4,_0x10bf4d;_0x50fab6[_0x8ceb('0x20')]=intUtil['stripTrailingSlash'](_0x50fab6[_0x8ceb('0x20')]);_0x50fab6[_0x8ceb('0x43')]=intUtil[_0x8ceb('0x44')](_0x50fab6[_0x8ceb('0x43')]);if(_0x34a386&&_0x34a386[_0x8ceb('0x45')]){logger[_0x8ceb('0xc')](_0x8ceb('0x5b'),'Recording\x20is\x20enabled!');_0xa1abc6[_0x8ceb('0x48')]=util[_0x8ceb('0xe')](_0x8ceb('0x49'),_0x50fab6[_0x8ceb('0x43')],_0xa1abc6[_0x8ceb('0x4a')],md5(_0xa1abc6['uniqueid']));}else{_0xa1abc6[_0x8ceb('0x48')]='';}headers['Authorization']=getAuth(_0x50fab6);return getUser(_0x50fab6,'')[_0x8ceb('0x1c')](function(_0x1bce75){_0x221ee4=_0x1bce75;logger[_0x8ceb('0xc')]('The\x20User\x20ID\x20is:',_0x221ee4['id']);return getEndUser(_0x50fab6,_0xa1abc6[_0x8ceb('0x10')],_0xa1abc6[_0x8ceb('0x4d')]);})[_0x8ceb('0x1c')](function(_0x122ba4){_0x10bf4d=_0x122ba4;logger[_0x8ceb('0xc')](_0x8ceb('0x4e'),_0x10bf4d['id']);return getTicket(_0x50fab6,_0x221ee4,_0x10bf4d,_0x2df8e2,_0xa1abc6);})['then'](function(_0x19e1c5){if(_0x19e1c5){logger[_0x8ceb('0xc')]('Ticket\x20created!');logger[_0x8ceb('0xc')](_0x8ceb('0x4f'),_0x19e1c5['id']);}})[_0x8ceb('0x5a')](function(_0x30278f){logger[_0x8ceb('0x54')](_0x8ceb('0x5b'),JSON[_0x8ceb('0x33')](_0x30278f));});};