Built motion from commit (unavailable).|2.5.13
[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 _0x8765=['message','detectIntent','length','queryResult','diagnosticInfo','fields','end_conversation','boolValue','action','DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT','DIALOGFLOWV2_ENDCONVERSATION','DIALOGFLOWV2_INTENTNAME','intent','DIALOGFLOWV2_INTENTDISPLAYNAME','displayName','DIALOGFLOWV2_ISFALLBACKINTENT','DIALOGFLOWV2_LANGUAGECODE','languageCode','queryText','DIALOGFLOWV2_SCORE','intentDetectionConfidence','providerName','providerResponse','fulfillmentText','push','lodash','dialogflow','md5','moment','bluebird','../../../../config/logger','routing','./utils','../../rpc/chatMessage','../../rpc/openchannelMessage','../../rpc/mailMessage','../../rpc/smsMessage','../../rpc/whatsappMessage','body','Message','create','then','info','[%s][DIALOGFLOWV2]\x20%s','error','stringify','isNil','interaction','UserId','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','next','contact','format','YYYY-MM-DD\x20HH:mm:ss','out','channel','whatsapp','phone','chat','ChatWebsiteId','account','upperFirst','AccountId','InteractionId','created','catch','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!','stop','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!','replace'];(function(_0x26d52e,_0x1dd054){var _0x3f1e11=function(_0x24d8f1){while(--_0x24d8f1){_0x26d52e['push'](_0x26d52e['shift']());}};_0x3f1e11(++_0x1dd054);}(_0x8765,0x131));var _0x5876=function(_0x5f1391,_0x19c132){_0x5f1391=_0x5f1391-0x0;var _0x50ebe3=_0x8765[_0x5f1391];return _0x50ebe3;};'use strict';var _=require(_0x5876('0x0'));var dialogflow=require(_0x5876('0x1'));var md5=require(_0x5876('0x2'));var moment=require(_0x5876('0x3'));var Promise=require(_0x5876('0x4'));var logger=require(_0x5876('0x5'))(_0x5876('0x6'));var utils=require(_0x5876('0x7'));var rpc={'chatMessage':require(_0x5876('0x8')),'openchannelMessage':require(_0x5876('0x9')),'mailMessage':require(_0x5876('0xa')),'smsMessage':require(_0x5876('0xb')),'faxMessage':require('../../rpc/faxMessage'),'whatsappMessage':require(_0x5876('0xc'))};function createMessage(_0x1d5dd7,_0x55cac2,_0x11d479,_0x91d82d){return new Promise(function(_0xf1a54d,_0x460a91){var _0x410e51=_['clone'](_0x11d479);_0x410e51[_0x5876('0xd')]=_0x91d82d;return rpc[_0x55cac2+_0x5876('0xe')][_0x5876('0xf')](_0x410e51)[_0x5876('0x10')](function(){logger[_0x5876('0x11')](_0x5876('0x12'),_0x1d5dd7,_0x410e51[_0x5876('0xd')]);_0xf1a54d();})['catch'](function(_0x69a901){logger[_0x5876('0x13')]('[%s][DIALOGFLOWV2]\x20%s',_0x1d5dd7,JSON[_0x5876('0x14')](_0x69a901));_0x460a91(_0x69a901);});});}exports['consequence']=function(_0x118b22,_0x3eb6c3){return function(_0x38a9d7){try{if(!_[_0x5876('0x15')](this[_0x5876('0x16')][_0x5876('0x17')])){logger[_0x5876('0x11')](_0x5876('0x12'),_0x118b22,_0x5876('0x18'));return _0x38a9d7[_0x5876('0x19')]();}var _0x137dd8={'ContactId':this[_0x5876('0x1a')]['id'],'sentAt':moment()[_0x5876('0x1b')](_0x5876('0x1c')),'direction':_0x5876('0x1d')};if(this[_0x5876('0x1e')]==='sms'||this[_0x5876('0x1e')]===_0x5876('0x1f')){_0x137dd8[_0x5876('0x20')]=this[_0x5876('0x16')][_0x5876('0x20')];}var _0x533ca1=this[_0x5876('0x1e')];if(_0x533ca1===_0x5876('0x21')){_0x137dd8[_0x5876('0x22')]=this[_0x5876('0x23')]['id'];}else{_0x137dd8[_[_0x5876('0x24')](_0x533ca1)+_0x5876('0x25')]=this[_0x5876('0x23')]['id'];}_0x137dd8[_[_0x5876('0x24')](_0x533ca1)+_0x5876('0x26')]=this[_0x5876('0x16')]['id'];if(this['interaction'][_0x5876('0x27')]){var _0x418bb2=_0x3eb6c3[0x4];if(!_['isEmpty'](_0x418bb2)){_0x137dd8['body']=utils['getMustacheRender'](_0x418bb2,this);return rpc[_0x533ca1+_0x5876('0xe')][_0x5876('0xf')](_0x137dd8)[_0x5876('0x10')](function(_0x4d608c){logger[_0x5876('0x11')](_0x5876('0x12'),_0x118b22,_0x137dd8['body']);})[_0x5876('0x28')](function(_0x2d48d7){logger[_0x5876('0x13')](_0x5876('0x12'),_0x118b22,JSON[_0x5876('0x14')](_0x2d48d7));})['finally'](function(){logger['info'](_0x5876('0x29'),_0x118b22);_0x38a9d7[_0x5876('0x2a')]();});}else{logger[_0x5876('0x11')](_0x5876('0x2b'),_0x118b22);_0x38a9d7['stop']();}}else{var _0x3fbb48=this;var _0x465d2f=_0x3eb6c3[0x2][_0x5876('0x2c')](/\\n/g,'\x0a');var _0x529b51={'credentials':{'private_key':_0x465d2f,'client_email':_0x3eb6c3[0x1]}};var _0x294753=new dialogflow['SessionsClient'](_0x529b51);var _0x28049f=_0x294753['sessionPath'](_0x3eb6c3[0x0],md5(_0x3fbb48[_0x5876('0x16')]['id']));var _0x34b989={'session':_0x28049f,'queryInput':{'text':{'text':_0x3fbb48[_0x5876('0x2d')]['body'],'languageCode':_0x3eb6c3[0x3]||'en'}}};return _0x294753[_0x5876('0x2e')](_0x34b989)['then'](function(_0x15a871){if(_0x15a871&&_0x15a871[_0x5876('0x2f')]){_0x3fbb48['body']['dialogflowV2']=_0x15a871;var _0x281b32=_0x15a871[0x0];var _0x3317a0=_0x281b32[_0x5876('0x30')][_0x5876('0x31')]&&_0x281b32[_0x5876('0x30')][_0x5876('0x31')][_0x5876('0x32')]?_0x281b32[_0x5876('0x30')][_0x5876('0x31')][_0x5876('0x32')][_0x5876('0x33')][_0x5876('0x34')]:![];_0x3fbb48[_0x5876('0xd')]['DIALOGFLOWV2_ACTION']=_0x281b32[_0x5876('0x30')][_0x5876('0x35')];_0x3fbb48[_0x5876('0xd')][_0x5876('0x36')]=_0x281b32[_0x5876('0x30')]['allRequiredParamsPresent'];_0x3fbb48['body'][_0x5876('0x37')]=_0x281b32['queryResult'][_0x5876('0x31')]&&_0x281b32[_0x5876('0x30')][_0x5876('0x31')]['fields']?_0x281b32[_0x5876('0x30')][_0x5876('0x31')][_0x5876('0x32')]['end_conversation'][_0x5876('0x34')]:![];_0x3fbb48[_0x5876('0xd')][_0x5876('0x38')]=_0x281b32[_0x5876('0x30')][_0x5876('0x39')]['name'];_0x3fbb48[_0x5876('0xd')][_0x5876('0x3a')]=_0x281b32[_0x5876('0x30')]['intent'][_0x5876('0x3b')];_0x3fbb48['body'][_0x5876('0x3c')]=_0x281b32[_0x5876('0x30')][_0x5876('0x39')]['isFallback'];_0x3fbb48[_0x5876('0xd')][_0x5876('0x3d')]=_0x281b32[_0x5876('0x30')][_0x5876('0x3e')];_0x3fbb48[_0x5876('0xd')]['DIALOGFLOWV2_QUERYTEXT']=_0x281b32[_0x5876('0x30')][_0x5876('0x3f')];_0x3fbb48[_0x5876('0xd')][_0x5876('0x40')]=_0x281b32['queryResult'][_0x5876('0x41')];_0x137dd8[_0x5876('0x42')]='dialogflowV2';_0x137dd8[_0x5876('0x43')]=JSON[_0x5876('0x14')](_0x15a871);var _0x4aaf23=[];for(var _0x4d4f2d=0x0;_0x4d4f2d<_0x15a871[_0x5876('0x2f')];_0x4d4f2d++){var _0x20e599=_0x15a871[_0x4d4f2d];if(_0x20e599&&_0x20e599[_0x5876('0x30')]&&_0x20e599[_0x5876('0x30')][_0x5876('0x44')]){_0x4aaf23[_0x5876('0x45')](createMessage(_0x118b22,_0x533ca1,_0x137dd8,_0x20e599[_0x5876('0x30')][_0x5876('0x44')]));}}return Promise['all'](_0x4aaf23)['then'](function(){if(_0x3317a0){_0x38a9d7[_0x5876('0x19')]();}else{_0x38a9d7[_0x5876('0x2a')]();}})[_0x5876('0x28')](function(_0x472d26){logger[_0x5876('0x13')](_0x5876('0x12'),_0x118b22,JSON[_0x5876('0x14')](_0x472d26));_0x38a9d7[_0x5876('0x19')]();});}})[_0x5876('0x28')](function(_0x56c938){logger[_0x5876('0x13')]('[%s][DIALOGFLOWV2]\x20%s',_0x118b22,_0x56c938['message']);_0x38a9d7['next']();});}}catch(_0x4492a1){logger[_0x5876('0x13')]('[%s][DIALOGFLOWV2]\x20%s',_0x118b22,JSON[_0x5876('0x14')](_0x4492a1));_0x38a9d7[_0x5876('0x2a')]();}};};