Built motion from commit (unavailable).|2.2.0
[motion2.git] / server / services / ami / trigger / integration / desk.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 _0x326f=['[ABANDONED]','lodash','util','ioredis','./util','../../../../config/environment','../../../../config/logger','desk','redis','defaults','localhost','info','Getting\x20agent\x20with\x20email','format','remoteUri','find','entries','then','Agent\x20found!','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/users/me','debug','[HTTP]','stringify','GET','authType','%s:%s','username','password','token','base64','%s/customers/search','total_entries','Enduser\x20found!','_embedded','%s/customers','POST','work','length','map','forEach','type','nameField','customField','toLowerCase','content','toString','string','idField','custom_fields','variable','variableName','customVariable','Variable','name','priority','Creating\x20new\x20ticket...','merge','getString','Subjects','phone','xCALLY','Motion','CTI','concat','Tags','Descriptions','user','Fields','%s/customers/%d/cases','queue','stripTrailingSlash','serverUrl','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','uniqueid','Authorization','email','calleridname','calleridnum','The\x20Enduser\x20ID\x20is:','Ticket\x20created!','The\x20Ticket\x20ID\x20is:','integrationTab','Displaying\x20ticket\x20to\x20agent...','user:%s','trigger:desk:display','newTab','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','https://xcally.desk.com/web/agent/case/%d','error','catch','recordingFormat','none','[OUTBOUND]','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','The\x20User\x20ID\x20is:','destcalleridname','destcalleridnum','trigger:browser:url','Desk\x20account\x20type\x20not\x20recognized','abandoned'];(function(_0x20a584,_0xe384a5){var _0x4b41a8=function(_0x2127c6){while(--_0x2127c6){_0x20a584['push'](_0x20a584['shift']());}};_0x4b41a8(++_0xe384a5);}(_0x326f,0xd1));var _0xf326=function(_0x2bec21,_0x2ad08f){_0x2bec21=_0x2bec21-0x0;var _0x27ab9b=_0x326f[_0x2bec21];return _0x27ab9b;};'use strict';var _=require(_0xf326('0x0'));var rp=require('request-promise');var md5=require('md5');var util=require(_0xf326('0x1'));var Redis=require(_0xf326('0x2'));var intUtil=require(_0xf326('0x3'));var config=require(_0xf326('0x4'));var logger=require(_0xf326('0x5'))(_0xf326('0x6'));config[_0xf326('0x7')]=_[_0xf326('0x8')](config[_0xf326('0x7')],{'host':_0xf326('0x9'),'port':0x18eb});var io=require('socket.io-emitter')(new Redis(config[_0xf326('0x7')]));function emit(_0x3851eb,_0x165f95,_0x1e7b8a){io['to'](_0x3851eb)['emit'](_0x165f95,_0x1e7b8a);}var headers={'Content-Type':'application/json','Accept':'application/json'};function getUser(_0x3bacf2,_0x1fc80e){logger[_0xf326('0xa')](_0xf326('0xb'),_0x1fc80e);return request({'uri':util[_0xf326('0xc')]('%s/users',_0x3bacf2[_0xf326('0xd')])})['then'](function(_0x1cf441){if(_0x1cf441['total_entries']){return _[_0xf326('0xe')](_0x1cf441['_embedded'][_0xf326('0xf')],{'email':_0x1fc80e});}})[_0xf326('0x10')](function(_0x4b3966){if(_0x4b3966){logger[_0xf326('0xa')](_0xf326('0x11'));return _0x4b3966;}else{logger[_0xf326('0xa')](_0xf326('0x12'));return request({'uri':util['format'](_0xf326('0x13'),_0x3bacf2['remoteUri'])});}});}function request(_0x54bd1a){logger[_0xf326('0x14')](_0xf326('0x15'),JSON[_0xf326('0x16')](_0x54bd1a));return rp(_[_0xf326('0x8')](_0x54bd1a,{'method':_0xf326('0x17'),'headers':headers,'json':!![]}));}function getAuth(_0x287a2a){var _0x34128e;if(_0x287a2a[_0xf326('0x18')]==='basic'){_0x34128e=util[_0xf326('0xc')](_0xf326('0x19'),_0x287a2a[_0xf326('0x1a')],_0x287a2a[_0xf326('0x1b')]);}else if(_0x287a2a[_0xf326('0x18')]===_0xf326('0x1c')){_0x34128e=util['format']('%s/token:%s',_0x287a2a['username'],_0x287a2a[_0xf326('0x1c')]);}return util[_0xf326('0xc')]('Basic\x20%s',new Buffer(_0x34128e)['toString'](_0xf326('0x1d')));}function getEndUser(_0x4dfa76,_0x5b6d3c,_0x448b92){logger[_0xf326('0xa')]('Searching\x20enduser\x20with\x20phone',_0x448b92);return request({'uri':util['format'](_0xf326('0x1e'),_0x4dfa76[_0xf326('0xd')]),'qs':{'phone':_0x448b92}})['then'](function(_0x337ed5){if(_0x337ed5[_0xf326('0x1f')]){logger[_0xf326('0xa')](_0xf326('0x20'));return _0x337ed5[_0xf326('0x21')][_0xf326('0xf')][0x0];}else{logger['info']('Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!');return request({'uri':util['format'](_0xf326('0x22'),_0x4dfa76[_0xf326('0xd')]),'method':_0xf326('0x23'),'body':{'first_name':_0x5b6d3c,'title':_0x5b6d3c,'phone_numbers':[{'type':_0xf326('0x24'),'value':_0x448b92}]}});}});}function getTags(_0x148953,_0x410237){var _0x5217d4=[];if(_0x148953&&_0x148953[_0xf326('0x25')]>0x0){_0x5217d4=_[_0xf326('0x26')](_0x148953,_0x410237);}return _0x5217d4;}function getCustomFields(_0x212d5d,_0x581531){var _0x1617d2=[];var _0x55ad0f={'custom_fields':{}};if(_0x212d5d['length']>0x0){_[_0xf326('0x27')](_0x212d5d,function(_0x1ae23a){if(_0x1ae23a[_0xf326('0x28')]==='string'&&_0x1ae23a[_0xf326('0x29')]&&!_0x1ae23a[_0xf326('0x2a')]){_0x55ad0f[_0x1ae23a['nameField'][_0xf326('0x2b')]()]=_0x1ae23a[_0xf326('0x2c')][_0xf326('0x2d')]();}else if(_0x1ae23a[_0xf326('0x28')]===_0xf326('0x2e')&&_0x1ae23a[_0xf326('0x2f')]&&_0x1ae23a[_0xf326('0x2a')]){_0x55ad0f[_0xf326('0x30')][_0x1ae23a[_0xf326('0x2f')]]=_0x1ae23a[_0xf326('0x2c')]['toString']();}else if(_0x1ae23a[_0xf326('0x28')]===_0xf326('0x31')&&_0x1ae23a[_0xf326('0x32')]&&_0x1ae23a['idField']&&_0x1ae23a[_0xf326('0x2a')]){_0x581531[_0x1ae23a[_0xf326('0x32')]]&&(_0x55ad0f[_0xf326('0x30')][_0x1ae23a['idField']]=_0x581531[_0x1ae23a['variableName']][_0xf326('0x2d')]());}else if(_0x1ae23a[_0xf326('0x28')]===_0xf326('0x33')&&_0x1ae23a[_0xf326('0x34')]&&_0x1ae23a[_0xf326('0x34')][_0xf326('0x35')]&&_0x1ae23a[_0xf326('0x2f')]&&_0x1ae23a[_0xf326('0x2a')]){_0x581531[_0x1ae23a['Variable'][_0xf326('0x35')][_0xf326('0x2b')]()]&&(_0x55ad0f['custom_fields'][_0x1ae23a[_0xf326('0x2f')]]=_0x581531[_0x1ae23a['Variable'][_0xf326('0x35')]['toLowerCase']()]['toString']());}});}if(_0x55ad0f[_0xf326('0x36')]){_0x55ad0f['priority']=Number(_0x55ad0f[_0xf326('0x36')]);}return _0x55ad0f;}function getTicket(_0x57aa27,_0x310dbb,_0x409260,_0x2750ae,_0x529cbb){logger[_0xf326('0xa')](_0xf326('0x37'));var _0x48525a=_[_0xf326('0x38')]({'subject':intUtil[_0xf326('0x39')](_0x2750ae[_0xf326('0x3a')],_0x529cbb,'\x20'),'type':_0xf326('0x3b'),'status':'new','labels':[_0xf326('0x3c'),_0xf326('0x3d'),_0xf326('0x3e')][_0xf326('0x3f')](getTags(_0x2750ae[_0xf326('0x40')],'name')),'description':intUtil[_0xf326('0x39')](_0x2750ae[_0xf326('0x41')],_0x529cbb,'\x0a'),'_links':{'assigned_user':{'href':util['format']('/api/v2/users/%d',_0x310dbb['id']),'class':_0xf326('0x42')}},'message':{'direction':'in','body':intUtil['getString'](_0x2750ae['Descriptions'],_0x529cbb,'\x0a')}},getCustomFields(_0x2750ae[_0xf326('0x43')],_0x529cbb));return request({'uri':util['format'](_0xf326('0x44'),_0x57aa27[_0xf326('0xd')],_0x409260['id']),'method':_0xf326('0x23'),'body':_0x48525a});}exports[_0xf326('0x45')]=function(_0x49112d,_0x9dda89,_0x45d344,_0x347e7b,_0x365627,_0xc4fda4){var _0x28f857,_0x2ad841;_0xc4fda4[_0xf326('0xd')]=intUtil[_0xf326('0x46')](_0xc4fda4[_0xf326('0xd')]);_0xc4fda4[_0xf326('0x47')]=intUtil[_0xf326('0x46')](_0xc4fda4['serverUrl']);if(_0x365627&&_0x365627[_0xf326('0x48')]){logger[_0xf326('0xa')](_0xf326('0x49'),_0xf326('0x4a'));_0x9dda89[_0xf326('0x4b')]=util[_0xf326('0xc')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0xc4fda4[_0xf326('0x47')],_0x9dda89[_0xf326('0x4c')],md5(_0x9dda89['uniqueid']));}else{_0x9dda89[_0xf326('0x4b')]='';}headers[_0xf326('0x4d')]=getAuth(_0xc4fda4);return getUser(_0xc4fda4,_0x347e7b[_0xf326('0x4e')])[_0xf326('0x10')](function(_0x427c34){_0x28f857=_0x427c34;logger['info']('The\x20User\x20ID\x20is:',_0x28f857['id']);return getEndUser(_0xc4fda4,_0x9dda89[_0xf326('0x4f')],_0x9dda89[_0xf326('0x50')]);})['then'](function(_0x4bd865){_0x2ad841=_0x4bd865;logger[_0xf326('0xa')](_0xf326('0x51'),_0x2ad841['id']);return getTicket(_0xc4fda4,_0x28f857,_0x2ad841,_0x45d344,_0x9dda89);})[_0xf326('0x10')](function(_0x5b3705){if(_0x5b3705){logger[_0xf326('0xa')](_0xf326('0x52'));logger[_0xf326('0xa')](_0xf326('0x53'),_0x5b3705['id']);switch(_0xc4fda4[_0xf326('0x28')]){case _0xf326('0x54'):logger[_0xf326('0xa')](_0xf326('0x49'),_0xf326('0x55'));emit(util[_0xf326('0xc')](_0xf326('0x56'),_0x347e7b[_0xf326('0x35')]),_0xf326('0x57'),{'recordId':_0x5b3705['id']});break;case _0xf326('0x58'):logger[_0xf326('0xa')]('[QUEUE]',_0xf326('0x59'));emit(util[_0xf326('0xc')](_0xf326('0x56'),_0x347e7b[_0xf326('0x35')]),'trigger:browser:url',{'uri':util[_0xf326('0xc')](_0xf326('0x5a'),_0x5b3705['id'])});break;default:logger[_0xf326('0x5b')](_0xf326('0x49'),'Desk\x20account\x20type\x20not\x20recognized');}}})[_0xf326('0x5c')](function(_0x1967ab){console[_0xf326('0x5b')](_0x1967ab);logger[_0xf326('0x5b')](_0xf326('0x49'),JSON['stringify'](_0x1967ab));});};exports['outbound']=function(_0x15b469,_0x38ff64,_0x2ff126,_0x4cb25b,_0x102fba,_0x4650db,_0x44eab7){var _0x5e3532,_0x47b308;_0x4650db[_0xf326('0xd')]=intUtil['stripTrailingSlash'](_0x4650db[_0xf326('0xd')]);_0x4650db['serverUrl']=intUtil[_0xf326('0x46')](_0x4650db[_0xf326('0x47')]);if(_0x44eab7&&_0x44eab7[_0xf326('0x5d')]&&_0x44eab7[_0xf326('0x5d')]!==_0xf326('0x5e')){logger[_0xf326('0xa')](_0xf326('0x5f'),'Recording\x20is\x20enabled!');_0x38ff64[_0xf326('0x4b')]=util[_0xf326('0xc')](_0xf326('0x60'),_0x4650db['serverUrl'],_0x38ff64[_0xf326('0x4c')],md5(_0x38ff64[_0xf326('0x4c')]));}headers[_0xf326('0x4d')]=getAuth(_0x4650db);return getUser(_0x4650db,_0x4cb25b[_0xf326('0x4e')])[_0xf326('0x10')](function(_0x58fcf0){_0x5e3532=_0x58fcf0;logger[_0xf326('0xa')](_0xf326('0x61'),_0x5e3532['id']);return getEndUser(_0x4650db,_0x38ff64[_0xf326('0x62')],_0x38ff64[_0xf326('0x63')]);})['then'](function(_0x3d0c12){_0x47b308=_0x3d0c12;logger[_0xf326('0xa')](_0xf326('0x51'),_0x47b308['id']);return getTicket(_0x4650db,_0x5e3532,_0x47b308,_0x2ff126,_0x38ff64);})[_0xf326('0x10')](function(_0x4d3b9e){if(_0x4d3b9e){logger['info'](_0xf326('0x52'));logger[_0xf326('0xa')]('The\x20Ticket\x20ID\x20is:',_0x4d3b9e['id']);switch(_0x4650db[_0xf326('0x28')]){case _0xf326('0x54'):logger[_0xf326('0xa')](_0xf326('0x49'),'Displaying\x20ticket\x20to\x20agent...');emit(util[_0xf326('0xc')]('user:%s',_0x4cb25b['name']),_0xf326('0x57'),{'recordId':_0x4d3b9e['id']});break;case'newTab':logger[_0xf326('0xa')](_0xf326('0x49'),_0xf326('0x59'));emit(util['format']('user:%s',_0x4cb25b[_0xf326('0x35')]),_0xf326('0x64'),{'uri':util[_0xf326('0xc')]('%s/agent/tickets/%s',_0x4650db[_0xf326('0xd')],_0x4d3b9e['id'])});break;default:logger['error'](_0xf326('0x5f'),_0xf326('0x65'));}}})[_0xf326('0x5c')](function(_0x217897){logger[_0xf326('0x5b')](_0xf326('0x5f'),JSON[_0xf326('0x16')](_0x217897));});};exports[_0xf326('0x66')]=function(_0x411f0a,_0x33f9dd,_0x33ffb8,_0x1e56fb,_0x40f7cb,_0x4ca1b8){var _0x25a429,_0x5ae666;_0x4ca1b8[_0xf326('0xd')]=intUtil[_0xf326('0x46')](_0x4ca1b8['remoteUri']);_0x4ca1b8[_0xf326('0x47')]=intUtil['stripTrailingSlash'](_0x4ca1b8[_0xf326('0x47')]);if(_0x40f7cb&&_0x40f7cb[_0xf326('0x48')]){logger[_0xf326('0xa')](_0xf326('0x67'),_0xf326('0x4a'));_0x33f9dd['recordingURL']=util['format'](_0xf326('0x60'),_0x4ca1b8[_0xf326('0x47')],_0x33f9dd[_0xf326('0x4c')],md5(_0x33f9dd[_0xf326('0x4c')]));}else{_0x33f9dd[_0xf326('0x4b')]='';}headers['Authorization']=getAuth(_0x4ca1b8);return getUser(_0x4ca1b8)[_0xf326('0x10')](function(_0x471da8){_0x25a429=_0x471da8;logger['info']('The\x20User\x20ID\x20is:',_0x25a429['id']);return getEndUser(_0x4ca1b8,_0x33f9dd['calleridname'],_0x33f9dd[_0xf326('0x50')]);})[_0xf326('0x10')](function(_0x4150fa){_0x5ae666=_0x4150fa;logger[_0xf326('0xa')](_0xf326('0x51'),_0x5ae666['id']);return getTicket(_0x4ca1b8,_0x25a429,_0x5ae666,_0x33ffb8,_0x33f9dd);})[_0xf326('0x10')](function(_0x230aea){if(_0x230aea){logger['info'](_0xf326('0x52'));logger[_0xf326('0xa')](_0xf326('0x53'),_0x230aea['id']);}})[_0xf326('0x5c')](function(_0x58de1e){logger[_0xf326('0x5b')]('[ABANDONED]',JSON['stringify'](_0x58de1e));});};