Built motion from commit 980b26c3.|2.6.29
[motion2.git] / server / services / routing / server / applications / dialogflowV2.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 _0xf0f4=['end_conversation','boolValue','DIALOGFLOWV2_ACTION','action','allRequiredParamsPresent','DIALOGFLOWV2_ENDCONVERSATION','DIALOGFLOWV2_INTENTNAME','intent','displayName','languageCode','DIALOGFLOWV2_QUERYTEXT','queryText','intentDetectionConfidence','providerName','providerResponse','fulfillmentText','all','stack','lodash','md5','moment','bluebird','../../../../config/logger','./utils','../../rpc/chatMessage','../../rpc/openchannelMessage','../../rpc/smsMessage','../../rpc/faxMessage','../../rpc/whatsappMessage','clone','body','create','info','[%s][DIALOGFLOWV2]\x20%s','error','stringify','consequence','isNil','interaction','UserId','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','next','format','YYYY-MM-DD\x20HH:mm:ss','out','channel','whatsapp','phone','chat','ChatWebsiteId','account','upperFirst','AccountId','InteractionId','created','isEmpty','then','catch','finally','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!','stop','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!','replace','SessionsClient','sessionPath','message','length','queryResult','diagnosticInfo','fields'];(function(_0x1993b5,_0x31e4b8){var _0x5a43b4=function(_0x552373){while(--_0x552373){_0x1993b5['push'](_0x1993b5['shift']());}};_0x5a43b4(++_0x31e4b8);}(_0xf0f4,0x9e));var _0x4f0f=function(_0x31610c,_0x2c52e9){_0x31610c=_0x31610c-0x0;var _0xd330aa=_0xf0f4[_0x31610c];return _0xd330aa;};'use strict';var _=require(_0x4f0f('0x0'));var dialogflow=require('dialogflow');var md5=require(_0x4f0f('0x1'));var moment=require(_0x4f0f('0x2'));var Promise=require(_0x4f0f('0x3'));var logger=require(_0x4f0f('0x4'))('routing');var utils=require(_0x4f0f('0x5'));var rpc={'chatMessage':require(_0x4f0f('0x6')),'openchannelMessage':require(_0x4f0f('0x7')),'mailMessage':require('../../rpc/mailMessage'),'smsMessage':require(_0x4f0f('0x8')),'faxMessage':require(_0x4f0f('0x9')),'whatsappMessage':require(_0x4f0f('0xa'))};function createMessage(_0x4457b7,_0x3aa81e,_0x264602,_0x59c56e){return new Promise(function(_0x49d930,_0x54275d){var _0x3e7904=_[_0x4f0f('0xb')](_0x264602);_0x3e7904[_0x4f0f('0xc')]=_0x59c56e;return rpc[_0x3aa81e+'Message'][_0x4f0f('0xd')](_0x3e7904)['then'](function(){logger[_0x4f0f('0xe')](_0x4f0f('0xf'),_0x4457b7,_0x3e7904['body']);_0x49d930();})['catch'](function(_0x3eb591){logger[_0x4f0f('0x10')](_0x4f0f('0xf'),_0x4457b7,JSON[_0x4f0f('0x11')](_0x3eb591));_0x54275d(_0x3eb591);});});}exports[_0x4f0f('0x12')]=function(_0x1a762d,_0x792e65){return function(_0x473dc4){try{if(!_[_0x4f0f('0x13')](this[_0x4f0f('0x14')][_0x4f0f('0x15')])){logger[_0x4f0f('0xe')](_0x4f0f('0xf'),_0x1a762d,_0x4f0f('0x16'));return _0x473dc4[_0x4f0f('0x17')]();}var _0x40b107={'ContactId':this['contact']['id'],'sentAt':moment()[_0x4f0f('0x18')](_0x4f0f('0x19')),'direction':_0x4f0f('0x1a')};if(this[_0x4f0f('0x1b')]==='sms'||this['channel']===_0x4f0f('0x1c')){_0x40b107[_0x4f0f('0x1d')]=this[_0x4f0f('0x14')][_0x4f0f('0x1d')];}var _0xe0cf15=this[_0x4f0f('0x1b')];if(_0xe0cf15===_0x4f0f('0x1e')){_0x40b107[_0x4f0f('0x1f')]=this[_0x4f0f('0x20')]['id'];}else{_0x40b107[_[_0x4f0f('0x21')](_0xe0cf15)+_0x4f0f('0x22')]=this[_0x4f0f('0x20')]['id'];}_0x40b107[_[_0x4f0f('0x21')](_0xe0cf15)+_0x4f0f('0x23')]=this[_0x4f0f('0x14')]['id'];if(this[_0x4f0f('0x14')][_0x4f0f('0x24')]){var _0x50995f=_0x792e65[0x4];if(!_[_0x4f0f('0x25')](_0x50995f)){_0x40b107[_0x4f0f('0xc')]=utils['getMustacheRender'](_0x50995f,this);return rpc[_0xe0cf15+'Message'][_0x4f0f('0xd')](_0x40b107)[_0x4f0f('0x26')](function(_0x6b1dbd){logger[_0x4f0f('0xe')](_0x4f0f('0xf'),_0x1a762d,_0x40b107['body']);})[_0x4f0f('0x27')](function(_0x6d9c6d){logger[_0x4f0f('0x10')]('[%s][DIALOGFLOWV2]\x20%s',_0x1a762d,JSON['stringify'](_0x6d9c6d));})[_0x4f0f('0x28')](function(){logger['info'](_0x4f0f('0x29'),_0x1a762d);_0x473dc4[_0x4f0f('0x2a')]();});}else{logger[_0x4f0f('0xe')](_0x4f0f('0x2b'),_0x1a762d);_0x473dc4[_0x4f0f('0x2a')]();}}else{var _0x46788e=this;var _0x507785=_0x792e65[0x2][_0x4f0f('0x2c')](/\\n/g,'\x0a');var _0x3b8213={'credentials':{'private_key':_0x507785,'client_email':_0x792e65[0x1]}};var _0x42fe60=new dialogflow[(_0x4f0f('0x2d'))](_0x3b8213);var _0x53edb3=_0x42fe60[_0x4f0f('0x2e')](_0x792e65[0x0],md5(_0x46788e[_0x4f0f('0x14')]['id']));var _0x200d18={'session':_0x53edb3,'queryInput':{'text':{'text':_0x46788e[_0x4f0f('0x2f')][_0x4f0f('0xc')],'languageCode':_0x792e65[0x3]||'en'}}};return _0x42fe60['detectIntent'](_0x200d18)[_0x4f0f('0x26')](function(_0x2589f6){if(_0x2589f6&&_0x2589f6[_0x4f0f('0x30')]){_0x46788e[_0x4f0f('0xc')]['dialogflowV2']=_0x2589f6;var _0x327c95=_0x2589f6[0x0];var _0x1ab0c4=_0x327c95['queryResult']['diagnosticInfo']&&_0x327c95[_0x4f0f('0x31')][_0x4f0f('0x32')]['fields']?_0x327c95['queryResult'][_0x4f0f('0x32')][_0x4f0f('0x33')][_0x4f0f('0x34')][_0x4f0f('0x35')]:![];_0x46788e['body'][_0x4f0f('0x36')]=_0x327c95[_0x4f0f('0x31')][_0x4f0f('0x37')];_0x46788e[_0x4f0f('0xc')]['DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT']=_0x327c95[_0x4f0f('0x31')][_0x4f0f('0x38')];_0x46788e[_0x4f0f('0xc')][_0x4f0f('0x39')]=_0x327c95[_0x4f0f('0x31')][_0x4f0f('0x32')]&&_0x327c95[_0x4f0f('0x31')]['diagnosticInfo'][_0x4f0f('0x33')]?_0x327c95[_0x4f0f('0x31')]['diagnosticInfo'][_0x4f0f('0x33')][_0x4f0f('0x34')][_0x4f0f('0x35')]:![];_0x46788e['body'][_0x4f0f('0x3a')]=_0x327c95[_0x4f0f('0x31')][_0x4f0f('0x3b')]['name'];_0x46788e['body']['DIALOGFLOWV2_INTENTDISPLAYNAME']=_0x327c95[_0x4f0f('0x31')][_0x4f0f('0x3b')][_0x4f0f('0x3c')];_0x46788e[_0x4f0f('0xc')]['DIALOGFLOWV2_ISFALLBACKINTENT']=_0x327c95[_0x4f0f('0x31')][_0x4f0f('0x3b')]['isFallback'];_0x46788e[_0x4f0f('0xc')]['DIALOGFLOWV2_LANGUAGECODE']=_0x327c95['queryResult'][_0x4f0f('0x3d')];_0x46788e['body'][_0x4f0f('0x3e')]=_0x327c95[_0x4f0f('0x31')][_0x4f0f('0x3f')];_0x46788e[_0x4f0f('0xc')]['DIALOGFLOWV2_SCORE']=_0x327c95['queryResult'][_0x4f0f('0x40')];_0x40b107[_0x4f0f('0x41')]='dialogflowV2';_0x40b107[_0x4f0f('0x42')]=JSON[_0x4f0f('0x11')](_0x2589f6);var _0x24963b=[];for(var _0x4ccb3b=0x0;_0x4ccb3b<_0x2589f6['length'];_0x4ccb3b++){var _0x2df9d0=_0x2589f6[_0x4ccb3b];if(_0x2df9d0&&_0x2df9d0[_0x4f0f('0x31')]&&_0x2df9d0[_0x4f0f('0x31')][_0x4f0f('0x43')]){_0x24963b['push'](createMessage(_0x1a762d,_0xe0cf15,_0x40b107,_0x2df9d0['queryResult']['fulfillmentText']));}}return Promise[_0x4f0f('0x44')](_0x24963b)['then'](function(){if(_0x1ab0c4){_0x473dc4[_0x4f0f('0x17')]();}else{_0x473dc4[_0x4f0f('0x2a')]();}})[_0x4f0f('0x27')](function(_0x36d36c){logger[_0x4f0f('0x10')](_0x4f0f('0xf'),_0x1a762d,JSON['stringify'](_0x36d36c));_0x473dc4[_0x4f0f('0x17')]();});}})[_0x4f0f('0x27')](function(_0x33bc67){logger[_0x4f0f('0x10')](_0x4f0f('0xf'),_0x1a762d,_0x33bc67[_0x4f0f('0x2f')],_0x33bc67[_0x4f0f('0x45')]);_0x473dc4[_0x4f0f('0x17')]();});}}catch(_0x53718f){logger['error'](_0x4f0f('0xf'),_0x1a762d,JSON['stringify'](_0x53718f));_0x473dc4['stop']();}};};