c660925c191eb12cfa7b5a279aff56b8cfed79b2
[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 _0x3542=['bluebird','../../../../config/logger','./utils','../../rpc/mailMessage','clone','body','create','then','info','catch','error','[%s][DIALOGFLOWV2]\x20%s','isNil','UserId','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','format','YYYY-MM-DD\x20HH:mm:ss','channel','whatsapp','interaction','phone','chat','upperFirst','account','InteractionId','created','getMustacheRender','stringify','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!','stop','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!','replace','SessionsClient','message','detectIntent','length','queryResult','diagnosticInfo','fields','boolValue','action','DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT','allRequiredParamsPresent','DIALOGFLOWV2_ENDCONVERSATION','end_conversation','DIALOGFLOWV2_INTENTNAME','name','intent','displayName','DIALOGFLOWV2_ISFALLBACKINTENT','DIALOGFLOWV2_LANGUAGECODE','languageCode','DIALOGFLOWV2_QUERYTEXT','queryText','DIALOGFLOWV2_SCORE','intentDetectionConfidence','providerName','dialogflowV2','providerResponse','push','all','next','stack','moment'];(function(_0xae7c2f,_0xf31e04){var _0x5d7001=function(_0x4eeadb){while(--_0x4eeadb){_0xae7c2f['push'](_0xae7c2f['shift']());}};_0x5d7001(++_0xf31e04);}(_0x3542,0xff));var _0x2354=function(_0x3b632f,_0x453380){_0x3b632f=_0x3b632f-0x0;var _0x10e788=_0x3542[_0x3b632f];return _0x10e788;};'use strict';var _=require('lodash');var dialogflow=require('dialogflow');var md5=require('md5');var moment=require(_0x2354('0x0'));var Promise=require(_0x2354('0x1'));var logger=require(_0x2354('0x2'))('routing');var utils=require(_0x2354('0x3'));var rpc={'chatMessage':require('../../rpc/chatMessage'),'openchannelMessage':require('../../rpc/openchannelMessage'),'mailMessage':require(_0x2354('0x4')),'smsMessage':require('../../rpc/smsMessage'),'faxMessage':require('../../rpc/faxMessage'),'whatsappMessage':require('../../rpc/whatsappMessage')};function createMessage(_0x1cb3cc,_0x21373d,_0x4d62a2,_0x5723df){return new Promise(function(_0x3ac384,_0x58a7ff){var _0x2ae567=_[_0x2354('0x5')](_0x4d62a2);_0x2ae567[_0x2354('0x6')]=_0x5723df;return rpc[_0x21373d+'Message'][_0x2354('0x7')](_0x2ae567)[_0x2354('0x8')](function(){logger[_0x2354('0x9')]('[%s][DIALOGFLOWV2]\x20%s',_0x1cb3cc,_0x2ae567[_0x2354('0x6')]);_0x3ac384();})[_0x2354('0xa')](function(_0x22d6a9){logger[_0x2354('0xb')](_0x2354('0xc'),_0x1cb3cc,JSON['stringify'](_0x22d6a9));_0x58a7ff(_0x22d6a9);});});}exports['consequence']=function(_0x33fdb1,_0x2775c1){return function(_0x5c00de){try{if(!_[_0x2354('0xd')](this['interaction'][_0x2354('0xe')])){logger[_0x2354('0x9')](_0x2354('0xc'),_0x33fdb1,_0x2354('0xf'));return _0x5c00de['next']();}var _0x30f1a4={'ContactId':this['contact']['id'],'sentAt':moment()[_0x2354('0x10')](_0x2354('0x11')),'direction':'out'};if(this[_0x2354('0x12')]==='sms'||this['channel']===_0x2354('0x13')){_0x30f1a4['phone']=this[_0x2354('0x14')][_0x2354('0x15')];}var _0x25a3b0=this[_0x2354('0x12')];if(_0x25a3b0===_0x2354('0x16')){_0x30f1a4['ChatWebsiteId']=this['account']['id'];}else{_0x30f1a4[_[_0x2354('0x17')](_0x25a3b0)+'AccountId']=this[_0x2354('0x18')]['id'];}_0x30f1a4[_['upperFirst'](_0x25a3b0)+_0x2354('0x19')]=this[_0x2354('0x14')]['id'];if(this[_0x2354('0x14')][_0x2354('0x1a')]){var _0xa645df=_0x2775c1[0x4];if(!_['isEmpty'](_0xa645df)){_0x30f1a4['body']=utils[_0x2354('0x1b')](_0xa645df,this);return rpc[_0x25a3b0+'Message']['create'](_0x30f1a4)[_0x2354('0x8')](function(_0x50cc08){logger[_0x2354('0x9')]('[%s][DIALOGFLOWV2]\x20%s',_0x33fdb1,_0x30f1a4[_0x2354('0x6')]);})[_0x2354('0xa')](function(_0x348943){logger[_0x2354('0xb')]('[%s][DIALOGFLOWV2]\x20%s',_0x33fdb1,JSON[_0x2354('0x1c')](_0x348943));})['finally'](function(){logger[_0x2354('0x9')](_0x2354('0x1d'),_0x33fdb1);_0x5c00de[_0x2354('0x1e')]();});}else{logger[_0x2354('0x9')](_0x2354('0x1f'),_0x33fdb1);_0x5c00de[_0x2354('0x1e')]();}}else{var _0x186f3c=this;var _0x49f751=_0x2775c1[0x2][_0x2354('0x20')](/\\n/g,'\x0a');var _0x5af569={'credentials':{'private_key':_0x49f751,'client_email':_0x2775c1[0x1]}};var _0x40a113=new dialogflow[(_0x2354('0x21'))](_0x5af569);var _0x535a0f=_0x40a113['sessionPath'](_0x2775c1[0x0],md5(_0x186f3c[_0x2354('0x14')]['id']));var _0x15f161={'session':_0x535a0f,'queryInput':{'text':{'text':_0x186f3c[_0x2354('0x22')][_0x2354('0x6')],'languageCode':_0x2775c1[0x3]||'en'}}};return _0x40a113[_0x2354('0x23')](_0x15f161)[_0x2354('0x8')](function(_0x374c6d){if(_0x374c6d&&_0x374c6d[_0x2354('0x24')]){_0x186f3c[_0x2354('0x6')]['dialogflowV2']=_0x374c6d;var _0x390e99=_0x374c6d[0x0];var _0x3a7cf7=_0x390e99[_0x2354('0x25')][_0x2354('0x26')]&&_0x390e99[_0x2354('0x25')][_0x2354('0x26')][_0x2354('0x27')]?_0x390e99[_0x2354('0x25')][_0x2354('0x26')][_0x2354('0x27')]['end_conversation'][_0x2354('0x28')]:![];_0x186f3c[_0x2354('0x6')]['DIALOGFLOWV2_ACTION']=_0x390e99[_0x2354('0x25')][_0x2354('0x29')];_0x186f3c[_0x2354('0x6')][_0x2354('0x2a')]=_0x390e99[_0x2354('0x25')][_0x2354('0x2b')];_0x186f3c[_0x2354('0x6')][_0x2354('0x2c')]=_0x390e99['queryResult']['diagnosticInfo']&&_0x390e99[_0x2354('0x25')][_0x2354('0x26')][_0x2354('0x27')]?_0x390e99[_0x2354('0x25')][_0x2354('0x26')][_0x2354('0x27')][_0x2354('0x2d')][_0x2354('0x28')]:![];_0x186f3c[_0x2354('0x6')][_0x2354('0x2e')]=_0x390e99[_0x2354('0x25')]['intent'][_0x2354('0x2f')];_0x186f3c[_0x2354('0x6')]['DIALOGFLOWV2_INTENTDISPLAYNAME']=_0x390e99[_0x2354('0x25')][_0x2354('0x30')][_0x2354('0x31')];_0x186f3c[_0x2354('0x6')][_0x2354('0x32')]=_0x390e99[_0x2354('0x25')][_0x2354('0x30')]['isFallback'];_0x186f3c[_0x2354('0x6')][_0x2354('0x33')]=_0x390e99[_0x2354('0x25')][_0x2354('0x34')];_0x186f3c[_0x2354('0x6')][_0x2354('0x35')]=_0x390e99[_0x2354('0x25')][_0x2354('0x36')];_0x186f3c['body'][_0x2354('0x37')]=_0x390e99[_0x2354('0x25')][_0x2354('0x38')];_0x30f1a4[_0x2354('0x39')]=_0x2354('0x3a');_0x30f1a4[_0x2354('0x3b')]=JSON[_0x2354('0x1c')](_0x374c6d);var _0xd5fe8b=[];for(var _0x13b361=0x0;_0x13b361<_0x374c6d['length'];_0x13b361++){var _0x42cb9a=_0x374c6d[_0x13b361];if(_0x42cb9a&&_0x42cb9a[_0x2354('0x25')]&&_0x42cb9a[_0x2354('0x25')]['fulfillmentText']){_0xd5fe8b[_0x2354('0x3c')](createMessage(_0x33fdb1,_0x25a3b0,_0x30f1a4,_0x42cb9a[_0x2354('0x25')]['fulfillmentText']));}}return Promise[_0x2354('0x3d')](_0xd5fe8b)[_0x2354('0x8')](function(){if(_0x3a7cf7){_0x5c00de['next']();}else{_0x5c00de[_0x2354('0x1e')]();}})[_0x2354('0xa')](function(_0x3a8bd2){logger[_0x2354('0xb')]('[%s][DIALOGFLOWV2]\x20%s',_0x33fdb1,JSON[_0x2354('0x1c')](_0x3a8bd2));_0x5c00de[_0x2354('0x3e')]();});}})['catch'](function(_0x351927){logger[_0x2354('0xb')](_0x2354('0xc'),_0x33fdb1,_0x351927[_0x2354('0x22')],_0x351927[_0x2354('0x3f')]);_0x5c00de[_0x2354('0x3e')]();});}}catch(_0x3ae11f){logger[_0x2354('0xb')](_0x2354('0xc'),_0x33fdb1,JSON[_0x2354('0x1c')](_0x3ae11f));_0x5c00de[_0x2354('0x1e')]();}};};