Built motion from commit (unavailable).|2.5.28
[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 _0xc893=['created','getMustacheRender','create','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!','stop','replace','SessionsClient','sessionPath','message','detectIntent','length','dialogflowV2','queryResult','diagnosticInfo','fields','end_conversation','boolValue','DIALOGFLOWV2_ACTION','action','allRequiredParamsPresent','DIALOGFLOWV2_INTENTNAME','intent','name','DIALOGFLOWV2_INTENTDISPLAYNAME','displayName','DIALOGFLOWV2_ISFALLBACKINTENT','DIALOGFLOWV2_LANGUAGECODE','languageCode','DIALOGFLOWV2_QUERYTEXT','queryText','DIALOGFLOWV2_SCORE','intentDetectionConfidence','push','fulfillmentText','all','next','lodash','moment','bluebird','../../../../config/logger','routing','./utils','../../rpc/openchannelMessage','../../rpc/mailMessage','../../rpc/smsMessage','../../rpc/whatsappMessage','clone','Message','then','info','[%s][DIALOGFLOWV2]\x20%s','body','catch','error','stringify','consequence','isNil','UserId','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','format','YYYY-MM-DD\x20HH:mm:ss','out','channel','sms','phone','chat','ChatWebsiteId','account','upperFirst','AccountId','InteractionId','interaction'];(function(_0x3a6eb9,_0x3ced50){var _0x299553=function(_0x437935){while(--_0x437935){_0x3a6eb9['push'](_0x3a6eb9['shift']());}};_0x299553(++_0x3ced50);}(_0xc893,0x1d4));var _0x3c89=function(_0x3749c0,_0x3288f9){_0x3749c0=_0x3749c0-0x0;var _0x2d29f3=_0xc893[_0x3749c0];return _0x2d29f3;};'use strict';var _=require(_0x3c89('0x0'));var dialogflow=require('dialogflow');var md5=require('md5');var moment=require(_0x3c89('0x1'));var Promise=require(_0x3c89('0x2'));var logger=require(_0x3c89('0x3'))(_0x3c89('0x4'));var utils=require(_0x3c89('0x5'));var rpc={'chatMessage':require('../../rpc/chatMessage'),'openchannelMessage':require(_0x3c89('0x6')),'mailMessage':require(_0x3c89('0x7')),'smsMessage':require(_0x3c89('0x8')),'faxMessage':require('../../rpc/faxMessage'),'whatsappMessage':require(_0x3c89('0x9'))};function createMessage(_0x7bba50,_0x3983d3,_0x1c30ad,_0x4e07cf){return new Promise(function(_0x1057ed,_0x2bd7a5){var _0x1cdfed=_[_0x3c89('0xa')](_0x1c30ad);_0x1cdfed['body']=_0x4e07cf;return rpc[_0x3983d3+_0x3c89('0xb')]['create'](_0x1cdfed)[_0x3c89('0xc')](function(){logger[_0x3c89('0xd')](_0x3c89('0xe'),_0x7bba50,_0x1cdfed[_0x3c89('0xf')]);_0x1057ed();})[_0x3c89('0x10')](function(_0x13890b){logger[_0x3c89('0x11')](_0x3c89('0xe'),_0x7bba50,JSON[_0x3c89('0x12')](_0x13890b));_0x2bd7a5(_0x13890b);});});}exports[_0x3c89('0x13')]=function(_0x5d44e1,_0x4d88a9){return function(_0x44b7b1){try{if(!_[_0x3c89('0x14')](this['interaction'][_0x3c89('0x15')])){logger[_0x3c89('0xd')](_0x3c89('0xe'),_0x5d44e1,_0x3c89('0x16'));return _0x44b7b1['next']();}var _0x51c2a8={'ContactId':this['contact']['id'],'sentAt':moment()[_0x3c89('0x17')](_0x3c89('0x18')),'direction':_0x3c89('0x19')};if(this[_0x3c89('0x1a')]===_0x3c89('0x1b')||this[_0x3c89('0x1a')]==='whatsapp'){_0x51c2a8[_0x3c89('0x1c')]=this['interaction'][_0x3c89('0x1c')];}var _0x59e021=this[_0x3c89('0x1a')];if(_0x59e021===_0x3c89('0x1d')){_0x51c2a8[_0x3c89('0x1e')]=this[_0x3c89('0x1f')]['id'];}else{_0x51c2a8[_[_0x3c89('0x20')](_0x59e021)+_0x3c89('0x21')]=this[_0x3c89('0x1f')]['id'];}_0x51c2a8[_[_0x3c89('0x20')](_0x59e021)+_0x3c89('0x22')]=this['interaction']['id'];if(this[_0x3c89('0x23')][_0x3c89('0x24')]){var _0x2c3bc7=_0x4d88a9[0x4];if(!_['isEmpty'](_0x2c3bc7)){_0x51c2a8[_0x3c89('0xf')]=utils[_0x3c89('0x25')](_0x2c3bc7,this);return rpc[_0x59e021+_0x3c89('0xb')][_0x3c89('0x26')](_0x51c2a8)[_0x3c89('0xc')](function(_0x2271fc){logger[_0x3c89('0xd')](_0x3c89('0xe'),_0x5d44e1,_0x51c2a8['body']);})['catch'](function(_0x3729b3){logger[_0x3c89('0x11')](_0x3c89('0xe'),_0x5d44e1,JSON[_0x3c89('0x12')](_0x3729b3));})['finally'](function(){logger['info']('[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!',_0x5d44e1);_0x44b7b1['stop']();});}else{logger[_0x3c89('0xd')](_0x3c89('0x27'),_0x5d44e1);_0x44b7b1[_0x3c89('0x28')]();}}else{var _0x307c97=this;var _0x11eecc=_0x4d88a9[0x2][_0x3c89('0x29')](/\\n/g,'\x0a');var _0x148d83={'credentials':{'private_key':_0x11eecc,'client_email':_0x4d88a9[0x1]}};var _0x3450c5=new dialogflow[(_0x3c89('0x2a'))](_0x148d83);var _0x5648c4=_0x3450c5[_0x3c89('0x2b')](_0x4d88a9[0x0],md5(_0x307c97[_0x3c89('0x23')]['id']));var _0x5ab892={'session':_0x5648c4,'queryInput':{'text':{'text':_0x307c97[_0x3c89('0x2c')][_0x3c89('0xf')],'languageCode':_0x4d88a9[0x3]||'en'}}};return _0x3450c5[_0x3c89('0x2d')](_0x5ab892)[_0x3c89('0xc')](function(_0x1a5ed9){if(_0x1a5ed9&&_0x1a5ed9[_0x3c89('0x2e')]){_0x307c97['body'][_0x3c89('0x2f')]=_0x1a5ed9;var _0x3c5b82=_0x1a5ed9[0x0];var _0x314120=_0x3c5b82[_0x3c89('0x30')]['diagnosticInfo']&&_0x3c5b82['queryResult'][_0x3c89('0x31')][_0x3c89('0x32')]?_0x3c5b82[_0x3c89('0x30')][_0x3c89('0x31')][_0x3c89('0x32')][_0x3c89('0x33')][_0x3c89('0x34')]:![];_0x307c97[_0x3c89('0xf')][_0x3c89('0x35')]=_0x3c5b82['queryResult'][_0x3c89('0x36')];_0x307c97[_0x3c89('0xf')]['DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT']=_0x3c5b82[_0x3c89('0x30')][_0x3c89('0x37')];_0x307c97[_0x3c89('0xf')]['DIALOGFLOWV2_ENDCONVERSATION']=_0x3c5b82[_0x3c89('0x30')][_0x3c89('0x31')]&&_0x3c5b82[_0x3c89('0x30')]['diagnosticInfo'][_0x3c89('0x32')]?_0x3c5b82['queryResult']['diagnosticInfo'][_0x3c89('0x32')][_0x3c89('0x33')]['boolValue']:![];_0x307c97[_0x3c89('0xf')][_0x3c89('0x38')]=_0x3c5b82[_0x3c89('0x30')][_0x3c89('0x39')][_0x3c89('0x3a')];_0x307c97[_0x3c89('0xf')][_0x3c89('0x3b')]=_0x3c5b82[_0x3c89('0x30')][_0x3c89('0x39')][_0x3c89('0x3c')];_0x307c97[_0x3c89('0xf')][_0x3c89('0x3d')]=_0x3c5b82['queryResult'][_0x3c89('0x39')]['isFallback'];_0x307c97[_0x3c89('0xf')][_0x3c89('0x3e')]=_0x3c5b82[_0x3c89('0x30')][_0x3c89('0x3f')];_0x307c97['body'][_0x3c89('0x40')]=_0x3c5b82['queryResult'][_0x3c89('0x41')];_0x307c97[_0x3c89('0xf')][_0x3c89('0x42')]=_0x3c5b82[_0x3c89('0x30')][_0x3c89('0x43')];_0x51c2a8['providerName']='dialogflowV2';_0x51c2a8['providerResponse']=JSON['stringify'](_0x1a5ed9);var _0x15a98b=[];for(var _0x3d8047=0x0;_0x3d8047<_0x1a5ed9[_0x3c89('0x2e')];_0x3d8047++){var _0x427bfe=_0x1a5ed9[_0x3d8047];if(_0x427bfe&&_0x427bfe[_0x3c89('0x30')]&&_0x427bfe[_0x3c89('0x30')]['fulfillmentText']){_0x15a98b[_0x3c89('0x44')](createMessage(_0x5d44e1,_0x59e021,_0x51c2a8,_0x427bfe['queryResult'][_0x3c89('0x45')]));}}return Promise[_0x3c89('0x46')](_0x15a98b)[_0x3c89('0xc')](function(){if(_0x314120){_0x44b7b1[_0x3c89('0x47')]();}else{_0x44b7b1[_0x3c89('0x28')]();}})['catch'](function(_0x3a2bd7){logger[_0x3c89('0x11')](_0x3c89('0xe'),_0x5d44e1,JSON[_0x3c89('0x12')](_0x3a2bd7));_0x44b7b1['next']();});}})[_0x3c89('0x10')](function(_0x411392){logger[_0x3c89('0x11')](_0x3c89('0xe'),_0x5d44e1,_0x411392[_0x3c89('0x2c')]);_0x44b7b1[_0x3c89('0x47')]();});}}catch(_0xbdee9f){logger[_0x3c89('0x11')](_0x3c89('0xe'),_0x5d44e1,JSON[_0x3c89('0x12')](_0xbdee9f));_0x44b7b1['stop']();}};};