Built motion from commit fe909640.|2.6.8
[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 _0x78bf=['map','forEach','includes','string','picklist','type','customField','nameField','toLowerCase','content','idField','push','variable','variableName','customVariable','Variable','queue','stripTrailingSlash','serverUrl','monitor_format','[QUEUE]','Recording\x20is\x20enabled!','recordingURL','%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s','uniqueid','The\x20Enduser\x20ID\x20is:','The\x20Ticket\x20ID\x20is:','Displaying\x20ticket\x20to\x20agent\x20in\x20a\x20new\x20tab...','user:%s','%s/helpdesk/tickets/%d','error','outbound','recordingFormat','[OUTBOUND]','Authorization','destcalleridname','trigger:browser:url','catch','unmanaged','lastevent','The\x20User\x20ID\x20is:','toUpperCase','lodash','request-promise','md5','util','../../../../config/environment','../../../../config/logger','redis','defaults','localhost','socket.io-emitter','application/json','emit','info','Creating\x20new\x20ticket...','merge','format','Caller\x20-\x20%s','calleridname','calleridnum','getString','Subjects','Descriptions','<br>','Tags','name','keyBy','custom_fields','value','%s/api/v2/tickets','remoteUri','POST','Ticket\x20created!','validateEmail','%s/api/v2/agents','then','Agent\x20found!','find','contact','email','Agent\x20not\x20found,\x20using\x20the\x20admin\x20account!','%s/api/v2/agents/me','Searching\x20enduser\x20with\x20phone','%s/api/v2/contacts','length','Enduser\x20not\x20found,\x20creating\x20a\x20new\x20enduser!','Enduser\x20created.','apiKey','toString','base64','debug','[HTTP]','stringify','GET'];(function(_0x38d2ee,_0x3711d6){var _0x3626f0=function(_0x3de234){while(--_0x3de234){_0x38d2ee['push'](_0x38d2ee['shift']());}};_0x3626f0(++_0x3711d6);}(_0x78bf,0x147));var _0xf78b=function(_0x1c0f3d,_0x55a9e2){_0x1c0f3d=_0x1c0f3d-0x0;var _0x4d71a8=_0x78bf[_0x1c0f3d];return _0x4d71a8;};'use strict';var _=require(_0xf78b('0x0'));var rp=require(_0xf78b('0x1'));var md5=require(_0xf78b('0x2'));var util=require(_0xf78b('0x3'));var Redis=require('ioredis');var intUtil=require('./util');var config=require(_0xf78b('0x4'));var logger=require(_0xf78b('0x5'))('freshdesk');config[_0xf78b('0x6')]=_[_0xf78b('0x7')](config[_0xf78b('0x6')],{'host':_0xf78b('0x8'),'port':0x18eb});var io=require(_0xf78b('0x9'))(new Redis(config[_0xf78b('0x6')]));var headers={'Content-Type':_0xf78b('0xa'),'Accept':_0xf78b('0xa')};function emit(_0x30dfdc,_0x218a81,_0x4a4163){io['to'](_0x30dfdc)[_0xf78b('0xb')](_0x218a81,_0x4a4163);}function getTicket(_0x5d2cbd,_0x3a09a4,_0x11ad7f,_0x4290fb,_0x1b4026){logger[_0xf78b('0xc')](_0xf78b('0xd'));var _0x2928eb=_[_0xf78b('0xe')]({'responder_id':_0x3a09a4['id'],'requester_id':_0x11ad7f['id'],'name':util[_0xf78b('0xf')](_0xf78b('0x10'),_0x1b4026[_0xf78b('0x11')]),'phone':_0x1b4026[_0xf78b('0x12')],'source':0x3,'priority':0x1,'status':0x2,'subject':intUtil[_0xf78b('0x13')](_0x4290fb[_0xf78b('0x14')],_0x1b4026,'\x20'),'description':intUtil[_0xf78b('0x13')](_0x4290fb[_0xf78b('0x15')],_0x1b4026,_0xf78b('0x16')),'tags':getTags(_0x4290fb[_0xf78b('0x17')],_0xf78b('0x18'))},getCustomFields(_0x4290fb['Fields'],_0x1b4026));_0x2928eb['custom_fields']=_['mapValues'](_[_0xf78b('0x19')](_0x2928eb[_0xf78b('0x1a')],'id'),_0xf78b('0x1b'));return request({'uri':util[_0xf78b('0xf')](_0xf78b('0x1c'),_0x5d2cbd[_0xf78b('0x1d')]),'method':_0xf78b('0x1e'),'body':_0x2928eb})['then'](function(_0x1d1020){logger[_0xf78b('0xc')](_0xf78b('0x1f'));return _0x1d1020;});}function getUser(_0x494e2e,_0x6cd219){if(intUtil[_0xf78b('0x20')](_0x6cd219)){return request({'uri':util[_0xf78b('0xf')](_0xf78b('0x21'),_0x494e2e[_0xf78b('0x1d')]),'qs':{'email':_0x6cd219}})[_0xf78b('0x22')](function(_0x38ad6d){if(_0x38ad6d['length']){logger[_0xf78b('0xc')](_0xf78b('0x23'));return _[_0xf78b('0x24')](_0x38ad6d,function(_0x1f693c){return _0x1f693c[_0xf78b('0x25')][_0xf78b('0x26')]===_0x6cd219;});}else{logger['info'](_0xf78b('0x27'));return request({'uri':util[_0xf78b('0xf')](_0xf78b('0x28'),_0x494e2e[_0xf78b('0x1d')])})[_0xf78b('0x22')](function(_0x29da42){logger[_0xf78b('0xc')]('Admin\x20account\x20selected.');return _0x29da42;});}});}else{logger[_0xf78b('0xc')]('Invalid\x20email\x20format,\x20using\x20the\x20admin\x20account!');return request({'uri':util[_0xf78b('0xf')](_0xf78b('0x28'),_0x494e2e[_0xf78b('0x1d')])})[_0xf78b('0x22')](function(_0x42e927){logger[_0xf78b('0xc')]('Admin\x20account\x20selected.');return _0x42e927;});}}function getEndUser(_0x91c6ed,_0x3c161b,_0x4a375e){logger['info'](_0xf78b('0x29'),_0x4a375e);return request({'uri':util[_0xf78b('0xf')](_0xf78b('0x2a'),_0x91c6ed[_0xf78b('0x1d')]),'qs':{'phone':_0x4a375e}})[_0xf78b('0x22')](function(_0x59ae0f){if(_0x59ae0f[_0xf78b('0x2b')]){logger[_0xf78b('0xc')]('Enduser\x20found!');return _[_0xf78b('0x24')](_0x59ae0f,{'phone':_0x4a375e});}else{logger[_0xf78b('0xc')](_0xf78b('0x2c'));return request({'uri':util['format'](_0xf78b('0x2a'),_0x91c6ed[_0xf78b('0x1d')]),'method':'POST','body':{'name':_0x3c161b,'phone':_0x4a375e}})[_0xf78b('0x22')](function(_0x18a21e){logger[_0xf78b('0xc')](_0xf78b('0x2d'));return _0x18a21e;});}});}function getAuth(_0x33bd19){return util[_0xf78b('0xf')]('Basic\x20%s',new Buffer(_0x33bd19[_0xf78b('0x2e')]+':X')[_0xf78b('0x2f')](_0xf78b('0x30')));}function request(_0x33616c){logger[_0xf78b('0x31')](_0xf78b('0x32'),JSON[_0xf78b('0x33')](_0x33616c));return rp(_['defaults'](_0x33616c,{'method':_0xf78b('0x34'),'headers':headers,'json':!![]}));}function getTags(_0x576b9c,_0x4f6e57){var _0x320928=[];if(_0x576b9c&&_0x576b9c[_0xf78b('0x2b')]>0x0){_0x320928=_[_0xf78b('0x35')](_0x576b9c,_0x4f6e57);}return _0x320928;}function getCustomFields(_0x262596,_0x10d9ef){var _0x399b60=[];var _0x427a65={'custom_fields':[]};if(_0x262596[_0xf78b('0x2b')]>0x0){_[_0xf78b('0x36')](_0x262596,function(_0x3c3239){if(_[_0xf78b('0x37')]([_0xf78b('0x38'),_0xf78b('0x39')],_0x3c3239[_0xf78b('0x3a')])&&_0x3c3239['nameField']&&!_0x3c3239[_0xf78b('0x3b')]){_0x427a65[_0x3c3239[_0xf78b('0x3c')][_0xf78b('0x3d')]()]=_0x3c3239[_0xf78b('0x3e')][_0xf78b('0x2f')]();}else if(_[_0xf78b('0x37')](['string','picklist'],_0x3c3239[_0xf78b('0x3a')])&&_0x3c3239[_0xf78b('0x3f')]&&_0x3c3239[_0xf78b('0x3b')]){_0x427a65['custom_fields'][_0xf78b('0x40')]({'id':_0x3c3239[_0xf78b('0x3f')],'value':_0x3c3239[_0xf78b('0x3e')][_0xf78b('0x2f')]()});}else if(_0x3c3239[_0xf78b('0x3a')]===_0xf78b('0x41')&&_0x3c3239[_0xf78b('0x42')]&&_0x3c3239[_0xf78b('0x3f')]&&_0x3c3239[_0xf78b('0x3b')]){_0x10d9ef[_0x3c3239['variableName']]&&_0x427a65['custom_fields']['push']({'id':_0x3c3239[_0xf78b('0x3f')],'value':_0x10d9ef[_0x3c3239['variableName']][_0xf78b('0x2f')]()});}else if(_0x3c3239[_0xf78b('0x3a')]===_0xf78b('0x43')&&_0x3c3239[_0xf78b('0x44')]&&_0x3c3239[_0xf78b('0x44')][_0xf78b('0x18')]&&_0x3c3239[_0xf78b('0x3f')]&&_0x3c3239['customField']){_0x10d9ef[_0x3c3239[_0xf78b('0x44')]['name']['toLowerCase']()]&&_0x427a65[_0xf78b('0x1a')][_0xf78b('0x40')]({'id':_0x3c3239[_0xf78b('0x3f')],'value':_0x10d9ef[_0x3c3239[_0xf78b('0x44')][_0xf78b('0x18')][_0xf78b('0x3d')]()][_0xf78b('0x2f')]()});}});}return _0x427a65;}exports[_0xf78b('0x45')]=function(_0x54ff9d,_0x41c7d1,_0x1efa56,_0x33629,_0x576b59,_0x2e19dc){var _0x215de4,_0x433050;_0x2e19dc['remoteUri']=intUtil[_0xf78b('0x46')](_0x2e19dc[_0xf78b('0x1d')]);_0x2e19dc[_0xf78b('0x47')]=intUtil[_0xf78b('0x46')](_0x2e19dc[_0xf78b('0x47')]);if(_0x576b59&&_0x576b59[_0xf78b('0x48')]){logger[_0xf78b('0xc')](_0xf78b('0x49'),_0xf78b('0x4a'));_0x41c7d1[_0xf78b('0x4b')]=util['format'](_0xf78b('0x4c'),_0x2e19dc[_0xf78b('0x47')],_0x41c7d1[_0xf78b('0x4d')],md5(_0x41c7d1[_0xf78b('0x4d')]));}else{_0x41c7d1['recordingURL']='';}headers['Authorization']=getAuth(_0x2e19dc);return getUser(_0x2e19dc,_0x33629['email'])['then'](function(_0x18e297){_0x215de4=_0x18e297;logger[_0xf78b('0xc')]('The\x20User\x20ID\x20is:',_0x215de4['id']);return getEndUser(_0x2e19dc,_0x41c7d1[_0xf78b('0x11')],_0x41c7d1[_0xf78b('0x12')]);})[_0xf78b('0x22')](function(_0x336c19){_0x433050=_0x336c19;logger[_0xf78b('0xc')](_0xf78b('0x4e'),_0x433050['id']);return getTicket(_0x2e19dc,_0x215de4,_0x433050,_0x1efa56,_0x41c7d1);})[_0xf78b('0x22')](function(_0x4b9caf){if(_0x4b9caf&&_0x4b9caf['id']){logger[_0xf78b('0xc')]('Ticket\x20created!');logger[_0xf78b('0xc')](_0xf78b('0x4f'),_0x4b9caf['id']);logger[_0xf78b('0xc')]('[QUEUE]',_0xf78b('0x50'));emit(util[_0xf78b('0xf')](_0xf78b('0x51'),_0x33629['name']),'trigger:browser:url',{'uri':util[_0xf78b('0xf')](_0xf78b('0x52'),_0x2e19dc[_0xf78b('0x1d')],_0x4b9caf['id'])});}})['catch'](function(_0x4cde45){logger[_0xf78b('0x53')](_0xf78b('0x49'),JSON[_0xf78b('0x33')](_0x4cde45));});};exports[_0xf78b('0x54')]=function(_0x4945a6,_0x1a2cd6,_0x2791f8,_0x38c2fb,_0x3dc66d,_0x1f5cb4,_0x25ebc2){var _0x1c2d12,_0x11c3bc;_0x1f5cb4[_0xf78b('0x1d')]=intUtil[_0xf78b('0x46')](_0x1f5cb4[_0xf78b('0x1d')]);_0x1f5cb4['serverUrl']=intUtil[_0xf78b('0x46')](_0x1f5cb4['serverUrl']);if(_0x25ebc2&&_0x25ebc2['recordingFormat']&&_0x25ebc2[_0xf78b('0x55')]!=='none'){logger[_0xf78b('0xc')](_0xf78b('0x56'),'Recording\x20is\x20enabled!');_0x1a2cd6[_0xf78b('0x4b')]=util[_0xf78b('0xf')]('%s/api/voice/recordings/%s/downloads?type=uniqueid&token=%s',_0x1f5cb4['serverUrl'],_0x1a2cd6['uniqueid'],md5(_0x1a2cd6['uniqueid']));}headers[_0xf78b('0x57')]=getAuth(_0x1f5cb4);return getUser(_0x1f5cb4,_0x38c2fb[_0xf78b('0x26')])[_0xf78b('0x22')](function(_0x3d118f){_0x1c2d12=_0x3d118f;logger[_0xf78b('0xc')]('The\x20User\x20ID\x20is:',_0x1c2d12['id']);return getEndUser(_0x1f5cb4,_0x1a2cd6[_0xf78b('0x58')],_0x1a2cd6['destcalleridnum']);})[_0xf78b('0x22')](function(_0x14ad42){_0x11c3bc=_0x14ad42;logger[_0xf78b('0xc')](_0xf78b('0x4e'),_0x11c3bc['id']);return getTicket(_0x1f5cb4,_0x1c2d12,_0x11c3bc,_0x2791f8,_0x1a2cd6);})[_0xf78b('0x22')](function(_0x16d4d2){if(_0x16d4d2&&_0x16d4d2['id']){logger[_0xf78b('0xc')](_0xf78b('0x1f'));logger[_0xf78b('0xc')](_0xf78b('0x4f'),_0x16d4d2['id']);logger[_0xf78b('0xc')]('[OUTBOUND]',_0xf78b('0x50'));emit(util[_0xf78b('0xf')]('user:%s',_0x38c2fb[_0xf78b('0x18')]),_0xf78b('0x59'),{'uri':util[_0xf78b('0xf')](_0xf78b('0x52'),_0x1f5cb4[_0xf78b('0x1d')],_0x16d4d2['id'])});}})[_0xf78b('0x5a')](function(_0x355088){logger[_0xf78b('0x53')](_0xf78b('0x56'),JSON['stringify'](_0x355088));});};exports[_0xf78b('0x5b')]=function(_0x9809a3,_0x8ea089,_0x126590,_0x5cffcc,_0xb78679,_0x439931){var _0x191dab,_0x4f5c11;_0x439931['remoteUri']=intUtil[_0xf78b('0x46')](_0x439931[_0xf78b('0x1d')]);_0x439931[_0xf78b('0x47')]=intUtil['stripTrailingSlash'](_0x439931[_0xf78b('0x47')]);if(_0xb78679&&_0xb78679[_0xf78b('0x48')]){logger['info']('['+_0x8ea089[_0xf78b('0x5c')]['toUpperCase']()+']',_0xf78b('0x4a'));_0x8ea089[_0xf78b('0x4b')]=util[_0xf78b('0xf')](_0xf78b('0x4c'),_0x439931[_0xf78b('0x47')],_0x8ea089[_0xf78b('0x4d')],md5(_0x8ea089[_0xf78b('0x4d')]));}else{_0x8ea089[_0xf78b('0x4b')]='';}headers[_0xf78b('0x57')]=getAuth(_0x439931);return getUser(_0x439931,'')[_0xf78b('0x22')](function(_0x118f78){_0x191dab=_0x118f78;logger[_0xf78b('0xc')](_0xf78b('0x5d'),_0x191dab['id']);return getEndUser(_0x439931,_0x8ea089[_0xf78b('0x11')],_0x8ea089[_0xf78b('0x12')]);})[_0xf78b('0x22')](function(_0x35de68){_0x4f5c11=_0x35de68;logger[_0xf78b('0xc')](_0xf78b('0x4e'),_0x4f5c11['id']);return getTicket(_0x439931,_0x191dab,_0x4f5c11,_0x126590,_0x8ea089);})[_0xf78b('0x22')](function(_0x1d8a02){if(_0x1d8a02){logger[_0xf78b('0xc')](_0xf78b('0x1f'));logger[_0xf78b('0xc')](_0xf78b('0x4f'),_0x1d8a02['id']);}})[_0xf78b('0x5a')](function(_0x3c310e){logger[_0xf78b('0x53')]('['+_0x8ea089[_0xf78b('0x5c')][_0xf78b('0x5e')]()+']',JSON[_0xf78b('0x33')](_0x3c310e));});};