Built motion from commit (unavailable).|2.4.3
[motion2.git] / server / services / ami / trigger / integration / zendesk.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 _0x835f=['destcalleridname','destcalleridnum','newTab','[OUTBOUND]\x20Error\x20running\x20Zendesk\x20integration','inspect','abandoned','[ABANDONED]','Authorization','calleridname','util','./util','../../../../config/environment','../../../../config/logger','zendesk','redis','defaults','localhost','socket.io-emitter','emit','application/json','info','Creating\x20new\x20ticket...','remoteUri','POST','merge','Tags','name','getString','Subjects','Descriptions','Fields','then','Ticket\x20created!','ticket','validateEmail','Getting\x20agent\x20with\x20email','format','%s/search.json','type:user\x20role:agent\x20email:%s','Agent\x20found!','find','results','agent','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/users/me.json','Admin\x20account\x20selected.','user','type:user\x20role:end-user\x20phone:%s','filter','end-user','length','first','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','%s/users.json','Caller\x20-\x20%s','Enduser\x20created.','authType','password','%s:%s','username','token','Basic\x20%s','toString','debug','[HTTP]','stringify','map','nameField','customField','toLowerCase','string','idField','custom_fields','content','variable','variableName','push','type','customVariable','Variable','queue','stripTrailingSlash','serverUrl','monitor_format','Recording\x20is\x20enabled!','recordingURL','uniqueid','email','The\x20User\x20ID\x20is:','calleridnum','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','integrationTab','[QUEUE]','Displaying\x20ticket\x20to\x20agent...','%s/channels/voice/agents/%d/tickets/%d/display.json','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','trigger:browser:url','%s/agent/tickets/%s','Zendesk\x20account\x20type\x20not\x20recognized','catch','error','[QUEUE]\x20Error\x20running\x20Zendesk\x20integration','recordingFormat','none','[OUTBOUND]','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s'];(function(_0x1aa4c8,_0x3e4c6d){var _0x35edc5=function(_0x469caf){while(--_0x469caf){_0x1aa4c8['push'](_0x1aa4c8['shift']());}};_0x35edc5(++_0x3e4c6d);}(_0x835f,0x76));var _0xf835=function(_0x531d15,_0x599af5){_0x531d15=_0x531d15-0x0;var _0x271a18=_0x835f[_0x531d15];return _0x271a18;};'use strict';var _=require('lodash');var rp=require('request-promise');var md5=require('md5');var util=require(_0xf835('0x0'));var Redis=require('ioredis');var intUtil=require(_0xf835('0x1'));var config=require(_0xf835('0x2'));var logger=require(_0xf835('0x3'))(_0xf835('0x4'));config[_0xf835('0x5')]=_[_0xf835('0x6')](config['redis'],{'host':_0xf835('0x7'),'port':0x18eb});var io=require(_0xf835('0x8'))(new Redis(config[_0xf835('0x5')]));function emit(_0x80a661,_0x37a8a8,_0x23df1e){io['to'](_0x80a661)[_0xf835('0x9')](_0x37a8a8,_0x23df1e);}var headers={'Content-Type':_0xf835('0xa'),'Accept':_0xf835('0xa')};function getTicket(_0x42ba64,_0x38fb63,_0x4c5633,_0x1ec532,_0x48bdf0){logger[_0xf835('0xb')](_0xf835('0xc'));return request({'uri':util['format']('%s/tickets.json',_0x42ba64[_0xf835('0xd')]),'method':_0xf835('0xe'),'body':{'ticket':_[_0xf835('0xf')]({'requester_id':_0x4c5633['id'],'submitter_id':_0x38fb63?_0x38fb63['id']:undefined,'assignee_id':_0x38fb63?_0x38fb63['id']:undefined,'tags':getTags(_0x1ec532[_0xf835('0x10')],_0xf835('0x11')),'subject':intUtil[_0xf835('0x12')](_0x1ec532[_0xf835('0x13')],_0x48bdf0,'\x20'),'description':intUtil[_0xf835('0x12')](_0x1ec532[_0xf835('0x14')],_0x48bdf0,'\x0a'),'comment':{'public':![],'body':intUtil[_0xf835('0x12')](_0x1ec532[_0xf835('0x14')],_0x48bdf0,'\x0a')}},getCustomFields(_0x1ec532[_0xf835('0x15')],_0x48bdf0))}})[_0xf835('0x16')](function(_0x3f8b40){logger[_0xf835('0xb')](_0xf835('0x17'));return _0x3f8b40[_0xf835('0x18')];});}function getUser(_0x4523fc,_0x4d955c){if(intUtil[_0xf835('0x19')](_0x4d955c)){logger[_0xf835('0xb')](_0xf835('0x1a'),_0x4d955c);return request({'uri':util[_0xf835('0x1b')](_0xf835('0x1c'),_0x4523fc[_0xf835('0xd')]),'qs':{'query':util[_0xf835('0x1b')](_0xf835('0x1d'),_0x4d955c)}})['then'](function(_0x277c58){if(_0x277c58['count']){logger[_0xf835('0xb')](_0xf835('0x1e'));return _[_0xf835('0x1f')](_0x277c58[_0xf835('0x20')],{'email':_0x4d955c,'role':_0xf835('0x21')});}else{logger[_0xf835('0xb')](_0xf835('0x22'));return request({'uri':util[_0xf835('0x1b')](_0xf835('0x23'),_0x4523fc[_0xf835('0xd')])})['then'](function(_0x34e1a7){logger['info'](_0xf835('0x24'));return _0x34e1a7[_0xf835('0x25')];});}});}else{logger[_0xf835('0xb')](_0xf835('0x22'));return request({'uri':util[_0xf835('0x1b')](_0xf835('0x23'),_0x4523fc[_0xf835('0xd')])})[_0xf835('0x16')](function(_0x4b822e){logger[_0xf835('0xb')](_0xf835('0x24'));return _0x4b822e[_0xf835('0x25')];});}}function getEndUser(_0x3650cc,_0x24f4fa,_0x9ad008){logger['info']('Searching\x20enduser\x20with\x20phone',_0x9ad008);return request({'uri':util[_0xf835('0x1b')]('%s/search.json',_0x3650cc['remoteUri']),'qs':{'query':util['format'](_0xf835('0x26'),_0x9ad008)}})[_0xf835('0x16')](function(_0x441299){var _0x357d05;if(_0x441299['results']['length']){_0x441299[_0xf835('0x20')]=_[_0xf835('0x27')](_0x441299[_0xf835('0x20')],{'suspended':![],'role':_0xf835('0x28')});if(_0x441299[_0xf835('0x20')][_0xf835('0x29')]){_0x357d05=_[_0xf835('0x2a')](_0x441299[_0xf835('0x20')]);}}if(_0x357d05){logger[_0xf835('0xb')](_0xf835('0x2b'));return _0x357d05;}else{logger['info'](_0xf835('0x2c'));return request({'uri':util['format'](_0xf835('0x2d'),_0x3650cc[_0xf835('0xd')]),'method':_0xf835('0xe'),'body':{'user':{'role':_0xf835('0x28'),'name':util[_0xf835('0x1b')](_0xf835('0x2e'),_0x24f4fa||_0x9ad008),'phone':_0x9ad008}}})['then'](function(_0xb3ea1b){logger['info'](_0xf835('0x2f'));return _0xb3ea1b[_0xf835('0x25')];});}});}function getAuth(_0x3399e4){var _0x4dd60b;if(_0x3399e4[_0xf835('0x30')]===_0xf835('0x31')){_0x4dd60b=util[_0xf835('0x1b')](_0xf835('0x32'),_0x3399e4['username'],_0x3399e4[_0xf835('0x31')]);}else if(_0x3399e4['authType']==='token'){_0x4dd60b=util[_0xf835('0x1b')]('%s/token:%s',_0x3399e4[_0xf835('0x33')],_0x3399e4[_0xf835('0x34')]);}return util[_0xf835('0x1b')](_0xf835('0x35'),new Buffer(_0x4dd60b)[_0xf835('0x36')]('base64'));}function request(_0x2c7667){logger[_0xf835('0x37')](_0xf835('0x38'),JSON[_0xf835('0x39')](_0x2c7667));return rp(_[_0xf835('0x6')](_0x2c7667,{'method':'GET','headers':headers,'json':!![]}));}function getTags(_0x446a56,_0x2d2efb){var _0x123dac=[];if(_0x446a56&&_0x446a56[_0xf835('0x29')]>0x0){_0x123dac=_[_0xf835('0x3a')](_0x446a56,_0x2d2efb);}return _0x123dac;}function getCustomFields(_0x447ed2,_0x2e186a){var _0x2a10df=[];var _0x5f234d={'custom_fields':[]};if(_0x447ed2['length']>0x0){_['forEach'](_0x447ed2,function(_0x58389a){if(_0x58389a['type']==='string'&&_0x58389a[_0xf835('0x3b')]&&!_0x58389a[_0xf835('0x3c')]){_0x5f234d[_0x58389a[_0xf835('0x3b')][_0xf835('0x3d')]()]=_0x58389a['content'][_0xf835('0x36')]();}else if(_0x58389a['type']===_0xf835('0x3e')&&_0x58389a[_0xf835('0x3f')]&&_0x58389a['customField']){_0x5f234d[_0xf835('0x40')]['push']({'id':_0x58389a['idField'],'value':_0x58389a[_0xf835('0x41')][_0xf835('0x36')]()});}else if(_0x58389a['type']===_0xf835('0x42')&&_0x58389a[_0xf835('0x43')]&&_0x58389a[_0xf835('0x3f')]&&_0x58389a[_0xf835('0x3c')]){_0x2e186a[_0x58389a[_0xf835('0x43')]]&&_0x5f234d['custom_fields'][_0xf835('0x44')]({'id':_0x58389a[_0xf835('0x3f')],'value':_0x2e186a[_0x58389a['variableName']][_0xf835('0x36')]()});}else if(_0x58389a[_0xf835('0x45')]===_0xf835('0x46')&&_0x58389a[_0xf835('0x47')]&&_0x58389a['Variable'][_0xf835('0x11')]&&_0x58389a[_0xf835('0x3f')]&&_0x58389a[_0xf835('0x3c')]){_0x2e186a[_0x58389a[_0xf835('0x47')][_0xf835('0x11')][_0xf835('0x3d')]()]&&_0x5f234d[_0xf835('0x40')]['push']({'id':_0x58389a[_0xf835('0x3f')],'value':_0x2e186a[_0x58389a[_0xf835('0x47')][_0xf835('0x11')][_0xf835('0x3d')]()]['toString']()});}});}return _0x5f234d;}exports[_0xf835('0x48')]=function(_0x15e595,_0x269e68,_0xa03b79,_0x1000cb,_0xf7ba34,_0x53c9a7){var _0x2a2a8f,_0x54e8d8;_0x53c9a7[_0xf835('0xd')]=intUtil[_0xf835('0x49')](_0x53c9a7[_0xf835('0xd')]);_0x53c9a7[_0xf835('0x4a')]=intUtil['stripTrailingSlash'](_0x53c9a7[_0xf835('0x4a')]);if(_0xf7ba34&&_0xf7ba34[_0xf835('0x4b')]){logger[_0xf835('0xb')]('[QUEUE]',_0xf835('0x4c'));_0x269e68[_0xf835('0x4d')]=util[_0xf835('0x1b')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x53c9a7[_0xf835('0x4a')],_0x269e68['uniqueid'],md5(_0x269e68[_0xf835('0x4e')]));}else{_0x269e68[_0xf835('0x4d')]='';}headers['Authorization']=getAuth(_0x53c9a7);return getUser(_0x53c9a7,_0x1000cb[_0xf835('0x4f')])[_0xf835('0x16')](function(_0x213e31){_0x2a2a8f=_0x213e31;logger[_0xf835('0xb')](_0xf835('0x50'),_0x2a2a8f['id']);return getEndUser(_0x53c9a7,_0x269e68['calleridname'],_0x269e68[_0xf835('0x51')]);})['then'](function(_0x264fb0){_0x54e8d8=_0x264fb0;logger[_0xf835('0xb')](_0xf835('0x52'),_0x54e8d8['id']);return getTicket(_0x53c9a7,_0x2a2a8f,_0x54e8d8,_0xa03b79,_0x269e68);})[_0xf835('0x16')](function(_0x16b9f8){if(_0x16b9f8){logger[_0xf835('0xb')](_0xf835('0x53'),_0x16b9f8['id']);switch(_0x53c9a7[_0xf835('0x45')]){case _0xf835('0x54'):logger[_0xf835('0xb')](_0xf835('0x55'),_0xf835('0x56'));return request({'uri':util[_0xf835('0x1b')](_0xf835('0x57'),_0x53c9a7[_0xf835('0xd')],_0x2a2a8f['id'],_0x16b9f8['id']),'method':_0xf835('0xe')});case'newTab':logger['info'](_0xf835('0x55'),_0xf835('0x58'));emit(util[_0xf835('0x1b')](_0xf835('0x59'),_0x1000cb[_0xf835('0x11')]),_0xf835('0x5a'),{'uri':util['format'](_0xf835('0x5b'),_0x53c9a7['remoteUri'],_0x16b9f8['id'])});break;default:logger['error'](_0xf835('0x55'),_0xf835('0x5c'));}}})[_0xf835('0x5d')](function(_0x16dbd2){logger[_0xf835('0x5e')](_0xf835('0x5f'),util['inspect'](_0x16dbd2,{'showHidden':!![],'depth':null}));});};exports['outbound']=function(_0x59298e,_0x358f44,_0x519163,_0x5c49cd,_0x212818,_0x7a5a68,_0x11a1e9){var _0x302d1c,_0x133909;_0x7a5a68[_0xf835('0xd')]=intUtil[_0xf835('0x49')](_0x7a5a68['remoteUri']);_0x7a5a68[_0xf835('0x4a')]=intUtil['stripTrailingSlash'](_0x7a5a68[_0xf835('0x4a')]);if(_0x11a1e9&&_0x11a1e9[_0xf835('0x60')]&&_0x11a1e9[_0xf835('0x60')]!==_0xf835('0x61')){logger[_0xf835('0xb')](_0xf835('0x62'),_0xf835('0x4c'));_0x358f44['recordingURL']=util[_0xf835('0x1b')](_0xf835('0x63'),_0x7a5a68[_0xf835('0x4a')],_0x358f44[_0xf835('0x4e')],md5(_0x358f44[_0xf835('0x4e')]));}headers['Authorization']=getAuth(_0x7a5a68);return getUser(_0x7a5a68,_0x5c49cd['email'])[_0xf835('0x16')](function(_0x4ffa79){_0x302d1c=_0x4ffa79;logger[_0xf835('0xb')](_0xf835('0x50'),_0x302d1c['id']);return getEndUser(_0x7a5a68,_0x358f44[_0xf835('0x64')],_0x358f44[_0xf835('0x65')]);})[_0xf835('0x16')](function(_0x4acfa6){_0x133909=_0x4acfa6;logger['info'](_0xf835('0x52'),_0x133909['id']);return getTicket(_0x7a5a68,_0x302d1c,_0x133909,_0x519163,_0x358f44);})['then'](function(_0x22680b){if(_0x22680b){logger[_0xf835('0xb')](_0xf835('0x53'),_0x22680b['id']);switch(_0x7a5a68[_0xf835('0x45')]){case _0xf835('0x54'):logger[_0xf835('0xb')](_0xf835('0x62'),'Displaying\x20ticket\x20to\x20agent...');return request({'uri':util[_0xf835('0x1b')](_0xf835('0x57'),_0x7a5a68['remoteUri'],_0x302d1c['id'],_0x22680b['id']),'method':_0xf835('0xe')});case _0xf835('0x66'):logger[_0xf835('0xb')](_0xf835('0x62'),_0xf835('0x58'));emit(util[_0xf835('0x1b')](_0xf835('0x59'),_0x5c49cd[_0xf835('0x11')]),_0xf835('0x5a'),{'uri':util[_0xf835('0x1b')](_0xf835('0x5b'),_0x7a5a68['remoteUri'],_0x22680b['id'])});break;default:logger[_0xf835('0x5e')](_0xf835('0x62'),_0xf835('0x5c'));}}})['catch'](function(_0x446948){logger['error'](_0xf835('0x67'),util[_0xf835('0x68')](_0x446948,{'showHidden':!![],'depth':null}));});};exports[_0xf835('0x69')]=function(_0x5113e3,_0x5b2d36,_0xab908e,_0x45432e,_0x3012f0,_0x4084fd){var _0x581438,_0x3ccad8;_0x4084fd[_0xf835('0xd')]=intUtil[_0xf835('0x49')](_0x4084fd[_0xf835('0xd')]);_0x4084fd[_0xf835('0x4a')]=intUtil[_0xf835('0x49')](_0x4084fd[_0xf835('0x4a')]);if(_0x3012f0&&_0x3012f0[_0xf835('0x4b')]){logger[_0xf835('0xb')](_0xf835('0x6a'),_0xf835('0x4c'));_0x5b2d36[_0xf835('0x4d')]=util[_0xf835('0x1b')](_0xf835('0x63'),_0x4084fd[_0xf835('0x4a')],_0x5b2d36[_0xf835('0x4e')],md5(_0x5b2d36['uniqueid']));}else{_0x5b2d36[_0xf835('0x4d')]='';}headers[_0xf835('0x6b')]=getAuth(_0x4084fd);return getUser(_0x4084fd)['then'](function(_0x45304b){_0x581438=_0x45304b;logger[_0xf835('0xb')](_0xf835('0x50'),_0x581438['id']);return getEndUser(_0x4084fd,_0x5b2d36[_0xf835('0x6c')],_0x5b2d36[_0xf835('0x51')]);})['then'](function(_0x1f77cc){_0x3ccad8=_0x1f77cc;logger[_0xf835('0xb')](_0xf835('0x52'),_0x3ccad8['id']);return getTicket(_0x4084fd,_0x581438,_0x3ccad8,_0xab908e,_0x5b2d36);})[_0xf835('0x16')](function(_0x5d8ba9){if(_0x5d8ba9){logger[_0xf835('0xb')]('The\x20Ticket\x20ID\x20is:',_0x5d8ba9['id']);}})[_0xf835('0x5d')](function(_0x302ac3){logger[_0xf835('0x5e')]('[ABANDONED]\x20Error\x20running\x20Zendesk\x20integration',util[_0xf835('0x68')](_0x302ac3,{'showHidden':!![],'depth':null}));});};