e3e4a8f333beac9f49b22bb7abdf71804268f9cb
[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 _0x80cc=['uniqueid','Authorization','email','The\x20User\x20ID\x20is:','calleridname','calleridnum','The\x20Ticket\x20ID\x20is:','integrationTab','Displaying\x20ticket\x20to\x20agent...','newTab','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','%s/agent/tickets/%s','replace','/api/v2','Zendesk\x20account\x20type\x20not\x20recognized','[QUEUE]\x20Error\x20running\x20Zendesk\x20integration','recordingFormat','[OUTBOUND]','recordingURL','The\x20Enduser\x20ID\x20is:','%s/channels/voice/agents/%d/tickets/%d/display.json','trigger:browser:url','error','[OUTBOUND]\x20Error\x20running\x20Zendesk\x20integration','inspect','unmanaged','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','catch','lastevent','toUpperCase','lodash','request-promise','md5','util','ioredis','./util','../../../../config/environment','../../../../config/logger','zendesk','redis','defaults','localhost','emit','info','format','%s/tickets.json','remoteUri','POST','Tags','name','getString','Subjects','Descriptions','Fields','then','Ticket\x20created!','ticket','%s/search.json','type:user\x20role:agent\x20email:%s','count','Agent\x20found!','find','agent','%s/users/me.json','Admin\x20account\x20selected.','user','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','results','length','filter','first','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','%s/users.json','end-user','authType','%s:%s','token','%s/token:%s','username','Basic\x20%s','toString','base64','GET','map','forEach','includes','string','type','nameField','customField','toLowerCase','content','picklist','idField','variableName','custom_fields','push','customVariable','Variable','stripTrailingSlash','serverUrl','[QUEUE]','Recording\x20is\x20enabled!'];(function(_0x548f2f,_0x43cc68){var _0x497c2b=function(_0x2fda8b){while(--_0x2fda8b){_0x548f2f['push'](_0x548f2f['shift']());}};_0x497c2b(++_0x43cc68);}(_0x80cc,0x15a));var _0xc80c=function(_0x4d750e,_0x35fb00){_0x4d750e=_0x4d750e-0x0;var _0x1a1e3f=_0x80cc[_0x4d750e];return _0x1a1e3f;};'use strict';var _=require(_0xc80c('0x0'));var rp=require(_0xc80c('0x1'));var md5=require(_0xc80c('0x2'));var util=require(_0xc80c('0x3'));var Redis=require(_0xc80c('0x4'));var intUtil=require(_0xc80c('0x5'));var config=require(_0xc80c('0x6'));var logger=require(_0xc80c('0x7'))(_0xc80c('0x8'));config[_0xc80c('0x9')]=_[_0xc80c('0xa')](config['redis'],{'host':_0xc80c('0xb'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0xc80c('0x9')]));function emit(_0xf4fdda,_0x543110,_0x5b6e66){io['to'](_0xf4fdda)[_0xc80c('0xc')](_0x543110,_0x5b6e66);}var headers={'Content-Type':'application/json','Accept':'application/json'};function getTicket(_0x2bad82,_0x5ef639,_0x2b9d73,_0x5bdfa6,_0x2447bd){logger[_0xc80c('0xd')]('Creating\x20new\x20ticket...');return request({'uri':util[_0xc80c('0xe')](_0xc80c('0xf'),_0x2bad82[_0xc80c('0x10')]),'method':_0xc80c('0x11'),'body':{'ticket':_['merge']({'requester_id':_0x2b9d73['id'],'submitter_id':_0x5ef639?_0x5ef639['id']:undefined,'assignee_id':_0x5ef639?_0x5ef639['id']:undefined,'tags':getTags(_0x5bdfa6[_0xc80c('0x12')],_0xc80c('0x13')),'subject':intUtil[_0xc80c('0x14')](_0x5bdfa6[_0xc80c('0x15')],_0x2447bd,'\x20'),'description':intUtil[_0xc80c('0x14')](_0x5bdfa6[_0xc80c('0x16')],_0x2447bd,'\x0a'),'comment':{'public':![],'body':intUtil[_0xc80c('0x14')](_0x5bdfa6['Descriptions'],_0x2447bd,'\x0a')}},getCustomFields(_0x5bdfa6[_0xc80c('0x17')],_0x2447bd))}})[_0xc80c('0x18')](function(_0x73e79b){logger[_0xc80c('0xd')](_0xc80c('0x19'));return _0x73e79b[_0xc80c('0x1a')];});}function getUser(_0x2cfd7e,_0x5bb1f8){if(intUtil['validateEmail'](_0x5bb1f8)){logger[_0xc80c('0xd')]('Getting\x20agent\x20with\x20email',_0x5bb1f8);return request({'uri':util[_0xc80c('0xe')](_0xc80c('0x1b'),_0x2cfd7e[_0xc80c('0x10')]),'qs':{'query':util[_0xc80c('0xe')](_0xc80c('0x1c'),_0x5bb1f8)}})[_0xc80c('0x18')](function(_0x3b5e0a){if(_0x3b5e0a[_0xc80c('0x1d')]){logger[_0xc80c('0xd')](_0xc80c('0x1e'));return _[_0xc80c('0x1f')](_0x3b5e0a['results'],{'email':_0x5bb1f8,'role':_0xc80c('0x20')});}else{logger[_0xc80c('0xd')]('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');return request({'uri':util['format'](_0xc80c('0x21'),_0x2cfd7e['remoteUri'])})[_0xc80c('0x18')](function(_0x31da01){logger['info'](_0xc80c('0x22'));return _0x31da01[_0xc80c('0x23')];});}});}else{logger[_0xc80c('0xd')](_0xc80c('0x24'));return request({'uri':util['format'](_0xc80c('0x21'),_0x2cfd7e[_0xc80c('0x10')])})['then'](function(_0xd365fd){logger[_0xc80c('0xd')](_0xc80c('0x22'));return _0xd365fd['user'];});}}function getEndUser(_0x4b756f,_0x3db798,_0x31f4e6){logger['info']('Searching\x20enduser\x20with\x20phone',_0x31f4e6);return request({'uri':util[_0xc80c('0xe')](_0xc80c('0x1b'),_0x4b756f['remoteUri']),'qs':{'query':util[_0xc80c('0xe')]('type:user\x20role:end-user\x20phone:%s',_0x31f4e6)}})[_0xc80c('0x18')](function(_0x911c94){var _0xdbe585;if(_0x911c94[_0xc80c('0x25')][_0xc80c('0x26')]){_0x911c94[_0xc80c('0x25')]=_[_0xc80c('0x27')](_0x911c94[_0xc80c('0x25')],{'suspended':![],'role':'end-user'});if(_0x911c94[_0xc80c('0x25')][_0xc80c('0x26')]){_0xdbe585=_[_0xc80c('0x28')](_0x911c94[_0xc80c('0x25')]);}}if(_0xdbe585){logger[_0xc80c('0xd')](_0xc80c('0x29'));return _0xdbe585;}else{logger[_0xc80c('0xd')](_0xc80c('0x2a'));return request({'uri':util[_0xc80c('0xe')](_0xc80c('0x2b'),_0x4b756f[_0xc80c('0x10')]),'method':_0xc80c('0x11'),'body':{'user':{'role':_0xc80c('0x2c'),'name':util[_0xc80c('0xe')]('Caller\x20-\x20%s',_0x3db798||_0x31f4e6),'phone':_0x31f4e6}}})['then'](function(_0x4eb5d){logger[_0xc80c('0xd')]('Enduser\x20created.');return _0x4eb5d[_0xc80c('0x23')];});}});}function getAuth(_0xd67a35){var _0x5b641e;if(_0xd67a35[_0xc80c('0x2d')]==='password'){_0x5b641e=util[_0xc80c('0xe')](_0xc80c('0x2e'),_0xd67a35['username'],_0xd67a35['password']);}else if(_0xd67a35[_0xc80c('0x2d')]===_0xc80c('0x2f')){_0x5b641e=util[_0xc80c('0xe')](_0xc80c('0x30'),_0xd67a35[_0xc80c('0x31')],_0xd67a35['token']);}return util[_0xc80c('0xe')](_0xc80c('0x32'),new Buffer(_0x5b641e)[_0xc80c('0x33')](_0xc80c('0x34')));}function request(_0x55bb5e){logger['debug']('[HTTP]',JSON['stringify'](_0x55bb5e));return rp(_[_0xc80c('0xa')](_0x55bb5e,{'method':_0xc80c('0x35'),'headers':headers,'json':!![]}));}function getTags(_0x1822fc,_0xc1e9b7){var _0x2b5591=[];if(_0x1822fc&&_0x1822fc[_0xc80c('0x26')]>0x0){_0x2b5591=_[_0xc80c('0x36')](_0x1822fc,_0xc1e9b7);}return _0x2b5591;}function getCustomFields(_0x274b60,_0x5442e7){var _0x9f80a5=[];var _0x2a0e8b={'custom_fields':[]};if(_0x274b60[_0xc80c('0x26')]>0x0){_[_0xc80c('0x37')](_0x274b60,function(_0x1b0101){if(_[_0xc80c('0x38')]([_0xc80c('0x39'),'picklist'],_0x1b0101[_0xc80c('0x3a')])&&_0x1b0101[_0xc80c('0x3b')]&&!_0x1b0101[_0xc80c('0x3c')]){_0x2a0e8b[_0x1b0101[_0xc80c('0x3b')][_0xc80c('0x3d')]()]=_0x1b0101[_0xc80c('0x3e')][_0xc80c('0x33')]();}else if(_['includes']([_0xc80c('0x39'),_0xc80c('0x3f')],_0x1b0101[_0xc80c('0x3a')])&&_0x1b0101[_0xc80c('0x40')]&&_0x1b0101[_0xc80c('0x3c')]){_0x2a0e8b['custom_fields']['push']({'id':_0x1b0101['idField'],'value':_0x1b0101['content']['toString']()});}else if(_0x1b0101['type']==='variable'&&_0x1b0101[_0xc80c('0x41')]&&_0x1b0101['idField']&&_0x1b0101[_0xc80c('0x3c')]){_0x5442e7[_0x1b0101[_0xc80c('0x41')]]&&_0x2a0e8b[_0xc80c('0x42')][_0xc80c('0x43')]({'id':_0x1b0101['idField'],'value':_0x5442e7[_0x1b0101[_0xc80c('0x41')]][_0xc80c('0x33')]()});}else if(_0x1b0101[_0xc80c('0x3a')]===_0xc80c('0x44')&&_0x1b0101[_0xc80c('0x45')]&&_0x1b0101['Variable'][_0xc80c('0x13')]&&_0x1b0101[_0xc80c('0x40')]&&_0x1b0101[_0xc80c('0x3c')]){_0x5442e7[_0x1b0101[_0xc80c('0x45')]['name'][_0xc80c('0x3d')]()]&&_0x2a0e8b[_0xc80c('0x42')][_0xc80c('0x43')]({'id':_0x1b0101[_0xc80c('0x40')],'value':_0x5442e7[_0x1b0101[_0xc80c('0x45')][_0xc80c('0x13')]['toLowerCase']()][_0xc80c('0x33')]()});}});}return _0x2a0e8b;}exports['queue']=function(_0x562b41,_0x276e68,_0x15d395,_0x204a32,_0x51f484,_0x243fe9){var _0x5e30ab,_0x1706a1;_0x243fe9[_0xc80c('0x10')]=intUtil[_0xc80c('0x46')](_0x243fe9[_0xc80c('0x10')]);_0x243fe9[_0xc80c('0x47')]=intUtil[_0xc80c('0x46')](_0x243fe9[_0xc80c('0x47')]);if(_0x51f484&&_0x51f484['monitor_format']){logger[_0xc80c('0xd')](_0xc80c('0x48'),_0xc80c('0x49'));_0x276e68['recordingURL']=util[_0xc80c('0xe')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x243fe9[_0xc80c('0x47')],_0x276e68[_0xc80c('0x4a')],md5(_0x276e68['uniqueid']));}else{_0x276e68['recordingURL']='';}headers[_0xc80c('0x4b')]=getAuth(_0x243fe9);return getUser(_0x243fe9,_0x204a32[_0xc80c('0x4c')])[_0xc80c('0x18')](function(_0x18b445){_0x5e30ab=_0x18b445;logger[_0xc80c('0xd')](_0xc80c('0x4d'),_0x5e30ab['id']);return getEndUser(_0x243fe9,_0x276e68[_0xc80c('0x4e')],_0x276e68[_0xc80c('0x4f')]);})['then'](function(_0x2cadc0){_0x1706a1=_0x2cadc0;logger[_0xc80c('0xd')]('The\x20Enduser\x20ID\x20is:',_0x1706a1['id']);return getTicket(_0x243fe9,_0x5e30ab,_0x1706a1,_0x15d395,_0x276e68);})['then'](function(_0x30f0b7){if(_0x30f0b7){logger[_0xc80c('0xd')](_0xc80c('0x50'),_0x30f0b7['id']);switch(_0x243fe9[_0xc80c('0x3a')]){case _0xc80c('0x51'):logger[_0xc80c('0xd')](_0xc80c('0x48'),_0xc80c('0x52'));return request({'uri':util[_0xc80c('0xe')]('%s/channels/voice/agents/%d/tickets/%d/display.json',_0x243fe9[_0xc80c('0x10')],_0x5e30ab['id'],_0x30f0b7['id']),'method':_0xc80c('0x11')});case _0xc80c('0x53'):logger[_0xc80c('0xd')]('[QUEUE]',_0xc80c('0x54'));emit(util[_0xc80c('0xe')](_0xc80c('0x55'),_0x204a32[_0xc80c('0x13')]),'trigger:browser:url',{'uri':util['format'](_0xc80c('0x56'),_0x243fe9['remoteUri'][_0xc80c('0x57')](_0xc80c('0x58'),''),_0x30f0b7['id'])});break;default:logger['error'](_0xc80c('0x48'),_0xc80c('0x59'));}}})['catch'](function(_0x162dbc){logger['error'](_0xc80c('0x5a'),util['inspect'](_0x162dbc,{'showHidden':!![],'depth':null}));});};exports['outbound']=function(_0x4ec8a9,_0x2c77fb,_0x3e2f66,_0x5e5367,_0x27b8d4,_0x1c545b,_0xd99678){var _0xac8bb3,_0x3bc2a2;_0x1c545b['remoteUri']=intUtil['stripTrailingSlash'](_0x1c545b[_0xc80c('0x10')]);_0x1c545b[_0xc80c('0x47')]=intUtil[_0xc80c('0x46')](_0x1c545b[_0xc80c('0x47')]);if(_0xd99678&&_0xd99678[_0xc80c('0x5b')]&&_0xd99678[_0xc80c('0x5b')]!=='none'){logger[_0xc80c('0xd')](_0xc80c('0x5c'),'Recording\x20is\x20enabled!');_0x2c77fb[_0xc80c('0x5d')]=util[_0xc80c('0xe')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x1c545b[_0xc80c('0x47')],_0x2c77fb[_0xc80c('0x4a')],md5(_0x2c77fb[_0xc80c('0x4a')]));}headers['Authorization']=getAuth(_0x1c545b);return getUser(_0x1c545b,_0x5e5367[_0xc80c('0x4c')])['then'](function(_0x2d15c4){_0xac8bb3=_0x2d15c4;logger[_0xc80c('0xd')](_0xc80c('0x4d'),_0xac8bb3['id']);return getEndUser(_0x1c545b,_0x2c77fb['destcalleridname'],_0x2c77fb['destcalleridnum']);})[_0xc80c('0x18')](function(_0x296fef){_0x3bc2a2=_0x296fef;logger[_0xc80c('0xd')](_0xc80c('0x5e'),_0x3bc2a2['id']);return getTicket(_0x1c545b,_0xac8bb3,_0x3bc2a2,_0x3e2f66,_0x2c77fb);})[_0xc80c('0x18')](function(_0xf1654){if(_0xf1654){logger[_0xc80c('0xd')](_0xc80c('0x50'),_0xf1654['id']);switch(_0x1c545b[_0xc80c('0x3a')]){case _0xc80c('0x51'):logger[_0xc80c('0xd')](_0xc80c('0x5c'),'Displaying\x20ticket\x20to\x20agent...');return request({'uri':util[_0xc80c('0xe')](_0xc80c('0x5f'),_0x1c545b[_0xc80c('0x10')],_0xac8bb3['id'],_0xf1654['id']),'method':_0xc80c('0x11')});case _0xc80c('0x53'):logger[_0xc80c('0xd')](_0xc80c('0x5c'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0xc80c('0xe')](_0xc80c('0x55'),_0x5e5367[_0xc80c('0x13')]),_0xc80c('0x60'),{'uri':util['format'](_0xc80c('0x56'),_0x1c545b['remoteUri']['replace'](_0xc80c('0x58'),''),_0xf1654['id'])});break;default:logger[_0xc80c('0x61')]('[OUTBOUND]',_0xc80c('0x59'));}}})['catch'](function(_0x485ea6){logger[_0xc80c('0x61')](_0xc80c('0x62'),util[_0xc80c('0x63')](_0x485ea6,{'showHidden':!![],'depth':null}));});};exports[_0xc80c('0x64')]=function(_0x47e04a,_0x2d17b1,_0x1bc557,_0x3e38bb,_0x5b906b,_0x2b7b46){var _0x747dd9,_0x533c61;_0x2b7b46[_0xc80c('0x10')]=intUtil['stripTrailingSlash'](_0x2b7b46[_0xc80c('0x10')]);_0x2b7b46[_0xc80c('0x47')]=intUtil['stripTrailingSlash'](_0x2b7b46['serverUrl']);if(_0x5b906b&&_0x5b906b['monitor_format']){logger['info']('['+_0x2d17b1['lastevent']['toUpperCase']()+']',_0xc80c('0x49'));_0x2d17b1[_0xc80c('0x5d')]=util[_0xc80c('0xe')](_0xc80c('0x65'),_0x2b7b46[_0xc80c('0x47')],_0x2d17b1[_0xc80c('0x4a')],md5(_0x2d17b1[_0xc80c('0x4a')]));}else{_0x2d17b1[_0xc80c('0x5d')]='';}headers[_0xc80c('0x4b')]=getAuth(_0x2b7b46);return getUser(_0x2b7b46)['then'](function(_0x3ecbc8){_0x747dd9=_0x3ecbc8;logger[_0xc80c('0xd')]('The\x20User\x20ID\x20is:',_0x747dd9['id']);return getEndUser(_0x2b7b46,_0x2d17b1[_0xc80c('0x4e')],_0x2d17b1[_0xc80c('0x4f')]);})[_0xc80c('0x18')](function(_0x3ceda0){_0x533c61=_0x3ceda0;logger['info'](_0xc80c('0x5e'),_0x533c61['id']);return getTicket(_0x2b7b46,_0x747dd9,_0x533c61,_0x1bc557,_0x2d17b1);})['then'](function(_0x7a9c61){if(_0x7a9c61){logger['info']('The\x20Ticket\x20ID\x20is:',_0x7a9c61['id']);}})[_0xc80c('0x66')](function(_0x364846){logger[_0xc80c('0x61')]('['+_0x2d17b1[_0xc80c('0x67')][_0xc80c('0x68')]()+']'+'\x20Error\x20running\x20Zendesk\x20integration',util['inspect'](_0x364846,{'showHidden':!![],'depth':null}));});};