8b6c05163e4703f78763c9342da00c518bb88fed
[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 _0x38c7=['ChatWebsiteId','account','upperFirst','AccountId','InteractionId','isEmpty','getMustacheRender','finally','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!','stop','replace','sessionPath','message','detectIntent','then','dialogflowV2','queryResult','diagnosticInfo','fields','end_conversation','boolValue','DIALOGFLOWV2_ACTION','action','DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT','allRequiredParamsPresent','DIALOGFLOWV2_ENDCONVERSATION','DIALOGFLOWV2_INTENTNAME','intent','name','displayName','isFallback','DIALOGFLOWV2_LANGUAGECODE','languageCode','DIALOGFLOWV2_QUERYTEXT','queryText','DIALOGFLOWV2_SCORE','intentDetectionConfidence','providerName','providerResponse','fulfillmentText','push','all','next','dialogflow','md5','moment','../../../../config/logger','routing','../../rpc/chatMessage','../../rpc/mailMessage','../../rpc/smsMessage','../../rpc/faxMessage','../../rpc/whatsappMessage','clone','body','Message','create','info','[%s][DIALOGFLOWV2]\x20%s','catch','error','stringify','consequence','isNil','UserId','format','YYYY-MM-DD\x20HH:mm:ss','out','channel','whatsapp','phone','interaction','chat'];(function(_0x20a584,_0xe384a5){var _0x4b41a8=function(_0x2127c6){while(--_0x2127c6){_0x20a584['push'](_0x20a584['shift']());}};_0x4b41a8(++_0xe384a5);}(_0x38c7,0x106));var _0x738c=function(_0x2b434c,_0x5d6f2a){_0x2b434c=_0x2b434c-0x0;var _0x30f1ef=_0x38c7[_0x2b434c];return _0x30f1ef;};'use strict';var _=require('lodash');var dialogflow=require(_0x738c('0x0'));var md5=require(_0x738c('0x1'));var moment=require(_0x738c('0x2'));var Promise=require('bluebird');var logger=require(_0x738c('0x3'))(_0x738c('0x4'));var utils=require('./utils');var rpc={'chatMessage':require(_0x738c('0x5')),'openchannelMessage':require('../../rpc/openchannelMessage'),'mailMessage':require(_0x738c('0x6')),'smsMessage':require(_0x738c('0x7')),'faxMessage':require(_0x738c('0x8')),'whatsappMessage':require(_0x738c('0x9'))};function createMessage(_0x3c0a30,_0x532e72,_0x1e2cd3,_0x767b1e){return new Promise(function(_0x315ee9,_0x3252ec){var _0x4aec2f=_[_0x738c('0xa')](_0x1e2cd3);_0x4aec2f[_0x738c('0xb')]=_0x767b1e;return rpc[_0x532e72+_0x738c('0xc')][_0x738c('0xd')](_0x4aec2f)['then'](function(){logger[_0x738c('0xe')](_0x738c('0xf'),_0x3c0a30,_0x4aec2f[_0x738c('0xb')]);_0x315ee9();})[_0x738c('0x10')](function(_0x401670){logger[_0x738c('0x11')](_0x738c('0xf'),_0x3c0a30,JSON[_0x738c('0x12')](_0x401670));_0x3252ec(_0x401670);});});}exports[_0x738c('0x13')]=function(_0x2a6dda,_0x210abf){return function(_0x4f315f){try{if(!_[_0x738c('0x14')](this['interaction'][_0x738c('0x15')])){logger[_0x738c('0xe')]('[%s][DIALOGFLOWV2]\x20%s',_0x2a6dda,'Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.');return _0x4f315f['next']();}var _0x34af2e={'ContactId':this['contact']['id'],'sentAt':moment()[_0x738c('0x16')](_0x738c('0x17')),'direction':_0x738c('0x18')};if(this[_0x738c('0x19')]==='sms'||this[_0x738c('0x19')]===_0x738c('0x1a')){_0x34af2e[_0x738c('0x1b')]=this[_0x738c('0x1c')]['phone'];}var _0x62ee1b=this[_0x738c('0x19')];if(_0x62ee1b===_0x738c('0x1d')){_0x34af2e[_0x738c('0x1e')]=this[_0x738c('0x1f')]['id'];}else{_0x34af2e[_[_0x738c('0x20')](_0x62ee1b)+_0x738c('0x21')]=this[_0x738c('0x1f')]['id'];}_0x34af2e[_[_0x738c('0x20')](_0x62ee1b)+_0x738c('0x22')]=this['interaction']['id'];if(this[_0x738c('0x1c')]['created']){var _0x3e7ce8=_0x210abf[0x4];if(!_[_0x738c('0x23')](_0x3e7ce8)){_0x34af2e[_0x738c('0xb')]=utils[_0x738c('0x24')](_0x3e7ce8,this);return rpc[_0x62ee1b+_0x738c('0xc')][_0x738c('0xd')](_0x34af2e)['then'](function(_0x1e7cac){logger['info'](_0x738c('0xf'),_0x2a6dda,_0x34af2e[_0x738c('0xb')]);})[_0x738c('0x10')](function(_0x3512ab){logger[_0x738c('0x11')](_0x738c('0xf'),_0x2a6dda,JSON[_0x738c('0x12')](_0x3512ab));})[_0x738c('0x25')](function(){logger[_0x738c('0xe')](_0x738c('0x26'),_0x2a6dda);_0x4f315f[_0x738c('0x27')]();});}else{logger[_0x738c('0xe')]('[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!',_0x2a6dda);_0x4f315f[_0x738c('0x27')]();}}else{var _0x3e0ad4=this;var _0x104689=_0x210abf[0x2][_0x738c('0x28')](/\\n/g,'\x0a');var _0x29455b={'credentials':{'private_key':_0x104689,'client_email':_0x210abf[0x1]}};var _0x5d9a22=new dialogflow['SessionsClient'](_0x29455b);var _0x59628e=_0x5d9a22[_0x738c('0x29')](_0x210abf[0x0],md5(_0x3e0ad4[_0x738c('0x1c')]['id']));var _0x1b7320={'session':_0x59628e,'queryInput':{'text':{'text':_0x3e0ad4[_0x738c('0x2a')][_0x738c('0xb')],'languageCode':_0x210abf[0x3]||'en'}}};return _0x5d9a22[_0x738c('0x2b')](_0x1b7320)[_0x738c('0x2c')](function(_0x365ce5){if(_0x365ce5&&_0x365ce5['length']){_0x3e0ad4[_0x738c('0xb')][_0x738c('0x2d')]=_0x365ce5;var _0x4db831=_0x365ce5[0x0];var _0x2e44c7=_0x4db831[_0x738c('0x2e')]['diagnosticInfo']&&_0x4db831[_0x738c('0x2e')][_0x738c('0x2f')][_0x738c('0x30')]?_0x4db831[_0x738c('0x2e')]['diagnosticInfo'][_0x738c('0x30')][_0x738c('0x31')][_0x738c('0x32')]:![];_0x3e0ad4[_0x738c('0xb')][_0x738c('0x33')]=_0x4db831['queryResult'][_0x738c('0x34')];_0x3e0ad4[_0x738c('0xb')][_0x738c('0x35')]=_0x4db831[_0x738c('0x2e')][_0x738c('0x36')];_0x3e0ad4[_0x738c('0xb')][_0x738c('0x37')]=_0x4db831[_0x738c('0x2e')]['diagnosticInfo']&&_0x4db831[_0x738c('0x2e')][_0x738c('0x2f')]['fields']?_0x4db831[_0x738c('0x2e')][_0x738c('0x2f')][_0x738c('0x30')]['end_conversation'][_0x738c('0x32')]:![];_0x3e0ad4['body'][_0x738c('0x38')]=_0x4db831['queryResult'][_0x738c('0x39')][_0x738c('0x3a')];_0x3e0ad4['body']['DIALOGFLOWV2_INTENTDISPLAYNAME']=_0x4db831['queryResult'][_0x738c('0x39')][_0x738c('0x3b')];_0x3e0ad4[_0x738c('0xb')]['DIALOGFLOWV2_ISFALLBACKINTENT']=_0x4db831['queryResult'][_0x738c('0x39')][_0x738c('0x3c')];_0x3e0ad4['body'][_0x738c('0x3d')]=_0x4db831[_0x738c('0x2e')][_0x738c('0x3e')];_0x3e0ad4[_0x738c('0xb')][_0x738c('0x3f')]=_0x4db831[_0x738c('0x2e')][_0x738c('0x40')];_0x3e0ad4[_0x738c('0xb')][_0x738c('0x41')]=_0x4db831[_0x738c('0x2e')][_0x738c('0x42')];_0x34af2e[_0x738c('0x43')]='dialogflowV2';_0x34af2e[_0x738c('0x44')]=JSON[_0x738c('0x12')](_0x365ce5);var _0x53e203=[];for(var _0x49b00a=0x0;_0x49b00a<_0x365ce5['length'];_0x49b00a++){var _0x5cafa1=_0x365ce5[_0x49b00a];if(_0x5cafa1&&_0x5cafa1['queryResult']&&_0x5cafa1[_0x738c('0x2e')][_0x738c('0x45')]){_0x53e203[_0x738c('0x46')](createMessage(_0x2a6dda,_0x62ee1b,_0x34af2e,_0x5cafa1[_0x738c('0x2e')][_0x738c('0x45')]));}}return Promise[_0x738c('0x47')](_0x53e203)[_0x738c('0x2c')](function(){if(_0x2e44c7){_0x4f315f[_0x738c('0x48')]();}else{_0x4f315f['stop']();}})['catch'](function(_0x2af256){logger[_0x738c('0x11')](_0x738c('0xf'),_0x2a6dda,JSON[_0x738c('0x12')](_0x2af256));_0x4f315f[_0x738c('0x48')]();});}})['catch'](function(_0x2da758){logger[_0x738c('0x11')]('[%s][DIALOGFLOWV2]\x20%s',_0x2a6dda,_0x2da758[_0x738c('0x2a')]);_0x4f315f[_0x738c('0x48')]();});}}catch(_0x2703da){logger[_0x738c('0x11')](_0x738c('0xf'),_0x2a6dda,JSON[_0x738c('0x12')](_0x2703da));_0x4f315f[_0x738c('0x27')]();}};};