Built motion from commit (unavailable).|2.5.11
[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 _0x9fb9=['DIALOGFLOWV2_ACTION','action','DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT','allRequiredParamsPresent','DIALOGFLOWV2_ENDCONVERSATION','DIALOGFLOWV2_INTENTNAME','name','DIALOGFLOWV2_INTENTDISPLAYNAME','intent','DIALOGFLOWV2_ISFALLBACKINTENT','isFallback','DIALOGFLOWV2_LANGUAGECODE','languageCode','DIALOGFLOWV2_QUERYTEXT','queryText','DIALOGFLOWV2_SCORE','providerName','providerResponse','fulfillmentText','push','all','next','stop','lodash','md5','moment','bluebird','../../../../config/logger','routing','./utils','../../rpc/chatMessage','../../rpc/mailMessage','../../rpc/smsMessage','../../rpc/whatsappMessage','clone','Message','create','then','info','body','catch','error','[%s][DIALOGFLOWV2]\x20%s','stringify','consequence','isNil','interaction','UserId','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','contact','YYYY-MM-DD\x20HH:mm:ss','channel','whatsapp','phone','chat','ChatWebsiteId','account','upperFirst','AccountId','created','getMustacheRender','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!','replace','SessionsClient','message','length','queryResult','diagnosticInfo','fields','end_conversation','boolValue'];(function(_0x2d89ce,_0x24d58d){var _0x3fad10=function(_0xad0ec){while(--_0xad0ec){_0x2d89ce['push'](_0x2d89ce['shift']());}};_0x3fad10(++_0x24d58d);}(_0x9fb9,0xef));var _0x99fb=function(_0x6060dc,_0x5bc649){_0x6060dc=_0x6060dc-0x0;var _0x45edcc=_0x9fb9[_0x6060dc];return _0x45edcc;};'use strict';var _=require(_0x99fb('0x0'));var dialogflow=require('dialogflow');var md5=require(_0x99fb('0x1'));var moment=require(_0x99fb('0x2'));var Promise=require(_0x99fb('0x3'));var logger=require(_0x99fb('0x4'))(_0x99fb('0x5'));var utils=require(_0x99fb('0x6'));var rpc={'chatMessage':require(_0x99fb('0x7')),'openchannelMessage':require('../../rpc/openchannelMessage'),'mailMessage':require(_0x99fb('0x8')),'smsMessage':require(_0x99fb('0x9')),'faxMessage':require('../../rpc/faxMessage'),'whatsappMessage':require(_0x99fb('0xa'))};function createMessage(_0x165f53,_0x2efc03,_0x514052,_0x21f9bf){return new Promise(function(_0xfafb2a,_0x3d1136){var _0x496570=_[_0x99fb('0xb')](_0x514052);_0x496570['body']=_0x21f9bf;return rpc[_0x2efc03+_0x99fb('0xc')][_0x99fb('0xd')](_0x496570)[_0x99fb('0xe')](function(){logger[_0x99fb('0xf')]('[%s][DIALOGFLOWV2]\x20%s',_0x165f53,_0x496570[_0x99fb('0x10')]);_0xfafb2a();})[_0x99fb('0x11')](function(_0x5b14a7){logger[_0x99fb('0x12')](_0x99fb('0x13'),_0x165f53,JSON[_0x99fb('0x14')](_0x5b14a7));_0x3d1136(_0x5b14a7);});});}exports[_0x99fb('0x15')]=function(_0x1158c1,_0x35f489){return function(_0x3fb6b9){try{if(!_[_0x99fb('0x16')](this[_0x99fb('0x17')][_0x99fb('0x18')])){logger[_0x99fb('0xf')]('[%s][DIALOGFLOWV2]\x20%s',_0x1158c1,_0x99fb('0x19'));return _0x3fb6b9['next']();}var _0x4c4a67={'ContactId':this[_0x99fb('0x1a')]['id'],'sentAt':moment()['format'](_0x99fb('0x1b')),'direction':'out'};if(this[_0x99fb('0x1c')]==='sms'||this[_0x99fb('0x1c')]===_0x99fb('0x1d')){_0x4c4a67[_0x99fb('0x1e')]=this[_0x99fb('0x17')][_0x99fb('0x1e')];}var _0x302cb6=this[_0x99fb('0x1c')];if(_0x302cb6===_0x99fb('0x1f')){_0x4c4a67[_0x99fb('0x20')]=this[_0x99fb('0x21')]['id'];}else{_0x4c4a67[_[_0x99fb('0x22')](_0x302cb6)+_0x99fb('0x23')]=this[_0x99fb('0x21')]['id'];}_0x4c4a67[_[_0x99fb('0x22')](_0x302cb6)+'InteractionId']=this['interaction']['id'];if(this[_0x99fb('0x17')][_0x99fb('0x24')]){var _0x33c127=_0x35f489[0x4];if(!_['isEmpty'](_0x33c127)){_0x4c4a67['body']=utils[_0x99fb('0x25')](_0x33c127,this);return rpc[_0x302cb6+_0x99fb('0xc')]['create'](_0x4c4a67)[_0x99fb('0xe')](function(_0xa7733a){logger['info'](_0x99fb('0x13'),_0x1158c1,_0x4c4a67['body']);})['catch'](function(_0x2e172e){logger[_0x99fb('0x12')](_0x99fb('0x13'),_0x1158c1,JSON[_0x99fb('0x14')](_0x2e172e));})['finally'](function(){logger['info'](_0x99fb('0x26'),_0x1158c1);_0x3fb6b9['stop']();});}else{logger[_0x99fb('0xf')](_0x99fb('0x27'),_0x1158c1);_0x3fb6b9['stop']();}}else{var _0x3fb3a3=this;var _0x365404=_0x35f489[0x2][_0x99fb('0x28')](/\\n/g,'\x0a');var _0x4269a7={'credentials':{'private_key':_0x365404,'client_email':_0x35f489[0x1]}};var _0x57763b=new dialogflow[(_0x99fb('0x29'))](_0x4269a7);var _0x2513eb=_0x57763b['sessionPath'](_0x35f489[0x0],md5(_0x3fb3a3['interaction']['id']));var _0x364572={'session':_0x2513eb,'queryInput':{'text':{'text':_0x3fb3a3[_0x99fb('0x2a')][_0x99fb('0x10')],'languageCode':_0x35f489[0x3]||'en'}}};return _0x57763b['detectIntent'](_0x364572)[_0x99fb('0xe')](function(_0x4406b8){if(_0x4406b8&&_0x4406b8[_0x99fb('0x2b')]){_0x3fb3a3['body']['dialogflowV2']=_0x4406b8;var _0x4b7461=_0x4406b8[0x0];var _0x262caa=_0x4b7461[_0x99fb('0x2c')][_0x99fb('0x2d')]&&_0x4b7461[_0x99fb('0x2c')][_0x99fb('0x2d')][_0x99fb('0x2e')]?_0x4b7461[_0x99fb('0x2c')]['diagnosticInfo']['fields'][_0x99fb('0x2f')][_0x99fb('0x30')]:![];_0x3fb3a3[_0x99fb('0x10')][_0x99fb('0x31')]=_0x4b7461[_0x99fb('0x2c')][_0x99fb('0x32')];_0x3fb3a3[_0x99fb('0x10')][_0x99fb('0x33')]=_0x4b7461['queryResult'][_0x99fb('0x34')];_0x3fb3a3['body'][_0x99fb('0x35')]=_0x4b7461[_0x99fb('0x2c')][_0x99fb('0x2d')]&&_0x4b7461[_0x99fb('0x2c')][_0x99fb('0x2d')]['fields']?_0x4b7461['queryResult'][_0x99fb('0x2d')][_0x99fb('0x2e')]['end_conversation'][_0x99fb('0x30')]:![];_0x3fb3a3[_0x99fb('0x10')][_0x99fb('0x36')]=_0x4b7461[_0x99fb('0x2c')]['intent'][_0x99fb('0x37')];_0x3fb3a3['body'][_0x99fb('0x38')]=_0x4b7461[_0x99fb('0x2c')][_0x99fb('0x39')]['displayName'];_0x3fb3a3['body'][_0x99fb('0x3a')]=_0x4b7461[_0x99fb('0x2c')][_0x99fb('0x39')][_0x99fb('0x3b')];_0x3fb3a3[_0x99fb('0x10')][_0x99fb('0x3c')]=_0x4b7461[_0x99fb('0x2c')][_0x99fb('0x3d')];_0x3fb3a3[_0x99fb('0x10')][_0x99fb('0x3e')]=_0x4b7461['queryResult'][_0x99fb('0x3f')];_0x3fb3a3[_0x99fb('0x10')][_0x99fb('0x40')]=_0x4b7461[_0x99fb('0x2c')]['intentDetectionConfidence'];_0x4c4a67[_0x99fb('0x41')]='dialogflowV2';_0x4c4a67[_0x99fb('0x42')]=JSON[_0x99fb('0x14')](_0x4406b8);var _0xe73790=[];for(var _0xf5398=0x0;_0xf5398<_0x4406b8[_0x99fb('0x2b')];_0xf5398++){var _0x56539d=_0x4406b8[_0xf5398];if(_0x56539d&&_0x56539d['queryResult']&&_0x56539d[_0x99fb('0x2c')][_0x99fb('0x43')]){_0xe73790[_0x99fb('0x44')](createMessage(_0x1158c1,_0x302cb6,_0x4c4a67,_0x56539d[_0x99fb('0x2c')][_0x99fb('0x43')]));}}return Promise[_0x99fb('0x45')](_0xe73790)['then'](function(){if(_0x262caa){_0x3fb6b9[_0x99fb('0x46')]();}else{_0x3fb6b9[_0x99fb('0x47')]();}})[_0x99fb('0x11')](function(_0x37218c){logger[_0x99fb('0x12')]('[%s][DIALOGFLOWV2]\x20%s',_0x1158c1,JSON[_0x99fb('0x14')](_0x37218c));_0x3fb6b9['next']();});}})['catch'](function(_0x2f13f1){logger[_0x99fb('0x12')](_0x99fb('0x13'),_0x1158c1,_0x2f13f1[_0x99fb('0x2a')]);_0x3fb6b9[_0x99fb('0x46')]();});}}catch(_0x33a88c){logger[_0x99fb('0x12')]('[%s][DIALOGFLOWV2]\x20%s',_0x1158c1,JSON[_0x99fb('0x14')](_0x33a88c));_0x3fb6b9['stop']();}};};