5f567711554280d5744723e572d4364f6284c3ed
[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 _0x5d7c=['./utils','../../rpc/chatMessage','../../rpc/openchannelMessage','../../rpc/smsMessage','../../rpc/faxMessage','clone','Message','create','then','[%s][DIALOGFLOWV2]\x20%s','catch','error','stringify','consequence','info','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','next','contact','format','out','channel','sms','whatsapp','phone','interaction','chat','ChatWebsiteId','account','upperFirst','AccountId','InteractionId','created','isEmpty','body','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','detectIntent','length','queryResult','fields','diagnosticInfo','DIALOGFLOWV2_ACTION','DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT','allRequiredParamsPresent','DIALOGFLOWV2_ENDCONVERSATION','boolValue','DIALOGFLOWV2_INTENTNAME','intent','DIALOGFLOWV2_INTENTDISPLAYNAME','displayName','DIALOGFLOWV2_ISFALLBACKINTENT','DIALOGFLOWV2_LANGUAGECODE','languageCode','DIALOGFLOWV2_QUERYTEXT','queryText','intentDetectionConfidence','providerResponse','fulfillmentText','all','lodash','md5','moment','bluebird','../../../../config/logger','routing'];(function(_0x334fea,_0x1f1e18){var _0x3c5829=function(_0x4fd6ac){while(--_0x4fd6ac){_0x334fea['push'](_0x334fea['shift']());}};_0x3c5829(++_0x1f1e18);}(_0x5d7c,0x116));var _0xc5d7=function(_0x152e53,_0xf6761e){_0x152e53=_0x152e53-0x0;var _0x37a703=_0x5d7c[_0x152e53];return _0x37a703;};'use strict';var _=require(_0xc5d7('0x0'));var dialogflow=require('dialogflow');var md5=require(_0xc5d7('0x1'));var moment=require(_0xc5d7('0x2'));var Promise=require(_0xc5d7('0x3'));var logger=require(_0xc5d7('0x4'))(_0xc5d7('0x5'));var utils=require(_0xc5d7('0x6'));var rpc={'chatMessage':require(_0xc5d7('0x7')),'openchannelMessage':require(_0xc5d7('0x8')),'mailMessage':require('../../rpc/mailMessage'),'smsMessage':require(_0xc5d7('0x9')),'faxMessage':require(_0xc5d7('0xa')),'whatsappMessage':require('../../rpc/whatsappMessage')};function createMessage(_0x2aff1c,_0x210ea3,_0x53fc4b,_0x4423e0){return new Promise(function(_0x31b3c4,_0x2ffffb){var _0x51de47=_[_0xc5d7('0xb')](_0x53fc4b);_0x51de47['body']=_0x4423e0;return rpc[_0x210ea3+_0xc5d7('0xc')][_0xc5d7('0xd')](_0x51de47)[_0xc5d7('0xe')](function(){logger['info'](_0xc5d7('0xf'),_0x2aff1c,_0x51de47['body']);_0x31b3c4();})[_0xc5d7('0x10')](function(_0x91f85c){logger[_0xc5d7('0x11')]('[%s][DIALOGFLOWV2]\x20%s',_0x2aff1c,JSON[_0xc5d7('0x12')](_0x91f85c));_0x2ffffb(_0x91f85c);});});}exports[_0xc5d7('0x13')]=function(_0x17a732,_0x538764){return function(_0x1c7c1d){try{if(!_['isNil'](this['interaction']['UserId'])){logger[_0xc5d7('0x14')](_0xc5d7('0xf'),_0x17a732,_0xc5d7('0x15'));return _0x1c7c1d[_0xc5d7('0x16')]();}var _0x486027={'ContactId':this[_0xc5d7('0x17')]['id'],'sentAt':moment()[_0xc5d7('0x18')]('YYYY-MM-DD\x20HH:mm:ss'),'direction':_0xc5d7('0x19')};if(this[_0xc5d7('0x1a')]===_0xc5d7('0x1b')||this[_0xc5d7('0x1a')]===_0xc5d7('0x1c')){_0x486027[_0xc5d7('0x1d')]=this[_0xc5d7('0x1e')][_0xc5d7('0x1d')];}var _0x4239d2=this[_0xc5d7('0x1a')];if(_0x4239d2===_0xc5d7('0x1f')){_0x486027[_0xc5d7('0x20')]=this[_0xc5d7('0x21')]['id'];}else{_0x486027[_[_0xc5d7('0x22')](_0x4239d2)+_0xc5d7('0x23')]=this['account']['id'];}_0x486027[_[_0xc5d7('0x22')](_0x4239d2)+_0xc5d7('0x24')]=this[_0xc5d7('0x1e')]['id'];if(this['interaction'][_0xc5d7('0x25')]){var _0x509828=_0x538764[0x4];if(!_[_0xc5d7('0x26')](_0x509828)){_0x486027[_0xc5d7('0x27')]=utils['getMustacheRender'](_0x509828,this);return rpc[_0x4239d2+_0xc5d7('0xc')][_0xc5d7('0xd')](_0x486027)[_0xc5d7('0xe')](function(_0x1ba170){logger['info']('[%s][DIALOGFLOWV2]\x20%s',_0x17a732,_0x486027[_0xc5d7('0x27')]);})[_0xc5d7('0x10')](function(_0x4b36b4){logger[_0xc5d7('0x11')]('[%s][DIALOGFLOWV2]\x20%s',_0x17a732,JSON[_0xc5d7('0x12')](_0x4b36b4));})[_0xc5d7('0x28')](function(){logger[_0xc5d7('0x14')](_0xc5d7('0x29'),_0x17a732);_0x1c7c1d[_0xc5d7('0x2a')]();});}else{logger[_0xc5d7('0x14')](_0xc5d7('0x2b'),_0x17a732);_0x1c7c1d[_0xc5d7('0x2a')]();}}else{var _0x3f3c82=this;var _0x248e89=_0x538764[0x2][_0xc5d7('0x2c')](/\\n/g,'\x0a');var _0xcaa2b5={'credentials':{'private_key':_0x248e89,'client_email':_0x538764[0x1]}};var _0xf0726c=new dialogflow[(_0xc5d7('0x2d'))](_0xcaa2b5);var _0x1967fb=_0xf0726c[_0xc5d7('0x2e')](_0x538764[0x0],md5(_0x3f3c82['interaction']['id']));var _0x3dbca0={'session':_0x1967fb,'queryInput':{'text':{'text':_0x3f3c82[_0xc5d7('0x2f')]['body'],'languageCode':_0x538764[0x3]||'en'}}};return _0xf0726c[_0xc5d7('0x30')](_0x3dbca0)['then'](function(_0x32da9a){if(_0x32da9a&&_0x32da9a[_0xc5d7('0x31')]){_0x3f3c82['body']['dialogflowV2']=_0x32da9a;var _0x207ed5=_0x32da9a[0x0];var _0x25c1c6=_0x207ed5[_0xc5d7('0x32')]['diagnosticInfo']&&_0x207ed5['queryResult']['diagnosticInfo'][_0xc5d7('0x33')]?_0x207ed5['queryResult'][_0xc5d7('0x34')][_0xc5d7('0x33')]['end_conversation']['boolValue']:![];_0x3f3c82['body'][_0xc5d7('0x35')]=_0x207ed5[_0xc5d7('0x32')]['action'];_0x3f3c82['body'][_0xc5d7('0x36')]=_0x207ed5[_0xc5d7('0x32')][_0xc5d7('0x37')];_0x3f3c82['body'][_0xc5d7('0x38')]=_0x207ed5[_0xc5d7('0x32')]['diagnosticInfo']&&_0x207ed5['queryResult'][_0xc5d7('0x34')][_0xc5d7('0x33')]?_0x207ed5['queryResult'][_0xc5d7('0x34')]['fields']['end_conversation'][_0xc5d7('0x39')]:![];_0x3f3c82[_0xc5d7('0x27')][_0xc5d7('0x3a')]=_0x207ed5[_0xc5d7('0x32')][_0xc5d7('0x3b')]['name'];_0x3f3c82[_0xc5d7('0x27')][_0xc5d7('0x3c')]=_0x207ed5[_0xc5d7('0x32')][_0xc5d7('0x3b')][_0xc5d7('0x3d')];_0x3f3c82[_0xc5d7('0x27')][_0xc5d7('0x3e')]=_0x207ed5[_0xc5d7('0x32')][_0xc5d7('0x3b')]['isFallback'];_0x3f3c82[_0xc5d7('0x27')][_0xc5d7('0x3f')]=_0x207ed5['queryResult'][_0xc5d7('0x40')];_0x3f3c82[_0xc5d7('0x27')][_0xc5d7('0x41')]=_0x207ed5['queryResult'][_0xc5d7('0x42')];_0x3f3c82['body']['DIALOGFLOWV2_SCORE']=_0x207ed5[_0xc5d7('0x32')][_0xc5d7('0x43')];_0x486027['providerName']='dialogflowV2';_0x486027[_0xc5d7('0x44')]=JSON[_0xc5d7('0x12')](_0x32da9a);var _0x5ef07a=[];for(var _0x4d7e61=0x0;_0x4d7e61<_0x32da9a[_0xc5d7('0x31')];_0x4d7e61++){var _0x48772c=_0x32da9a[_0x4d7e61];if(_0x48772c&&_0x48772c['queryResult']&&_0x48772c['queryResult'][_0xc5d7('0x45')]){_0x5ef07a['push'](createMessage(_0x17a732,_0x4239d2,_0x486027,_0x48772c[_0xc5d7('0x32')][_0xc5d7('0x45')]));}}return Promise[_0xc5d7('0x46')](_0x5ef07a)['then'](function(){if(_0x25c1c6){_0x1c7c1d[_0xc5d7('0x16')]();}else{_0x1c7c1d['stop']();}})[_0xc5d7('0x10')](function(_0x2588c9){logger[_0xc5d7('0x11')](_0xc5d7('0xf'),_0x17a732,JSON['stringify'](_0x2588c9));_0x1c7c1d[_0xc5d7('0x16')]();});}})[_0xc5d7('0x10')](function(_0x173d50){logger[_0xc5d7('0x11')](_0xc5d7('0xf'),_0x17a732,_0x173d50['message']);_0x1c7c1d['next']();});}}catch(_0x199ef6){logger[_0xc5d7('0x11')](_0xc5d7('0xf'),_0x17a732,JSON['stringify'](_0x199ef6));_0x1c7c1d[_0xc5d7('0x2a')]();}};};