d5982c9e81f73a22bc63b93e6b2e3774a7062f6a
[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 _0xe3aa=['lodash','md5','moment','routing','../../rpc/chatMessage','../../rpc/openchannelMessage','../../rpc/mailMessage','../../rpc/smsMessage','../../rpc/whatsappMessage','clone','create','then','info','[%s][DIALOGFLOWV2]\x20%s','catch','stringify','consequence','isNil','interaction','UserId','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','next','YYYY-MM-DD\x20HH:mm:ss','channel','whatsapp','phone','chat','ChatWebsiteId','upperFirst','AccountId','account','created','isEmpty','body','getMustacheRender','Message','finally','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!','stop','replace','SessionsClient','sessionPath','detectIntent','length','dialogflowV2','queryResult','fields','diagnosticInfo','end_conversation','boolValue','DIALOGFLOWV2_ACTION','action','allRequiredParamsPresent','DIALOGFLOWV2_ENDCONVERSATION','intent','name','DIALOGFLOWV2_INTENTDISPLAYNAME','displayName','DIALOGFLOWV2_ISFALLBACKINTENT','languageCode','queryText','DIALOGFLOWV2_SCORE','intentDetectionConfidence','providerName','push','all','message','error'];(function(_0x9c7df6,_0x406480){var _0x389f88=function(_0x52d41c){while(--_0x52d41c){_0x9c7df6['push'](_0x9c7df6['shift']());}};_0x389f88(++_0x406480);}(_0xe3aa,0x1e3));var _0xae3a=function(_0x57a2fa,_0x44e38f){_0x57a2fa=_0x57a2fa-0x0;var _0x587709=_0xe3aa[_0x57a2fa];return _0x587709;};'use strict';var _=require(_0xae3a('0x0'));var dialogflow=require('dialogflow');var md5=require(_0xae3a('0x1'));var moment=require(_0xae3a('0x2'));var Promise=require('bluebird');var logger=require('../../../../config/logger')(_0xae3a('0x3'));var utils=require('./utils');var rpc={'chatMessage':require(_0xae3a('0x4')),'openchannelMessage':require(_0xae3a('0x5')),'mailMessage':require(_0xae3a('0x6')),'smsMessage':require(_0xae3a('0x7')),'faxMessage':require('../../rpc/faxMessage'),'whatsappMessage':require(_0xae3a('0x8'))};function createMessage(_0x133ee6,_0x881969,_0x2a55d3,_0x30638c){return new Promise(function(_0x4fe6b8,_0x4f44b9){var _0x1c012b=_[_0xae3a('0x9')](_0x2a55d3);_0x1c012b['body']=_0x30638c;return rpc[_0x881969+'Message'][_0xae3a('0xa')](_0x1c012b)[_0xae3a('0xb')](function(){logger[_0xae3a('0xc')](_0xae3a('0xd'),_0x133ee6,_0x1c012b['body']);_0x4fe6b8();})[_0xae3a('0xe')](function(_0x1a7ac4){logger['error'](_0xae3a('0xd'),_0x133ee6,JSON[_0xae3a('0xf')](_0x1a7ac4));_0x4f44b9(_0x1a7ac4);});});}exports[_0xae3a('0x10')]=function(_0x3c6c45,_0x5736ab){return function(_0x5323b7){try{if(!_[_0xae3a('0x11')](this[_0xae3a('0x12')][_0xae3a('0x13')])){logger[_0xae3a('0xc')](_0xae3a('0xd'),_0x3c6c45,_0xae3a('0x14'));return _0x5323b7[_0xae3a('0x15')]();}var _0x4db7a5={'ContactId':this['contact']['id'],'sentAt':moment()['format'](_0xae3a('0x16')),'direction':'out'};if(this[_0xae3a('0x17')]==='sms'||this[_0xae3a('0x17')]===_0xae3a('0x18')){_0x4db7a5[_0xae3a('0x19')]=this['interaction'][_0xae3a('0x19')];}var _0x2a55de=this[_0xae3a('0x17')];if(_0x2a55de===_0xae3a('0x1a')){_0x4db7a5[_0xae3a('0x1b')]=this['account']['id'];}else{_0x4db7a5[_[_0xae3a('0x1c')](_0x2a55de)+_0xae3a('0x1d')]=this[_0xae3a('0x1e')]['id'];}_0x4db7a5[_[_0xae3a('0x1c')](_0x2a55de)+'InteractionId']=this['interaction']['id'];if(this[_0xae3a('0x12')][_0xae3a('0x1f')]){var _0x4bd270=_0x5736ab[0x4];if(!_[_0xae3a('0x20')](_0x4bd270)){_0x4db7a5[_0xae3a('0x21')]=utils[_0xae3a('0x22')](_0x4bd270,this);return rpc[_0x2a55de+_0xae3a('0x23')]['create'](_0x4db7a5)[_0xae3a('0xb')](function(_0x2d036b){logger[_0xae3a('0xc')]('[%s][DIALOGFLOWV2]\x20%s',_0x3c6c45,_0x4db7a5[_0xae3a('0x21')]);})['catch'](function(_0x417224){logger['error'](_0xae3a('0xd'),_0x3c6c45,JSON[_0xae3a('0xf')](_0x417224));})[_0xae3a('0x24')](function(){logger[_0xae3a('0xc')](_0xae3a('0x25'),_0x3c6c45);_0x5323b7['stop']();});}else{logger['info'](_0xae3a('0x26'),_0x3c6c45);_0x5323b7[_0xae3a('0x27')]();}}else{var _0x2d74e3=this;var _0x2f49e0=_0x5736ab[0x2][_0xae3a('0x28')](/\\n/g,'\x0a');var _0xed1337={'credentials':{'private_key':_0x2f49e0,'client_email':_0x5736ab[0x1]}};var _0x5ec058=new dialogflow[(_0xae3a('0x29'))](_0xed1337);var _0x305247=_0x5ec058[_0xae3a('0x2a')](_0x5736ab[0x0],md5(_0x2d74e3[_0xae3a('0x12')]['id']));var _0x8fcf5b={'session':_0x305247,'queryInput':{'text':{'text':_0x2d74e3['message']['body'],'languageCode':_0x5736ab[0x3]||'en'}}};return _0x5ec058[_0xae3a('0x2b')](_0x8fcf5b)[_0xae3a('0xb')](function(_0x56b3e0){if(_0x56b3e0&&_0x56b3e0[_0xae3a('0x2c')]){_0x2d74e3[_0xae3a('0x21')][_0xae3a('0x2d')]=_0x56b3e0;var _0x21f12c=_0x56b3e0[0x0];var _0x5528c1=_0x21f12c[_0xae3a('0x2e')]['diagnosticInfo']&&_0x21f12c['queryResult']['diagnosticInfo'][_0xae3a('0x2f')]?_0x21f12c['queryResult'][_0xae3a('0x30')]['fields'][_0xae3a('0x31')][_0xae3a('0x32')]:![];_0x2d74e3[_0xae3a('0x21')][_0xae3a('0x33')]=_0x21f12c[_0xae3a('0x2e')][_0xae3a('0x34')];_0x2d74e3[_0xae3a('0x21')]['DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT']=_0x21f12c[_0xae3a('0x2e')][_0xae3a('0x35')];_0x2d74e3['body'][_0xae3a('0x36')]=_0x21f12c[_0xae3a('0x2e')][_0xae3a('0x30')]&&_0x21f12c[_0xae3a('0x2e')][_0xae3a('0x30')]['fields']?_0x21f12c['queryResult']['diagnosticInfo'][_0xae3a('0x2f')]['end_conversation'][_0xae3a('0x32')]:![];_0x2d74e3[_0xae3a('0x21')]['DIALOGFLOWV2_INTENTNAME']=_0x21f12c[_0xae3a('0x2e')][_0xae3a('0x37')][_0xae3a('0x38')];_0x2d74e3['body'][_0xae3a('0x39')]=_0x21f12c[_0xae3a('0x2e')][_0xae3a('0x37')][_0xae3a('0x3a')];_0x2d74e3[_0xae3a('0x21')][_0xae3a('0x3b')]=_0x21f12c[_0xae3a('0x2e')]['intent']['isFallback'];_0x2d74e3[_0xae3a('0x21')]['DIALOGFLOWV2_LANGUAGECODE']=_0x21f12c[_0xae3a('0x2e')][_0xae3a('0x3c')];_0x2d74e3[_0xae3a('0x21')]['DIALOGFLOWV2_QUERYTEXT']=_0x21f12c[_0xae3a('0x2e')][_0xae3a('0x3d')];_0x2d74e3[_0xae3a('0x21')][_0xae3a('0x3e')]=_0x21f12c[_0xae3a('0x2e')][_0xae3a('0x3f')];_0x4db7a5[_0xae3a('0x40')]=_0xae3a('0x2d');_0x4db7a5['providerResponse']=JSON['stringify'](_0x56b3e0);var _0x49f040=[];for(var _0x2995c5=0x0;_0x2995c5<_0x56b3e0[_0xae3a('0x2c')];_0x2995c5++){var _0x2d25fe=_0x56b3e0[_0x2995c5];if(_0x2d25fe&&_0x2d25fe[_0xae3a('0x2e')]&&_0x2d25fe[_0xae3a('0x2e')]['fulfillmentText']){_0x49f040[_0xae3a('0x41')](createMessage(_0x3c6c45,_0x2a55de,_0x4db7a5,_0x2d25fe[_0xae3a('0x2e')]['fulfillmentText']));}}return Promise[_0xae3a('0x42')](_0x49f040)[_0xae3a('0xb')](function(){if(_0x5528c1){_0x5323b7[_0xae3a('0x15')]();}else{_0x5323b7[_0xae3a('0x27')]();}})[_0xae3a('0xe')](function(_0x2181c2){logger['error'](_0xae3a('0xd'),_0x3c6c45,JSON['stringify'](_0x2181c2));_0x5323b7['next']();});}})[_0xae3a('0xe')](function(_0xe35c0d){logger['error'](_0xae3a('0xd'),_0x3c6c45,_0xe35c0d[_0xae3a('0x43')]);_0x5323b7[_0xae3a('0x15')]();});}}catch(_0x262e23){logger[_0xae3a('0x44')](_0xae3a('0xd'),_0x3c6c45,JSON[_0xae3a('0xf')](_0x262e23));_0x5323b7[_0xae3a('0x27')]();}};};