2bea9358a070fc0e5969de97a6f05612519369c0
[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 _0x0306=['boolValue','DIALOGFLOWV2_ACTION','action','DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT','allRequiredParamsPresent','DIALOGFLOWV2_ENDCONVERSATION','DIALOGFLOWV2_INTENTNAME','intent','name','DIALOGFLOWV2_INTENTDISPLAYNAME','displayName','DIALOGFLOWV2_ISFALLBACKINTENT','isFallback','languageCode','DIALOGFLOWV2_QUERYTEXT','queryText','DIALOGFLOWV2_SCORE','intentDetectionConfidence','providerName','providerResponse','fulfillmentText','all','lodash','dialogflow','md5','routing','./utils','../../rpc/chatMessage','../../rpc/openchannelMessage','../../rpc/mailMessage','../../rpc/faxMessage','../../rpc/whatsappMessage','clone','body','Message','then','info','error','[%s][DIALOGFLOWV2]\x20%s','stringify','consequence','isNil','interaction','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','next','contact','YYYY-MM-DD\x20HH:mm:ss','out','channel','whatsapp','phone','ChatWebsiteId','account','upperFirst','AccountId','InteractionId','created','isEmpty','getMustacheRender','catch','finally','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!','stop','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!','replace','SessionsClient','sessionPath','message','detectIntent','dialogflowV2','queryResult','diagnosticInfo','fields','end_conversation'];(function(_0x1a72f3,_0x217011){var _0x57ef47=function(_0x20123d){while(--_0x20123d){_0x1a72f3['push'](_0x1a72f3['shift']());}};_0x57ef47(++_0x217011);}(_0x0306,0x13e));var _0x6030=function(_0x575d48,_0x1e7f40){_0x575d48=_0x575d48-0x0;var _0x115315=_0x0306[_0x575d48];return _0x115315;};'use strict';var _=require(_0x6030('0x0'));var dialogflow=require(_0x6030('0x1'));var md5=require(_0x6030('0x2'));var moment=require('moment');var Promise=require('bluebird');var logger=require('../../../../config/logger')(_0x6030('0x3'));var utils=require(_0x6030('0x4'));var rpc={'chatMessage':require(_0x6030('0x5')),'openchannelMessage':require(_0x6030('0x6')),'mailMessage':require(_0x6030('0x7')),'smsMessage':require('../../rpc/smsMessage'),'faxMessage':require(_0x6030('0x8')),'whatsappMessage':require(_0x6030('0x9'))};function createMessage(_0x5596c8,_0x57ff02,_0x43a46c,_0x20df28){return new Promise(function(_0x266ebe,_0x257b1a){var _0x5e2df2=_[_0x6030('0xa')](_0x43a46c);_0x5e2df2[_0x6030('0xb')]=_0x20df28;return rpc[_0x57ff02+_0x6030('0xc')]['create'](_0x5e2df2)[_0x6030('0xd')](function(){logger[_0x6030('0xe')]('[%s][DIALOGFLOWV2]\x20%s',_0x5596c8,_0x5e2df2[_0x6030('0xb')]);_0x266ebe();})['catch'](function(_0x57bb3e){logger[_0x6030('0xf')](_0x6030('0x10'),_0x5596c8,JSON[_0x6030('0x11')](_0x57bb3e));_0x257b1a(_0x57bb3e);});});}exports[_0x6030('0x12')]=function(_0x587ec8,_0x12bbcb){return function(_0x31d671){try{if(!_[_0x6030('0x13')](this[_0x6030('0x14')]['UserId'])){logger[_0x6030('0xe')](_0x6030('0x10'),_0x587ec8,_0x6030('0x15'));return _0x31d671[_0x6030('0x16')]();}var _0x2a3c75={'ContactId':this[_0x6030('0x17')]['id'],'sentAt':moment()['format'](_0x6030('0x18')),'direction':_0x6030('0x19')};if(this['channel']==='sms'||this[_0x6030('0x1a')]===_0x6030('0x1b')){_0x2a3c75[_0x6030('0x1c')]=this[_0x6030('0x14')][_0x6030('0x1c')];}var _0x191e32=this[_0x6030('0x1a')];if(_0x191e32==='chat'){_0x2a3c75[_0x6030('0x1d')]=this[_0x6030('0x1e')]['id'];}else{_0x2a3c75[_[_0x6030('0x1f')](_0x191e32)+_0x6030('0x20')]=this[_0x6030('0x1e')]['id'];}_0x2a3c75[_[_0x6030('0x1f')](_0x191e32)+_0x6030('0x21')]=this[_0x6030('0x14')]['id'];if(this['interaction'][_0x6030('0x22')]){var _0x4e063f=_0x12bbcb[0x4];if(!_[_0x6030('0x23')](_0x4e063f)){_0x2a3c75[_0x6030('0xb')]=utils[_0x6030('0x24')](_0x4e063f,this);return rpc[_0x191e32+_0x6030('0xc')]['create'](_0x2a3c75)[_0x6030('0xd')](function(_0x2cbdc5){logger[_0x6030('0xe')](_0x6030('0x10'),_0x587ec8,_0x2a3c75[_0x6030('0xb')]);})[_0x6030('0x25')](function(_0xb2acef){logger[_0x6030('0xf')](_0x6030('0x10'),_0x587ec8,JSON['stringify'](_0xb2acef));})[_0x6030('0x26')](function(){logger[_0x6030('0xe')](_0x6030('0x27'),_0x587ec8);_0x31d671[_0x6030('0x28')]();});}else{logger[_0x6030('0xe')](_0x6030('0x29'),_0x587ec8);_0x31d671[_0x6030('0x28')]();}}else{var _0x3633e0=this;var _0x59d256=_0x12bbcb[0x2][_0x6030('0x2a')](/\\n/g,'\x0a');var _0x184c2c={'credentials':{'private_key':_0x59d256,'client_email':_0x12bbcb[0x1]}};var _0x5752a2=new dialogflow[(_0x6030('0x2b'))](_0x184c2c);var _0x23bab5=_0x5752a2[_0x6030('0x2c')](_0x12bbcb[0x0],md5(_0x3633e0[_0x6030('0x14')]['id']));var _0x398441={'session':_0x23bab5,'queryInput':{'text':{'text':_0x3633e0[_0x6030('0x2d')][_0x6030('0xb')],'languageCode':_0x12bbcb[0x3]||'en'}}};return _0x5752a2[_0x6030('0x2e')](_0x398441)[_0x6030('0xd')](function(_0xf9f4c7){if(_0xf9f4c7&&_0xf9f4c7['length']){_0x3633e0['body'][_0x6030('0x2f')]=_0xf9f4c7;var _0x396b4c=_0xf9f4c7[0x0];var _0x160961=_0x396b4c['queryResult']['diagnosticInfo']&&_0x396b4c[_0x6030('0x30')][_0x6030('0x31')]['fields']?_0x396b4c['queryResult'][_0x6030('0x31')][_0x6030('0x32')][_0x6030('0x33')][_0x6030('0x34')]:![];_0x3633e0[_0x6030('0xb')][_0x6030('0x35')]=_0x396b4c['queryResult'][_0x6030('0x36')];_0x3633e0['body'][_0x6030('0x37')]=_0x396b4c[_0x6030('0x30')][_0x6030('0x38')];_0x3633e0[_0x6030('0xb')][_0x6030('0x39')]=_0x396b4c['queryResult'][_0x6030('0x31')]&&_0x396b4c[_0x6030('0x30')][_0x6030('0x31')][_0x6030('0x32')]?_0x396b4c[_0x6030('0x30')][_0x6030('0x31')]['fields'][_0x6030('0x33')][_0x6030('0x34')]:![];_0x3633e0['body'][_0x6030('0x3a')]=_0x396b4c[_0x6030('0x30')][_0x6030('0x3b')][_0x6030('0x3c')];_0x3633e0[_0x6030('0xb')][_0x6030('0x3d')]=_0x396b4c[_0x6030('0x30')][_0x6030('0x3b')][_0x6030('0x3e')];_0x3633e0[_0x6030('0xb')][_0x6030('0x3f')]=_0x396b4c['queryResult'][_0x6030('0x3b')][_0x6030('0x40')];_0x3633e0[_0x6030('0xb')]['DIALOGFLOWV2_LANGUAGECODE']=_0x396b4c[_0x6030('0x30')][_0x6030('0x41')];_0x3633e0[_0x6030('0xb')][_0x6030('0x42')]=_0x396b4c[_0x6030('0x30')][_0x6030('0x43')];_0x3633e0['body'][_0x6030('0x44')]=_0x396b4c[_0x6030('0x30')][_0x6030('0x45')];_0x2a3c75[_0x6030('0x46')]='dialogflowV2';_0x2a3c75[_0x6030('0x47')]=JSON[_0x6030('0x11')](_0xf9f4c7);var _0x126ddc=[];for(var _0x821e62=0x0;_0x821e62<_0xf9f4c7['length'];_0x821e62++){var _0x26461f=_0xf9f4c7[_0x821e62];if(_0x26461f&&_0x26461f[_0x6030('0x30')]&&_0x26461f[_0x6030('0x30')][_0x6030('0x48')]){_0x126ddc['push'](createMessage(_0x587ec8,_0x191e32,_0x2a3c75,_0x26461f[_0x6030('0x30')][_0x6030('0x48')]));}}return Promise[_0x6030('0x49')](_0x126ddc)[_0x6030('0xd')](function(){if(_0x160961){_0x31d671['next']();}else{_0x31d671['stop']();}})[_0x6030('0x25')](function(_0x417f0e){logger[_0x6030('0xf')](_0x6030('0x10'),_0x587ec8,JSON[_0x6030('0x11')](_0x417f0e));_0x31d671['next']();});}})[_0x6030('0x25')](function(_0x5cc437){logger[_0x6030('0xf')]('[%s][DIALOGFLOWV2]\x20%s',_0x587ec8,_0x5cc437['message']);_0x31d671[_0x6030('0x16')]();});}}catch(_0x175751){logger['error'](_0x6030('0x10'),_0x587ec8,JSON[_0x6030('0x11')](_0x175751));_0x31d671['stop']();}};};