Built motion from commit b5996064.|2.6.21
[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 _0x457f=['DIALOGFLOWV2_SCORE','intentDetectionConfidence','providerName','providerResponse','length','fulfillmentText','push','all','message','stack','dialogflow','md5','moment','bluebird','../../../../config/logger','routing','./utils','../../rpc/openchannelMessage','../../rpc/mailMessage','../../rpc/faxMessage','../../rpc/whatsappMessage','clone','body','then','[%s][DIALOGFLOWV2]\x20%s','catch','error','stringify','consequence','isNil','interaction','UserId','info','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','next','contact','YYYY-MM-DD\x20HH:mm:ss','channel','sms','phone','chat','ChatWebsiteId','account','upperFirst','AccountId','created','isEmpty','finally','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!','stop','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!','replace','sessionPath','dialogflowV2','queryResult','diagnosticInfo','fields','end_conversation','boolValue','action','DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT','allRequiredParamsPresent','DIALOGFLOWV2_ENDCONVERSATION','intent','name','DIALOGFLOWV2_INTENTDISPLAYNAME','displayName','DIALOGFLOWV2_ISFALLBACKINTENT','isFallback','languageCode','DIALOGFLOWV2_QUERYTEXT','queryText'];(function(_0x14349b,_0x1eb24d){var _0x34f8cb=function(_0x100008){while(--_0x100008){_0x14349b['push'](_0x14349b['shift']());}};_0x34f8cb(++_0x1eb24d);}(_0x457f,0x172));var _0xf457=function(_0x1f6ff3,_0x462810){_0x1f6ff3=_0x1f6ff3-0x0;var _0x15488d=_0x457f[_0x1f6ff3];return _0x15488d;};'use strict';var _=require('lodash');var dialogflow=require(_0xf457('0x0'));var md5=require(_0xf457('0x1'));var moment=require(_0xf457('0x2'));var Promise=require(_0xf457('0x3'));var logger=require(_0xf457('0x4'))(_0xf457('0x5'));var utils=require(_0xf457('0x6'));var rpc={'chatMessage':require('../../rpc/chatMessage'),'openchannelMessage':require(_0xf457('0x7')),'mailMessage':require(_0xf457('0x8')),'smsMessage':require('../../rpc/smsMessage'),'faxMessage':require(_0xf457('0x9')),'whatsappMessage':require(_0xf457('0xa'))};function createMessage(_0x3ccfea,_0x37496e,_0x9d10f5,_0x362e88){return new Promise(function(_0x4684db,_0x585933){var _0x365d0c=_[_0xf457('0xb')](_0x9d10f5);_0x365d0c[_0xf457('0xc')]=_0x362e88;return rpc[_0x37496e+'Message']['create'](_0x365d0c)[_0xf457('0xd')](function(){logger['info'](_0xf457('0xe'),_0x3ccfea,_0x365d0c[_0xf457('0xc')]);_0x4684db();})[_0xf457('0xf')](function(_0x3f1a8d){logger[_0xf457('0x10')](_0xf457('0xe'),_0x3ccfea,JSON[_0xf457('0x11')](_0x3f1a8d));_0x585933(_0x3f1a8d);});});}exports[_0xf457('0x12')]=function(_0x307ff7,_0x19cc3d){return function(_0x14bcac){try{if(!_[_0xf457('0x13')](this[_0xf457('0x14')][_0xf457('0x15')])){logger[_0xf457('0x16')](_0xf457('0xe'),_0x307ff7,_0xf457('0x17'));return _0x14bcac[_0xf457('0x18')]();}var _0x1e6635={'ContactId':this[_0xf457('0x19')]['id'],'sentAt':moment()['format'](_0xf457('0x1a')),'direction':'out'};if(this[_0xf457('0x1b')]===_0xf457('0x1c')||this['channel']==='whatsapp'){_0x1e6635['phone']=this[_0xf457('0x14')][_0xf457('0x1d')];}var _0xdad716=this[_0xf457('0x1b')];if(_0xdad716===_0xf457('0x1e')){_0x1e6635[_0xf457('0x1f')]=this[_0xf457('0x20')]['id'];}else{_0x1e6635[_[_0xf457('0x21')](_0xdad716)+_0xf457('0x22')]=this[_0xf457('0x20')]['id'];}_0x1e6635[_[_0xf457('0x21')](_0xdad716)+'InteractionId']=this[_0xf457('0x14')]['id'];if(this[_0xf457('0x14')][_0xf457('0x23')]){var _0x11bd9a=_0x19cc3d[0x4];if(!_[_0xf457('0x24')](_0x11bd9a)){_0x1e6635[_0xf457('0xc')]=utils['getMustacheRender'](_0x11bd9a,this);return rpc[_0xdad716+'Message']['create'](_0x1e6635)[_0xf457('0xd')](function(_0xdcff4e){logger['info']('[%s][DIALOGFLOWV2]\x20%s',_0x307ff7,_0x1e6635['body']);})[_0xf457('0xf')](function(_0x174610){logger['error'](_0xf457('0xe'),_0x307ff7,JSON[_0xf457('0x11')](_0x174610));})[_0xf457('0x25')](function(){logger[_0xf457('0x16')](_0xf457('0x26'),_0x307ff7);_0x14bcac[_0xf457('0x27')]();});}else{logger[_0xf457('0x16')](_0xf457('0x28'),_0x307ff7);_0x14bcac['stop']();}}else{var _0x211627=this;var _0x201654=_0x19cc3d[0x2][_0xf457('0x29')](/\\n/g,'\x0a');var _0x5c88a9={'credentials':{'private_key':_0x201654,'client_email':_0x19cc3d[0x1]}};var _0x13941b=new dialogflow['SessionsClient'](_0x5c88a9);var _0x3c58bb=_0x13941b[_0xf457('0x2a')](_0x19cc3d[0x0],md5(_0x211627[_0xf457('0x14')]['id']));var _0x438f22={'session':_0x3c58bb,'queryInput':{'text':{'text':_0x211627['message'][_0xf457('0xc')],'languageCode':_0x19cc3d[0x3]||'en'}}};return _0x13941b['detectIntent'](_0x438f22)[_0xf457('0xd')](function(_0x38ab1c){if(_0x38ab1c&&_0x38ab1c['length']){_0x211627[_0xf457('0xc')][_0xf457('0x2b')]=_0x38ab1c;var _0x312ff5=_0x38ab1c[0x0];var _0x3bc6a8=_0x312ff5[_0xf457('0x2c')][_0xf457('0x2d')]&&_0x312ff5[_0xf457('0x2c')][_0xf457('0x2d')]['fields']?_0x312ff5[_0xf457('0x2c')]['diagnosticInfo'][_0xf457('0x2e')][_0xf457('0x2f')][_0xf457('0x30')]:![];_0x211627['body']['DIALOGFLOWV2_ACTION']=_0x312ff5['queryResult'][_0xf457('0x31')];_0x211627[_0xf457('0xc')][_0xf457('0x32')]=_0x312ff5[_0xf457('0x2c')][_0xf457('0x33')];_0x211627[_0xf457('0xc')][_0xf457('0x34')]=_0x312ff5[_0xf457('0x2c')][_0xf457('0x2d')]&&_0x312ff5[_0xf457('0x2c')][_0xf457('0x2d')][_0xf457('0x2e')]?_0x312ff5[_0xf457('0x2c')][_0xf457('0x2d')][_0xf457('0x2e')][_0xf457('0x2f')][_0xf457('0x30')]:![];_0x211627[_0xf457('0xc')]['DIALOGFLOWV2_INTENTNAME']=_0x312ff5[_0xf457('0x2c')][_0xf457('0x35')][_0xf457('0x36')];_0x211627['body'][_0xf457('0x37')]=_0x312ff5['queryResult'][_0xf457('0x35')][_0xf457('0x38')];_0x211627[_0xf457('0xc')][_0xf457('0x39')]=_0x312ff5['queryResult'][_0xf457('0x35')][_0xf457('0x3a')];_0x211627[_0xf457('0xc')]['DIALOGFLOWV2_LANGUAGECODE']=_0x312ff5[_0xf457('0x2c')][_0xf457('0x3b')];_0x211627[_0xf457('0xc')][_0xf457('0x3c')]=_0x312ff5[_0xf457('0x2c')][_0xf457('0x3d')];_0x211627[_0xf457('0xc')][_0xf457('0x3e')]=_0x312ff5[_0xf457('0x2c')][_0xf457('0x3f')];_0x1e6635[_0xf457('0x40')]=_0xf457('0x2b');_0x1e6635[_0xf457('0x41')]=JSON['stringify'](_0x38ab1c);var _0x4f33a4=[];for(var _0x26306d=0x0;_0x26306d<_0x38ab1c[_0xf457('0x42')];_0x26306d++){var _0x51339a=_0x38ab1c[_0x26306d];if(_0x51339a&&_0x51339a[_0xf457('0x2c')]&&_0x51339a['queryResult'][_0xf457('0x43')]){_0x4f33a4[_0xf457('0x44')](createMessage(_0x307ff7,_0xdad716,_0x1e6635,_0x51339a[_0xf457('0x2c')][_0xf457('0x43')]));}}return Promise[_0xf457('0x45')](_0x4f33a4)['then'](function(){if(_0x3bc6a8){_0x14bcac[_0xf457('0x18')]();}else{_0x14bcac['stop']();}})[_0xf457('0xf')](function(_0x1192e2){logger[_0xf457('0x10')](_0xf457('0xe'),_0x307ff7,JSON['stringify'](_0x1192e2));_0x14bcac['next']();});}})[_0xf457('0xf')](function(_0x3dc3ca){logger[_0xf457('0x10')](_0xf457('0xe'),_0x307ff7,_0x3dc3ca[_0xf457('0x46')],_0x3dc3ca[_0xf457('0x47')]);_0x14bcac['next']();});}}catch(_0x1a3c61){logger['error'](_0xf457('0xe'),_0x307ff7,JSON[_0xf457('0x11')](_0x1a3c61));_0x14bcac['stop']();}};};