e70ea4dde7f83d021275103b885c5a01d2042bd4
[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 _0x05c7=['calleridname','calleridnum','getString','Subjects','Descriptions','<br>','name','Fields','keyBy','custom_fields','value','%s/api/v2/tickets','remoteUri','then','info','Ticket\x20created!','validateEmail','%s/api/v2/agents','length','find','contact','%s/api/v2/agents/me','Admin\x20account\x20selected.','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','Searching\x20enduser\x20with\x20phone','%s/api/v2/contacts','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','POST','Basic\x20%s','toString','base64','[HTTP]','stringify','defaults','GET','forEach','includes','string','picklist','type','nameField','idField','customField','content','variable','variableName','push','customVariable','Variable','toLowerCase','queue','serverUrl','stripTrailingSlash','monitor_format','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','Authorization','email','The\x20User\x20ID\x20is:','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','[QUEUE]','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','%s/helpdesk/tickets/%d','catch','outbound','recordingFormat','[OUTBOUND]','Recording\x20is\x20enabled!','uniqueid','destcalleridname','trigger:browser:url','error','unmanaged','toUpperCase','lastevent','md5','util','ioredis','./util','../../../../config/environment','../../../../config/logger','freshdesk','redis','localhost','application/json','Creating\x20new\x20ticket...','merge','format','Caller\x20-\x20%s'];(function(_0x59e862,_0x477bf2){var _0x1a25ee=function(_0xc61e64){while(--_0xc61e64){_0x59e862['push'](_0x59e862['shift']());}};_0x1a25ee(++_0x477bf2);}(_0x05c7,0x162));var _0x705c=function(_0x309ee3,_0x26c85f){_0x309ee3=_0x309ee3-0x0;var _0x2268e0=_0x05c7[_0x309ee3];return _0x2268e0;};'use strict';var _=require('lodash');var rp=require('request-promise');var md5=require(_0x705c('0x0'));var util=require(_0x705c('0x1'));var Redis=require(_0x705c('0x2'));var intUtil=require(_0x705c('0x3'));var config=require(_0x705c('0x4'));var logger=require(_0x705c('0x5'))(_0x705c('0x6'));config[_0x705c('0x7')]=_['defaults'](config[_0x705c('0x7')],{'host':_0x705c('0x8'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0x705c('0x7')]));var headers={'Content-Type':_0x705c('0x9'),'Accept':'application/json'};function emit(_0x439c9e,_0x235850,_0x3fa9b3){io['to'](_0x439c9e)['emit'](_0x235850,_0x3fa9b3);}function getTicket(_0x370db8,_0x5434db,_0xf39764,_0x160e1e,_0x2ddbed){logger['info'](_0x705c('0xa'));var _0x4b92c9=_[_0x705c('0xb')]({'responder_id':_0x5434db['id'],'requester_id':_0xf39764['id'],'name':util[_0x705c('0xc')](_0x705c('0xd'),_0x2ddbed[_0x705c('0xe')]),'phone':_0x2ddbed[_0x705c('0xf')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0x705c('0x10')](_0x160e1e[_0x705c('0x11')],_0x2ddbed,'\x20'),'description':intUtil[_0x705c('0x10')](_0x160e1e[_0x705c('0x12')],_0x2ddbed,_0x705c('0x13')),'tags':getTags(_0x160e1e['Tags'],_0x705c('0x14'))},getCustomFields(_0x160e1e[_0x705c('0x15')],_0x2ddbed));_0x4b92c9['custom_fields']=_['mapValues'](_[_0x705c('0x16')](_0x4b92c9[_0x705c('0x17')],'id'),_0x705c('0x18'));return request({'uri':util[_0x705c('0xc')](_0x705c('0x19'),_0x370db8[_0x705c('0x1a')]),'method':'POST','body':_0x4b92c9})[_0x705c('0x1b')](function(_0x203107){logger[_0x705c('0x1c')](_0x705c('0x1d'));return _0x203107;});}function getUser(_0x4aa842,_0x53d984){if(intUtil[_0x705c('0x1e')](_0x53d984)){return request({'uri':util[_0x705c('0xc')](_0x705c('0x1f'),_0x4aa842[_0x705c('0x1a')]),'qs':{'email':_0x53d984}})['then'](function(_0xd3d3bb){if(_0xd3d3bb[_0x705c('0x20')]){logger[_0x705c('0x1c')]('Agent\x20found!');return _[_0x705c('0x21')](_0xd3d3bb,function(_0x5b5cb4){return _0x5b5cb4[_0x705c('0x22')]['email']===_0x53d984;});}else{logger[_0x705c('0x1c')]('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');return request({'uri':util['format'](_0x705c('0x23'),_0x4aa842[_0x705c('0x1a')])})['then'](function(_0x1cec58){logger[_0x705c('0x1c')](_0x705c('0x24'));return _0x1cec58;});}});}else{logger[_0x705c('0x1c')](_0x705c('0x25'));return request({'uri':util['format'](_0x705c('0x23'),_0x4aa842[_0x705c('0x1a')])})[_0x705c('0x1b')](function(_0x326ba5){logger[_0x705c('0x1c')](_0x705c('0x24'));return _0x326ba5;});}}function getEndUser(_0x16e2d7,_0x1e0990,_0xebd0fc){logger[_0x705c('0x1c')](_0x705c('0x26'),_0xebd0fc);return request({'uri':util[_0x705c('0xc')](_0x705c('0x27'),_0x16e2d7[_0x705c('0x1a')]),'qs':{'phone':_0xebd0fc}})[_0x705c('0x1b')](function(_0x57ac4f){if(_0x57ac4f['length']){logger['info'](_0x705c('0x28'));return _[_0x705c('0x21')](_0x57ac4f,{'phone':_0xebd0fc});}else{logger['info'](_0x705c('0x29'));return request({'uri':util[_0x705c('0xc')](_0x705c('0x27'),_0x16e2d7[_0x705c('0x1a')]),'method':_0x705c('0x2a'),'body':{'name':_0x1e0990,'phone':_0xebd0fc}})[_0x705c('0x1b')](function(_0x42b2a8){logger[_0x705c('0x1c')]('Enduser\x20created.');return _0x42b2a8;});}});}function getAuth(_0x3c15cb){return util[_0x705c('0xc')](_0x705c('0x2b'),new Buffer(_0x3c15cb['apiKey']+':X')[_0x705c('0x2c')](_0x705c('0x2d')));}function request(_0x4630c0){logger['debug'](_0x705c('0x2e'),JSON[_0x705c('0x2f')](_0x4630c0));return rp(_[_0x705c('0x30')](_0x4630c0,{'method':_0x705c('0x31'),'headers':headers,'json':!![]}));}function getTags(_0x48fe55,_0x1cbf8d){var _0x5acaea=[];if(_0x48fe55&&_0x48fe55[_0x705c('0x20')]>0x0){_0x5acaea=_['map'](_0x48fe55,_0x1cbf8d);}return _0x5acaea;}function getCustomFields(_0x518746,_0x113391){var _0x393581=[];var _0x1ba888={'custom_fields':[]};if(_0x518746[_0x705c('0x20')]>0x0){_[_0x705c('0x32')](_0x518746,function(_0x4de52b){if(_[_0x705c('0x33')]([_0x705c('0x34'),_0x705c('0x35')],_0x4de52b[_0x705c('0x36')])&&_0x4de52b[_0x705c('0x37')]&&!_0x4de52b['customField']){_0x1ba888[_0x4de52b[_0x705c('0x37')]['toLowerCase']()]=_0x4de52b['content'][_0x705c('0x2c')]();}else if(_['includes'](['string',_0x705c('0x35')],_0x4de52b[_0x705c('0x36')])&&_0x4de52b[_0x705c('0x38')]&&_0x4de52b[_0x705c('0x39')]){_0x1ba888[_0x705c('0x17')]['push']({'id':_0x4de52b['idField'],'value':_0x4de52b[_0x705c('0x3a')]['toString']()});}else if(_0x4de52b[_0x705c('0x36')]===_0x705c('0x3b')&&_0x4de52b[_0x705c('0x3c')]&&_0x4de52b[_0x705c('0x38')]&&_0x4de52b[_0x705c('0x39')]){_0x113391[_0x4de52b['variableName']]&&_0x1ba888[_0x705c('0x17')][_0x705c('0x3d')]({'id':_0x4de52b[_0x705c('0x38')],'value':_0x113391[_0x4de52b['variableName']][_0x705c('0x2c')]()});}else if(_0x4de52b['type']===_0x705c('0x3e')&&_0x4de52b[_0x705c('0x3f')]&&_0x4de52b['Variable'][_0x705c('0x14')]&&_0x4de52b[_0x705c('0x38')]&&_0x4de52b[_0x705c('0x39')]){_0x113391[_0x4de52b[_0x705c('0x3f')]['name'][_0x705c('0x40')]()]&&_0x1ba888[_0x705c('0x17')]['push']({'id':_0x4de52b[_0x705c('0x38')],'value':_0x113391[_0x4de52b['Variable']['name'][_0x705c('0x40')]()][_0x705c('0x2c')]()});}});}return _0x1ba888;}exports[_0x705c('0x41')]=function(_0x11b1f5,_0x51dc26,_0x158318,_0x1ae75e,_0x2c1099,_0xa695b5){var _0x487340,_0x4aab8a;_0xa695b5[_0x705c('0x1a')]=intUtil['stripTrailingSlash'](_0xa695b5['remoteUri']);_0xa695b5[_0x705c('0x42')]=intUtil[_0x705c('0x43')](_0xa695b5[_0x705c('0x42')]);if(_0x2c1099&&_0x2c1099[_0x705c('0x44')]){logger[_0x705c('0x1c')]('[QUEUE]','Recording\x20is\x20enabled!');_0x51dc26[_0x705c('0x45')]=util['format'](_0x705c('0x46'),_0xa695b5[_0x705c('0x42')],_0x51dc26['uniqueid'],md5(_0x51dc26['uniqueid']));}else{_0x51dc26[_0x705c('0x45')]='';}headers[_0x705c('0x47')]=getAuth(_0xa695b5);return getUser(_0xa695b5,_0x1ae75e[_0x705c('0x48')])[_0x705c('0x1b')](function(_0x287718){_0x487340=_0x287718;logger[_0x705c('0x1c')](_0x705c('0x49'),_0x487340['id']);return getEndUser(_0xa695b5,_0x51dc26[_0x705c('0xe')],_0x51dc26[_0x705c('0xf')]);})[_0x705c('0x1b')](function(_0x3cd743){_0x4aab8a=_0x3cd743;logger[_0x705c('0x1c')](_0x705c('0x4a'),_0x4aab8a['id']);return getTicket(_0xa695b5,_0x487340,_0x4aab8a,_0x158318,_0x51dc26);})[_0x705c('0x1b')](function(_0x2c62f1){if(_0x2c62f1&&_0x2c62f1['id']){logger[_0x705c('0x1c')]('Ticket\x20created!');logger['info'](_0x705c('0x4b'),_0x2c62f1['id']);logger[_0x705c('0x1c')](_0x705c('0x4c'),_0x705c('0x4d'));emit(util[_0x705c('0xc')](_0x705c('0x4e'),_0x1ae75e[_0x705c('0x14')]),'trigger:browser:url',{'uri':util[_0x705c('0xc')](_0x705c('0x4f'),_0xa695b5[_0x705c('0x1a')],_0x2c62f1['id'])});}})[_0x705c('0x50')](function(_0x59ee17){logger['error']('[QUEUE]',JSON[_0x705c('0x2f')](_0x59ee17));});};exports[_0x705c('0x51')]=function(_0x3470ab,_0x91a427,_0x2cf479,_0x9c5cc6,_0x125f0f,_0x345ce9,_0x76e7cc){var _0x21ee49,_0x525b03;_0x345ce9[_0x705c('0x1a')]=intUtil[_0x705c('0x43')](_0x345ce9['remoteUri']);_0x345ce9['serverUrl']=intUtil[_0x705c('0x43')](_0x345ce9[_0x705c('0x42')]);if(_0x76e7cc&&_0x76e7cc[_0x705c('0x52')]&&_0x76e7cc[_0x705c('0x52')]!=='none'){logger['info'](_0x705c('0x53'),_0x705c('0x54'));_0x91a427[_0x705c('0x45')]=util[_0x705c('0xc')](_0x705c('0x46'),_0x345ce9['serverUrl'],_0x91a427[_0x705c('0x55')],md5(_0x91a427[_0x705c('0x55')]));}headers[_0x705c('0x47')]=getAuth(_0x345ce9);return getUser(_0x345ce9,_0x9c5cc6[_0x705c('0x48')])[_0x705c('0x1b')](function(_0x35e72a){_0x21ee49=_0x35e72a;logger['info'](_0x705c('0x49'),_0x21ee49['id']);return getEndUser(_0x345ce9,_0x91a427[_0x705c('0x56')],_0x91a427['destcalleridnum']);})[_0x705c('0x1b')](function(_0x40fc7c){_0x525b03=_0x40fc7c;logger['info']('The\x20Enduser\x20ID\x20is:',_0x525b03['id']);return getTicket(_0x345ce9,_0x21ee49,_0x525b03,_0x2cf479,_0x91a427);})['then'](function(_0x176cdc){if(_0x176cdc&&_0x176cdc['id']){logger['info'](_0x705c('0x1d'));logger[_0x705c('0x1c')](_0x705c('0x4b'),_0x176cdc['id']);logger[_0x705c('0x1c')]('[OUTBOUND]',_0x705c('0x4d'));emit(util[_0x705c('0xc')]('user:%s',_0x9c5cc6[_0x705c('0x14')]),_0x705c('0x57'),{'uri':util[_0x705c('0xc')](_0x705c('0x4f'),_0x345ce9[_0x705c('0x1a')],_0x176cdc['id'])});}})['catch'](function(_0x48e67f){logger[_0x705c('0x58')](_0x705c('0x53'),JSON['stringify'](_0x48e67f));});};exports[_0x705c('0x59')]=function(_0x2a5a11,_0x121028,_0x2f490b,_0x3a8b2c,_0x513d7c,_0xa32651){var _0x15a88c,_0x35cebb;_0xa32651['remoteUri']=intUtil[_0x705c('0x43')](_0xa32651[_0x705c('0x1a')]);_0xa32651[_0x705c('0x42')]=intUtil['stripTrailingSlash'](_0xa32651[_0x705c('0x42')]);if(_0x513d7c&&_0x513d7c[_0x705c('0x44')]){logger[_0x705c('0x1c')]('['+_0x121028['lastevent'][_0x705c('0x5a')]()+']',_0x705c('0x54'));_0x121028['recordingURL']=util[_0x705c('0xc')](_0x705c('0x46'),_0xa32651[_0x705c('0x42')],_0x121028[_0x705c('0x55')],md5(_0x121028[_0x705c('0x55')]));}else{_0x121028[_0x705c('0x45')]='';}headers[_0x705c('0x47')]=getAuth(_0xa32651);return getUser(_0xa32651,'')[_0x705c('0x1b')](function(_0x2d4c4a){_0x15a88c=_0x2d4c4a;logger[_0x705c('0x1c')]('The\x20User\x20ID\x20is:',_0x15a88c['id']);return getEndUser(_0xa32651,_0x121028[_0x705c('0xe')],_0x121028[_0x705c('0xf')]);})[_0x705c('0x1b')](function(_0x496b98){_0x35cebb=_0x496b98;logger[_0x705c('0x1c')](_0x705c('0x4a'),_0x35cebb['id']);return getTicket(_0xa32651,_0x15a88c,_0x35cebb,_0x2f490b,_0x121028);})[_0x705c('0x1b')](function(_0x540f26){if(_0x540f26){logger[_0x705c('0x1c')](_0x705c('0x1d'));logger[_0x705c('0x1c')](_0x705c('0x4b'),_0x540f26['id']);}})[_0x705c('0x50')](function(_0x355d3a){logger[_0x705c('0x58')]('['+_0x121028[_0x705c('0x5b')][_0x705c('0x5a')]()+']',JSON[_0x705c('0x2f')](_0x355d3a));});};