Built motion from commit f7863d46.|2.5.41
[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 _0xc46b=['toLowerCase','string','push','variable','variableName','idField','customVariable','Variable','custom_fields','stripTrailingSlash','serverUrl','monitor_format','Recording\x20is\x20enabled!','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','Authorization','email','The\x20User\x20ID\x20is:','calleridname','calleridnum','The\x20Enduser\x20ID\x20is:','integrationTab','[QUEUE]','Displaying\x20ticket\x20to\x20agent...','user:%s','trigger:browser:url','%s/agent/tickets/%s','replace','error','Zendesk\x20account\x20type\x20not\x20recognized','[QUEUE]\x20Error\x20running\x20Zendesk\x20integration','inspect','outbound','recordingFormat','none','recordingURL','destcalleridname','The\x20Ticket\x20ID\x20is:','[OUTBOUND]','%s/channels/voice/agents/%d/tickets/%d/display.json','newTab','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','/api/v2','catch','[OUTBOUND]\x20Error\x20running\x20Zendesk\x20integration','lastevent','toUpperCase','\x20Error\x20running\x20Zendesk\x20integration','lodash','request-promise','util','ioredis','../../../../config/environment','../../../../config/logger','zendesk','redis','defaults','localhost','socket.io-emitter','emit','application/json','info','format','%s/tickets.json','POST','merge','Tags','name','getString','Subjects','Descriptions','then','ticket','validateEmail','Getting\x20agent\x20with\x20email','%s/search.json','remoteUri','type:user\x20role:agent\x20email:%s','count','Agent\x20found!','find','results','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/users/me.json','Admin\x20account\x20selected.','user','Searching\x20enduser\x20with\x20phone','type:user\x20role:end-user\x20phone:%s','length','filter','Enduser\x20found!','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Caller\x20-\x20%s','Enduser\x20created.','authType','password','%s:%s','username','token','%s/token:%s','Basic\x20%s','toString','base64','debug','[HTTP]','stringify','GET','map','forEach','includes','picklist','type','nameField','customField'];(function(_0xbc7d4d,_0x1ea9cf){var _0x45cc9a=function(_0x3603ca){while(--_0x3603ca){_0xbc7d4d['push'](_0xbc7d4d['shift']());}};_0x45cc9a(++_0x1ea9cf);}(_0xc46b,0xa2));var _0xbc46=function(_0x550eaf,_0x42447d){_0x550eaf=_0x550eaf-0x0;var _0x2ffe90=_0xc46b[_0x550eaf];return _0x2ffe90;};'use strict';var _=require(_0xbc46('0x0'));var rp=require(_0xbc46('0x1'));var md5=require('md5');var util=require(_0xbc46('0x2'));var Redis=require(_0xbc46('0x3'));var intUtil=require('./util');var config=require(_0xbc46('0x4'));var logger=require(_0xbc46('0x5'))(_0xbc46('0x6'));config[_0xbc46('0x7')]=_[_0xbc46('0x8')](config['redis'],{'host':_0xbc46('0x9'),'port':0x18eb});var io=require(_0xbc46('0xa'))(new Redis(config[_0xbc46('0x7')]));function emit(_0x26ec89,_0x637b24,_0x3f35cc){io['to'](_0x26ec89)[_0xbc46('0xb')](_0x637b24,_0x3f35cc);}var headers={'Content-Type':_0xbc46('0xc'),'Accept':_0xbc46('0xc')};function getTicket(_0x211a4d,_0x4d3add,_0x41cc44,_0x52136f,_0x151e38){logger[_0xbc46('0xd')]('Creating\x20new\x20ticket...');return request({'uri':util[_0xbc46('0xe')](_0xbc46('0xf'),_0x211a4d['remoteUri']),'method':_0xbc46('0x10'),'body':{'ticket':_[_0xbc46('0x11')]({'requester_id':_0x41cc44['id'],'submitter_id':_0x4d3add?_0x4d3add['id']:undefined,'assignee_id':_0x4d3add?_0x4d3add['id']:undefined,'tags':getTags(_0x52136f[_0xbc46('0x12')],_0xbc46('0x13')),'subject':intUtil[_0xbc46('0x14')](_0x52136f[_0xbc46('0x15')],_0x151e38,'\x20'),'description':intUtil['getString'](_0x52136f[_0xbc46('0x16')],_0x151e38,'\x0a'),'comment':{'public':![],'body':intUtil[_0xbc46('0x14')](_0x52136f[_0xbc46('0x16')],_0x151e38,'\x0a')}},getCustomFields(_0x52136f['Fields'],_0x151e38))}})[_0xbc46('0x17')](function(_0x2cd2e9){logger[_0xbc46('0xd')]('Ticket\x20created!');return _0x2cd2e9[_0xbc46('0x18')];});}function getUser(_0x1b27ed,_0x228251){if(intUtil[_0xbc46('0x19')](_0x228251)){logger[_0xbc46('0xd')](_0xbc46('0x1a'),_0x228251);return request({'uri':util[_0xbc46('0xe')](_0xbc46('0x1b'),_0x1b27ed[_0xbc46('0x1c')]),'qs':{'query':util[_0xbc46('0xe')](_0xbc46('0x1d'),_0x228251)}})[_0xbc46('0x17')](function(_0x3e584e){if(_0x3e584e[_0xbc46('0x1e')]){logger['info'](_0xbc46('0x1f'));return _[_0xbc46('0x20')](_0x3e584e[_0xbc46('0x21')],{'email':_0x228251,'role':'agent'});}else{logger[_0xbc46('0xd')](_0xbc46('0x22'));return request({'uri':util['format'](_0xbc46('0x23'),_0x1b27ed[_0xbc46('0x1c')])})[_0xbc46('0x17')](function(_0x2d2f86){logger[_0xbc46('0xd')](_0xbc46('0x24'));return _0x2d2f86[_0xbc46('0x25')];});}});}else{logger[_0xbc46('0xd')](_0xbc46('0x22'));return request({'uri':util[_0xbc46('0xe')]('%s/users/me.json',_0x1b27ed[_0xbc46('0x1c')])})[_0xbc46('0x17')](function(_0x52d1f5){logger[_0xbc46('0xd')](_0xbc46('0x24'));return _0x52d1f5[_0xbc46('0x25')];});}}function getEndUser(_0x1489fc,_0x1cf8ab,_0x3be542){logger[_0xbc46('0xd')](_0xbc46('0x26'),_0x3be542);return request({'uri':util[_0xbc46('0xe')](_0xbc46('0x1b'),_0x1489fc[_0xbc46('0x1c')]),'qs':{'query':util[_0xbc46('0xe')](_0xbc46('0x27'),_0x3be542)}})[_0xbc46('0x17')](function(_0x5b9e5c){var _0x3fd69e;if(_0x5b9e5c[_0xbc46('0x21')][_0xbc46('0x28')]){_0x5b9e5c[_0xbc46('0x21')]=_[_0xbc46('0x29')](_0x5b9e5c[_0xbc46('0x21')],{'suspended':![],'role':'end-user'});if(_0x5b9e5c[_0xbc46('0x21')][_0xbc46('0x28')]){_0x3fd69e=_['first'](_0x5b9e5c['results']);}}if(_0x3fd69e){logger[_0xbc46('0xd')](_0xbc46('0x2a'));return _0x3fd69e;}else{logger[_0xbc46('0xd')](_0xbc46('0x2b'));return request({'uri':util[_0xbc46('0xe')]('%s/users.json',_0x1489fc[_0xbc46('0x1c')]),'method':_0xbc46('0x10'),'body':{'user':{'role':'end-user','name':util[_0xbc46('0xe')](_0xbc46('0x2c'),_0x1cf8ab||_0x3be542),'phone':_0x3be542}}})[_0xbc46('0x17')](function(_0x44208f){logger[_0xbc46('0xd')](_0xbc46('0x2d'));return _0x44208f[_0xbc46('0x25')];});}});}function getAuth(_0x1bbb35){var _0x5e7988;if(_0x1bbb35[_0xbc46('0x2e')]===_0xbc46('0x2f')){_0x5e7988=util[_0xbc46('0xe')](_0xbc46('0x30'),_0x1bbb35[_0xbc46('0x31')],_0x1bbb35['password']);}else if(_0x1bbb35['authType']===_0xbc46('0x32')){_0x5e7988=util[_0xbc46('0xe')](_0xbc46('0x33'),_0x1bbb35['username'],_0x1bbb35[_0xbc46('0x32')]);}return util[_0xbc46('0xe')](_0xbc46('0x34'),new Buffer(_0x5e7988)[_0xbc46('0x35')](_0xbc46('0x36')));}function request(_0x5b03d0){logger[_0xbc46('0x37')](_0xbc46('0x38'),JSON[_0xbc46('0x39')](_0x5b03d0));return rp(_[_0xbc46('0x8')](_0x5b03d0,{'method':_0xbc46('0x3a'),'headers':headers,'json':!![]}));}function getTags(_0x3adbef,_0x279395){var _0x27331f=[];if(_0x3adbef&&_0x3adbef[_0xbc46('0x28')]>0x0){_0x27331f=_[_0xbc46('0x3b')](_0x3adbef,_0x279395);}return _0x27331f;}function getCustomFields(_0x5ad12b,_0xf734ab){var _0x3e7c1b=[];var _0x441179={'custom_fields':[]};if(_0x5ad12b[_0xbc46('0x28')]>0x0){_[_0xbc46('0x3c')](_0x5ad12b,function(_0x4fbbdb){if(_[_0xbc46('0x3d')](['string',_0xbc46('0x3e')],_0x4fbbdb[_0xbc46('0x3f')])&&_0x4fbbdb[_0xbc46('0x40')]&&!_0x4fbbdb[_0xbc46('0x41')]){_0x441179[_0x4fbbdb[_0xbc46('0x40')][_0xbc46('0x42')]()]=_0x4fbbdb['content'][_0xbc46('0x35')]();}else if(_[_0xbc46('0x3d')]([_0xbc46('0x43'),_0xbc46('0x3e')],_0x4fbbdb[_0xbc46('0x3f')])&&_0x4fbbdb['idField']&&_0x4fbbdb[_0xbc46('0x41')]){_0x441179['custom_fields'][_0xbc46('0x44')]({'id':_0x4fbbdb['idField'],'value':_0x4fbbdb['content'][_0xbc46('0x35')]()});}else if(_0x4fbbdb[_0xbc46('0x3f')]===_0xbc46('0x45')&&_0x4fbbdb[_0xbc46('0x46')]&&_0x4fbbdb[_0xbc46('0x47')]&&_0x4fbbdb[_0xbc46('0x41')]){_0xf734ab[_0x4fbbdb[_0xbc46('0x46')]]&&_0x441179['custom_fields'][_0xbc46('0x44')]({'id':_0x4fbbdb['idField'],'value':_0xf734ab[_0x4fbbdb['variableName']][_0xbc46('0x35')]()});}else if(_0x4fbbdb[_0xbc46('0x3f')]===_0xbc46('0x48')&&_0x4fbbdb[_0xbc46('0x49')]&&_0x4fbbdb[_0xbc46('0x49')][_0xbc46('0x13')]&&_0x4fbbdb[_0xbc46('0x47')]&&_0x4fbbdb[_0xbc46('0x41')]){_0xf734ab[_0x4fbbdb['Variable'][_0xbc46('0x13')][_0xbc46('0x42')]()]&&_0x441179[_0xbc46('0x4a')][_0xbc46('0x44')]({'id':_0x4fbbdb[_0xbc46('0x47')],'value':_0xf734ab[_0x4fbbdb[_0xbc46('0x49')][_0xbc46('0x13')]['toLowerCase']()][_0xbc46('0x35')]()});}});}return _0x441179;}exports['queue']=function(_0x4682c8,_0x12287e,_0x5562bc,_0x74ee73,_0x47e19f,_0x4c3e4c){var _0x5929ff,_0x16cb56;_0x4c3e4c[_0xbc46('0x1c')]=intUtil[_0xbc46('0x4b')](_0x4c3e4c[_0xbc46('0x1c')]);_0x4c3e4c[_0xbc46('0x4c')]=intUtil[_0xbc46('0x4b')](_0x4c3e4c[_0xbc46('0x4c')]);if(_0x47e19f&&_0x47e19f[_0xbc46('0x4d')]){logger[_0xbc46('0xd')]('[QUEUE]',_0xbc46('0x4e'));_0x12287e['recordingURL']=util['format'](_0xbc46('0x4f'),_0x4c3e4c['serverUrl'],_0x12287e['uniqueid'],md5(_0x12287e[_0xbc46('0x50')]));}else{_0x12287e['recordingURL']='';}headers[_0xbc46('0x51')]=getAuth(_0x4c3e4c);return getUser(_0x4c3e4c,_0x74ee73[_0xbc46('0x52')])[_0xbc46('0x17')](function(_0x19ad57){_0x5929ff=_0x19ad57;logger[_0xbc46('0xd')](_0xbc46('0x53'),_0x5929ff['id']);return getEndUser(_0x4c3e4c,_0x12287e[_0xbc46('0x54')],_0x12287e[_0xbc46('0x55')]);})[_0xbc46('0x17')](function(_0xe23576){_0x16cb56=_0xe23576;logger['info'](_0xbc46('0x56'),_0x16cb56['id']);return getTicket(_0x4c3e4c,_0x5929ff,_0x16cb56,_0x5562bc,_0x12287e);})[_0xbc46('0x17')](function(_0x11bffb){if(_0x11bffb){logger[_0xbc46('0xd')]('The\x20Ticket\x20ID\x20is:',_0x11bffb['id']);switch(_0x4c3e4c[_0xbc46('0x3f')]){case _0xbc46('0x57'):logger[_0xbc46('0xd')](_0xbc46('0x58'),_0xbc46('0x59'));return request({'uri':util[_0xbc46('0xe')]('%s/channels/voice/agents/%d/tickets/%d/display.json',_0x4c3e4c[_0xbc46('0x1c')],_0x5929ff['id'],_0x11bffb['id']),'method':_0xbc46('0x10')});case'newTab':logger['info'](_0xbc46('0x58'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0xbc46('0xe')](_0xbc46('0x5a'),_0x74ee73[_0xbc46('0x13')]),_0xbc46('0x5b'),{'uri':util[_0xbc46('0xe')](_0xbc46('0x5c'),_0x4c3e4c[_0xbc46('0x1c')][_0xbc46('0x5d')]('/api/v2',''),_0x11bffb['id'])});break;default:logger[_0xbc46('0x5e')](_0xbc46('0x58'),_0xbc46('0x5f'));}}})['catch'](function(_0x25ba98){logger[_0xbc46('0x5e')](_0xbc46('0x60'),util[_0xbc46('0x61')](_0x25ba98,{'showHidden':!![],'depth':null}));});};exports[_0xbc46('0x62')]=function(_0x459d01,_0x416ebe,_0x324a37,_0x41924f,_0x441e00,_0x32bda3,_0x2b568f){var _0x534cd7,_0xdb5332;_0x32bda3['remoteUri']=intUtil[_0xbc46('0x4b')](_0x32bda3[_0xbc46('0x1c')]);_0x32bda3[_0xbc46('0x4c')]=intUtil[_0xbc46('0x4b')](_0x32bda3[_0xbc46('0x4c')]);if(_0x2b568f&&_0x2b568f[_0xbc46('0x63')]&&_0x2b568f[_0xbc46('0x63')]!==_0xbc46('0x64')){logger[_0xbc46('0xd')]('[OUTBOUND]',_0xbc46('0x4e'));_0x416ebe[_0xbc46('0x65')]=util[_0xbc46('0xe')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x32bda3[_0xbc46('0x4c')],_0x416ebe['uniqueid'],md5(_0x416ebe['uniqueid']));}headers[_0xbc46('0x51')]=getAuth(_0x32bda3);return getUser(_0x32bda3,_0x41924f[_0xbc46('0x52')])[_0xbc46('0x17')](function(_0x400324){_0x534cd7=_0x400324;logger['info']('The\x20User\x20ID\x20is:',_0x534cd7['id']);return getEndUser(_0x32bda3,_0x416ebe[_0xbc46('0x66')],_0x416ebe['destcalleridnum']);})[_0xbc46('0x17')](function(_0x8654e0){_0xdb5332=_0x8654e0;logger[_0xbc46('0xd')](_0xbc46('0x56'),_0xdb5332['id']);return getTicket(_0x32bda3,_0x534cd7,_0xdb5332,_0x324a37,_0x416ebe);})[_0xbc46('0x17')](function(_0x443b27){if(_0x443b27){logger['info'](_0xbc46('0x67'),_0x443b27['id']);switch(_0x32bda3[_0xbc46('0x3f')]){case'integrationTab':logger[_0xbc46('0xd')](_0xbc46('0x68'),_0xbc46('0x59'));return request({'uri':util[_0xbc46('0xe')](_0xbc46('0x69'),_0x32bda3[_0xbc46('0x1c')],_0x534cd7['id'],_0x443b27['id']),'method':'POST'});case _0xbc46('0x6a'):logger[_0xbc46('0xd')](_0xbc46('0x68'),_0xbc46('0x6b'));emit(util[_0xbc46('0xe')](_0xbc46('0x5a'),_0x41924f[_0xbc46('0x13')]),_0xbc46('0x5b'),{'uri':util[_0xbc46('0xe')](_0xbc46('0x5c'),_0x32bda3[_0xbc46('0x1c')][_0xbc46('0x5d')](_0xbc46('0x6c'),''),_0x443b27['id'])});break;default:logger[_0xbc46('0x5e')]('[OUTBOUND]','Zendesk\x20account\x20type\x20not\x20recognized');}}})[_0xbc46('0x6d')](function(_0x2b06b5){logger[_0xbc46('0x5e')](_0xbc46('0x6e'),util[_0xbc46('0x61')](_0x2b06b5,{'showHidden':!![],'depth':null}));});};exports['unmanaged']=function(_0x15f867,_0x27e44f,_0x185fcd,_0x2f642,_0x57323e,_0x262b75){var _0x5e70f4,_0x2b67ea;_0x262b75[_0xbc46('0x1c')]=intUtil[_0xbc46('0x4b')](_0x262b75[_0xbc46('0x1c')]);_0x262b75[_0xbc46('0x4c')]=intUtil['stripTrailingSlash'](_0x262b75[_0xbc46('0x4c')]);if(_0x57323e&&_0x57323e['monitor_format']){logger['info']('['+_0x27e44f[_0xbc46('0x6f')][_0xbc46('0x70')]()+']','Recording\x20is\x20enabled!');_0x27e44f[_0xbc46('0x65')]=util[_0xbc46('0xe')](_0xbc46('0x4f'),_0x262b75[_0xbc46('0x4c')],_0x27e44f[_0xbc46('0x50')],md5(_0x27e44f[_0xbc46('0x50')]));}else{_0x27e44f['recordingURL']='';}headers[_0xbc46('0x51')]=getAuth(_0x262b75);return getUser(_0x262b75)['then'](function(_0x1e28a8){_0x5e70f4=_0x1e28a8;logger[_0xbc46('0xd')](_0xbc46('0x53'),_0x5e70f4['id']);return getEndUser(_0x262b75,_0x27e44f[_0xbc46('0x54')],_0x27e44f[_0xbc46('0x55')]);})[_0xbc46('0x17')](function(_0x19093a){_0x2b67ea=_0x19093a;logger[_0xbc46('0xd')](_0xbc46('0x56'),_0x2b67ea['id']);return getTicket(_0x262b75,_0x5e70f4,_0x2b67ea,_0x185fcd,_0x27e44f);})['then'](function(_0x21e72c){if(_0x21e72c){logger[_0xbc46('0xd')]('The\x20Ticket\x20ID\x20is:',_0x21e72c['id']);}})[_0xbc46('0x6d')](function(_0x364a73){logger[_0xbc46('0x5e')]('['+_0x27e44f[_0xbc46('0x6f')][_0xbc46('0x70')]()+']'+_0xbc46('0x71'),util['inspect'](_0x364a73,{'showHidden':!![],'depth':null}));});};