Built motion from commit (unavailable).|2.4.0
[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 _0xff27=['Descriptions','then','Ticket\x20created!','ticket','Getting\x20agent\x20with\x20email','%s/search.json','type:user\x20role:agent\x20email:%s','count','Agent\x20found!','results','agent','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','first','end-user','authType','password','%s:%s','token','username','Basic\x20%s','toString','base64','debug','[HTTP]','stringify','defaults','GET','map','forEach','string','nameField','customField','toLowerCase','content','type','idField','push','variable','variableName','custom_fields','customVariable','Variable','name','queue','stripTrailingSlash','serverUrl','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','Authorization','The\x20User\x20ID\x20is:','calleridname','calleridnum','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','integrationTab','%s/channels/voice/agents/%d/tickets/%d/display.json','newTab','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','trigger:browser:url','%s/agent/tickets/%s','catch','error','[QUEUE]\x20Error\x20running\x20Zendesk\x20integration','inspect','outbound','recordingFormat','none','[OUTBOUND]','destcalleridname','user:%s','Zendesk\x20account\x20type\x20not\x20recognized','monitor_format','[ABANDONED]\x20Error\x20running\x20Zendesk\x20integration','lodash','request-promise','md5','./util','../../../../config/environment','zendesk','redis','localhost','socket.io-emitter','emit','application/json','info','format','%s/tickets.json','remoteUri','POST','Subjects','getString'];(function(_0x12ca5f,_0x223d98){var _0xfc3ae9=function(_0x1928fb){while(--_0x1928fb){_0x12ca5f['push'](_0x12ca5f['shift']());}};_0xfc3ae9(++_0x223d98);}(_0xff27,0x11d));var _0x7ff2=function(_0x2b4448,_0x5bcc69){_0x2b4448=_0x2b4448-0x0;var _0xb4b465=_0xff27[_0x2b4448];return _0xb4b465;};'use strict';var _=require(_0x7ff2('0x0'));var rp=require(_0x7ff2('0x1'));var md5=require(_0x7ff2('0x2'));var util=require('util');var Redis=require('ioredis');var intUtil=require(_0x7ff2('0x3'));var config=require(_0x7ff2('0x4'));var logger=require('../../../../config/logger')(_0x7ff2('0x5'));config[_0x7ff2('0x6')]=_['defaults'](config[_0x7ff2('0x6')],{'host':_0x7ff2('0x7'),'port':0x18eb});var io=require(_0x7ff2('0x8'))(new Redis(config[_0x7ff2('0x6')]));function emit(_0x9b6b5f,_0x405acd,_0x1c33cb){io['to'](_0x9b6b5f)[_0x7ff2('0x9')](_0x405acd,_0x1c33cb);}var headers={'Content-Type':_0x7ff2('0xa'),'Accept':_0x7ff2('0xa')};function getTicket(_0x19d0b7,_0x464474,_0x2a6a3b,_0x769f56,_0x329481){logger[_0x7ff2('0xb')]('Creating\x20new\x20ticket...');return request({'uri':util[_0x7ff2('0xc')](_0x7ff2('0xd'),_0x19d0b7[_0x7ff2('0xe')]),'method':_0x7ff2('0xf'),'body':{'ticket':_['merge']({'requester_id':_0x2a6a3b['id'],'submitter_id':_0x464474?_0x464474['id']:undefined,'assignee_id':_0x464474?_0x464474['id']:undefined,'tags':getTags(_0x769f56['Tags'],'name'),'subject':intUtil['getString'](_0x769f56[_0x7ff2('0x10')],_0x329481,'\x20'),'description':intUtil[_0x7ff2('0x11')](_0x769f56['Descriptions'],_0x329481,'\x0a'),'comment':{'public':![],'body':intUtil[_0x7ff2('0x11')](_0x769f56[_0x7ff2('0x12')],_0x329481,'\x0a')}},getCustomFields(_0x769f56['Fields'],_0x329481))}})[_0x7ff2('0x13')](function(_0x55c177){logger[_0x7ff2('0xb')](_0x7ff2('0x14'));return _0x55c177[_0x7ff2('0x15')];});}function getUser(_0x9a7e64,_0x2a9fdd){if(intUtil['validateEmail'](_0x2a9fdd)){logger[_0x7ff2('0xb')](_0x7ff2('0x16'),_0x2a9fdd);return request({'uri':util[_0x7ff2('0xc')](_0x7ff2('0x17'),_0x9a7e64[_0x7ff2('0xe')]),'qs':{'query':util['format'](_0x7ff2('0x18'),_0x2a9fdd)}})[_0x7ff2('0x13')](function(_0x284717){if(_0x284717[_0x7ff2('0x19')]){logger['info'](_0x7ff2('0x1a'));return _['find'](_0x284717[_0x7ff2('0x1b')],{'email':_0x2a9fdd,'role':_0x7ff2('0x1c')});}else{logger['info'](_0x7ff2('0x1d'));return request({'uri':util['format'](_0x7ff2('0x1e'),_0x9a7e64['remoteUri'])})[_0x7ff2('0x13')](function(_0x46453c){logger['info'](_0x7ff2('0x1f'));return _0x46453c['user'];});}});}else{logger['info'](_0x7ff2('0x1d'));return request({'uri':util[_0x7ff2('0xc')](_0x7ff2('0x1e'),_0x9a7e64[_0x7ff2('0xe')])})['then'](function(_0x42bdef){logger[_0x7ff2('0xb')](_0x7ff2('0x1f'));return _0x42bdef[_0x7ff2('0x20')];});}}function getEndUser(_0x16f862,_0x485470,_0x34974c){logger[_0x7ff2('0xb')](_0x7ff2('0x21'),_0x34974c);return request({'uri':util[_0x7ff2('0xc')](_0x7ff2('0x17'),_0x16f862[_0x7ff2('0xe')]),'qs':{'query':util[_0x7ff2('0xc')](_0x7ff2('0x22'),_0x34974c)}})[_0x7ff2('0x13')](function(_0x57fd58){var _0x25f46a;if(_0x57fd58[_0x7ff2('0x1b')][_0x7ff2('0x23')]){_0x57fd58[_0x7ff2('0x1b')]=_[_0x7ff2('0x24')](_0x57fd58[_0x7ff2('0x1b')],{'suspended':![],'role':'end-user'});if(_0x57fd58['results'][_0x7ff2('0x23')]){_0x25f46a=_[_0x7ff2('0x25')](_0x57fd58[_0x7ff2('0x1b')]);}}if(_0x25f46a){logger['info']('Enduser\x20found!');return _0x25f46a;}else{logger[_0x7ff2('0xb')]('Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!');return request({'uri':util[_0x7ff2('0xc')]('%s/users.json',_0x16f862[_0x7ff2('0xe')]),'method':_0x7ff2('0xf'),'body':{'user':{'role':_0x7ff2('0x26'),'name':util[_0x7ff2('0xc')]('Caller\x20-\x20%s',_0x485470||_0x34974c),'phone':_0x34974c}}})[_0x7ff2('0x13')](function(_0x2fcae1){logger[_0x7ff2('0xb')]('Enduser\x20created.');return _0x2fcae1[_0x7ff2('0x20')];});}});}function getAuth(_0x2f7a68){var _0x5c782b;if(_0x2f7a68[_0x7ff2('0x27')]===_0x7ff2('0x28')){_0x5c782b=util[_0x7ff2('0xc')](_0x7ff2('0x29'),_0x2f7a68['username'],_0x2f7a68[_0x7ff2('0x28')]);}else if(_0x2f7a68[_0x7ff2('0x27')]===_0x7ff2('0x2a')){_0x5c782b=util['format']('%s/token:%s',_0x2f7a68[_0x7ff2('0x2b')],_0x2f7a68[_0x7ff2('0x2a')]);}return util[_0x7ff2('0xc')](_0x7ff2('0x2c'),new Buffer(_0x5c782b)[_0x7ff2('0x2d')](_0x7ff2('0x2e')));}function request(_0x5e1e34){logger[_0x7ff2('0x2f')](_0x7ff2('0x30'),JSON[_0x7ff2('0x31')](_0x5e1e34));return rp(_[_0x7ff2('0x32')](_0x5e1e34,{'method':_0x7ff2('0x33'),'headers':headers,'json':!![]}));}function getTags(_0x3f8374,_0x3ac91e){var _0x4c0a0c=[];if(_0x3f8374&&_0x3f8374[_0x7ff2('0x23')]>0x0){_0x4c0a0c=_[_0x7ff2('0x34')](_0x3f8374,_0x3ac91e);}return _0x4c0a0c;}function getCustomFields(_0x33e862,_0x598e50){var _0x3442a0=[];var _0xcdaf6e={'custom_fields':[]};if(_0x33e862[_0x7ff2('0x23')]>0x0){_[_0x7ff2('0x35')](_0x33e862,function(_0x2229e2){if(_0x2229e2['type']===_0x7ff2('0x36')&&_0x2229e2[_0x7ff2('0x37')]&&!_0x2229e2[_0x7ff2('0x38')]){_0xcdaf6e[_0x2229e2[_0x7ff2('0x37')][_0x7ff2('0x39')]()]=_0x2229e2[_0x7ff2('0x3a')]['toString']();}else if(_0x2229e2[_0x7ff2('0x3b')]===_0x7ff2('0x36')&&_0x2229e2[_0x7ff2('0x3c')]&&_0x2229e2[_0x7ff2('0x38')]){_0xcdaf6e['custom_fields'][_0x7ff2('0x3d')]({'id':_0x2229e2[_0x7ff2('0x3c')],'value':_0x2229e2[_0x7ff2('0x3a')][_0x7ff2('0x2d')]()});}else if(_0x2229e2[_0x7ff2('0x3b')]===_0x7ff2('0x3e')&&_0x2229e2['variableName']&&_0x2229e2['idField']&&_0x2229e2[_0x7ff2('0x38')]){_0x598e50[_0x2229e2[_0x7ff2('0x3f')]]&&_0xcdaf6e[_0x7ff2('0x40')][_0x7ff2('0x3d')]({'id':_0x2229e2['idField'],'value':_0x598e50[_0x2229e2['variableName']][_0x7ff2('0x2d')]()});}else if(_0x2229e2[_0x7ff2('0x3b')]===_0x7ff2('0x41')&&_0x2229e2[_0x7ff2('0x42')]&&_0x2229e2[_0x7ff2('0x42')][_0x7ff2('0x43')]&&_0x2229e2[_0x7ff2('0x3c')]&&_0x2229e2['customField']){_0x598e50[_0x2229e2[_0x7ff2('0x42')][_0x7ff2('0x43')]['toLowerCase']()]&&_0xcdaf6e[_0x7ff2('0x40')]['push']({'id':_0x2229e2[_0x7ff2('0x3c')],'value':_0x598e50[_0x2229e2[_0x7ff2('0x42')][_0x7ff2('0x43')][_0x7ff2('0x39')]()]['toString']()});}});}return _0xcdaf6e;}exports[_0x7ff2('0x44')]=function(_0x43bd54,_0x124bbe,_0x5e5634,_0x4fbabc,_0x29bf03,_0x5bdd02){var _0xd7091,_0x3e876b;_0x5bdd02[_0x7ff2('0xe')]=intUtil[_0x7ff2('0x45')](_0x5bdd02[_0x7ff2('0xe')]);_0x5bdd02[_0x7ff2('0x46')]=intUtil[_0x7ff2('0x45')](_0x5bdd02[_0x7ff2('0x46')]);if(_0x29bf03&&_0x29bf03['monitor_format']){logger[_0x7ff2('0xb')](_0x7ff2('0x47'),_0x7ff2('0x48'));_0x124bbe[_0x7ff2('0x49')]=util['format'](_0x7ff2('0x4a'),_0x5bdd02[_0x7ff2('0x46')],_0x124bbe['uniqueid'],md5(_0x124bbe[_0x7ff2('0x4b')]));}else{_0x124bbe[_0x7ff2('0x49')]='';}headers[_0x7ff2('0x4c')]=getAuth(_0x5bdd02);return getUser(_0x5bdd02,_0x4fbabc['email'])[_0x7ff2('0x13')](function(_0x5e20e2){_0xd7091=_0x5e20e2;logger[_0x7ff2('0xb')](_0x7ff2('0x4d'),_0xd7091['id']);return getEndUser(_0x5bdd02,_0x124bbe[_0x7ff2('0x4e')],_0x124bbe[_0x7ff2('0x4f')]);})['then'](function(_0x5a982f){_0x3e876b=_0x5a982f;logger[_0x7ff2('0xb')](_0x7ff2('0x50'),_0x3e876b['id']);return getTicket(_0x5bdd02,_0xd7091,_0x3e876b,_0x5e5634,_0x124bbe);})[_0x7ff2('0x13')](function(_0x555cd8){if(_0x555cd8){logger[_0x7ff2('0xb')](_0x7ff2('0x51'),_0x555cd8['id']);switch(_0x5bdd02[_0x7ff2('0x3b')]){case _0x7ff2('0x52'):logger[_0x7ff2('0xb')](_0x7ff2('0x47'),'Displaying\x20ticket\x20to\x20agent...');return request({'uri':util[_0x7ff2('0xc')](_0x7ff2('0x53'),_0x5bdd02[_0x7ff2('0xe')],_0xd7091['id'],_0x555cd8['id']),'method':_0x7ff2('0xf')});case _0x7ff2('0x54'):logger[_0x7ff2('0xb')](_0x7ff2('0x47'),_0x7ff2('0x55'));emit(util[_0x7ff2('0xc')]('user:%s',_0x4fbabc[_0x7ff2('0x43')]),_0x7ff2('0x56'),{'uri':util[_0x7ff2('0xc')](_0x7ff2('0x57'),_0x5bdd02[_0x7ff2('0xe')],_0x555cd8['id'])});break;default:logger['error'](_0x7ff2('0x47'),'Zendesk\x20account\x20type\x20not\x20recognized');}}})[_0x7ff2('0x58')](function(_0x3bbe47){logger[_0x7ff2('0x59')](_0x7ff2('0x5a'),util[_0x7ff2('0x5b')](_0x3bbe47,{'showHidden':!![],'depth':null}));});};exports[_0x7ff2('0x5c')]=function(_0x3f8656,_0x411b0f,_0x32994e,_0xe750a5,_0x51afa9,_0xb60146,_0x1e4846){var _0x566c40,_0x5077b9;_0xb60146[_0x7ff2('0xe')]=intUtil[_0x7ff2('0x45')](_0xb60146[_0x7ff2('0xe')]);_0xb60146[_0x7ff2('0x46')]=intUtil[_0x7ff2('0x45')](_0xb60146[_0x7ff2('0x46')]);if(_0x1e4846&&_0x1e4846[_0x7ff2('0x5d')]&&_0x1e4846[_0x7ff2('0x5d')]!==_0x7ff2('0x5e')){logger[_0x7ff2('0xb')](_0x7ff2('0x5f'),_0x7ff2('0x48'));_0x411b0f[_0x7ff2('0x49')]=util[_0x7ff2('0xc')](_0x7ff2('0x4a'),_0xb60146['serverUrl'],_0x411b0f[_0x7ff2('0x4b')],md5(_0x411b0f[_0x7ff2('0x4b')]));}headers[_0x7ff2('0x4c')]=getAuth(_0xb60146);return getUser(_0xb60146,_0xe750a5['email'])['then'](function(_0x5d3487){_0x566c40=_0x5d3487;logger[_0x7ff2('0xb')]('The\x20User\x20ID\x20is:',_0x566c40['id']);return getEndUser(_0xb60146,_0x411b0f[_0x7ff2('0x60')],_0x411b0f['destcalleridnum']);})[_0x7ff2('0x13')](function(_0x4e4978){_0x5077b9=_0x4e4978;logger[_0x7ff2('0xb')]('The\x20Enduser\x20ID\x20is:',_0x5077b9['id']);return getTicket(_0xb60146,_0x566c40,_0x5077b9,_0x32994e,_0x411b0f);})[_0x7ff2('0x13')](function(_0x1806ae){if(_0x1806ae){logger[_0x7ff2('0xb')](_0x7ff2('0x51'),_0x1806ae['id']);switch(_0xb60146['type']){case'integrationTab':logger['info'](_0x7ff2('0x5f'),'Displaying\x20ticket\x20to\x20agent...');return request({'uri':util[_0x7ff2('0xc')](_0x7ff2('0x53'),_0xb60146[_0x7ff2('0xe')],_0x566c40['id'],_0x1806ae['id']),'method':_0x7ff2('0xf')});case _0x7ff2('0x54'):logger['info']('[OUTBOUND]',_0x7ff2('0x55'));emit(util['format'](_0x7ff2('0x61'),_0xe750a5[_0x7ff2('0x43')]),_0x7ff2('0x56'),{'uri':util[_0x7ff2('0xc')](_0x7ff2('0x57'),_0xb60146[_0x7ff2('0xe')],_0x1806ae['id'])});break;default:logger['error'](_0x7ff2('0x5f'),_0x7ff2('0x62'));}}})['catch'](function(_0x32c93e){logger[_0x7ff2('0x59')]('[OUTBOUND]\x20Error\x20running\x20Zendesk\x20integration',util[_0x7ff2('0x5b')](_0x32c93e,{'showHidden':!![],'depth':null}));});};exports['abandoned']=function(_0x479be6,_0x21e979,_0x35b67c,_0x4bd921,_0x474f2d,_0x36b302){var _0x5c0546,_0x35fc96;_0x36b302[_0x7ff2('0xe')]=intUtil[_0x7ff2('0x45')](_0x36b302[_0x7ff2('0xe')]);_0x36b302[_0x7ff2('0x46')]=intUtil['stripTrailingSlash'](_0x36b302[_0x7ff2('0x46')]);if(_0x474f2d&&_0x474f2d[_0x7ff2('0x63')]){logger[_0x7ff2('0xb')]('[ABANDONED]',_0x7ff2('0x48'));_0x21e979[_0x7ff2('0x49')]=util['format'](_0x7ff2('0x4a'),_0x36b302[_0x7ff2('0x46')],_0x21e979[_0x7ff2('0x4b')],md5(_0x21e979[_0x7ff2('0x4b')]));}else{_0x21e979[_0x7ff2('0x49')]='';}headers[_0x7ff2('0x4c')]=getAuth(_0x36b302);return getUser(_0x36b302)[_0x7ff2('0x13')](function(_0x373d36){_0x5c0546=_0x373d36;logger[_0x7ff2('0xb')]('The\x20User\x20ID\x20is:',_0x5c0546['id']);return getEndUser(_0x36b302,_0x21e979[_0x7ff2('0x4e')],_0x21e979[_0x7ff2('0x4f')]);})[_0x7ff2('0x13')](function(_0x497ab5){_0x35fc96=_0x497ab5;logger[_0x7ff2('0xb')](_0x7ff2('0x50'),_0x35fc96['id']);return getTicket(_0x36b302,_0x5c0546,_0x35fc96,_0x35b67c,_0x21e979);})[_0x7ff2('0x13')](function(_0x4bcab7){if(_0x4bcab7){logger['info']('The\x20Ticket\x20ID\x20is:',_0x4bcab7['id']);}})['catch'](function(_0x51010b){logger[_0x7ff2('0x59')](_0x7ff2('0x64'),util['inspect'](_0x51010b,{'showHidden':!![],'depth':null}));});};