Built motion from commit 19a84241.|2.5.34
[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 _0x97d8=['host','port','secure','authentication','auth','user','pass','inReplyTo','messageId','html','<div\x20id=\x22xm-mail-content\x22','auto-replied','auto_reply','then','create','catch','error','[RPC][%s][AUTOREPLY]\x20%s','stringify','resolve','Interaction','update','[%s][AUTOREPLY][autoreplyExecuted]\x20%s','[RPC][%s][AUTOREPLY][autoreplyExecuted]\x20%s','finally','stop','lodash','moment','./utils','../../../../config/logger','routing','../../rpc/chatMessage','../../rpc/openchannelMessage','../../rpc/mailMessage','../../rpc/whatsappMessage','../../rpc/openchannelInteraction','../../rpc/mailInteraction','../../rpc/smsInteraction','../../rpc/faxInteraction','../../rpc/whatsappInteraction','consequence','mail','body','isAutoreply','info','[%s][AUTOREPLY]\x20%s','isNil','interaction','created','next','autoreplyExecuted','One\x20Time\x20already\x20sent','contact','format','YYYY-MM-DD\x20HH:mm:ss','getMustacheRender','message','from','channel','phone','read','subject','startsWith','Re:\x20','chat','ChatWebsiteId','account','upperFirst','AccountId','InteractionId','Message','Smtp','service'];(function(_0x3d4b29,_0x12ad14){var _0x324a3a=function(_0x267496){while(--_0x267496){_0x3d4b29['push'](_0x3d4b29['shift']());}};_0x324a3a(++_0x12ad14);}(_0x97d8,0x13e));var _0x897d=function(_0x4350c2,_0x112f3c){_0x4350c2=_0x4350c2-0x0;var _0x8b6406=_0x97d8[_0x4350c2];return _0x8b6406;};'use strict';var _=require(_0x897d('0x0'));var moment=require(_0x897d('0x1'));var utils=require(_0x897d('0x2'));var logger=require(_0x897d('0x3'))(_0x897d('0x4'));var rpc={'chatMessage':require(_0x897d('0x5')),'openchannelMessage':require(_0x897d('0x6')),'mailMessage':require(_0x897d('0x7')),'smsMessage':require('../../rpc/smsMessage'),'faxMessage':require('../../rpc/faxMessage'),'whatsappMessage':require(_0x897d('0x8')),'chatInteraction':require('../../rpc/chatInteraction'),'openchannelInteraction':require(_0x897d('0x9')),'mailInteraction':require(_0x897d('0xa')),'smsInteraction':require(_0x897d('0xb')),'faxInteraction':require(_0x897d('0xc')),'whatsappInteraction':require(_0x897d('0xd'))};var mailRpc=require('../../mailRpc');exports[_0x897d('0xe')]=function(_0x3bf46b,_0x2db2e4,_0x141f47,_0x2126e1){return function(_0x4db103){try{var _0x2ba6a3=this['channel'];if(_0x2ba6a3===_0x897d('0xf')&&_0x2126e1[_0x897d('0x10')][_0x897d('0x11')]){logger[_0x897d('0x12')](_0x897d('0x13'),_0x3bf46b,'Autoreply\x20not\x20executed\x20(message\x20is\x20autoreply)');return _0x4db103['next']();}if(_0x2db2e4[0x0]==='1'&&(_[_0x897d('0x14')](this[_0x897d('0x15')][_0x897d('0x16')])||!this['interaction']['created'])){logger[_0x897d('0x12')](_0x897d('0x13'),_0x3bf46b,'Autoreply\x20already\x20sent');return _0x4db103[_0x897d('0x17')]();}if(_0x2db2e4[0x0]==='2'&&this[_0x897d('0x15')][_0x897d('0x18')]){logger[_0x897d('0x12')]('[%s][AUTOREPLY][autoreplyExecuted]\x20%s',_0x3bf46b,_0x897d('0x19'));return _0x4db103[_0x897d('0x17')]();}var _0x4375d3={'ContactId':this[_0x897d('0x1a')]['id'],'sentAt':moment()[_0x897d('0x1b')](_0x897d('0x1c')),'direction':'out'};if(this['channel']===_0x897d('0xf')){_0x4375d3['body']=utils[_0x897d('0x1d')](_0x2db2e4[0x2],this);}else{_0x4375d3['body']=utils[_0x897d('0x1d')](_0x2db2e4[0x1],this);}if(this[_0x897d('0x1e')]){_0x4375d3['to']=this[_0x897d('0x1e')][_0x897d('0x1f')]||'';if(this[_0x897d('0x20')]===_0x897d('0xf')&&_0x2db2e4[0x1]==='1'){_0x4375d3['cc']=this[_0x897d('0x1e')]['cc']||'';}_0x4375d3[_0x897d('0x1f')]=this[_0x897d('0x1e')]['to']||'';_0x4375d3['phone']=this['message'][_0x897d('0x21')]||'';_0x4375d3[_0x897d('0x22')]=!![];if(this['message']['subject']){_0x4375d3[_0x897d('0x23')]='';_0x4375d3[_0x897d('0x23')]+=_[_0x897d('0x24')](this[_0x897d('0x1e')][_0x897d('0x23')],'Re:')?'':_0x897d('0x25');_0x4375d3[_0x897d('0x23')]+=this[_0x897d('0x1e')][_0x897d('0x23')];}}if(this['channel']===_0x897d('0x26')){_0x4375d3[_0x897d('0x27')]=this[_0x897d('0x28')]['id'];}else{_0x4375d3[_[_0x897d('0x29')](this[_0x897d('0x20')])+_0x897d('0x2a')]=this[_0x897d('0x28')]['id'];}_0x4375d3[_[_0x897d('0x29')](this[_0x897d('0x20')])+_0x897d('0x2b')]=this['interaction']['id'];var _0x23deb5=this[_0x897d('0x15')];if(rpc[this[_0x897d('0x20')]+_0x897d('0x2c')]){if(this[_0x897d('0x20')]==='mail'){var _0x1f9ffd={'tls':{'rejectUnauthorized':![]}};if(this['account'][_0x897d('0x2d')]['service']){_0x1f9ffd['service']=this[_0x897d('0x28')][_0x897d('0x2d')][_0x897d('0x2e')];}else{_0x1f9ffd[_0x897d('0x2f')]=this[_0x897d('0x28')][_0x897d('0x2d')][_0x897d('0x2f')];_0x1f9ffd['port']=this[_0x897d('0x28')][_0x897d('0x2d')][_0x897d('0x30')];_0x1f9ffd[_0x897d('0x31')]=this[_0x897d('0x28')][_0x897d('0x2d')][_0x897d('0x31')];}if(this['account']['Smtp'][_0x897d('0x32')]){_0x1f9ffd[_0x897d('0x33')]={'user':this[_0x897d('0x28')][_0x897d('0x2d')][_0x897d('0x34')],'pass':this[_0x897d('0x28')][_0x897d('0x2d')][_0x897d('0x35')]};}_0x4375d3[_0x897d('0x36')]=this[_0x897d('0x1e')][_0x897d('0x37')];_0x4375d3[_0x897d('0x38')]='<div\x20id=\x22xm-mail-content\x22>'+_['replace'](_0x4375d3[_0x897d('0x10')],_0x897d('0x39'),'<div')+'</div>';_0x4375d3['headers']={'Auto-Submitted':_0x897d('0x3a'),'X-Auto-Response-Suppress':'All','Precedence':_0x897d('0x3b')};return mailRpc['sendMailMessage'](_0x1f9ffd,_0x4375d3)[_0x897d('0x3c')](function(_0x5b866c){logger[_0x897d('0x12')]('[%s][AUTOREPLY]\x20SMTP\x20SENT',_0x3bf46b);_0x4375d3[_0x897d('0x37')]=_0x5b866c[_0x897d('0x1e')][_0x897d('0x37')];return rpc[_0x2ba6a3+_0x897d('0x2c')][_0x897d('0x3d')](_0x4375d3);})['then'](function(_0x4550cb){logger[_0x897d('0x12')](_0x897d('0x13'),_0x3bf46b,JSON['stringify'](_0x4550cb));})[_0x897d('0x3e')](function(_0x1fec3d){logger[_0x897d('0x3f')](_0x897d('0x40'),_0x3bf46b,JSON[_0x897d('0x41')](_0x1fec3d));})['finally'](function(){Promise[_0x897d('0x42')]()[_0x897d('0x3c')](function(){if(_0x2db2e4[0x0]!=='2'||_0x23deb5['autoreplyExecuted'])return;return rpc[_0x2ba6a3+_0x897d('0x43')][_0x897d('0x44')](_0x23deb5['id'],{'autoreplyExecuted':!![]})['then'](function(_0x366aaf){logger['info'](_0x897d('0x45'),_0x3bf46b,JSON[_0x897d('0x41')](_0x366aaf));})['catch'](function(_0xe9f1ac){logger['error'](_0x897d('0x46'),_0x3bf46b,JSON[_0x897d('0x41')](_0xe9f1ac));});})[_0x897d('0x3c')](function(){_0x4db103[_0x897d('0x17')]();});});}else{return rpc[this[_0x897d('0x20')]+_0x897d('0x2c')][_0x897d('0x3d')](_0x4375d3)[_0x897d('0x3c')](function(_0x5a7428){logger[_0x897d('0x12')](_0x897d('0x13'),_0x3bf46b,_0x4375d3[_0x897d('0x10')]);})[_0x897d('0x3e')](function(_0x3628a0){logger['error'](_0x897d('0x13'),_0x3bf46b,JSON['stringify'](_0x3628a0));})[_0x897d('0x47')](function(){Promise[_0x897d('0x42')]()[_0x897d('0x3c')](function(){if(_0x2db2e4[0x0]!=='2'||_0x23deb5['autoreplyExecuted'])return;return rpc[_0x2ba6a3+'Interaction'][_0x897d('0x44')](_0x23deb5['id'],{'autoreplyExecuted':!![]})['then'](function(_0x2d3027){logger[_0x897d('0x12')](_0x897d('0x45'),_0x3bf46b,JSON[_0x897d('0x41')](_0x2d3027));})['catch'](function(_0x2b97de){logger['error']('[RPC][%s][AUTOREPLY][autoreplyExecuted]\x20%s',_0x3bf46b,JSON['stringify'](_0x2b97de));});})[_0x897d('0x3c')](function(){_0x4db103[_0x897d('0x17')]();});});}}}catch(_0x3f3638){logger[_0x897d('0x3f')](_0x897d('0x13'),_0x3bf46b,JSON['stringify'](_0x3f3638));_0x4db103[_0x897d('0x48')]();}};};