8b661a1e642d174803a8a397a2b75526d3a699c0
[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 _0x5ceb=['Descriptions','name','Fields','mapValues','keyBy','custom_fields','value','%s/api/v2/tickets','POST','Ticket\x20created!','validateEmail','%s/api/v2/agents','remoteUri','then','length','Agent\x20found!','find','contact','email','%s/api/v2/agents/me','Admin\x20account\x20selected.','Searching\x20enduser\x20with\x20phone','%s/api/v2/contacts','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Enduser\x20created.','Basic\x20%s','base64','debug','[HTTP]','stringify','GET','forEach','includes','string','picklist','type','nameField','toLowerCase','content','idField','customField','toString','variable','variableName','Variable','push','queue','stripTrailingSlash','serverUrl','monitor_format','[QUEUE]','recordingURL','uniqueid','Authorization','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','catch','outbound','recordingFormat','none','Recording\x20is\x20enabled!','The\x20User\x20ID\x20is:','destcalleridname','[OUTBOUND]','unmanaged','lastevent','toUpperCase','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','error','request-promise','md5','util','ioredis','./util','../../../../config/environment','../../../../config/logger','redis','defaults','localhost','socket.io-emitter','application/json','info','Creating\x20new\x20ticket...','format','Caller\x20-\x20%s','calleridname','calleridnum','Subjects','getString'];(function(_0x12f1f7,_0x17a67e){var _0x2ed954=function(_0x3aa63a){while(--_0x3aa63a){_0x12f1f7['push'](_0x12f1f7['shift']());}};_0x2ed954(++_0x17a67e);}(_0x5ceb,0x1c2));var _0xb5ce=function(_0x46698a,_0x8de1e4){_0x46698a=_0x46698a-0x0;var _0x356db0=_0x5ceb[_0x46698a];return _0x356db0;};'use strict';var _=require('lodash');var rp=require(_0xb5ce('0x0'));var md5=require(_0xb5ce('0x1'));var util=require(_0xb5ce('0x2'));var Redis=require(_0xb5ce('0x3'));var intUtil=require(_0xb5ce('0x4'));var config=require(_0xb5ce('0x5'));var logger=require(_0xb5ce('0x6'))('freshdesk');config[_0xb5ce('0x7')]=_[_0xb5ce('0x8')](config[_0xb5ce('0x7')],{'host':_0xb5ce('0x9'),'port':0x18eb});var io=require(_0xb5ce('0xa'))(new Redis(config[_0xb5ce('0x7')]));var headers={'Content-Type':_0xb5ce('0xb'),'Accept':_0xb5ce('0xb')};function emit(_0x19d303,_0x31a2fc,_0x5e64bc){io['to'](_0x19d303)['emit'](_0x31a2fc,_0x5e64bc);}function getTicket(_0x5249cc,_0x5c1f83,_0x47b6bb,_0x239c8e,_0x4bf6d0){logger[_0xb5ce('0xc')](_0xb5ce('0xd'));var _0x4041cf=_['merge']({'responder_id':_0x5c1f83['id'],'requester_id':_0x47b6bb['id'],'name':util[_0xb5ce('0xe')](_0xb5ce('0xf'),_0x4bf6d0[_0xb5ce('0x10')]),'phone':_0x4bf6d0[_0xb5ce('0x11')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil['getString'](_0x239c8e[_0xb5ce('0x12')],_0x4bf6d0,'\x20'),'description':intUtil[_0xb5ce('0x13')](_0x239c8e[_0xb5ce('0x14')],_0x4bf6d0,'<br>'),'tags':getTags(_0x239c8e['Tags'],_0xb5ce('0x15'))},getCustomFields(_0x239c8e[_0xb5ce('0x16')],_0x4bf6d0));_0x4041cf['custom_fields']=_[_0xb5ce('0x17')](_[_0xb5ce('0x18')](_0x4041cf[_0xb5ce('0x19')],'id'),_0xb5ce('0x1a'));return request({'uri':util[_0xb5ce('0xe')](_0xb5ce('0x1b'),_0x5249cc['remoteUri']),'method':_0xb5ce('0x1c'),'body':_0x4041cf})['then'](function(_0xbca53c){logger[_0xb5ce('0xc')](_0xb5ce('0x1d'));return _0xbca53c;});}function getUser(_0x387f09,_0x3b2cb8){if(intUtil[_0xb5ce('0x1e')](_0x3b2cb8)){return request({'uri':util['format'](_0xb5ce('0x1f'),_0x387f09[_0xb5ce('0x20')]),'qs':{'email':_0x3b2cb8}})[_0xb5ce('0x21')](function(_0xc4cb5d){if(_0xc4cb5d[_0xb5ce('0x22')]){logger[_0xb5ce('0xc')](_0xb5ce('0x23'));return _[_0xb5ce('0x24')](_0xc4cb5d,function(_0x2a20c2){return _0x2a20c2[_0xb5ce('0x25')][_0xb5ce('0x26')]===_0x3b2cb8;});}else{logger[_0xb5ce('0xc')]('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');return request({'uri':util[_0xb5ce('0xe')](_0xb5ce('0x27'),_0x387f09[_0xb5ce('0x20')])})[_0xb5ce('0x21')](function(_0x487568){logger['info'](_0xb5ce('0x28'));return _0x487568;});}});}else{logger[_0xb5ce('0xc')]('Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!');return request({'uri':util[_0xb5ce('0xe')](_0xb5ce('0x27'),_0x387f09[_0xb5ce('0x20')])})['then'](function(_0x15d999){logger[_0xb5ce('0xc')](_0xb5ce('0x28'));return _0x15d999;});}}function getEndUser(_0x910619,_0x40b3ef,_0x599859){logger['info'](_0xb5ce('0x29'),_0x599859);return request({'uri':util[_0xb5ce('0xe')](_0xb5ce('0x2a'),_0x910619['remoteUri']),'qs':{'phone':_0x599859}})[_0xb5ce('0x21')](function(_0x4fd1b6){if(_0x4fd1b6[_0xb5ce('0x22')]){logger[_0xb5ce('0xc')](_0xb5ce('0x2b'));return _[_0xb5ce('0x24')](_0x4fd1b6,{'phone':_0x599859});}else{logger[_0xb5ce('0xc')](_0xb5ce('0x2c'));return request({'uri':util['format'](_0xb5ce('0x2a'),_0x910619[_0xb5ce('0x20')]),'method':'POST','body':{'name':_0x40b3ef,'phone':_0x599859}})['then'](function(_0x43d134){logger[_0xb5ce('0xc')](_0xb5ce('0x2d'));return _0x43d134;});}});}function getAuth(_0xd3874f){return util[_0xb5ce('0xe')](_0xb5ce('0x2e'),new Buffer(_0xd3874f['apiKey']+':X')['toString'](_0xb5ce('0x2f')));}function request(_0x579254){logger[_0xb5ce('0x30')](_0xb5ce('0x31'),JSON[_0xb5ce('0x32')](_0x579254));return rp(_[_0xb5ce('0x8')](_0x579254,{'method':_0xb5ce('0x33'),'headers':headers,'json':!![]}));}function getTags(_0x8e8522,_0x2698df){var _0x20d360=[];if(_0x8e8522&&_0x8e8522['length']>0x0){_0x20d360=_['map'](_0x8e8522,_0x2698df);}return _0x20d360;}function getCustomFields(_0x11d9c8,_0x2d9ec4){var _0x24812e=[];var _0x1e1fe9={'custom_fields':[]};if(_0x11d9c8[_0xb5ce('0x22')]>0x0){_[_0xb5ce('0x34')](_0x11d9c8,function(_0x4b8c32){if(_[_0xb5ce('0x35')]([_0xb5ce('0x36'),_0xb5ce('0x37')],_0x4b8c32[_0xb5ce('0x38')])&&_0x4b8c32[_0xb5ce('0x39')]&&!_0x4b8c32['customField']){_0x1e1fe9[_0x4b8c32[_0xb5ce('0x39')][_0xb5ce('0x3a')]()]=_0x4b8c32[_0xb5ce('0x3b')]['toString']();}else if(_[_0xb5ce('0x35')]([_0xb5ce('0x36'),_0xb5ce('0x37')],_0x4b8c32[_0xb5ce('0x38')])&&_0x4b8c32[_0xb5ce('0x3c')]&&_0x4b8c32[_0xb5ce('0x3d')]){_0x1e1fe9[_0xb5ce('0x19')]['push']({'id':_0x4b8c32[_0xb5ce('0x3c')],'value':_0x4b8c32[_0xb5ce('0x3b')][_0xb5ce('0x3e')]()});}else if(_0x4b8c32['type']===_0xb5ce('0x3f')&&_0x4b8c32['variableName']&&_0x4b8c32[_0xb5ce('0x3c')]&&_0x4b8c32[_0xb5ce('0x3d')]){_0x2d9ec4[_0x4b8c32[_0xb5ce('0x40')]]&&_0x1e1fe9[_0xb5ce('0x19')]['push']({'id':_0x4b8c32[_0xb5ce('0x3c')],'value':_0x2d9ec4[_0x4b8c32[_0xb5ce('0x40')]][_0xb5ce('0x3e')]()});}else if(_0x4b8c32[_0xb5ce('0x38')]==='customVariable'&&_0x4b8c32[_0xb5ce('0x41')]&&_0x4b8c32[_0xb5ce('0x41')]['name']&&_0x4b8c32['idField']&&_0x4b8c32['customField']){_0x2d9ec4[_0x4b8c32[_0xb5ce('0x41')][_0xb5ce('0x15')][_0xb5ce('0x3a')]()]&&_0x1e1fe9[_0xb5ce('0x19')][_0xb5ce('0x42')]({'id':_0x4b8c32[_0xb5ce('0x3c')],'value':_0x2d9ec4[_0x4b8c32[_0xb5ce('0x41')][_0xb5ce('0x15')][_0xb5ce('0x3a')]()][_0xb5ce('0x3e')]()});}});}return _0x1e1fe9;}exports[_0xb5ce('0x43')]=function(_0x495867,_0x522c5a,_0xab0364,_0x3daf8f,_0x498281,_0x3a1390){var _0x3a9592,_0x3e6f09;_0x3a1390[_0xb5ce('0x20')]=intUtil[_0xb5ce('0x44')](_0x3a1390[_0xb5ce('0x20')]);_0x3a1390[_0xb5ce('0x45')]=intUtil['stripTrailingSlash'](_0x3a1390[_0xb5ce('0x45')]);if(_0x498281&&_0x498281[_0xb5ce('0x46')]){logger[_0xb5ce('0xc')](_0xb5ce('0x47'),'Recording\x20is\x20enabled!');_0x522c5a[_0xb5ce('0x48')]=util[_0xb5ce('0xe')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x3a1390['serverUrl'],_0x522c5a[_0xb5ce('0x49')],md5(_0x522c5a[_0xb5ce('0x49')]));}else{_0x522c5a[_0xb5ce('0x48')]='';}headers[_0xb5ce('0x4a')]=getAuth(_0x3a1390);return getUser(_0x3a1390,_0x3daf8f[_0xb5ce('0x26')])[_0xb5ce('0x21')](function(_0x2a7386){_0x3a9592=_0x2a7386;logger[_0xb5ce('0xc')]('The\x20User\x20ID\x20is:',_0x3a9592['id']);return getEndUser(_0x3a1390,_0x522c5a['calleridname'],_0x522c5a['calleridnum']);})[_0xb5ce('0x21')](function(_0xa0e397){_0x3e6f09=_0xa0e397;logger[_0xb5ce('0xc')](_0xb5ce('0x4b'),_0x3e6f09['id']);return getTicket(_0x3a1390,_0x3a9592,_0x3e6f09,_0xab0364,_0x522c5a);})[_0xb5ce('0x21')](function(_0xe4e806){if(_0xe4e806&&_0xe4e806['id']){logger[_0xb5ce('0xc')]('Ticket\x20created!');logger[_0xb5ce('0xc')](_0xb5ce('0x4c'),_0xe4e806['id']);logger[_0xb5ce('0xc')]('[QUEUE]',_0xb5ce('0x4d'));emit(util[_0xb5ce('0xe')](_0xb5ce('0x4e'),_0x3daf8f[_0xb5ce('0x15')]),_0xb5ce('0x4f'),{'uri':util[_0xb5ce('0xe')](_0xb5ce('0x50'),_0x3a1390['remoteUri'],_0xe4e806['id'])});}})[_0xb5ce('0x51')](function(_0x41cafd){logger['error'](_0xb5ce('0x47'),JSON[_0xb5ce('0x32')](_0x41cafd));});};exports[_0xb5ce('0x52')]=function(_0x59709d,_0x51edc0,_0x4b0fd8,_0x259dfd,_0x5dad1c,_0x187e5c,_0x17f6b3){var _0x38f9b8,_0x3c1deb;_0x187e5c[_0xb5ce('0x20')]=intUtil[_0xb5ce('0x44')](_0x187e5c[_0xb5ce('0x20')]);_0x187e5c[_0xb5ce('0x45')]=intUtil[_0xb5ce('0x44')](_0x187e5c['serverUrl']);if(_0x17f6b3&&_0x17f6b3[_0xb5ce('0x53')]&&_0x17f6b3[_0xb5ce('0x53')]!==_0xb5ce('0x54')){logger[_0xb5ce('0xc')]('[OUTBOUND]',_0xb5ce('0x55'));_0x51edc0[_0xb5ce('0x48')]=util[_0xb5ce('0xe')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x187e5c[_0xb5ce('0x45')],_0x51edc0[_0xb5ce('0x49')],md5(_0x51edc0[_0xb5ce('0x49')]));}headers[_0xb5ce('0x4a')]=getAuth(_0x187e5c);return getUser(_0x187e5c,_0x259dfd[_0xb5ce('0x26')])[_0xb5ce('0x21')](function(_0x341393){_0x38f9b8=_0x341393;logger[_0xb5ce('0xc')](_0xb5ce('0x56'),_0x38f9b8['id']);return getEndUser(_0x187e5c,_0x51edc0[_0xb5ce('0x57')],_0x51edc0['destcalleridnum']);})[_0xb5ce('0x21')](function(_0x4f43b3){_0x3c1deb=_0x4f43b3;logger[_0xb5ce('0xc')](_0xb5ce('0x4b'),_0x3c1deb['id']);return getTicket(_0x187e5c,_0x38f9b8,_0x3c1deb,_0x4b0fd8,_0x51edc0);})['then'](function(_0x459d48){if(_0x459d48&&_0x459d48['id']){logger[_0xb5ce('0xc')]('Ticket\x20created!');logger[_0xb5ce('0xc')](_0xb5ce('0x4c'),_0x459d48['id']);logger['info'](_0xb5ce('0x58'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0xb5ce('0xe')](_0xb5ce('0x4e'),_0x259dfd[_0xb5ce('0x15')]),_0xb5ce('0x4f'),{'uri':util[_0xb5ce('0xe')](_0xb5ce('0x50'),_0x187e5c[_0xb5ce('0x20')],_0x459d48['id'])});}})[_0xb5ce('0x51')](function(_0x51f438){logger['error']('[OUTBOUND]',JSON[_0xb5ce('0x32')](_0x51f438));});};exports[_0xb5ce('0x59')]=function(_0xc676ce,_0x324fe9,_0x52b530,_0x1ab586,_0x18a60b,_0x5026f6){var _0x2402d5,_0x534e7f;_0x5026f6[_0xb5ce('0x20')]=intUtil[_0xb5ce('0x44')](_0x5026f6[_0xb5ce('0x20')]);_0x5026f6[_0xb5ce('0x45')]=intUtil[_0xb5ce('0x44')](_0x5026f6['serverUrl']);if(_0x18a60b&&_0x18a60b[_0xb5ce('0x46')]){logger[_0xb5ce('0xc')]('['+_0x324fe9[_0xb5ce('0x5a')][_0xb5ce('0x5b')]()+']',_0xb5ce('0x55'));_0x324fe9['recordingURL']=util[_0xb5ce('0xe')](_0xb5ce('0x5c'),_0x5026f6[_0xb5ce('0x45')],_0x324fe9[_0xb5ce('0x49')],md5(_0x324fe9[_0xb5ce('0x49')]));}else{_0x324fe9[_0xb5ce('0x48')]='';}headers[_0xb5ce('0x4a')]=getAuth(_0x5026f6);return getUser(_0x5026f6,'')[_0xb5ce('0x21')](function(_0x47d4da){_0x2402d5=_0x47d4da;logger['info'](_0xb5ce('0x56'),_0x2402d5['id']);return getEndUser(_0x5026f6,_0x324fe9['calleridname'],_0x324fe9['calleridnum']);})['then'](function(_0x3af78b){_0x534e7f=_0x3af78b;logger[_0xb5ce('0xc')]('The\x20Enduser\x20ID\x20is:',_0x534e7f['id']);return getTicket(_0x5026f6,_0x2402d5,_0x534e7f,_0x52b530,_0x324fe9);})[_0xb5ce('0x21')](function(_0x1976d2){if(_0x1976d2){logger['info'](_0xb5ce('0x1d'));logger['info'](_0xb5ce('0x4c'),_0x1976d2['id']);}})[_0xb5ce('0x51')](function(_0xe400e2){logger[_0xb5ce('0x5d')]('['+_0x324fe9[_0xb5ce('0x5a')]['toUpperCase']()+']',JSON[_0xb5ce('0x32')](_0xe400e2));});};