Built motion from commit da617fac.|2.6.6
[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 _0xb4f0=['validateEmail','%s/api/v2/agents','length','Agent\x20found!','find','contact','%s/api/v2/agents/me','remoteUri','Admin\x20account\x20selected.','Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!','Searching\x20enduser\x20with\x20phone','%s/api/v2/contacts','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Enduser\x20created.','Basic\x20%s','apiKey','base64','debug','[HTTP]','stringify','defaults','GET','map','forEach','includes','string','picklist','type','nameField','toLowerCase','content','toString','idField','customField','variableName','push','Variable','serverUrl','stripTrailingSlash','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','Authorization','calleridnum','The\x20Enduser\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','trigger:browser:url','catch','error','recordingFormat','[OUTBOUND]','recordingURL','email','The\x20User\x20ID\x20is:','destcalleridname','destcalleridnum','user:%s','unmanaged','lastevent','toUpperCase','The\x20Ticket\x20ID\x20is:','lodash','request-promise','md5','util','../../../../config/environment','freshdesk','redis','localhost','socket.io-emitter','application/json','emit','info','Creating\x20new\x20ticket...','merge','format','Caller\x20-\x20%s','calleridname','getString','Subjects','Descriptions','<br>','Tags','name','Fields','keyBy','custom_fields','value','%s/api/v2/tickets','POST','then','Ticket\x20created!'];(function(_0x188a28,_0x3018a9){var _0x5f3b63=function(_0x162f49){while(--_0x162f49){_0x188a28['push'](_0x188a28['shift']());}};_0x5f3b63(++_0x3018a9);}(_0xb4f0,0xfb));var _0x0b4f=function(_0x479160,_0x3ab560){_0x479160=_0x479160-0x0;var _0x47ac6d=_0xb4f0[_0x479160];return _0x47ac6d;};'use strict';var _=require(_0x0b4f('0x0'));var rp=require(_0x0b4f('0x1'));var md5=require(_0x0b4f('0x2'));var util=require(_0x0b4f('0x3'));var Redis=require('ioredis');var intUtil=require('./util');var config=require(_0x0b4f('0x4'));var logger=require('../../../../config/logger')(_0x0b4f('0x5'));config[_0x0b4f('0x6')]=_['defaults'](config[_0x0b4f('0x6')],{'host':_0x0b4f('0x7'),'port':0x18eb});var io=require(_0x0b4f('0x8'))(new Redis(config[_0x0b4f('0x6')]));var headers={'Content-Type':_0x0b4f('0x9'),'Accept':_0x0b4f('0x9')};function emit(_0x5d1ff5,_0x449460,_0x3747b9){io['to'](_0x5d1ff5)[_0x0b4f('0xa')](_0x449460,_0x3747b9);}function getTicket(_0x57c45f,_0x22b320,_0x1b69aa,_0x175155,_0x329087){logger[_0x0b4f('0xb')](_0x0b4f('0xc'));var _0x5adf4e=_[_0x0b4f('0xd')]({'responder_id':_0x22b320['id'],'requester_id':_0x1b69aa['id'],'name':util[_0x0b4f('0xe')](_0x0b4f('0xf'),_0x329087[_0x0b4f('0x10')]),'phone':_0x329087['calleridnum'],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0x0b4f('0x11')](_0x175155[_0x0b4f('0x12')],_0x329087,'\x20'),'description':intUtil[_0x0b4f('0x11')](_0x175155[_0x0b4f('0x13')],_0x329087,_0x0b4f('0x14')),'tags':getTags(_0x175155[_0x0b4f('0x15')],_0x0b4f('0x16'))},getCustomFields(_0x175155[_0x0b4f('0x17')],_0x329087));_0x5adf4e['custom_fields']=_['mapValues'](_[_0x0b4f('0x18')](_0x5adf4e[_0x0b4f('0x19')],'id'),_0x0b4f('0x1a'));return request({'uri':util[_0x0b4f('0xe')](_0x0b4f('0x1b'),_0x57c45f['remoteUri']),'method':_0x0b4f('0x1c'),'body':_0x5adf4e})[_0x0b4f('0x1d')](function(_0x367896){logger[_0x0b4f('0xb')](_0x0b4f('0x1e'));return _0x367896;});}function getUser(_0x47fa3e,_0x1ab0af){if(intUtil[_0x0b4f('0x1f')](_0x1ab0af)){return request({'uri':util[_0x0b4f('0xe')](_0x0b4f('0x20'),_0x47fa3e['remoteUri']),'qs':{'email':_0x1ab0af}})[_0x0b4f('0x1d')](function(_0x2ddf4f){if(_0x2ddf4f[_0x0b4f('0x21')]){logger['info'](_0x0b4f('0x22'));return _[_0x0b4f('0x23')](_0x2ddf4f,function(_0x6d2c84){return _0x6d2c84[_0x0b4f('0x24')]['email']===_0x1ab0af;});}else{logger['info']('Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!');return request({'uri':util['format'](_0x0b4f('0x25'),_0x47fa3e[_0x0b4f('0x26')])})[_0x0b4f('0x1d')](function(_0xf670ba){logger[_0x0b4f('0xb')](_0x0b4f('0x27'));return _0xf670ba;});}});}else{logger[_0x0b4f('0xb')](_0x0b4f('0x28'));return request({'uri':util['format']('%s/api/v2/agents/me',_0x47fa3e[_0x0b4f('0x26')])})[_0x0b4f('0x1d')](function(_0x9b632a){logger['info'](_0x0b4f('0x27'));return _0x9b632a;});}}function getEndUser(_0x419ec0,_0x41e7d0,_0x1d003b){logger['info'](_0x0b4f('0x29'),_0x1d003b);return request({'uri':util[_0x0b4f('0xe')](_0x0b4f('0x2a'),_0x419ec0[_0x0b4f('0x26')]),'qs':{'phone':_0x1d003b}})[_0x0b4f('0x1d')](function(_0x44233a){if(_0x44233a[_0x0b4f('0x21')]){logger[_0x0b4f('0xb')]('Enduser\x20found!');return _[_0x0b4f('0x23')](_0x44233a,{'phone':_0x1d003b});}else{logger[_0x0b4f('0xb')](_0x0b4f('0x2b'));return request({'uri':util[_0x0b4f('0xe')](_0x0b4f('0x2a'),_0x419ec0['remoteUri']),'method':'POST','body':{'name':_0x41e7d0,'phone':_0x1d003b}})[_0x0b4f('0x1d')](function(_0x5b9853){logger[_0x0b4f('0xb')](_0x0b4f('0x2c'));return _0x5b9853;});}});}function getAuth(_0x4620cc){return util['format'](_0x0b4f('0x2d'),new Buffer(_0x4620cc[_0x0b4f('0x2e')]+':X')['toString'](_0x0b4f('0x2f')));}function request(_0x398220){logger[_0x0b4f('0x30')](_0x0b4f('0x31'),JSON[_0x0b4f('0x32')](_0x398220));return rp(_[_0x0b4f('0x33')](_0x398220,{'method':_0x0b4f('0x34'),'headers':headers,'json':!![]}));}function getTags(_0x563797,_0x100c09){var _0x107ab9=[];if(_0x563797&&_0x563797[_0x0b4f('0x21')]>0x0){_0x107ab9=_[_0x0b4f('0x35')](_0x563797,_0x100c09);}return _0x107ab9;}function getCustomFields(_0x210ce4,_0x185b9b){var _0x3c664c=[];var _0x5a7e12={'custom_fields':[]};if(_0x210ce4[_0x0b4f('0x21')]>0x0){_[_0x0b4f('0x36')](_0x210ce4,function(_0x13a8c1){if(_[_0x0b4f('0x37')]([_0x0b4f('0x38'),_0x0b4f('0x39')],_0x13a8c1[_0x0b4f('0x3a')])&&_0x13a8c1[_0x0b4f('0x3b')]&&!_0x13a8c1['customField']){_0x5a7e12[_0x13a8c1['nameField'][_0x0b4f('0x3c')]()]=_0x13a8c1[_0x0b4f('0x3d')][_0x0b4f('0x3e')]();}else if(_[_0x0b4f('0x37')]([_0x0b4f('0x38'),_0x0b4f('0x39')],_0x13a8c1[_0x0b4f('0x3a')])&&_0x13a8c1[_0x0b4f('0x3f')]&&_0x13a8c1[_0x0b4f('0x40')]){_0x5a7e12['custom_fields']['push']({'id':_0x13a8c1[_0x0b4f('0x3f')],'value':_0x13a8c1[_0x0b4f('0x3d')]['toString']()});}else if(_0x13a8c1[_0x0b4f('0x3a')]==='variable'&&_0x13a8c1[_0x0b4f('0x41')]&&_0x13a8c1[_0x0b4f('0x3f')]&&_0x13a8c1[_0x0b4f('0x40')]){_0x185b9b[_0x13a8c1['variableName']]&&_0x5a7e12['custom_fields'][_0x0b4f('0x42')]({'id':_0x13a8c1['idField'],'value':_0x185b9b[_0x13a8c1[_0x0b4f('0x41')]][_0x0b4f('0x3e')]()});}else if(_0x13a8c1[_0x0b4f('0x3a')]==='customVariable'&&_0x13a8c1['Variable']&&_0x13a8c1[_0x0b4f('0x43')]['name']&&_0x13a8c1['idField']&&_0x13a8c1[_0x0b4f('0x40')]){_0x185b9b[_0x13a8c1[_0x0b4f('0x43')][_0x0b4f('0x16')]['toLowerCase']()]&&_0x5a7e12['custom_fields'][_0x0b4f('0x42')]({'id':_0x13a8c1['idField'],'value':_0x185b9b[_0x13a8c1[_0x0b4f('0x43')][_0x0b4f('0x16')]['toLowerCase']()][_0x0b4f('0x3e')]()});}});}return _0x5a7e12;}exports['queue']=function(_0x4e58c3,_0x1755fd,_0x25b5c5,_0x58110d,_0x1b41f7,_0x54debc){var _0x968bfd,_0x3a131f;_0x54debc['remoteUri']=intUtil['stripTrailingSlash'](_0x54debc[_0x0b4f('0x26')]);_0x54debc[_0x0b4f('0x44')]=intUtil[_0x0b4f('0x45')](_0x54debc['serverUrl']);if(_0x1b41f7&&_0x1b41f7[_0x0b4f('0x46')]){logger[_0x0b4f('0xb')](_0x0b4f('0x47'),_0x0b4f('0x48'));_0x1755fd['recordingURL']=util[_0x0b4f('0xe')](_0x0b4f('0x49'),_0x54debc[_0x0b4f('0x44')],_0x1755fd[_0x0b4f('0x4a')],md5(_0x1755fd[_0x0b4f('0x4a')]));}else{_0x1755fd['recordingURL']='';}headers[_0x0b4f('0x4b')]=getAuth(_0x54debc);return getUser(_0x54debc,_0x58110d['email'])[_0x0b4f('0x1d')](function(_0x3e533c){_0x968bfd=_0x3e533c;logger[_0x0b4f('0xb')]('The\x20User\x20ID\x20is:',_0x968bfd['id']);return getEndUser(_0x54debc,_0x1755fd['calleridname'],_0x1755fd[_0x0b4f('0x4c')]);})[_0x0b4f('0x1d')](function(_0x3c40f6){_0x3a131f=_0x3c40f6;logger[_0x0b4f('0xb')](_0x0b4f('0x4d'),_0x3a131f['id']);return getTicket(_0x54debc,_0x968bfd,_0x3a131f,_0x25b5c5,_0x1755fd);})[_0x0b4f('0x1d')](function(_0x11cd1c){if(_0x11cd1c&&_0x11cd1c['id']){logger[_0x0b4f('0xb')](_0x0b4f('0x1e'));logger[_0x0b4f('0xb')]('The\x20Ticket\x20ID\x20is:',_0x11cd1c['id']);logger['info']('[QUEUE]',_0x0b4f('0x4e'));emit(util[_0x0b4f('0xe')]('user:%s',_0x58110d[_0x0b4f('0x16')]),_0x0b4f('0x4f'),{'uri':util[_0x0b4f('0xe')]('%s/helpdesk/tickets/%d',_0x54debc[_0x0b4f('0x26')],_0x11cd1c['id'])});}})[_0x0b4f('0x50')](function(_0x1417fb){logger[_0x0b4f('0x51')]('[QUEUE]',JSON['stringify'](_0x1417fb));});};exports['outbound']=function(_0x48b901,_0x2c7bde,_0xd316d6,_0x2c846b,_0x1e0567,_0x3f8c70,_0x154cd9){var _0x5d9cc1,_0x2394f5;_0x3f8c70['remoteUri']=intUtil[_0x0b4f('0x45')](_0x3f8c70['remoteUri']);_0x3f8c70[_0x0b4f('0x44')]=intUtil['stripTrailingSlash'](_0x3f8c70[_0x0b4f('0x44')]);if(_0x154cd9&&_0x154cd9[_0x0b4f('0x52')]&&_0x154cd9[_0x0b4f('0x52')]!=='none'){logger['info'](_0x0b4f('0x53'),'Recording\x20is\x20enabled!');_0x2c7bde[_0x0b4f('0x54')]=util[_0x0b4f('0xe')](_0x0b4f('0x49'),_0x3f8c70['serverUrl'],_0x2c7bde['uniqueid'],md5(_0x2c7bde[_0x0b4f('0x4a')]));}headers[_0x0b4f('0x4b')]=getAuth(_0x3f8c70);return getUser(_0x3f8c70,_0x2c846b[_0x0b4f('0x55')])[_0x0b4f('0x1d')](function(_0x29d06d){_0x5d9cc1=_0x29d06d;logger[_0x0b4f('0xb')](_0x0b4f('0x56'),_0x5d9cc1['id']);return getEndUser(_0x3f8c70,_0x2c7bde[_0x0b4f('0x57')],_0x2c7bde[_0x0b4f('0x58')]);})[_0x0b4f('0x1d')](function(_0x2508ea){_0x2394f5=_0x2508ea;logger[_0x0b4f('0xb')](_0x0b4f('0x4d'),_0x2394f5['id']);return getTicket(_0x3f8c70,_0x5d9cc1,_0x2394f5,_0xd316d6,_0x2c7bde);})['then'](function(_0xf7c0f5){if(_0xf7c0f5&&_0xf7c0f5['id']){logger[_0x0b4f('0xb')](_0x0b4f('0x1e'));logger['info']('The\x20Ticket\x20ID\x20is:',_0xf7c0f5['id']);logger['info'](_0x0b4f('0x53'),'Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...');emit(util[_0x0b4f('0xe')](_0x0b4f('0x59'),_0x2c846b[_0x0b4f('0x16')]),_0x0b4f('0x4f'),{'uri':util['format']('%s/helpdesk/tickets/%d',_0x3f8c70[_0x0b4f('0x26')],_0xf7c0f5['id'])});}})[_0x0b4f('0x50')](function(_0x2e7920){logger[_0x0b4f('0x51')]('[OUTBOUND]',JSON[_0x0b4f('0x32')](_0x2e7920));});};exports[_0x0b4f('0x5a')]=function(_0x59231e,_0x1bf7de,_0x439d26,_0x16bb64,_0x2662d0,_0x46062e){var _0x5ce069,_0x1ce019;_0x46062e[_0x0b4f('0x26')]=intUtil[_0x0b4f('0x45')](_0x46062e['remoteUri']);_0x46062e[_0x0b4f('0x44')]=intUtil[_0x0b4f('0x45')](_0x46062e[_0x0b4f('0x44')]);if(_0x2662d0&&_0x2662d0[_0x0b4f('0x46')]){logger[_0x0b4f('0xb')]('['+_0x1bf7de[_0x0b4f('0x5b')][_0x0b4f('0x5c')]()+']',_0x0b4f('0x48'));_0x1bf7de[_0x0b4f('0x54')]=util[_0x0b4f('0xe')](_0x0b4f('0x49'),_0x46062e[_0x0b4f('0x44')],_0x1bf7de[_0x0b4f('0x4a')],md5(_0x1bf7de['uniqueid']));}else{_0x1bf7de[_0x0b4f('0x54')]='';}headers[_0x0b4f('0x4b')]=getAuth(_0x46062e);return getUser(_0x46062e,'')['then'](function(_0x35f42c){_0x5ce069=_0x35f42c;logger['info']('The\x20User\x20ID\x20is:',_0x5ce069['id']);return getEndUser(_0x46062e,_0x1bf7de[_0x0b4f('0x10')],_0x1bf7de[_0x0b4f('0x4c')]);})[_0x0b4f('0x1d')](function(_0x52ee9f){_0x1ce019=_0x52ee9f;logger[_0x0b4f('0xb')](_0x0b4f('0x4d'),_0x1ce019['id']);return getTicket(_0x46062e,_0x5ce069,_0x1ce019,_0x439d26,_0x1bf7de);})[_0x0b4f('0x1d')](function(_0x49a1a6){if(_0x49a1a6){logger['info']('Ticket\x20created!');logger[_0x0b4f('0xb')](_0x0b4f('0x5d'),_0x49a1a6['id']);}})['catch'](function(_0x23183e){logger['error']('['+_0x1bf7de[_0x0b4f('0x5b')][_0x0b4f('0x5c')]()+']',JSON[_0x0b4f('0x32')](_0x23183e));});};