3c17b85c411b034238de4147af6733c86bcc09d0
[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 _0x06c5=['../../../../config/logger','./utils','../../rpc/openchannelMessage','../../rpc/smsMessage','../../rpc/faxMessage','clone','body','Message','create','info','[%s][DIALOGFLOWV2]\x20%s','catch','stringify','consequence','isNil','interaction','UserId','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','next','contact','YYYY-MM-DD\x20HH:mm:ss','channel','sms','whatsapp','phone','chat','ChatWebsiteId','account','upperFirst','AccountId','InteractionId','created','getMustacheRender','error','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!','stop','replace','sessionPath','message','detectIntent','then','length','dialogflowV2','queryResult','diagnosticInfo','fields','end_conversation','boolValue','action','DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT','allRequiredParamsPresent','DIALOGFLOWV2_ENDCONVERSATION','DIALOGFLOWV2_INTENTNAME','DIALOGFLOWV2_INTENTDISPLAYNAME','displayName','intent','DIALOGFLOWV2_LANGUAGECODE','languageCode','DIALOGFLOWV2_QUERYTEXT','queryText','DIALOGFLOWV2_SCORE','intentDetectionConfidence','providerResponse','push','all','lodash','dialogflow','md5','moment','bluebird'];(function(_0x14add3,_0x13ce8d){var _0x4a174f=function(_0x2b3ed7){while(--_0x2b3ed7){_0x14add3['push'](_0x14add3['shift']());}};_0x4a174f(++_0x13ce8d);}(_0x06c5,0x19f));var _0x506c=function(_0x4f2931,_0x1b89ad){_0x4f2931=_0x4f2931-0x0;var _0x453a06=_0x06c5[_0x4f2931];return _0x453a06;};'use strict';var _=require(_0x506c('0x0'));var dialogflow=require(_0x506c('0x1'));var md5=require(_0x506c('0x2'));var moment=require(_0x506c('0x3'));var Promise=require(_0x506c('0x4'));var logger=require(_0x506c('0x5'))('routing');var utils=require(_0x506c('0x6'));var rpc={'chatMessage':require('../../rpc/chatMessage'),'openchannelMessage':require(_0x506c('0x7')),'mailMessage':require('../../rpc/mailMessage'),'smsMessage':require(_0x506c('0x8')),'faxMessage':require(_0x506c('0x9')),'whatsappMessage':require('../../rpc/whatsappMessage')};function createMessage(_0x26026d,_0x2ea253,_0x3ca8c2,_0x5a5857){return new Promise(function(_0x1becd9,_0x41edd3){var _0x466834=_[_0x506c('0xa')](_0x3ca8c2);_0x466834[_0x506c('0xb')]=_0x5a5857;return rpc[_0x2ea253+_0x506c('0xc')][_0x506c('0xd')](_0x466834)['then'](function(){logger[_0x506c('0xe')](_0x506c('0xf'),_0x26026d,_0x466834[_0x506c('0xb')]);_0x1becd9();})[_0x506c('0x10')](function(_0x57ff16){logger['error']('[%s][DIALOGFLOWV2]\x20%s',_0x26026d,JSON[_0x506c('0x11')](_0x57ff16));_0x41edd3(_0x57ff16);});});}exports[_0x506c('0x12')]=function(_0xe3744a,_0x18dbef){return function(_0x249f95){try{if(!_[_0x506c('0x13')](this[_0x506c('0x14')][_0x506c('0x15')])){logger['info'](_0x506c('0xf'),_0xe3744a,_0x506c('0x16'));return _0x249f95[_0x506c('0x17')]();}var _0x1c6755={'ContactId':this[_0x506c('0x18')]['id'],'sentAt':moment()['format'](_0x506c('0x19')),'direction':'out'};if(this[_0x506c('0x1a')]===_0x506c('0x1b')||this[_0x506c('0x1a')]===_0x506c('0x1c')){_0x1c6755[_0x506c('0x1d')]=this['interaction'][_0x506c('0x1d')];}var _0x2cc59c=this[_0x506c('0x1a')];if(_0x2cc59c===_0x506c('0x1e')){_0x1c6755[_0x506c('0x1f')]=this[_0x506c('0x20')]['id'];}else{_0x1c6755[_[_0x506c('0x21')](_0x2cc59c)+_0x506c('0x22')]=this[_0x506c('0x20')]['id'];}_0x1c6755[_[_0x506c('0x21')](_0x2cc59c)+_0x506c('0x23')]=this[_0x506c('0x14')]['id'];if(this['interaction'][_0x506c('0x24')]){var _0xaf038=_0x18dbef[0x4];if(!_['isEmpty'](_0xaf038)){_0x1c6755[_0x506c('0xb')]=utils[_0x506c('0x25')](_0xaf038,this);return rpc[_0x2cc59c+_0x506c('0xc')][_0x506c('0xd')](_0x1c6755)['then'](function(_0x3ac240){logger[_0x506c('0xe')](_0x506c('0xf'),_0xe3744a,_0x1c6755[_0x506c('0xb')]);})[_0x506c('0x10')](function(_0x225a6d){logger[_0x506c('0x26')](_0x506c('0xf'),_0xe3744a,JSON[_0x506c('0x11')](_0x225a6d));})['finally'](function(){logger[_0x506c('0xe')](_0x506c('0x27'),_0xe3744a);_0x249f95[_0x506c('0x28')]();});}else{logger[_0x506c('0xe')]('[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!',_0xe3744a);_0x249f95[_0x506c('0x28')]();}}else{var _0x59e724=this;var _0x5a1292=_0x18dbef[0x2][_0x506c('0x29')](/\\n/g,'\x0a');var _0x261467={'credentials':{'private_key':_0x5a1292,'client_email':_0x18dbef[0x1]}};var _0x747d23=new dialogflow['SessionsClient'](_0x261467);var _0x5468bc=_0x747d23[_0x506c('0x2a')](_0x18dbef[0x0],md5(_0x59e724[_0x506c('0x14')]['id']));var _0x2d0c94={'session':_0x5468bc,'queryInput':{'text':{'text':_0x59e724[_0x506c('0x2b')][_0x506c('0xb')],'languageCode':_0x18dbef[0x3]||'en'}}};return _0x747d23[_0x506c('0x2c')](_0x2d0c94)[_0x506c('0x2d')](function(_0x49e2c1){if(_0x49e2c1&&_0x49e2c1[_0x506c('0x2e')]){_0x59e724[_0x506c('0xb')][_0x506c('0x2f')]=_0x49e2c1;var _0x3df8a0=_0x49e2c1[0x0];var _0x28d5c9=_0x3df8a0[_0x506c('0x30')][_0x506c('0x31')]&&_0x3df8a0['queryResult'][_0x506c('0x31')]['fields']?_0x3df8a0[_0x506c('0x30')][_0x506c('0x31')][_0x506c('0x32')][_0x506c('0x33')][_0x506c('0x34')]:![];_0x59e724[_0x506c('0xb')]['DIALOGFLOWV2_ACTION']=_0x3df8a0[_0x506c('0x30')][_0x506c('0x35')];_0x59e724[_0x506c('0xb')][_0x506c('0x36')]=_0x3df8a0['queryResult'][_0x506c('0x37')];_0x59e724['body'][_0x506c('0x38')]=_0x3df8a0['queryResult'][_0x506c('0x31')]&&_0x3df8a0['queryResult'][_0x506c('0x31')]['fields']?_0x3df8a0['queryResult'][_0x506c('0x31')]['fields'][_0x506c('0x33')]['boolValue']:![];_0x59e724[_0x506c('0xb')][_0x506c('0x39')]=_0x3df8a0[_0x506c('0x30')]['intent']['name'];_0x59e724['body'][_0x506c('0x3a')]=_0x3df8a0[_0x506c('0x30')]['intent'][_0x506c('0x3b')];_0x59e724[_0x506c('0xb')]['DIALOGFLOWV2_ISFALLBACKINTENT']=_0x3df8a0[_0x506c('0x30')][_0x506c('0x3c')]['isFallback'];_0x59e724['body'][_0x506c('0x3d')]=_0x3df8a0[_0x506c('0x30')][_0x506c('0x3e')];_0x59e724[_0x506c('0xb')][_0x506c('0x3f')]=_0x3df8a0[_0x506c('0x30')][_0x506c('0x40')];_0x59e724[_0x506c('0xb')][_0x506c('0x41')]=_0x3df8a0['queryResult'][_0x506c('0x42')];_0x1c6755['providerName']='dialogflowV2';_0x1c6755[_0x506c('0x43')]=JSON[_0x506c('0x11')](_0x49e2c1);var _0x665edd=[];for(var _0x460f1b=0x0;_0x460f1b<_0x49e2c1['length'];_0x460f1b++){var _0x222a94=_0x49e2c1[_0x460f1b];if(_0x222a94&&_0x222a94[_0x506c('0x30')]&&_0x222a94[_0x506c('0x30')]['fulfillmentText']){_0x665edd[_0x506c('0x44')](createMessage(_0xe3744a,_0x2cc59c,_0x1c6755,_0x222a94['queryResult']['fulfillmentText']));}}return Promise[_0x506c('0x45')](_0x665edd)[_0x506c('0x2d')](function(){if(_0x28d5c9){_0x249f95['next']();}else{_0x249f95[_0x506c('0x28')]();}})['catch'](function(_0x3890ce){logger[_0x506c('0x26')](_0x506c('0xf'),_0xe3744a,JSON[_0x506c('0x11')](_0x3890ce));_0x249f95[_0x506c('0x17')]();});}})[_0x506c('0x10')](function(_0xfe9675){logger[_0x506c('0x26')](_0x506c('0xf'),_0xe3744a,_0xfe9675['message']);_0x249f95[_0x506c('0x17')]();});}}catch(_0x2b6e66){logger[_0x506c('0x26')](_0x506c('0xf'),_0xe3744a,JSON[_0x506c('0x11')](_0x2b6e66));_0x249f95['stop']();}};};