Built motion from commit 14588770.|2.5.38
[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 _0x5844=['Searching\x20enduser\x20with\x20phone','%s/api/v2/contacts','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','base64','debug','[HTTP]','stringify','GET','map','forEach','type','customField','toLowerCase','toString','includes','string','push','idField','variable','variableName','customVariable','Variable','queue','serverUrl','stripTrailingSlash','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','uniqueid','The\x20User\x20ID\x20is:','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','user:%s','trigger:browser:url','%s/helpdesk/tickets/%d','catch','error','recordingFormat','none','[OUTBOUND]','Authorization','destcalleridname','destcalleridnum','toUpperCase','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','lastevent','lodash','request-promise','md5','ioredis','./util','../../../../config/logger','freshdesk','redis','defaults','localhost','application/json','emit','Creating\x20new\x20ticket...','merge','format','Caller\x20-\x20%s','calleridname','calleridnum','getString','Subjects','Descriptions','<br>','Tags','name','Fields','mapValues','keyBy','custom_fields','%s/api/v2/tickets','remoteUri','POST','info','Ticket\x20created!','validateEmail','%s/api/v2/agents','then','length','find','contact','email','%s/api/v2/agents/me','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','Admin\x20account\x20selected.'];(function(_0x40a045,_0x516f0e){var _0x1a687c=function(_0x48c9fe){while(--_0x48c9fe){_0x40a045['push'](_0x40a045['shift']());}};_0x1a687c(++_0x516f0e);}(_0x5844,0x141));var _0x4584=function(_0x4bf0e5,_0x5e0954){_0x4bf0e5=_0x4bf0e5-0x0;var _0x16cd41=_0x5844[_0x4bf0e5];return _0x16cd41;};'use strict';var _=require(_0x4584('0x0'));var rp=require(_0x4584('0x1'));var md5=require(_0x4584('0x2'));var util=require('util');var Redis=require(_0x4584('0x3'));var intUtil=require(_0x4584('0x4'));var config=require('../../../../config/environment');var logger=require(_0x4584('0x5'))(_0x4584('0x6'));config[_0x4584('0x7')]=_[_0x4584('0x8')](config[_0x4584('0x7')],{'host':_0x4584('0x9'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x4584('0x7')]));var headers={'Content-Type':'application/json','Accept':_0x4584('0xa')};function emit(_0x50f6a5,_0x119cac,_0x404f86){io['to'](_0x50f6a5)[_0x4584('0xb')](_0x119cac,_0x404f86);}function getTicket(_0x4aeba2,_0x48d513,_0x1b1183,_0x266167,_0x503f4e){logger['info'](_0x4584('0xc'));var _0x18b494=_[_0x4584('0xd')]({'responder_id':_0x48d513['id'],'requester_id':_0x1b1183['id'],'name':util[_0x4584('0xe')](_0x4584('0xf'),_0x503f4e[_0x4584('0x10')]),'phone':_0x503f4e[_0x4584('0x11')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0x4584('0x12')](_0x266167[_0x4584('0x13')],_0x503f4e,'\x20'),'description':intUtil['getString'](_0x266167[_0x4584('0x14')],_0x503f4e,_0x4584('0x15')),'tags':getTags(_0x266167[_0x4584('0x16')],_0x4584('0x17'))},getCustomFields(_0x266167[_0x4584('0x18')],_0x503f4e));_0x18b494['custom_fields']=_[_0x4584('0x19')](_[_0x4584('0x1a')](_0x18b494[_0x4584('0x1b')],'id'),'value');return request({'uri':util[_0x4584('0xe')](_0x4584('0x1c'),_0x4aeba2[_0x4584('0x1d')]),'method':_0x4584('0x1e'),'body':_0x18b494})['then'](function(_0x2513db){logger[_0x4584('0x1f')](_0x4584('0x20'));return _0x2513db;});}function getUser(_0x348f7d,_0x4de179){if(intUtil[_0x4584('0x21')](_0x4de179)){return request({'uri':util[_0x4584('0xe')](_0x4584('0x22'),_0x348f7d['remoteUri']),'qs':{'email':_0x4de179}})[_0x4584('0x23')](function(_0x189ff0){if(_0x189ff0[_0x4584('0x24')]){logger[_0x4584('0x1f')]('Agent\x20found!');return _[_0x4584('0x25')](_0x189ff0,function(_0x6d89d9){return _0x6d89d9[_0x4584('0x26')][_0x4584('0x27')]===_0x4de179;});}else{logger[_0x4584('0x1f')]('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');return request({'uri':util['format'](_0x4584('0x28'),_0x348f7d[_0x4584('0x1d')])})[_0x4584('0x23')](function(_0x44f8a7){logger[_0x4584('0x1f')]('Admin\x20account\x20selected.');return _0x44f8a7;});}});}else{logger['info'](_0x4584('0x29'));return request({'uri':util[_0x4584('0xe')](_0x4584('0x28'),_0x348f7d[_0x4584('0x1d')])})[_0x4584('0x23')](function(_0x5d1dbd){logger['info'](_0x4584('0x2a'));return _0x5d1dbd;});}}function getEndUser(_0x3fed48,_0x263a5d,_0x35179a){logger['info'](_0x4584('0x2b'),_0x35179a);return request({'uri':util['format'](_0x4584('0x2c'),_0x3fed48['remoteUri']),'qs':{'phone':_0x35179a}})[_0x4584('0x23')](function(_0x341b60){if(_0x341b60[_0x4584('0x24')]){logger[_0x4584('0x1f')](_0x4584('0x2d'));return _[_0x4584('0x25')](_0x341b60,{'phone':_0x35179a});}else{logger[_0x4584('0x1f')](_0x4584('0x2e'));return request({'uri':util[_0x4584('0xe')](_0x4584('0x2c'),_0x3fed48[_0x4584('0x1d')]),'method':_0x4584('0x1e'),'body':{'name':_0x263a5d,'phone':_0x35179a}})[_0x4584('0x23')](function(_0xfac315){logger[_0x4584('0x1f')]('Enduser\x20created.');return _0xfac315;});}});}function getAuth(_0x5065ce){return util['format']('Basic\x20%s',new Buffer(_0x5065ce['apiKey']+':X')['toString'](_0x4584('0x2f')));}function request(_0x3bc3f7){logger[_0x4584('0x30')](_0x4584('0x31'),JSON[_0x4584('0x32')](_0x3bc3f7));return rp(_['defaults'](_0x3bc3f7,{'method':_0x4584('0x33'),'headers':headers,'json':!![]}));}function getTags(_0x11160d,_0x3d5ec3){var _0x141e77=[];if(_0x11160d&&_0x11160d[_0x4584('0x24')]>0x0){_0x141e77=_[_0x4584('0x34')](_0x11160d,_0x3d5ec3);}return _0x141e77;}function getCustomFields(_0x2e6bbe,_0x45da03){var _0x21c82a=[];var _0x47c05c={'custom_fields':[]};if(_0x2e6bbe[_0x4584('0x24')]>0x0){_[_0x4584('0x35')](_0x2e6bbe,function(_0x1c9f3c){if(_['includes'](['string','picklist'],_0x1c9f3c[_0x4584('0x36')])&&_0x1c9f3c['nameField']&&!_0x1c9f3c[_0x4584('0x37')]){_0x47c05c[_0x1c9f3c['nameField'][_0x4584('0x38')]()]=_0x1c9f3c['content'][_0x4584('0x39')]();}else if(_[_0x4584('0x3a')]([_0x4584('0x3b'),'picklist'],_0x1c9f3c[_0x4584('0x36')])&&_0x1c9f3c['idField']&&_0x1c9f3c[_0x4584('0x37')]){_0x47c05c[_0x4584('0x1b')][_0x4584('0x3c')]({'id':_0x1c9f3c[_0x4584('0x3d')],'value':_0x1c9f3c['content'][_0x4584('0x39')]()});}else if(_0x1c9f3c['type']===_0x4584('0x3e')&&_0x1c9f3c[_0x4584('0x3f')]&&_0x1c9f3c[_0x4584('0x3d')]&&_0x1c9f3c['customField']){_0x45da03[_0x1c9f3c[_0x4584('0x3f')]]&&_0x47c05c[_0x4584('0x1b')]['push']({'id':_0x1c9f3c[_0x4584('0x3d')],'value':_0x45da03[_0x1c9f3c[_0x4584('0x3f')]][_0x4584('0x39')]()});}else if(_0x1c9f3c[_0x4584('0x36')]===_0x4584('0x40')&&_0x1c9f3c['Variable']&&_0x1c9f3c[_0x4584('0x41')][_0x4584('0x17')]&&_0x1c9f3c[_0x4584('0x3d')]&&_0x1c9f3c[_0x4584('0x37')]){_0x45da03[_0x1c9f3c[_0x4584('0x41')][_0x4584('0x17')][_0x4584('0x38')]()]&&_0x47c05c[_0x4584('0x1b')]['push']({'id':_0x1c9f3c['idField'],'value':_0x45da03[_0x1c9f3c['Variable']['name']['toLowerCase']()][_0x4584('0x39')]()});}});}return _0x47c05c;}exports[_0x4584('0x42')]=function(_0x2b43a0,_0x54f039,_0x143887,_0x560510,_0x2e6d1b,_0x2f2309){var _0x4e5c74,_0x3525c7;_0x2f2309[_0x4584('0x1d')]=intUtil['stripTrailingSlash'](_0x2f2309[_0x4584('0x1d')]);_0x2f2309[_0x4584('0x43')]=intUtil[_0x4584('0x44')](_0x2f2309[_0x4584('0x43')]);if(_0x2e6d1b&&_0x2e6d1b[_0x4584('0x45')]){logger[_0x4584('0x1f')](_0x4584('0x46'),_0x4584('0x47'));_0x54f039[_0x4584('0x48')]=util['format']('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x2f2309['serverUrl'],_0x54f039[_0x4584('0x49')],md5(_0x54f039[_0x4584('0x49')]));}else{_0x54f039[_0x4584('0x48')]='';}headers['Authorization']=getAuth(_0x2f2309);return getUser(_0x2f2309,_0x560510[_0x4584('0x27')])[_0x4584('0x23')](function(_0x51a555){_0x4e5c74=_0x51a555;logger[_0x4584('0x1f')](_0x4584('0x4a'),_0x4e5c74['id']);return getEndUser(_0x2f2309,_0x54f039[_0x4584('0x10')],_0x54f039[_0x4584('0x11')]);})[_0x4584('0x23')](function(_0x427e43){_0x3525c7=_0x427e43;logger[_0x4584('0x1f')](_0x4584('0x4b'),_0x3525c7['id']);return getTicket(_0x2f2309,_0x4e5c74,_0x3525c7,_0x143887,_0x54f039);})['then'](function(_0x278d16){if(_0x278d16&&_0x278d16['id']){logger['info'](_0x4584('0x20'));logger['info'](_0x4584('0x4c'),_0x278d16['id']);logger[_0x4584('0x1f')](_0x4584('0x46'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0x4584('0xe')](_0x4584('0x4d'),_0x560510[_0x4584('0x17')]),_0x4584('0x4e'),{'uri':util[_0x4584('0xe')](_0x4584('0x4f'),_0x2f2309[_0x4584('0x1d')],_0x278d16['id'])});}})[_0x4584('0x50')](function(_0x3dd5a9){logger[_0x4584('0x51')]('[QUEUE]',JSON[_0x4584('0x32')](_0x3dd5a9));});};exports['outbound']=function(_0x1c4882,_0x3fe4f3,_0x568490,_0x9d9a05,_0x21ca37,_0x42f763,_0x588337){var _0x4829c1,_0x25fe9f;_0x42f763['remoteUri']=intUtil[_0x4584('0x44')](_0x42f763[_0x4584('0x1d')]);_0x42f763[_0x4584('0x43')]=intUtil['stripTrailingSlash'](_0x42f763['serverUrl']);if(_0x588337&&_0x588337[_0x4584('0x52')]&&_0x588337[_0x4584('0x52')]!==_0x4584('0x53')){logger[_0x4584('0x1f')](_0x4584('0x54'),'Recording\x20is\x20enabled!');_0x3fe4f3['recordingURL']=util[_0x4584('0xe')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x42f763[_0x4584('0x43')],_0x3fe4f3[_0x4584('0x49')],md5(_0x3fe4f3[_0x4584('0x49')]));}headers[_0x4584('0x55')]=getAuth(_0x42f763);return getUser(_0x42f763,_0x9d9a05['email'])[_0x4584('0x23')](function(_0x42f9ca){_0x4829c1=_0x42f9ca;logger[_0x4584('0x1f')](_0x4584('0x4a'),_0x4829c1['id']);return getEndUser(_0x42f763,_0x3fe4f3[_0x4584('0x56')],_0x3fe4f3[_0x4584('0x57')]);})[_0x4584('0x23')](function(_0x40cb59){_0x25fe9f=_0x40cb59;logger[_0x4584('0x1f')](_0x4584('0x4b'),_0x25fe9f['id']);return getTicket(_0x42f763,_0x4829c1,_0x25fe9f,_0x568490,_0x3fe4f3);})[_0x4584('0x23')](function(_0x1cafcd){if(_0x1cafcd&&_0x1cafcd['id']){logger['info'](_0x4584('0x20'));logger['info']('The\x20Ticket\x20ID\x20is:',_0x1cafcd['id']);logger[_0x4584('0x1f')](_0x4584('0x54'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0x4584('0xe')]('user:%s',_0x9d9a05[_0x4584('0x17')]),_0x4584('0x4e'),{'uri':util[_0x4584('0xe')](_0x4584('0x4f'),_0x42f763['remoteUri'],_0x1cafcd['id'])});}})[_0x4584('0x50')](function(_0x3fa0df){logger[_0x4584('0x51')](_0x4584('0x54'),JSON[_0x4584('0x32')](_0x3fa0df));});};exports['unmanaged']=function(_0x6ceae2,_0x5f25e8,_0x181616,_0x16fc53,_0xabe1c0,_0x37c44a){var _0x7fd2d5,_0x55c600;_0x37c44a['remoteUri']=intUtil[_0x4584('0x44')](_0x37c44a[_0x4584('0x1d')]);_0x37c44a[_0x4584('0x43')]=intUtil[_0x4584('0x44')](_0x37c44a['serverUrl']);if(_0xabe1c0&&_0xabe1c0[_0x4584('0x45')]){logger['info']('['+_0x5f25e8['lastevent'][_0x4584('0x58')]()+']',_0x4584('0x47'));_0x5f25e8[_0x4584('0x48')]=util[_0x4584('0xe')](_0x4584('0x59'),_0x37c44a['serverUrl'],_0x5f25e8[_0x4584('0x49')],md5(_0x5f25e8['uniqueid']));}else{_0x5f25e8[_0x4584('0x48')]='';}headers[_0x4584('0x55')]=getAuth(_0x37c44a);return getUser(_0x37c44a,'')[_0x4584('0x23')](function(_0x275afa){_0x7fd2d5=_0x275afa;logger[_0x4584('0x1f')](_0x4584('0x4a'),_0x7fd2d5['id']);return getEndUser(_0x37c44a,_0x5f25e8[_0x4584('0x10')],_0x5f25e8[_0x4584('0x11')]);})['then'](function(_0x35e62d){_0x55c600=_0x35e62d;logger[_0x4584('0x1f')](_0x4584('0x4b'),_0x55c600['id']);return getTicket(_0x37c44a,_0x7fd2d5,_0x55c600,_0x181616,_0x5f25e8);})['then'](function(_0x319fe9){if(_0x319fe9){logger['info'](_0x4584('0x20'));logger['info'](_0x4584('0x4c'),_0x319fe9['id']);}})[_0x4584('0x50')](function(_0x506416){logger[_0x4584('0x51')]('['+_0x5f25e8[_0x4584('0x5a')][_0x4584('0x58')]()+']',JSON[_0x4584('0x32')](_0x506416));});};