8d660872904ff25a400b71ae21ff8547a0be9874
[motion2.git] / server / services / routing / server / applications / autoreply.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 _0x208f=['Message','service','Smtp','host','port','secure','auth','user','pass','inReplyTo','messageId','html','replace','<div','</div>','headers','auto-replied','All','auto_reply','then','create','[%s][AUTOREPLY]\x20%s','stringify','catch','error','[RPC][%s][AUTOREPLY]\x20%s','finally','resolve','autoreplyExecuted','Interaction','[RPC][%s][AUTOREPLY][autoreplyExecuted]\x20%s','update','stop','lodash','moment','./utils','../../../../config/logger','routing','../../rpc/chatMessage','../../rpc/openchannelMessage','../../rpc/mailMessage','../../rpc/smsMessage','../../rpc/whatsappMessage','../../rpc/openchannelInteraction','../../rpc/mailInteraction','../../rpc/faxInteraction','../../rpc/whatsappInteraction','../../mailRpc','consequence','channel','body','isAutoreply','info','isNil','interaction','created','Autoreply\x20already\x20sent','next','[%s][AUTOREPLY][autoreplyExecuted]\x20%s','One\x20Time\x20already\x20sent','contact','format','YYYY-MM-DD\x20HH:mm:ss','mail','getMustacheRender','message','from','phone','subject','Re:','Re:\x20','ChatWebsiteId','upperFirst','AccountId','account','InteractionId'];(function(_0x168fd3,_0x11fbbd){var _0x47f2b7=function(_0x5971ed){while(--_0x5971ed){_0x168fd3['push'](_0x168fd3['shift']());}};_0x47f2b7(++_0x11fbbd);}(_0x208f,0x6d));var _0xf208=function(_0x46ed3a,_0x35b0b3){_0x46ed3a=_0x46ed3a-0x0;var _0x3caeb8=_0x208f[_0x46ed3a];return _0x3caeb8;};'use strict';var _=require(_0xf208('0x0'));var moment=require(_0xf208('0x1'));var utils=require(_0xf208('0x2'));var logger=require(_0xf208('0x3'))(_0xf208('0x4'));var rpc={'chatMessage':require(_0xf208('0x5')),'openchannelMessage':require(_0xf208('0x6')),'mailMessage':require(_0xf208('0x7')),'smsMessage':require(_0xf208('0x8')),'faxMessage':require('../../rpc/faxMessage'),'whatsappMessage':require(_0xf208('0x9')),'chatInteraction':require('../../rpc/chatInteraction'),'openchannelInteraction':require(_0xf208('0xa')),'mailInteraction':require(_0xf208('0xb')),'smsInteraction':require('../../rpc/smsInteraction'),'faxInteraction':require(_0xf208('0xc')),'whatsappInteraction':require(_0xf208('0xd'))};var mailRpc=require(_0xf208('0xe'));exports[_0xf208('0xf')]=function(_0x505dff,_0x49bb78,_0x208f1f,_0x1bd1b8){return function(_0xf0738c){try{var _0xf76ff1=this[_0xf208('0x10')];if(_0xf76ff1==='mail'&&_0x1bd1b8[_0xf208('0x11')][_0xf208('0x12')]){logger[_0xf208('0x13')]('[%s][AUTOREPLY]\x20%s',_0x505dff,'Autoreply\x20not\x20executed\x20(message\x20is\x20autoreply)');return _0xf0738c['next']();}if(_0x49bb78[0x0]==='1'&&(_[_0xf208('0x14')](this[_0xf208('0x15')][_0xf208('0x16')])||!this[_0xf208('0x15')][_0xf208('0x16')])){logger[_0xf208('0x13')]('[%s][AUTOREPLY]\x20%s',_0x505dff,_0xf208('0x17'));return _0xf0738c[_0xf208('0x18')]();}if(_0x49bb78[0x0]==='2'&&this[_0xf208('0x15')]['autoreplyExecuted']){logger[_0xf208('0x13')](_0xf208('0x19'),_0x505dff,_0xf208('0x1a'));return _0xf0738c['next']();}var _0x46a65c={'ContactId':this[_0xf208('0x1b')]['id'],'sentAt':moment()[_0xf208('0x1c')](_0xf208('0x1d')),'direction':'out'};if(this['channel']===_0xf208('0x1e')){_0x46a65c[_0xf208('0x11')]=utils[_0xf208('0x1f')](_0x49bb78[0x2],this);}else{_0x46a65c[_0xf208('0x11')]=utils[_0xf208('0x1f')](_0x49bb78[0x1],this);}if(this[_0xf208('0x20')]){_0x46a65c['to']=this[_0xf208('0x20')][_0xf208('0x21')]||'';if(this[_0xf208('0x10')]==='mail'&&_0x49bb78[0x1]==='1'){_0x46a65c['cc']=this[_0xf208('0x20')]['cc']||'';}_0x46a65c[_0xf208('0x21')]=this[_0xf208('0x20')]['to']||'';_0x46a65c[_0xf208('0x22')]=this[_0xf208('0x20')][_0xf208('0x22')]||'';_0x46a65c['read']=!![];if(this[_0xf208('0x20')][_0xf208('0x23')]){_0x46a65c[_0xf208('0x23')]='';_0x46a65c[_0xf208('0x23')]+=_['startsWith'](this[_0xf208('0x20')][_0xf208('0x23')],_0xf208('0x24'))?'':_0xf208('0x25');_0x46a65c['subject']+=this[_0xf208('0x20')][_0xf208('0x23')];}}if(this[_0xf208('0x10')]==='chat'){_0x46a65c[_0xf208('0x26')]=this['account']['id'];}else{_0x46a65c[_[_0xf208('0x27')](this[_0xf208('0x10')])+_0xf208('0x28')]=this[_0xf208('0x29')]['id'];}_0x46a65c[_[_0xf208('0x27')](this[_0xf208('0x10')])+_0xf208('0x2a')]=this[_0xf208('0x15')]['id'];var _0x4e80d6=this[_0xf208('0x15')];if(rpc[this[_0xf208('0x10')]+_0xf208('0x2b')]){if(this[_0xf208('0x10')]===_0xf208('0x1e')){var _0x3061c3={'tls':{'rejectUnauthorized':![]}};if(this[_0xf208('0x29')]['Smtp'][_0xf208('0x2c')]){_0x3061c3[_0xf208('0x2c')]=this['account'][_0xf208('0x2d')][_0xf208('0x2c')];}else{_0x3061c3[_0xf208('0x2e')]=this[_0xf208('0x29')][_0xf208('0x2d')][_0xf208('0x2e')];_0x3061c3[_0xf208('0x2f')]=this['account'][_0xf208('0x2d')][_0xf208('0x2f')];_0x3061c3[_0xf208('0x30')]=this[_0xf208('0x29')]['Smtp'][_0xf208('0x30')];}if(this[_0xf208('0x29')][_0xf208('0x2d')]['authentication']){_0x3061c3[_0xf208('0x31')]={'user':this[_0xf208('0x29')]['Smtp'][_0xf208('0x32')],'pass':this['account'][_0xf208('0x2d')][_0xf208('0x33')]};}_0x46a65c[_0xf208('0x34')]=this[_0xf208('0x20')][_0xf208('0x35')];_0x46a65c[_0xf208('0x36')]='<div\x20id=\x22xm-mail-content\x22>'+_[_0xf208('0x37')](_0x46a65c[_0xf208('0x11')],'<div\x20id=\x22xm-mail-content\x22',_0xf208('0x38'))+_0xf208('0x39');_0x46a65c[_0xf208('0x3a')]={'Auto-Submitted':_0xf208('0x3b'),'X-Auto-Response-Suppress':_0xf208('0x3c'),'Precedence':_0xf208('0x3d')};return mailRpc['sendMailMessage'](_0x3061c3,_0x46a65c)[_0xf208('0x3e')](function(_0x116fa3){logger[_0xf208('0x13')]('[%s][AUTOREPLY]\x20SMTP\x20SENT',_0x505dff);_0x46a65c[_0xf208('0x35')]=_0x116fa3[_0xf208('0x20')]['messageId'];return rpc[_0xf76ff1+_0xf208('0x2b')][_0xf208('0x3f')](_0x46a65c);})[_0xf208('0x3e')](function(_0x3e9c3e){logger['info'](_0xf208('0x40'),_0x505dff,JSON[_0xf208('0x41')](_0x3e9c3e));})[_0xf208('0x42')](function(_0x2ee254){logger[_0xf208('0x43')](_0xf208('0x44'),_0x505dff,JSON['stringify'](_0x2ee254));})[_0xf208('0x45')](function(){Promise[_0xf208('0x46')]()[_0xf208('0x3e')](function(){if(_0x49bb78[0x0]!=='2'||_0x4e80d6[_0xf208('0x47')])return;return rpc[_0xf76ff1+_0xf208('0x48')]['update'](_0x4e80d6['id'],{'autoreplyExecuted':!![]})[_0xf208('0x3e')](function(_0x4cac0e){logger[_0xf208('0x13')](_0xf208('0x19'),_0x505dff,JSON[_0xf208('0x41')](_0x4cac0e));})[_0xf208('0x42')](function(_0x58f292){logger['error'](_0xf208('0x49'),_0x505dff,JSON[_0xf208('0x41')](_0x58f292));});})[_0xf208('0x3e')](function(){_0xf0738c[_0xf208('0x18')]();});});}else{return rpc[this[_0xf208('0x10')]+_0xf208('0x2b')][_0xf208('0x3f')](_0x46a65c)['then'](function(_0x36c418){logger['info'](_0xf208('0x40'),_0x505dff,_0x46a65c['body']);})[_0xf208('0x42')](function(_0x249150){logger[_0xf208('0x43')](_0xf208('0x40'),_0x505dff,JSON[_0xf208('0x41')](_0x249150));})[_0xf208('0x45')](function(){Promise[_0xf208('0x46')]()[_0xf208('0x3e')](function(){if(_0x49bb78[0x0]!=='2'||_0x4e80d6['autoreplyExecuted'])return;return rpc[_0xf76ff1+_0xf208('0x48')][_0xf208('0x4a')](_0x4e80d6['id'],{'autoreplyExecuted':!![]})[_0xf208('0x3e')](function(_0x37364d){logger[_0xf208('0x13')](_0xf208('0x19'),_0x505dff,JSON['stringify'](_0x37364d));})['catch'](function(_0xe19d1a){logger[_0xf208('0x43')](_0xf208('0x49'),_0x505dff,JSON[_0xf208('0x41')](_0xe19d1a));});})[_0xf208('0x3e')](function(){_0xf0738c[_0xf208('0x18')]();});});}}}catch(_0x10b3ab){logger['error'](_0xf208('0x40'),_0x505dff,JSON[_0xf208('0x41')](_0x10b3ab));_0xf0738c[_0xf208('0x4b')]();}};};