Built motion from commit b0a9a629.|2.6.33
[motion2.git] / server / services / ami / trigger / integration / freshdesk.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 _0xf527=['custom_fields','mapValues','keyBy','%s/api/v2/tickets','remoteUri','Ticket\x20created!','validateEmail','%s/api/v2/agents','then','length','Agent\x20found!','find','contact','email','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','Admin\x20account\x20selected.','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','Searching\x20enduser\x20with\x20phone','Enduser\x20found!','%s/api/v2/contacts','Enduser\x20created.','Basic\x20%s','toString','base64','[HTTP]','stringify','includes','string','picklist','type','nameField','toLowerCase','content','idField','customField','variable','variableName','push','Variable','queue','stripTrailingSlash','serverUrl','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','Authorization','The\x20User\x20ID\x20is:','calleridname','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','trigger:browser:url','%s/helpdesk/tickets/%d','catch','error','outbound','recordingFormat','none','[OUTBOUND]','destcalleridname','user:%s','toUpperCase','lastevent','lodash','md5','ioredis','./util','../../../../config/logger','freshdesk','redis','defaults','localhost','socket.io-emitter','application/json','emit','info','Creating\x20new\x20ticket...','format','Caller\x20-\x20%s','calleridnum','getString','Subjects','<br>','Tags','name','Fields'];(function(_0x3c6ae2,_0x5c52d8){var _0x4edda=function(_0x5593eb){while(--_0x5593eb){_0x3c6ae2['push'](_0x3c6ae2['shift']());}};_0x4edda(++_0x5c52d8);}(_0xf527,0x9b));var _0x7f52=function(_0x52e9e3,_0xe465a7){_0x52e9e3=_0x52e9e3-0x0;var _0x960a8d=_0xf527[_0x52e9e3];return _0x960a8d;};'use strict';var _=require(_0x7f52('0x0'));var rp=require('request-promise');var md5=require(_0x7f52('0x1'));var util=require('util');var Redis=require(_0x7f52('0x2'));var intUtil=require(_0x7f52('0x3'));var config=require('../../../../config/environment');var logger=require(_0x7f52('0x4'))(_0x7f52('0x5'));config[_0x7f52('0x6')]=_[_0x7f52('0x7')](config['redis'],{'host':_0x7f52('0x8'),'port':0x18eb});var io=require(_0x7f52('0x9'))(new Redis(config[_0x7f52('0x6')]));var headers={'Content-Type':_0x7f52('0xa'),'Accept':_0x7f52('0xa')};function emit(_0x1f62c3,_0x37b3eb,_0x36b72f){io['to'](_0x1f62c3)[_0x7f52('0xb')](_0x37b3eb,_0x36b72f);}function getTicket(_0x1f203f,_0x4a3a46,_0x374172,_0x320783,_0x7d2896){logger[_0x7f52('0xc')](_0x7f52('0xd'));var _0x264bd2=_['merge']({'responder_id':_0x4a3a46['id'],'requester_id':_0x374172['id'],'name':util[_0x7f52('0xe')](_0x7f52('0xf'),_0x7d2896['calleridname']),'phone':_0x7d2896[_0x7f52('0x10')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0x7f52('0x11')](_0x320783[_0x7f52('0x12')],_0x7d2896,'\x20'),'description':intUtil[_0x7f52('0x11')](_0x320783['Descriptions'],_0x7d2896,_0x7f52('0x13')),'tags':getTags(_0x320783[_0x7f52('0x14')],_0x7f52('0x15'))},getCustomFields(_0x320783[_0x7f52('0x16')],_0x7d2896));_0x264bd2[_0x7f52('0x17')]=_[_0x7f52('0x18')](_[_0x7f52('0x19')](_0x264bd2['custom_fields'],'id'),'value');return request({'uri':util[_0x7f52('0xe')](_0x7f52('0x1a'),_0x1f203f[_0x7f52('0x1b')]),'method':'POST','body':_0x264bd2})['then'](function(_0x480a20){logger[_0x7f52('0xc')](_0x7f52('0x1c'));return _0x480a20;});}function getUser(_0x3349f1,_0x550b72){if(intUtil[_0x7f52('0x1d')](_0x550b72)){return request({'uri':util[_0x7f52('0xe')](_0x7f52('0x1e'),_0x3349f1[_0x7f52('0x1b')]),'qs':{'email':_0x550b72}})[_0x7f52('0x1f')](function(_0x3f5521){if(_0x3f5521[_0x7f52('0x20')]){logger[_0x7f52('0xc')](_0x7f52('0x21'));return _[_0x7f52('0x22')](_0x3f5521,function(_0x2d248a){return _0x2d248a[_0x7f52('0x23')][_0x7f52('0x24')]===_0x550b72;});}else{logger['info'](_0x7f52('0x25'));return request({'uri':util[_0x7f52('0xe')]('%s/api/v2/agents/me',_0x3349f1[_0x7f52('0x1b')])})[_0x7f52('0x1f')](function(_0x3950d8){logger['info'](_0x7f52('0x26'));return _0x3950d8;});}});}else{logger[_0x7f52('0xc')](_0x7f52('0x27'));return request({'uri':util[_0x7f52('0xe')]('%s/api/v2/agents/me',_0x3349f1[_0x7f52('0x1b')])})[_0x7f52('0x1f')](function(_0x2892b3){logger[_0x7f52('0xc')](_0x7f52('0x26'));return _0x2892b3;});}}function getEndUser(_0x2c2dbd,_0x474f1a,_0x340952){logger[_0x7f52('0xc')](_0x7f52('0x28'),_0x340952);return request({'uri':util[_0x7f52('0xe')]('%s/api/v2/contacts',_0x2c2dbd[_0x7f52('0x1b')]),'qs':{'phone':_0x340952}})[_0x7f52('0x1f')](function(_0x26553a){if(_0x26553a[_0x7f52('0x20')]){logger[_0x7f52('0xc')](_0x7f52('0x29'));return _[_0x7f52('0x22')](_0x26553a,{'phone':_0x340952});}else{logger[_0x7f52('0xc')]('Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!');return request({'uri':util[_0x7f52('0xe')](_0x7f52('0x2a'),_0x2c2dbd['remoteUri']),'method':'POST','body':{'name':_0x474f1a,'phone':_0x340952}})[_0x7f52('0x1f')](function(_0x8bce5d){logger['info'](_0x7f52('0x2b'));return _0x8bce5d;});}});}function getAuth(_0x5c6f74){return util[_0x7f52('0xe')](_0x7f52('0x2c'),new Buffer(_0x5c6f74['apiKey']+':X')[_0x7f52('0x2d')](_0x7f52('0x2e')));}function request(_0x42bb6c){logger['debug'](_0x7f52('0x2f'),JSON[_0x7f52('0x30')](_0x42bb6c));return rp(_[_0x7f52('0x7')](_0x42bb6c,{'method':'GET','headers':headers,'json':!![]}));}function getTags(_0x1a403a,_0x490d8a){var _0x92ba53=[];if(_0x1a403a&&_0x1a403a[_0x7f52('0x20')]>0x0){_0x92ba53=_['map'](_0x1a403a,_0x490d8a);}return _0x92ba53;}function getCustomFields(_0x6d883f,_0x43ff5a){var _0x3da9fa=[];var _0x2196e6={'custom_fields':[]};if(_0x6d883f[_0x7f52('0x20')]>0x0){_['forEach'](_0x6d883f,function(_0x36fb29){if(_[_0x7f52('0x31')]([_0x7f52('0x32'),_0x7f52('0x33')],_0x36fb29[_0x7f52('0x34')])&&_0x36fb29[_0x7f52('0x35')]&&!_0x36fb29['customField']){_0x2196e6[_0x36fb29[_0x7f52('0x35')][_0x7f52('0x36')]()]=_0x36fb29[_0x7f52('0x37')][_0x7f52('0x2d')]();}else if(_[_0x7f52('0x31')]([_0x7f52('0x32'),'picklist'],_0x36fb29[_0x7f52('0x34')])&&_0x36fb29[_0x7f52('0x38')]&&_0x36fb29[_0x7f52('0x39')]){_0x2196e6[_0x7f52('0x17')]['push']({'id':_0x36fb29['idField'],'value':_0x36fb29[_0x7f52('0x37')]['toString']()});}else if(_0x36fb29[_0x7f52('0x34')]===_0x7f52('0x3a')&&_0x36fb29[_0x7f52('0x3b')]&&_0x36fb29[_0x7f52('0x38')]&&_0x36fb29['customField']){_0x43ff5a[_0x36fb29['variableName']]&&_0x2196e6[_0x7f52('0x17')][_0x7f52('0x3c')]({'id':_0x36fb29['idField'],'value':_0x43ff5a[_0x36fb29['variableName']][_0x7f52('0x2d')]()});}else if(_0x36fb29[_0x7f52('0x34')]==='customVariable'&&_0x36fb29[_0x7f52('0x3d')]&&_0x36fb29[_0x7f52('0x3d')][_0x7f52('0x15')]&&_0x36fb29[_0x7f52('0x38')]&&_0x36fb29[_0x7f52('0x39')]){_0x43ff5a[_0x36fb29[_0x7f52('0x3d')][_0x7f52('0x15')][_0x7f52('0x36')]()]&&_0x2196e6[_0x7f52('0x17')][_0x7f52('0x3c')]({'id':_0x36fb29['idField'],'value':_0x43ff5a[_0x36fb29[_0x7f52('0x3d')][_0x7f52('0x15')]['toLowerCase']()]['toString']()});}});}return _0x2196e6;}exports[_0x7f52('0x3e')]=function(_0x25bc69,_0x343325,_0x4f8541,_0x348610,_0x5eb762,_0x1b59d0){var _0x579a7c,_0x550788;_0x1b59d0[_0x7f52('0x1b')]=intUtil[_0x7f52('0x3f')](_0x1b59d0[_0x7f52('0x1b')]);_0x1b59d0[_0x7f52('0x40')]=intUtil[_0x7f52('0x3f')](_0x1b59d0[_0x7f52('0x40')]);if(_0x5eb762&&_0x5eb762[_0x7f52('0x41')]){logger[_0x7f52('0xc')](_0x7f52('0x42'),_0x7f52('0x43'));_0x343325[_0x7f52('0x44')]=util[_0x7f52('0xe')](_0x7f52('0x45'),_0x1b59d0['serverUrl'],_0x343325[_0x7f52('0x46')],md5(_0x343325[_0x7f52('0x46')]));}else{_0x343325['recordingURL']='';}headers[_0x7f52('0x47')]=getAuth(_0x1b59d0);return getUser(_0x1b59d0,_0x348610[_0x7f52('0x24')])[_0x7f52('0x1f')](function(_0x53af0a){_0x579a7c=_0x53af0a;logger[_0x7f52('0xc')](_0x7f52('0x48'),_0x579a7c['id']);return getEndUser(_0x1b59d0,_0x343325[_0x7f52('0x49')],_0x343325[_0x7f52('0x10')]);})[_0x7f52('0x1f')](function(_0xa1b9bd){_0x550788=_0xa1b9bd;logger['info'](_0x7f52('0x4a'),_0x550788['id']);return getTicket(_0x1b59d0,_0x579a7c,_0x550788,_0x4f8541,_0x343325);})['then'](function(_0x5afaad){if(_0x5afaad&&_0x5afaad['id']){logger[_0x7f52('0xc')](_0x7f52('0x1c'));logger[_0x7f52('0xc')](_0x7f52('0x4b'),_0x5afaad['id']);logger[_0x7f52('0xc')](_0x7f52('0x42'),_0x7f52('0x4c'));emit(util[_0x7f52('0xe')]('user:%s',_0x348610['name']),_0x7f52('0x4d'),{'uri':util[_0x7f52('0xe')](_0x7f52('0x4e'),_0x1b59d0['remoteUri'],_0x5afaad['id'])});}})[_0x7f52('0x4f')](function(_0x5ee835){logger[_0x7f52('0x50')](_0x7f52('0x42'),JSON[_0x7f52('0x30')](_0x5ee835));});};exports[_0x7f52('0x51')]=function(_0x95ad76,_0x39dc76,_0x1eb784,_0x5068dc,_0x11bdd2,_0x3c6593,_0x25105d){var _0x4b6d04,_0x19c741;_0x3c6593[_0x7f52('0x1b')]=intUtil['stripTrailingSlash'](_0x3c6593[_0x7f52('0x1b')]);_0x3c6593[_0x7f52('0x40')]=intUtil[_0x7f52('0x3f')](_0x3c6593['serverUrl']);if(_0x25105d&&_0x25105d['recordingFormat']&&_0x25105d[_0x7f52('0x52')]!==_0x7f52('0x53')){logger[_0x7f52('0xc')](_0x7f52('0x54'),'Recording\x20is\x20enabled!');_0x39dc76[_0x7f52('0x44')]=util[_0x7f52('0xe')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x3c6593[_0x7f52('0x40')],_0x39dc76['uniqueid'],md5(_0x39dc76['uniqueid']));}headers[_0x7f52('0x47')]=getAuth(_0x3c6593);return getUser(_0x3c6593,_0x5068dc[_0x7f52('0x24')])['then'](function(_0x582f99){_0x4b6d04=_0x582f99;logger[_0x7f52('0xc')](_0x7f52('0x48'),_0x4b6d04['id']);return getEndUser(_0x3c6593,_0x39dc76[_0x7f52('0x55')],_0x39dc76['destcalleridnum']);})[_0x7f52('0x1f')](function(_0x2f13ef){_0x19c741=_0x2f13ef;logger[_0x7f52('0xc')](_0x7f52('0x4a'),_0x19c741['id']);return getTicket(_0x3c6593,_0x4b6d04,_0x19c741,_0x1eb784,_0x39dc76);})['then'](function(_0x2a0014){if(_0x2a0014&&_0x2a0014['id']){logger['info'](_0x7f52('0x1c'));logger['info'](_0x7f52('0x4b'),_0x2a0014['id']);logger['info']('[OUTBOUND]',_0x7f52('0x4c'));emit(util[_0x7f52('0xe')](_0x7f52('0x56'),_0x5068dc[_0x7f52('0x15')]),_0x7f52('0x4d'),{'uri':util[_0x7f52('0xe')](_0x7f52('0x4e'),_0x3c6593[_0x7f52('0x1b')],_0x2a0014['id'])});}})[_0x7f52('0x4f')](function(_0x44562f){logger[_0x7f52('0x50')](_0x7f52('0x54'),JSON[_0x7f52('0x30')](_0x44562f));});};exports['unmanaged']=function(_0x5f17c2,_0x39e9e7,_0x22f274,_0x5664ed,_0xeaf317,_0x246419){var _0xac860c,_0x27ba26;_0x246419[_0x7f52('0x1b')]=intUtil[_0x7f52('0x3f')](_0x246419[_0x7f52('0x1b')]);_0x246419[_0x7f52('0x40')]=intUtil['stripTrailingSlash'](_0x246419[_0x7f52('0x40')]);if(_0xeaf317&&_0xeaf317[_0x7f52('0x41')]){logger['info']('['+_0x39e9e7['lastevent'][_0x7f52('0x57')]()+']',_0x7f52('0x43'));_0x39e9e7['recordingURL']=util['format'](_0x7f52('0x45'),_0x246419['serverUrl'],_0x39e9e7['uniqueid'],md5(_0x39e9e7['uniqueid']));}else{_0x39e9e7[_0x7f52('0x44')]='';}headers[_0x7f52('0x47')]=getAuth(_0x246419);return getUser(_0x246419,'')['then'](function(_0x3226df){_0xac860c=_0x3226df;logger[_0x7f52('0xc')](_0x7f52('0x48'),_0xac860c['id']);return getEndUser(_0x246419,_0x39e9e7['calleridname'],_0x39e9e7[_0x7f52('0x10')]);})[_0x7f52('0x1f')](function(_0x511e90){_0x27ba26=_0x511e90;logger[_0x7f52('0xc')](_0x7f52('0x4a'),_0x27ba26['id']);return getTicket(_0x246419,_0xac860c,_0x27ba26,_0x22f274,_0x39e9e7);})[_0x7f52('0x1f')](function(_0x1415cc){if(_0x1415cc){logger[_0x7f52('0xc')]('Ticket\x20created!');logger[_0x7f52('0xc')]('The\x20Ticket\x20ID\x20is:',_0x1415cc['id']);}})[_0x7f52('0x4f')](function(_0x116fb9){logger[_0x7f52('0x50')]('['+_0x39e9e7[_0x7f52('0x58')][_0x7f52('0x57')]()+']',JSON[_0x7f52('0x30')](_0x116fb9));});};