Built motion from commit 85328c08.|2.5.50
[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 _0x86be=['uniqueid','The\x20User\x20ID\x20is:','calleridname','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','trigger:browser:url','catch','[QUEUE]','stringify','outbound','recordingFormat','none','[OUTBOUND]','Authorization','destcalleridname','destcalleridnum','%s/helpdesk/tickets/%d','error','lastevent','toUpperCase','lodash','md5','util','ioredis','../../../../config/environment','freshdesk','defaults','redis','localhost','socket.io-emitter','application/json','info','merge','format','Caller\x20-\x20%s','calleridnum','getString','Subjects','Descriptions','<br>','Tags','name','keyBy','custom_fields','%s/api/v2/tickets','remoteUri','POST','then','Ticket\x20created!','validateEmail','%s/api/v2/agents','length','Agent\x20found!','contact','email','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/api/v2/agents/me','Admin\x20account\x20selected.','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','%s/api/v2/contacts','Basic\x20%s','toString','base64','debug','[HTTP]','GET','map','forEach','picklist','type','nameField','content','includes','string','idField','customField','variable','variableName','push','customVariable','Variable','toLowerCase','queue','stripTrailingSlash','serverUrl','monitor_format','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s'];(function(_0x609988,_0x507944){var _0x159984=function(_0x39d9ce){while(--_0x39d9ce){_0x609988['push'](_0x609988['shift']());}};_0x159984(++_0x507944);}(_0x86be,0xd0));var _0xe86b=function(_0x3481dc,_0x23c24b){_0x3481dc=_0x3481dc-0x0;var _0x1f2efa=_0x86be[_0x3481dc];return _0x1f2efa;};'use strict';var _=require(_0xe86b('0x0'));var rp=require('request-promise');var md5=require(_0xe86b('0x1'));var util=require(_0xe86b('0x2'));var Redis=require(_0xe86b('0x3'));var intUtil=require('./util');var config=require(_0xe86b('0x4'));var logger=require('../../../../config/logger')(_0xe86b('0x5'));config['redis']=_[_0xe86b('0x6')](config[_0xe86b('0x7')],{'host':_0xe86b('0x8'),'port':0x18eb});var io=require(_0xe86b('0x9'))(new Redis(config['redis']));var headers={'Content-Type':_0xe86b('0xa'),'Accept':'application/json'};function emit(_0x438009,_0xf9a47a,_0x18402e){io['to'](_0x438009)['emit'](_0xf9a47a,_0x18402e);}function getTicket(_0x5a5220,_0x4cd460,_0x452d87,_0x64b498,_0x42ae9d){logger[_0xe86b('0xb')]('Creating\x20new\x20ticket...');var _0x38ae03=_[_0xe86b('0xc')]({'responder_id':_0x4cd460['id'],'requester_id':_0x452d87['id'],'name':util[_0xe86b('0xd')](_0xe86b('0xe'),_0x42ae9d['calleridname']),'phone':_0x42ae9d[_0xe86b('0xf')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0xe86b('0x10')](_0x64b498[_0xe86b('0x11')],_0x42ae9d,'\x20'),'description':intUtil[_0xe86b('0x10')](_0x64b498[_0xe86b('0x12')],_0x42ae9d,_0xe86b('0x13')),'tags':getTags(_0x64b498[_0xe86b('0x14')],_0xe86b('0x15'))},getCustomFields(_0x64b498['Fields'],_0x42ae9d));_0x38ae03['custom_fields']=_['mapValues'](_[_0xe86b('0x16')](_0x38ae03[_0xe86b('0x17')],'id'),'value');return request({'uri':util[_0xe86b('0xd')](_0xe86b('0x18'),_0x5a5220[_0xe86b('0x19')]),'method':_0xe86b('0x1a'),'body':_0x38ae03})[_0xe86b('0x1b')](function(_0xd845eb){logger[_0xe86b('0xb')](_0xe86b('0x1c'));return _0xd845eb;});}function getUser(_0x85eced,_0xae7916){if(intUtil[_0xe86b('0x1d')](_0xae7916)){return request({'uri':util['format'](_0xe86b('0x1e'),_0x85eced[_0xe86b('0x19')]),'qs':{'email':_0xae7916}})[_0xe86b('0x1b')](function(_0x493d5a){if(_0x493d5a[_0xe86b('0x1f')]){logger[_0xe86b('0xb')](_0xe86b('0x20'));return _['find'](_0x493d5a,function(_0x324625){return _0x324625[_0xe86b('0x21')][_0xe86b('0x22')]===_0xae7916;});}else{logger[_0xe86b('0xb')](_0xe86b('0x23'));return request({'uri':util['format'](_0xe86b('0x24'),_0x85eced[_0xe86b('0x19')])})[_0xe86b('0x1b')](function(_0x4de336){logger[_0xe86b('0xb')](_0xe86b('0x25'));return _0x4de336;});}});}else{logger[_0xe86b('0xb')](_0xe86b('0x26'));return request({'uri':util[_0xe86b('0xd')](_0xe86b('0x24'),_0x85eced[_0xe86b('0x19')])})[_0xe86b('0x1b')](function(_0x2fa583){logger[_0xe86b('0xb')](_0xe86b('0x25'));return _0x2fa583;});}}function getEndUser(_0x482f21,_0x429b8a,_0x57a54f){logger['info']('Searching\x20enduser\x20with\x20phone',_0x57a54f);return request({'uri':util[_0xe86b('0xd')]('%s/api/v2/contacts',_0x482f21[_0xe86b('0x19')]),'qs':{'phone':_0x57a54f}})[_0xe86b('0x1b')](function(_0x388f38){if(_0x388f38[_0xe86b('0x1f')]){logger[_0xe86b('0xb')](_0xe86b('0x27'));return _['find'](_0x388f38,{'phone':_0x57a54f});}else{logger[_0xe86b('0xb')](_0xe86b('0x28'));return request({'uri':util[_0xe86b('0xd')](_0xe86b('0x29'),_0x482f21[_0xe86b('0x19')]),'method':_0xe86b('0x1a'),'body':{'name':_0x429b8a,'phone':_0x57a54f}})['then'](function(_0x1c537b){logger[_0xe86b('0xb')]('Enduser\x20created.');return _0x1c537b;});}});}function getAuth(_0xfadda5){return util[_0xe86b('0xd')](_0xe86b('0x2a'),new Buffer(_0xfadda5['apiKey']+':X')[_0xe86b('0x2b')](_0xe86b('0x2c')));}function request(_0x3a1862){logger[_0xe86b('0x2d')](_0xe86b('0x2e'),JSON['stringify'](_0x3a1862));return rp(_['defaults'](_0x3a1862,{'method':_0xe86b('0x2f'),'headers':headers,'json':!![]}));}function getTags(_0x9a51d5,_0x226547){var _0x4b7fb5=[];if(_0x9a51d5&&_0x9a51d5[_0xe86b('0x1f')]>0x0){_0x4b7fb5=_[_0xe86b('0x30')](_0x9a51d5,_0x226547);}return _0x4b7fb5;}function getCustomFields(_0x5a55fa,_0x123015){var _0x26d0c2=[];var _0x33b7e7={'custom_fields':[]};if(_0x5a55fa[_0xe86b('0x1f')]>0x0){_[_0xe86b('0x31')](_0x5a55fa,function(_0x326503){if(_['includes'](['string',_0xe86b('0x32')],_0x326503[_0xe86b('0x33')])&&_0x326503[_0xe86b('0x34')]&&!_0x326503['customField']){_0x33b7e7[_0x326503['nameField']['toLowerCase']()]=_0x326503[_0xe86b('0x35')]['toString']();}else if(_[_0xe86b('0x36')]([_0xe86b('0x37'),_0xe86b('0x32')],_0x326503[_0xe86b('0x33')])&&_0x326503[_0xe86b('0x38')]&&_0x326503[_0xe86b('0x39')]){_0x33b7e7[_0xe86b('0x17')]['push']({'id':_0x326503['idField'],'value':_0x326503[_0xe86b('0x35')][_0xe86b('0x2b')]()});}else if(_0x326503[_0xe86b('0x33')]===_0xe86b('0x3a')&&_0x326503['variableName']&&_0x326503[_0xe86b('0x38')]&&_0x326503[_0xe86b('0x39')]){_0x123015[_0x326503[_0xe86b('0x3b')]]&&_0x33b7e7[_0xe86b('0x17')][_0xe86b('0x3c')]({'id':_0x326503[_0xe86b('0x38')],'value':_0x123015[_0x326503[_0xe86b('0x3b')]][_0xe86b('0x2b')]()});}else if(_0x326503[_0xe86b('0x33')]===_0xe86b('0x3d')&&_0x326503[_0xe86b('0x3e')]&&_0x326503[_0xe86b('0x3e')][_0xe86b('0x15')]&&_0x326503[_0xe86b('0x38')]&&_0x326503[_0xe86b('0x39')]){_0x123015[_0x326503['Variable'][_0xe86b('0x15')][_0xe86b('0x3f')]()]&&_0x33b7e7[_0xe86b('0x17')][_0xe86b('0x3c')]({'id':_0x326503['idField'],'value':_0x123015[_0x326503[_0xe86b('0x3e')][_0xe86b('0x15')][_0xe86b('0x3f')]()][_0xe86b('0x2b')]()});}});}return _0x33b7e7;}exports[_0xe86b('0x40')]=function(_0xc9e0c6,_0x2b3081,_0x18c220,_0x20619a,_0x2d3426,_0x35108c){var _0x29cd88,_0x3f64d1;_0x35108c['remoteUri']=intUtil[_0xe86b('0x41')](_0x35108c[_0xe86b('0x19')]);_0x35108c['serverUrl']=intUtil[_0xe86b('0x41')](_0x35108c[_0xe86b('0x42')]);if(_0x2d3426&&_0x2d3426[_0xe86b('0x43')]){logger[_0xe86b('0xb')]('[QUEUE]',_0xe86b('0x44'));_0x2b3081[_0xe86b('0x45')]=util[_0xe86b('0xd')](_0xe86b('0x46'),_0x35108c[_0xe86b('0x42')],_0x2b3081[_0xe86b('0x47')],md5(_0x2b3081[_0xe86b('0x47')]));}else{_0x2b3081['recordingURL']='';}headers['Authorization']=getAuth(_0x35108c);return getUser(_0x35108c,_0x20619a['email'])[_0xe86b('0x1b')](function(_0x416298){_0x29cd88=_0x416298;logger[_0xe86b('0xb')](_0xe86b('0x48'),_0x29cd88['id']);return getEndUser(_0x35108c,_0x2b3081[_0xe86b('0x49')],_0x2b3081[_0xe86b('0xf')]);})[_0xe86b('0x1b')](function(_0x2903f6){_0x3f64d1=_0x2903f6;logger['info'](_0xe86b('0x4a'),_0x3f64d1['id']);return getTicket(_0x35108c,_0x29cd88,_0x3f64d1,_0x18c220,_0x2b3081);})['then'](function(_0x5af8af){if(_0x5af8af&&_0x5af8af['id']){logger[_0xe86b('0xb')](_0xe86b('0x1c'));logger[_0xe86b('0xb')](_0xe86b('0x4b'),_0x5af8af['id']);logger['info']('[QUEUE]',_0xe86b('0x4c'));emit(util[_0xe86b('0xd')](_0xe86b('0x4d'),_0x20619a[_0xe86b('0x15')]),_0xe86b('0x4e'),{'uri':util[_0xe86b('0xd')]('%s/helpdesk/tickets/%d',_0x35108c[_0xe86b('0x19')],_0x5af8af['id'])});}})[_0xe86b('0x4f')](function(_0x33f6a1){logger['error'](_0xe86b('0x50'),JSON[_0xe86b('0x51')](_0x33f6a1));});};exports[_0xe86b('0x52')]=function(_0x458fbf,_0x5adf4d,_0x5b2248,_0x49988b,_0x26883c,_0x53ce98,_0x238071){var _0x15f32b,_0x44eb03;_0x53ce98['remoteUri']=intUtil['stripTrailingSlash'](_0x53ce98[_0xe86b('0x19')]);_0x53ce98[_0xe86b('0x42')]=intUtil[_0xe86b('0x41')](_0x53ce98[_0xe86b('0x42')]);if(_0x238071&&_0x238071['recordingFormat']&&_0x238071[_0xe86b('0x53')]!==_0xe86b('0x54')){logger[_0xe86b('0xb')](_0xe86b('0x55'),_0xe86b('0x44'));_0x5adf4d['recordingURL']=util['format'](_0xe86b('0x46'),_0x53ce98[_0xe86b('0x42')],_0x5adf4d['uniqueid'],md5(_0x5adf4d[_0xe86b('0x47')]));}headers[_0xe86b('0x56')]=getAuth(_0x53ce98);return getUser(_0x53ce98,_0x49988b['email'])[_0xe86b('0x1b')](function(_0x4444fb){_0x15f32b=_0x4444fb;logger['info']('The\x20User\x20ID\x20is:',_0x15f32b['id']);return getEndUser(_0x53ce98,_0x5adf4d[_0xe86b('0x57')],_0x5adf4d[_0xe86b('0x58')]);})[_0xe86b('0x1b')](function(_0x2d2b34){_0x44eb03=_0x2d2b34;logger[_0xe86b('0xb')](_0xe86b('0x4a'),_0x44eb03['id']);return getTicket(_0x53ce98,_0x15f32b,_0x44eb03,_0x5b2248,_0x5adf4d);})[_0xe86b('0x1b')](function(_0x57443a){if(_0x57443a&&_0x57443a['id']){logger['info'](_0xe86b('0x1c'));logger[_0xe86b('0xb')]('The\x20Ticket\x20ID\x20is:',_0x57443a['id']);logger[_0xe86b('0xb')](_0xe86b('0x55'),_0xe86b('0x4c'));emit(util['format'](_0xe86b('0x4d'),_0x49988b['name']),_0xe86b('0x4e'),{'uri':util['format'](_0xe86b('0x59'),_0x53ce98[_0xe86b('0x19')],_0x57443a['id'])});}})[_0xe86b('0x4f')](function(_0x33b456){logger[_0xe86b('0x5a')](_0xe86b('0x55'),JSON['stringify'](_0x33b456));});};exports['unmanaged']=function(_0x2143ce,_0x1f8d73,_0x51dbe8,_0x125e9b,_0x5a4965,_0x1585c5){var _0x93dddd,_0x28ed64;_0x1585c5[_0xe86b('0x19')]=intUtil[_0xe86b('0x41')](_0x1585c5['remoteUri']);_0x1585c5[_0xe86b('0x42')]=intUtil[_0xe86b('0x41')](_0x1585c5[_0xe86b('0x42')]);if(_0x5a4965&&_0x5a4965[_0xe86b('0x43')]){logger['info']('['+_0x1f8d73[_0xe86b('0x5b')][_0xe86b('0x5c')]()+']',_0xe86b('0x44'));_0x1f8d73[_0xe86b('0x45')]=util[_0xe86b('0xd')](_0xe86b('0x46'),_0x1585c5[_0xe86b('0x42')],_0x1f8d73[_0xe86b('0x47')],md5(_0x1f8d73[_0xe86b('0x47')]));}else{_0x1f8d73[_0xe86b('0x45')]='';}headers['Authorization']=getAuth(_0x1585c5);return getUser(_0x1585c5,'')[_0xe86b('0x1b')](function(_0x5db820){_0x93dddd=_0x5db820;logger[_0xe86b('0xb')](_0xe86b('0x48'),_0x93dddd['id']);return getEndUser(_0x1585c5,_0x1f8d73['calleridname'],_0x1f8d73[_0xe86b('0xf')]);})[_0xe86b('0x1b')](function(_0x398c30){_0x28ed64=_0x398c30;logger[_0xe86b('0xb')](_0xe86b('0x4a'),_0x28ed64['id']);return getTicket(_0x1585c5,_0x93dddd,_0x28ed64,_0x51dbe8,_0x1f8d73);})['then'](function(_0x701fc0){if(_0x701fc0){logger[_0xe86b('0xb')]('Ticket\x20created!');logger[_0xe86b('0xb')](_0xe86b('0x4b'),_0x701fc0['id']);}})[_0xe86b('0x4f')](function(_0xc2f85f){logger[_0xe86b('0x5a')]('['+_0x1f8d73['lastevent'][_0xe86b('0x5c')]()+']',JSON[_0xe86b('0x51')](_0xc2f85f));});};