eb3d4def8265f056562bb419699cafdd0f23d155
[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 _0xc144=['Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','trigger:browser:url','%s/helpdesk/tickets/%d','catch','error','outbound','none','destcalleridnum','The\x20Ticket\x20ID\x20is:','[OUTBOUND]','lastevent','The\x20User\x20ID\x20is:','toUpperCase','stringify','lodash','md5','util','ioredis','./util','../../../../config/environment','../../../../config/logger','redis','localhost','socket.io-emitter','application/json','info','merge','format','calleridname','calleridnum','getString','Subjects','<br>','name','Fields','custom_fields','mapValues','keyBy','value','%s/api/v2/tickets','POST','then','validateEmail','%s/api/v2/agents','remoteUri','find','contact','email','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/api/v2/agents/me','Admin\x20account\x20selected.','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','Searching\x20enduser\x20with\x20phone','length','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','%s/api/v2/contacts','Basic\x20%s','apiKey','toString','base64','debug','defaults','GET','map','forEach','string','picklist','nameField','customField','toLowerCase','content','type','push','idField','variable','variableName','Variable','queue','stripTrailingSlash','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','serverUrl','uniqueid','Authorization','The\x20Enduser\x20ID\x20is:','Ticket\x20created!'];(function(_0x38b121,_0x561713){var _0x4fe806=function(_0x103045){while(--_0x103045){_0x38b121['push'](_0x38b121['shift']());}};_0x4fe806(++_0x561713);}(_0xc144,0x17b));var _0x4c14=function(_0x2940bf,_0x1a7281){_0x2940bf=_0x2940bf-0x0;var _0x4ca39e=_0xc144[_0x2940bf];return _0x4ca39e;};'use strict';var _=require(_0x4c14('0x0'));var rp=require('request-promise');var md5=require(_0x4c14('0x1'));var util=require(_0x4c14('0x2'));var Redis=require(_0x4c14('0x3'));var intUtil=require(_0x4c14('0x4'));var config=require(_0x4c14('0x5'));var logger=require(_0x4c14('0x6'))('freshdesk');config[_0x4c14('0x7')]=_['defaults'](config[_0x4c14('0x7')],{'host':_0x4c14('0x8'),'port':0x18eb});var io=require(_0x4c14('0x9'))(new Redis(config[_0x4c14('0x7')]));var headers={'Content-Type':_0x4c14('0xa'),'Accept':_0x4c14('0xa')};function emit(_0x162772,_0x5ba35d,_0x4c3469){io['to'](_0x162772)['emit'](_0x5ba35d,_0x4c3469);}function getTicket(_0x27807e,_0x6df13c,_0x1e065a,_0xeb98b2,_0x56aad3){logger[_0x4c14('0xb')]('Creating\x20new\x20ticket...');var _0x3182d8=_[_0x4c14('0xc')]({'responder_id':_0x6df13c['id'],'requester_id':_0x1e065a['id'],'name':util[_0x4c14('0xd')]('Caller\x20-\x20%s',_0x56aad3[_0x4c14('0xe')]),'phone':_0x56aad3[_0x4c14('0xf')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0x4c14('0x10')](_0xeb98b2[_0x4c14('0x11')],_0x56aad3,'\x20'),'description':intUtil[_0x4c14('0x10')](_0xeb98b2['Descriptions'],_0x56aad3,_0x4c14('0x12')),'tags':getTags(_0xeb98b2['Tags'],_0x4c14('0x13'))},getCustomFields(_0xeb98b2[_0x4c14('0x14')],_0x56aad3));_0x3182d8[_0x4c14('0x15')]=_[_0x4c14('0x16')](_[_0x4c14('0x17')](_0x3182d8[_0x4c14('0x15')],'id'),_0x4c14('0x18'));return request({'uri':util['format'](_0x4c14('0x19'),_0x27807e['remoteUri']),'method':_0x4c14('0x1a'),'body':_0x3182d8})[_0x4c14('0x1b')](function(_0x5cd438){logger[_0x4c14('0xb')]('Ticket\x20created!');return _0x5cd438;});}function getUser(_0x3130de,_0x10898f){if(intUtil[_0x4c14('0x1c')](_0x10898f)){return request({'uri':util[_0x4c14('0xd')](_0x4c14('0x1d'),_0x3130de[_0x4c14('0x1e')]),'qs':{'email':_0x10898f}})[_0x4c14('0x1b')](function(_0x818e5){if(_0x818e5['length']){logger['info']('Agent\x20found!');return _[_0x4c14('0x1f')](_0x818e5,function(_0x1db5d6){return _0x1db5d6[_0x4c14('0x20')][_0x4c14('0x21')]===_0x10898f;});}else{logger['info'](_0x4c14('0x22'));return request({'uri':util['format'](_0x4c14('0x23'),_0x3130de['remoteUri'])})[_0x4c14('0x1b')](function(_0x42c1f9){logger[_0x4c14('0xb')](_0x4c14('0x24'));return _0x42c1f9;});}});}else{logger[_0x4c14('0xb')](_0x4c14('0x25'));return request({'uri':util[_0x4c14('0xd')](_0x4c14('0x23'),_0x3130de['remoteUri'])})[_0x4c14('0x1b')](function(_0x56ccbe){logger['info'](_0x4c14('0x24'));return _0x56ccbe;});}}function getEndUser(_0x1723c3,_0x47e307,_0x32688a){logger[_0x4c14('0xb')](_0x4c14('0x26'),_0x32688a);return request({'uri':util['format']('%s/api/v2/contacts',_0x1723c3[_0x4c14('0x1e')]),'qs':{'phone':_0x32688a}})['then'](function(_0x4d0b60){if(_0x4d0b60[_0x4c14('0x27')]){logger[_0x4c14('0xb')](_0x4c14('0x28'));return _[_0x4c14('0x1f')](_0x4d0b60,{'phone':_0x32688a});}else{logger['info'](_0x4c14('0x29'));return request({'uri':util['format'](_0x4c14('0x2a'),_0x1723c3['remoteUri']),'method':_0x4c14('0x1a'),'body':{'name':_0x47e307,'phone':_0x32688a}})[_0x4c14('0x1b')](function(_0x62fcf7){logger['info']('Enduser\x20created.');return _0x62fcf7;});}});}function getAuth(_0x25418b){return util[_0x4c14('0xd')](_0x4c14('0x2b'),new Buffer(_0x25418b[_0x4c14('0x2c')]+':X')[_0x4c14('0x2d')](_0x4c14('0x2e')));}function request(_0x1e7c26){logger[_0x4c14('0x2f')]('[HTTP]',JSON['stringify'](_0x1e7c26));return rp(_[_0x4c14('0x30')](_0x1e7c26,{'method':_0x4c14('0x31'),'headers':headers,'json':!![]}));}function getTags(_0x6ff8a1,_0x191a12){var _0x15cc41=[];if(_0x6ff8a1&&_0x6ff8a1['length']>0x0){_0x15cc41=_[_0x4c14('0x32')](_0x6ff8a1,_0x191a12);}return _0x15cc41;}function getCustomFields(_0x351e40,_0x2068bb){var _0x8cdd21=[];var _0x573be9={'custom_fields':[]};if(_0x351e40[_0x4c14('0x27')]>0x0){_[_0x4c14('0x33')](_0x351e40,function(_0x1b52f6){if(_['includes']([_0x4c14('0x34'),_0x4c14('0x35')],_0x1b52f6['type'])&&_0x1b52f6[_0x4c14('0x36')]&&!_0x1b52f6[_0x4c14('0x37')]){_0x573be9[_0x1b52f6[_0x4c14('0x36')][_0x4c14('0x38')]()]=_0x1b52f6[_0x4c14('0x39')][_0x4c14('0x2d')]();}else if(_['includes']([_0x4c14('0x34'),_0x4c14('0x35')],_0x1b52f6[_0x4c14('0x3a')])&&_0x1b52f6['idField']&&_0x1b52f6[_0x4c14('0x37')]){_0x573be9[_0x4c14('0x15')][_0x4c14('0x3b')]({'id':_0x1b52f6[_0x4c14('0x3c')],'value':_0x1b52f6[_0x4c14('0x39')][_0x4c14('0x2d')]()});}else if(_0x1b52f6[_0x4c14('0x3a')]===_0x4c14('0x3d')&&_0x1b52f6['variableName']&&_0x1b52f6[_0x4c14('0x3c')]&&_0x1b52f6['customField']){_0x2068bb[_0x1b52f6[_0x4c14('0x3e')]]&&_0x573be9[_0x4c14('0x15')][_0x4c14('0x3b')]({'id':_0x1b52f6[_0x4c14('0x3c')],'value':_0x2068bb[_0x1b52f6['variableName']]['toString']()});}else if(_0x1b52f6[_0x4c14('0x3a')]==='customVariable'&&_0x1b52f6[_0x4c14('0x3f')]&&_0x1b52f6['Variable'][_0x4c14('0x13')]&&_0x1b52f6[_0x4c14('0x3c')]&&_0x1b52f6[_0x4c14('0x37')]){_0x2068bb[_0x1b52f6[_0x4c14('0x3f')][_0x4c14('0x13')][_0x4c14('0x38')]()]&&_0x573be9[_0x4c14('0x15')][_0x4c14('0x3b')]({'id':_0x1b52f6['idField'],'value':_0x2068bb[_0x1b52f6[_0x4c14('0x3f')]['name'][_0x4c14('0x38')]()][_0x4c14('0x2d')]()});}});}return _0x573be9;}exports[_0x4c14('0x40')]=function(_0x5e1346,_0x360ff7,_0x5b7a53,_0x2e72d7,_0x16b7d1,_0x5863a9){var _0x4e0a29,_0x28b608;_0x5863a9['remoteUri']=intUtil['stripTrailingSlash'](_0x5863a9[_0x4c14('0x1e')]);_0x5863a9['serverUrl']=intUtil[_0x4c14('0x41')](_0x5863a9['serverUrl']);if(_0x16b7d1&&_0x16b7d1[_0x4c14('0x42')]){logger[_0x4c14('0xb')](_0x4c14('0x43'),_0x4c14('0x44'));_0x360ff7[_0x4c14('0x45')]=util[_0x4c14('0xd')](_0x4c14('0x46'),_0x5863a9[_0x4c14('0x47')],_0x360ff7[_0x4c14('0x48')],md5(_0x360ff7[_0x4c14('0x48')]));}else{_0x360ff7[_0x4c14('0x45')]='';}headers[_0x4c14('0x49')]=getAuth(_0x5863a9);return getUser(_0x5863a9,_0x2e72d7[_0x4c14('0x21')])[_0x4c14('0x1b')](function(_0x8d0bf0){_0x4e0a29=_0x8d0bf0;logger[_0x4c14('0xb')]('The\x20User\x20ID\x20is:',_0x4e0a29['id']);return getEndUser(_0x5863a9,_0x360ff7[_0x4c14('0xe')],_0x360ff7[_0x4c14('0xf')]);})[_0x4c14('0x1b')](function(_0x54c2ba){_0x28b608=_0x54c2ba;logger[_0x4c14('0xb')](_0x4c14('0x4a'),_0x28b608['id']);return getTicket(_0x5863a9,_0x4e0a29,_0x28b608,_0x5b7a53,_0x360ff7);})[_0x4c14('0x1b')](function(_0x4978b6){if(_0x4978b6&&_0x4978b6['id']){logger[_0x4c14('0xb')](_0x4c14('0x4b'));logger[_0x4c14('0xb')]('The\x20Ticket\x20ID\x20is:',_0x4978b6['id']);logger[_0x4c14('0xb')](_0x4c14('0x43'),_0x4c14('0x4c'));emit(util[_0x4c14('0xd')](_0x4c14('0x4d'),_0x2e72d7[_0x4c14('0x13')]),_0x4c14('0x4e'),{'uri':util[_0x4c14('0xd')](_0x4c14('0x4f'),_0x5863a9[_0x4c14('0x1e')],_0x4978b6['id'])});}})[_0x4c14('0x50')](function(_0x4208fe){logger[_0x4c14('0x51')]('[QUEUE]',JSON['stringify'](_0x4208fe));});};exports[_0x4c14('0x52')]=function(_0x8b1513,_0x326711,_0x420893,_0x48cd73,_0x56b991,_0x5c9473,_0x29ddbb){var _0x5845d4,_0x18bae8;_0x5c9473[_0x4c14('0x1e')]=intUtil[_0x4c14('0x41')](_0x5c9473[_0x4c14('0x1e')]);_0x5c9473['serverUrl']=intUtil[_0x4c14('0x41')](_0x5c9473[_0x4c14('0x47')]);if(_0x29ddbb&&_0x29ddbb['recordingFormat']&&_0x29ddbb['recordingFormat']!==_0x4c14('0x53')){logger[_0x4c14('0xb')]('[OUTBOUND]',_0x4c14('0x44'));_0x326711[_0x4c14('0x45')]=util[_0x4c14('0xd')](_0x4c14('0x46'),_0x5c9473[_0x4c14('0x47')],_0x326711[_0x4c14('0x48')],md5(_0x326711[_0x4c14('0x48')]));}headers[_0x4c14('0x49')]=getAuth(_0x5c9473);return getUser(_0x5c9473,_0x48cd73[_0x4c14('0x21')])['then'](function(_0x2b563a){_0x5845d4=_0x2b563a;logger[_0x4c14('0xb')]('The\x20User\x20ID\x20is:',_0x5845d4['id']);return getEndUser(_0x5c9473,_0x326711['destcalleridname'],_0x326711[_0x4c14('0x54')]);})[_0x4c14('0x1b')](function(_0x9a06fa){_0x18bae8=_0x9a06fa;logger[_0x4c14('0xb')]('The\x20Enduser\x20ID\x20is:',_0x18bae8['id']);return getTicket(_0x5c9473,_0x5845d4,_0x18bae8,_0x420893,_0x326711);})[_0x4c14('0x1b')](function(_0x4711a8){if(_0x4711a8&&_0x4711a8['id']){logger[_0x4c14('0xb')]('Ticket\x20created!');logger[_0x4c14('0xb')](_0x4c14('0x55'),_0x4711a8['id']);logger['info'](_0x4c14('0x56'),_0x4c14('0x4c'));emit(util[_0x4c14('0xd')](_0x4c14('0x4d'),_0x48cd73['name']),_0x4c14('0x4e'),{'uri':util[_0x4c14('0xd')](_0x4c14('0x4f'),_0x5c9473['remoteUri'],_0x4711a8['id'])});}})[_0x4c14('0x50')](function(_0x45cfae){logger['error'](_0x4c14('0x56'),JSON['stringify'](_0x45cfae));});};exports['unmanaged']=function(_0x434d8c,_0x3eec8d,_0x1f192e,_0x4c77e3,_0xc0f25,_0x35c33c){var _0x3338aa,_0x58f0b6;_0x35c33c[_0x4c14('0x1e')]=intUtil[_0x4c14('0x41')](_0x35c33c[_0x4c14('0x1e')]);_0x35c33c[_0x4c14('0x47')]=intUtil[_0x4c14('0x41')](_0x35c33c[_0x4c14('0x47')]);if(_0xc0f25&&_0xc0f25[_0x4c14('0x42')]){logger[_0x4c14('0xb')]('['+_0x3eec8d[_0x4c14('0x57')]['toUpperCase']()+']',_0x4c14('0x44'));_0x3eec8d['recordingURL']=util['format'](_0x4c14('0x46'),_0x35c33c['serverUrl'],_0x3eec8d[_0x4c14('0x48')],md5(_0x3eec8d[_0x4c14('0x48')]));}else{_0x3eec8d[_0x4c14('0x45')]='';}headers[_0x4c14('0x49')]=getAuth(_0x35c33c);return getUser(_0x35c33c,'')[_0x4c14('0x1b')](function(_0x505c43){_0x3338aa=_0x505c43;logger[_0x4c14('0xb')](_0x4c14('0x58'),_0x3338aa['id']);return getEndUser(_0x35c33c,_0x3eec8d['calleridname'],_0x3eec8d[_0x4c14('0xf')]);})[_0x4c14('0x1b')](function(_0xa471d0){_0x58f0b6=_0xa471d0;logger[_0x4c14('0xb')](_0x4c14('0x4a'),_0x58f0b6['id']);return getTicket(_0x35c33c,_0x3338aa,_0x58f0b6,_0x1f192e,_0x3eec8d);})[_0x4c14('0x1b')](function(_0x1b53b3){if(_0x1b53b3){logger[_0x4c14('0xb')](_0x4c14('0x4b'));logger[_0x4c14('0xb')]('The\x20Ticket\x20ID\x20is:',_0x1b53b3['id']);}})['catch'](function(_0x3439ae){logger[_0x4c14('0x51')]('['+_0x3eec8d['lastevent'][_0x4c14('0x59')]()+']',JSON[_0x4c14('0x5a')](_0x3439ae));});};