ca8215250af0aa78e7ea4c61b75d86c422be0a6a
[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 _0x9ad7=['diagnosticInfo','end_conversation','DIALOGFLOWV2_ACTION','action','DIALOGFLOWV2_ENDCONVERSATION','fields','intent','name','DIALOGFLOWV2_INTENTDISPLAYNAME','displayName','DIALOGFLOWV2_ISFALLBACKINTENT','isFallback','languageCode','DIALOGFLOWV2_QUERYTEXT','queryText','DIALOGFLOWV2_SCORE','intentDetectionConfidence','providerResponse','fulfillmentText','push','next','stack','dialogflow','md5','moment','bluebird','../../../../config/logger','./utils','../../rpc/chatMessage','../../rpc/mailMessage','../../rpc/smsMessage','../../rpc/faxMessage','../../rpc/whatsappMessage','clone','body','Message','create','then','info','catch','error','[%s][DIALOGFLOWV2]\x20%s','stringify','isNil','interaction','UserId','contact','YYYY-MM-DD\x20HH:mm:ss','out','channel','sms','chat','AccountId','upperFirst','created','isEmpty','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!','stop','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!','replace','message','length','dialogflowV2','queryResult'];(function(_0x36ea0d,_0x492a9a){var _0x541d86=function(_0x34666c){while(--_0x34666c){_0x36ea0d['push'](_0x36ea0d['shift']());}};_0x541d86(++_0x492a9a);}(_0x9ad7,0x116));var _0x79ad=function(_0x1ae5ab,_0xc5d441){_0x1ae5ab=_0x1ae5ab-0x0;var _0x1ce001=_0x9ad7[_0x1ae5ab];return _0x1ce001;};'use strict';var _=require('lodash');var dialogflow=require(_0x79ad('0x0'));var md5=require(_0x79ad('0x1'));var moment=require(_0x79ad('0x2'));var Promise=require(_0x79ad('0x3'));var logger=require(_0x79ad('0x4'))('routing');var utils=require(_0x79ad('0x5'));var rpc={'chatMessage':require(_0x79ad('0x6')),'openchannelMessage':require('../../rpc/openchannelMessage'),'mailMessage':require(_0x79ad('0x7')),'smsMessage':require(_0x79ad('0x8')),'faxMessage':require(_0x79ad('0x9')),'whatsappMessage':require(_0x79ad('0xa'))};function createMessage(_0x479869,_0x286c6c,_0x4536b1,_0xfd5f30){return new Promise(function(_0x3d516d,_0x503bed){var _0x487fc9=_[_0x79ad('0xb')](_0x4536b1);_0x487fc9[_0x79ad('0xc')]=_0xfd5f30;return rpc[_0x286c6c+_0x79ad('0xd')][_0x79ad('0xe')](_0x487fc9)[_0x79ad('0xf')](function(){logger[_0x79ad('0x10')]('[%s][DIALOGFLOWV2]\x20%s',_0x479869,_0x487fc9[_0x79ad('0xc')]);_0x3d516d();})[_0x79ad('0x11')](function(_0x3b5c42){logger[_0x79ad('0x12')](_0x79ad('0x13'),_0x479869,JSON[_0x79ad('0x14')](_0x3b5c42));_0x503bed(_0x3b5c42);});});}exports['consequence']=function(_0x83bfe6,_0x6d258f){return function(_0x154d5f){try{if(!_[_0x79ad('0x15')](this[_0x79ad('0x16')][_0x79ad('0x17')])){logger[_0x79ad('0x10')](_0x79ad('0x13'),_0x83bfe6,'Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.');return _0x154d5f['next']();}var _0x588df2={'ContactId':this[_0x79ad('0x18')]['id'],'sentAt':moment()['format'](_0x79ad('0x19')),'direction':_0x79ad('0x1a')};if(this[_0x79ad('0x1b')]===_0x79ad('0x1c')||this[_0x79ad('0x1b')]==='whatsapp'){_0x588df2['phone']=this[_0x79ad('0x16')]['phone'];}var _0x719c6c=this[_0x79ad('0x1b')];if(_0x719c6c===_0x79ad('0x1d')){_0x588df2['ChatWebsiteId']=this['account']['id'];}else{_0x588df2[_['upperFirst'](_0x719c6c)+_0x79ad('0x1e')]=this['account']['id'];}_0x588df2[_[_0x79ad('0x1f')](_0x719c6c)+'InteractionId']=this[_0x79ad('0x16')]['id'];if(this[_0x79ad('0x16')][_0x79ad('0x20')]){var _0x51c830=_0x6d258f[0x4];if(!_[_0x79ad('0x21')](_0x51c830)){_0x588df2['body']=utils['getMustacheRender'](_0x51c830,this);return rpc[_0x719c6c+_0x79ad('0xd')]['create'](_0x588df2)[_0x79ad('0xf')](function(_0x415691){logger[_0x79ad('0x10')](_0x79ad('0x13'),_0x83bfe6,_0x588df2['body']);})['catch'](function(_0x643412){logger[_0x79ad('0x12')](_0x79ad('0x13'),_0x83bfe6,JSON['stringify'](_0x643412));})['finally'](function(){logger[_0x79ad('0x10')](_0x79ad('0x22'),_0x83bfe6);_0x154d5f[_0x79ad('0x23')]();});}else{logger[_0x79ad('0x10')](_0x79ad('0x24'),_0x83bfe6);_0x154d5f[_0x79ad('0x23')]();}}else{var _0x340745=this;var _0x25b42c=_0x6d258f[0x2][_0x79ad('0x25')](/\\n/g,'\x0a');var _0x122d7c={'credentials':{'private_key':_0x25b42c,'client_email':_0x6d258f[0x1]}};var _0x246cd9=new dialogflow['SessionsClient'](_0x122d7c);var _0x2d8553=_0x246cd9['sessionPath'](_0x6d258f[0x0],md5(_0x340745['interaction']['id']));var _0x48f70a={'session':_0x2d8553,'queryInput':{'text':{'text':_0x340745[_0x79ad('0x26')][_0x79ad('0xc')],'languageCode':_0x6d258f[0x3]||'en'}}};return _0x246cd9['detectIntent'](_0x48f70a)['then'](function(_0xbc876c){if(_0xbc876c&&_0xbc876c[_0x79ad('0x27')]){_0x340745[_0x79ad('0xc')][_0x79ad('0x28')]=_0xbc876c;var _0x186743=_0xbc876c[0x0];var _0xf18a2f=_0x186743[_0x79ad('0x29')][_0x79ad('0x2a')]&&_0x186743[_0x79ad('0x29')]['diagnosticInfo']['fields']?_0x186743[_0x79ad('0x29')]['diagnosticInfo']['fields'][_0x79ad('0x2b')]['boolValue']:![];_0x340745[_0x79ad('0xc')][_0x79ad('0x2c')]=_0x186743[_0x79ad('0x29')][_0x79ad('0x2d')];_0x340745[_0x79ad('0xc')]['DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT']=_0x186743[_0x79ad('0x29')]['allRequiredParamsPresent'];_0x340745['body'][_0x79ad('0x2e')]=_0x186743['queryResult'][_0x79ad('0x2a')]&&_0x186743[_0x79ad('0x29')][_0x79ad('0x2a')]['fields']?_0x186743[_0x79ad('0x29')][_0x79ad('0x2a')][_0x79ad('0x2f')]['end_conversation']['boolValue']:![];_0x340745[_0x79ad('0xc')]['DIALOGFLOWV2_INTENTNAME']=_0x186743[_0x79ad('0x29')][_0x79ad('0x30')][_0x79ad('0x31')];_0x340745[_0x79ad('0xc')][_0x79ad('0x32')]=_0x186743[_0x79ad('0x29')][_0x79ad('0x30')][_0x79ad('0x33')];_0x340745[_0x79ad('0xc')][_0x79ad('0x34')]=_0x186743[_0x79ad('0x29')][_0x79ad('0x30')][_0x79ad('0x35')];_0x340745[_0x79ad('0xc')]['DIALOGFLOWV2_LANGUAGECODE']=_0x186743[_0x79ad('0x29')][_0x79ad('0x36')];_0x340745['body'][_0x79ad('0x37')]=_0x186743['queryResult'][_0x79ad('0x38')];_0x340745['body'][_0x79ad('0x39')]=_0x186743['queryResult'][_0x79ad('0x3a')];_0x588df2['providerName']=_0x79ad('0x28');_0x588df2[_0x79ad('0x3b')]=JSON[_0x79ad('0x14')](_0xbc876c);var _0x531091=[];for(var _0x4f3553=0x0;_0x4f3553<_0xbc876c[_0x79ad('0x27')];_0x4f3553++){var _0x2d3733=_0xbc876c[_0x4f3553];if(_0x2d3733&&_0x2d3733['queryResult']&&_0x2d3733[_0x79ad('0x29')][_0x79ad('0x3c')]){_0x531091[_0x79ad('0x3d')](createMessage(_0x83bfe6,_0x719c6c,_0x588df2,_0x2d3733[_0x79ad('0x29')]['fulfillmentText']));}}return Promise['all'](_0x531091)[_0x79ad('0xf')](function(){if(_0xf18a2f){_0x154d5f[_0x79ad('0x3e')]();}else{_0x154d5f['stop']();}})[_0x79ad('0x11')](function(_0x4c3d29){logger['error'](_0x79ad('0x13'),_0x83bfe6,JSON['stringify'](_0x4c3d29));_0x154d5f[_0x79ad('0x3e')]();});}})[_0x79ad('0x11')](function(_0x278eac){logger[_0x79ad('0x12')](_0x79ad('0x13'),_0x83bfe6,_0x278eac[_0x79ad('0x26')],_0x278eac[_0x79ad('0x3f')]);_0x154d5f[_0x79ad('0x3e')]();});}}catch(_0x2c99b2){logger[_0x79ad('0x12')]('[%s][DIALOGFLOWV2]\x20%s',_0x83bfe6,JSON[_0x79ad('0x14')](_0x2c99b2));_0x154d5f[_0x79ad('0x23')]();}};};