Built motion from commit da617fac.|2.6.6
[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 _0x7e89=['stringify','consequence','isNil','interaction','UserId','next','contact','YYYY-MM-DD\x20HH:mm:ss','out','channel','whatsapp','phone','chat','ChatWebsiteId','account','upperFirst','created','isEmpty','getMustacheRender','Message','finally','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!','replace','sessionPath','message','detectIntent','length','dialogflowV2','queryResult','diagnosticInfo','fields','end_conversation','boolValue','DIALOGFLOWV2_ACTION','allRequiredParamsPresent','DIALOGFLOWV2_INTENTNAME','intent','name','DIALOGFLOWV2_INTENTDISPLAYNAME','displayName','DIALOGFLOWV2_ISFALLBACKINTENT','isFallback','DIALOGFLOWV2_QUERYTEXT','DIALOGFLOWV2_SCORE','intentDetectionConfidence','providerName','providerResponse','fulfillmentText','push','all','md5','moment','bluebird','./utils','../../rpc/chatMessage','../../rpc/openchannelMessage','../../rpc/mailMessage','../../rpc/faxMessage','../../rpc/whatsappMessage','clone','body','create','then','info','catch','error','[%s][DIALOGFLOWV2]\x20%s'];(function(_0x23e367,_0x521c59){var _0x1404a5=function(_0x1c9a65){while(--_0x1c9a65){_0x23e367['push'](_0x23e367['shift']());}};_0x1404a5(++_0x521c59);}(_0x7e89,0x181));var _0x97e8=function(_0x4f431d,_0xd51a3b){_0x4f431d=_0x4f431d-0x0;var _0x4f31e1=_0x7e89[_0x4f431d];return _0x4f31e1;};'use strict';var _=require('lodash');var dialogflow=require('dialogflow');var md5=require(_0x97e8('0x0'));var moment=require(_0x97e8('0x1'));var Promise=require(_0x97e8('0x2'));var logger=require('../../../../config/logger')('routing');var utils=require(_0x97e8('0x3'));var rpc={'chatMessage':require(_0x97e8('0x4')),'openchannelMessage':require(_0x97e8('0x5')),'mailMessage':require(_0x97e8('0x6')),'smsMessage':require('../../rpc/smsMessage'),'faxMessage':require(_0x97e8('0x7')),'whatsappMessage':require(_0x97e8('0x8'))};function createMessage(_0x543a6e,_0x543a45,_0x1f4cca,_0x23f864){return new Promise(function(_0x2a9326,_0x2b8aff){var _0x3c9e8a=_[_0x97e8('0x9')](_0x1f4cca);_0x3c9e8a[_0x97e8('0xa')]=_0x23f864;return rpc[_0x543a45+'Message'][_0x97e8('0xb')](_0x3c9e8a)[_0x97e8('0xc')](function(){logger[_0x97e8('0xd')]('[%s][DIALOGFLOWV2]\x20%s',_0x543a6e,_0x3c9e8a[_0x97e8('0xa')]);_0x2a9326();})[_0x97e8('0xe')](function(_0x37fd36){logger[_0x97e8('0xf')](_0x97e8('0x10'),_0x543a6e,JSON[_0x97e8('0x11')](_0x37fd36));_0x2b8aff(_0x37fd36);});});}exports[_0x97e8('0x12')]=function(_0x23b2fc,_0x4c23fd){return function(_0x2a9120){try{if(!_[_0x97e8('0x13')](this[_0x97e8('0x14')][_0x97e8('0x15')])){logger[_0x97e8('0xd')](_0x97e8('0x10'),_0x23b2fc,'Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.');return _0x2a9120[_0x97e8('0x16')]();}var _0x2ffdf1={'ContactId':this[_0x97e8('0x17')]['id'],'sentAt':moment()['format'](_0x97e8('0x18')),'direction':_0x97e8('0x19')};if(this['channel']==='sms'||this[_0x97e8('0x1a')]===_0x97e8('0x1b')){_0x2ffdf1[_0x97e8('0x1c')]=this[_0x97e8('0x14')][_0x97e8('0x1c')];}var _0x2413a7=this[_0x97e8('0x1a')];if(_0x2413a7===_0x97e8('0x1d')){_0x2ffdf1[_0x97e8('0x1e')]=this[_0x97e8('0x1f')]['id'];}else{_0x2ffdf1[_['upperFirst'](_0x2413a7)+'AccountId']=this[_0x97e8('0x1f')]['id'];}_0x2ffdf1[_[_0x97e8('0x20')](_0x2413a7)+'InteractionId']=this[_0x97e8('0x14')]['id'];if(this[_0x97e8('0x14')][_0x97e8('0x21')]){var _0x12e7ff=_0x4c23fd[0x4];if(!_[_0x97e8('0x22')](_0x12e7ff)){_0x2ffdf1[_0x97e8('0xa')]=utils[_0x97e8('0x23')](_0x12e7ff,this);return rpc[_0x2413a7+_0x97e8('0x24')][_0x97e8('0xb')](_0x2ffdf1)[_0x97e8('0xc')](function(_0x49f652){logger[_0x97e8('0xd')](_0x97e8('0x10'),_0x23b2fc,_0x2ffdf1[_0x97e8('0xa')]);})['catch'](function(_0x122fdc){logger['error']('[%s][DIALOGFLOWV2]\x20%s',_0x23b2fc,JSON[_0x97e8('0x11')](_0x122fdc));})[_0x97e8('0x25')](function(){logger[_0x97e8('0xd')]('[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!',_0x23b2fc);_0x2a9120['stop']();});}else{logger[_0x97e8('0xd')](_0x97e8('0x26'),_0x23b2fc);_0x2a9120['stop']();}}else{var _0x18b14f=this;var _0xb08816=_0x4c23fd[0x2][_0x97e8('0x27')](/\\n/g,'\x0a');var _0x24c4fc={'credentials':{'private_key':_0xb08816,'client_email':_0x4c23fd[0x1]}};var _0x543462=new dialogflow['SessionsClient'](_0x24c4fc);var _0x46da6a=_0x543462[_0x97e8('0x28')](_0x4c23fd[0x0],md5(_0x18b14f[_0x97e8('0x14')]['id']));var _0x3dc578={'session':_0x46da6a,'queryInput':{'text':{'text':_0x18b14f[_0x97e8('0x29')]['body'],'languageCode':_0x4c23fd[0x3]||'en'}}};return _0x543462[_0x97e8('0x2a')](_0x3dc578)['then'](function(_0x3a54e1){if(_0x3a54e1&&_0x3a54e1[_0x97e8('0x2b')]){_0x18b14f['body'][_0x97e8('0x2c')]=_0x3a54e1;var _0x5d941c=_0x3a54e1[0x0];var _0x404904=_0x5d941c[_0x97e8('0x2d')][_0x97e8('0x2e')]&&_0x5d941c[_0x97e8('0x2d')]['diagnosticInfo'][_0x97e8('0x2f')]?_0x5d941c[_0x97e8('0x2d')][_0x97e8('0x2e')][_0x97e8('0x2f')][_0x97e8('0x30')][_0x97e8('0x31')]:![];_0x18b14f[_0x97e8('0xa')][_0x97e8('0x32')]=_0x5d941c['queryResult']['action'];_0x18b14f['body']['DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT']=_0x5d941c[_0x97e8('0x2d')][_0x97e8('0x33')];_0x18b14f['body']['DIALOGFLOWV2_ENDCONVERSATION']=_0x5d941c['queryResult']['diagnosticInfo']&&_0x5d941c[_0x97e8('0x2d')][_0x97e8('0x2e')][_0x97e8('0x2f')]?_0x5d941c[_0x97e8('0x2d')]['diagnosticInfo']['fields']['end_conversation'][_0x97e8('0x31')]:![];_0x18b14f[_0x97e8('0xa')][_0x97e8('0x34')]=_0x5d941c[_0x97e8('0x2d')][_0x97e8('0x35')][_0x97e8('0x36')];_0x18b14f[_0x97e8('0xa')][_0x97e8('0x37')]=_0x5d941c['queryResult'][_0x97e8('0x35')][_0x97e8('0x38')];_0x18b14f[_0x97e8('0xa')][_0x97e8('0x39')]=_0x5d941c[_0x97e8('0x2d')][_0x97e8('0x35')][_0x97e8('0x3a')];_0x18b14f['body']['DIALOGFLOWV2_LANGUAGECODE']=_0x5d941c[_0x97e8('0x2d')]['languageCode'];_0x18b14f['body'][_0x97e8('0x3b')]=_0x5d941c['queryResult']['queryText'];_0x18b14f[_0x97e8('0xa')][_0x97e8('0x3c')]=_0x5d941c[_0x97e8('0x2d')][_0x97e8('0x3d')];_0x2ffdf1[_0x97e8('0x3e')]=_0x97e8('0x2c');_0x2ffdf1[_0x97e8('0x3f')]=JSON[_0x97e8('0x11')](_0x3a54e1);var _0x4596a8=[];for(var _0x519d46=0x0;_0x519d46<_0x3a54e1[_0x97e8('0x2b')];_0x519d46++){var _0xb4e97d=_0x3a54e1[_0x519d46];if(_0xb4e97d&&_0xb4e97d[_0x97e8('0x2d')]&&_0xb4e97d[_0x97e8('0x2d')][_0x97e8('0x40')]){_0x4596a8[_0x97e8('0x41')](createMessage(_0x23b2fc,_0x2413a7,_0x2ffdf1,_0xb4e97d[_0x97e8('0x2d')][_0x97e8('0x40')]));}}return Promise[_0x97e8('0x42')](_0x4596a8)[_0x97e8('0xc')](function(){if(_0x404904){_0x2a9120[_0x97e8('0x16')]();}else{_0x2a9120['stop']();}})[_0x97e8('0xe')](function(_0x46e637){logger[_0x97e8('0xf')]('[%s][DIALOGFLOWV2]\x20%s',_0x23b2fc,JSON[_0x97e8('0x11')](_0x46e637));_0x2a9120[_0x97e8('0x16')]();});}})['catch'](function(_0x514288){logger[_0x97e8('0xf')](_0x97e8('0x10'),_0x23b2fc,_0x514288['message']);_0x2a9120[_0x97e8('0x16')]();});}}catch(_0x253240){logger['error'](_0x97e8('0x10'),_0x23b2fc,JSON['stringify'](_0x253240));_0x2a9120['stop']();}};};