Built motion from commit cb845558.|2.6.34
[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 _0x2d4e=['DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT','allRequiredParamsPresent','DIALOGFLOWV2_ENDCONVERSATION','intent','name','DIALOGFLOWV2_INTENTDISPLAYNAME','isFallback','DIALOGFLOWV2_LANGUAGECODE','languageCode','DIALOGFLOWV2_QUERYTEXT','queryText','DIALOGFLOWV2_SCORE','intentDetectionConfidence','providerName','providerResponse','fulfillmentText','push','all','stack','lodash','dialogflow','md5','moment','bluebird','../../../../config/logger','routing','./utils','../../rpc/chatMessage','../../rpc/openchannelMessage','../../rpc/mailMessage','../../rpc/faxMessage','clone','body','Message','create','then','info','[%s][DIALOGFLOWV2]\x20%s','catch','error','consequence','isNil','interaction','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','next','format','out','channel','sms','whatsapp','phone','chat','ChatWebsiteId','upperFirst','account','InteractionId','isEmpty','stringify','finally','stop','SessionsClient','sessionPath','message','length','queryResult','diagnosticInfo','fields','end_conversation','boolValue','DIALOGFLOWV2_ACTION'];(function(_0x104c21,_0x532fce){var _0x500985=function(_0x446dd5){while(--_0x446dd5){_0x104c21['push'](_0x104c21['shift']());}};_0x500985(++_0x532fce);}(_0x2d4e,0x1b7));var _0xe2d4=function(_0x2c7e61,_0x595fec){_0x2c7e61=_0x2c7e61-0x0;var _0x859bbe=_0x2d4e[_0x2c7e61];return _0x859bbe;};'use strict';var _=require(_0xe2d4('0x0'));var dialogflow=require(_0xe2d4('0x1'));var md5=require(_0xe2d4('0x2'));var moment=require(_0xe2d4('0x3'));var Promise=require(_0xe2d4('0x4'));var logger=require(_0xe2d4('0x5'))(_0xe2d4('0x6'));var utils=require(_0xe2d4('0x7'));var rpc={'chatMessage':require(_0xe2d4('0x8')),'openchannelMessage':require(_0xe2d4('0x9')),'mailMessage':require(_0xe2d4('0xa')),'smsMessage':require('../../rpc/smsMessage'),'faxMessage':require(_0xe2d4('0xb')),'whatsappMessage':require('../../rpc/whatsappMessage')};function createMessage(_0x2cad04,_0x540cc8,_0x438af0,_0x289ecb){return new Promise(function(_0x1ce124,_0x2f1c2e){var _0x545933=_[_0xe2d4('0xc')](_0x438af0);_0x545933[_0xe2d4('0xd')]=_0x289ecb;return rpc[_0x540cc8+_0xe2d4('0xe')][_0xe2d4('0xf')](_0x545933)[_0xe2d4('0x10')](function(){logger[_0xe2d4('0x11')](_0xe2d4('0x12'),_0x2cad04,_0x545933[_0xe2d4('0xd')]);_0x1ce124();})[_0xe2d4('0x13')](function(_0x5ab2b9){logger[_0xe2d4('0x14')](_0xe2d4('0x12'),_0x2cad04,JSON['stringify'](_0x5ab2b9));_0x2f1c2e(_0x5ab2b9);});});}exports[_0xe2d4('0x15')]=function(_0x2a0cb1,_0x3ba583){return function(_0x534482){try{if(!_[_0xe2d4('0x16')](this[_0xe2d4('0x17')]['UserId'])){logger[_0xe2d4('0x11')](_0xe2d4('0x12'),_0x2a0cb1,_0xe2d4('0x18'));return _0x534482[_0xe2d4('0x19')]();}var _0xc46ed6={'ContactId':this['contact']['id'],'sentAt':moment()[_0xe2d4('0x1a')]('YYYY-MM-DD\x20HH:mm:ss'),'direction':_0xe2d4('0x1b')};if(this[_0xe2d4('0x1c')]===_0xe2d4('0x1d')||this[_0xe2d4('0x1c')]===_0xe2d4('0x1e')){_0xc46ed6[_0xe2d4('0x1f')]=this['interaction']['phone'];}var _0x10a392=this[_0xe2d4('0x1c')];if(_0x10a392===_0xe2d4('0x20')){_0xc46ed6[_0xe2d4('0x21')]=this['account']['id'];}else{_0xc46ed6[_[_0xe2d4('0x22')](_0x10a392)+'AccountId']=this[_0xe2d4('0x23')]['id'];}_0xc46ed6[_[_0xe2d4('0x22')](_0x10a392)+_0xe2d4('0x24')]=this[_0xe2d4('0x17')]['id'];if(this[_0xe2d4('0x17')]['created']){var _0x2fd626=_0x3ba583[0x4];if(!_[_0xe2d4('0x25')](_0x2fd626)){_0xc46ed6['body']=utils['getMustacheRender'](_0x2fd626,this);return rpc[_0x10a392+'Message'][_0xe2d4('0xf')](_0xc46ed6)[_0xe2d4('0x10')](function(_0x202914){logger[_0xe2d4('0x11')]('[%s][DIALOGFLOWV2]\x20%s',_0x2a0cb1,_0xc46ed6[_0xe2d4('0xd')]);})[_0xe2d4('0x13')](function(_0xe496ed){logger[_0xe2d4('0x14')](_0xe2d4('0x12'),_0x2a0cb1,JSON[_0xe2d4('0x26')](_0xe496ed));})[_0xe2d4('0x27')](function(){logger['info']('[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!',_0x2a0cb1);_0x534482[_0xe2d4('0x28')]();});}else{logger[_0xe2d4('0x11')]('[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!',_0x2a0cb1);_0x534482[_0xe2d4('0x28')]();}}else{var _0x5eb52d=this;var _0x5864e3=_0x3ba583[0x2]['replace'](/\\n/g,'\x0a');var _0x297ae2={'credentials':{'private_key':_0x5864e3,'client_email':_0x3ba583[0x1]}};var _0x4824be=new dialogflow[(_0xe2d4('0x29'))](_0x297ae2);var _0x53cc38=_0x4824be[_0xe2d4('0x2a')](_0x3ba583[0x0],md5(_0x5eb52d[_0xe2d4('0x17')]['id']));var _0x542515={'session':_0x53cc38,'queryInput':{'text':{'text':_0x5eb52d[_0xe2d4('0x2b')][_0xe2d4('0xd')],'languageCode':_0x3ba583[0x3]||'en'}}};return _0x4824be['detectIntent'](_0x542515)[_0xe2d4('0x10')](function(_0x5313d3){if(_0x5313d3&&_0x5313d3[_0xe2d4('0x2c')]){_0x5eb52d[_0xe2d4('0xd')]['dialogflowV2']=_0x5313d3;var _0x3c710a=_0x5313d3[0x0];var _0x361313=_0x3c710a[_0xe2d4('0x2d')]['diagnosticInfo']&&_0x3c710a['queryResult'][_0xe2d4('0x2e')][_0xe2d4('0x2f')]?_0x3c710a[_0xe2d4('0x2d')][_0xe2d4('0x2e')][_0xe2d4('0x2f')][_0xe2d4('0x30')][_0xe2d4('0x31')]:![];_0x5eb52d[_0xe2d4('0xd')][_0xe2d4('0x32')]=_0x3c710a[_0xe2d4('0x2d')]['action'];_0x5eb52d[_0xe2d4('0xd')][_0xe2d4('0x33')]=_0x3c710a[_0xe2d4('0x2d')][_0xe2d4('0x34')];_0x5eb52d['body'][_0xe2d4('0x35')]=_0x3c710a[_0xe2d4('0x2d')]['diagnosticInfo']&&_0x3c710a['queryResult']['diagnosticInfo'][_0xe2d4('0x2f')]?_0x3c710a[_0xe2d4('0x2d')][_0xe2d4('0x2e')][_0xe2d4('0x2f')][_0xe2d4('0x30')][_0xe2d4('0x31')]:![];_0x5eb52d[_0xe2d4('0xd')]['DIALOGFLOWV2_INTENTNAME']=_0x3c710a[_0xe2d4('0x2d')][_0xe2d4('0x36')][_0xe2d4('0x37')];_0x5eb52d[_0xe2d4('0xd')][_0xe2d4('0x38')]=_0x3c710a[_0xe2d4('0x2d')]['intent']['displayName'];_0x5eb52d[_0xe2d4('0xd')]['DIALOGFLOWV2_ISFALLBACKINTENT']=_0x3c710a[_0xe2d4('0x2d')][_0xe2d4('0x36')][_0xe2d4('0x39')];_0x5eb52d[_0xe2d4('0xd')][_0xe2d4('0x3a')]=_0x3c710a[_0xe2d4('0x2d')][_0xe2d4('0x3b')];_0x5eb52d[_0xe2d4('0xd')][_0xe2d4('0x3c')]=_0x3c710a[_0xe2d4('0x2d')][_0xe2d4('0x3d')];_0x5eb52d['body'][_0xe2d4('0x3e')]=_0x3c710a['queryResult'][_0xe2d4('0x3f')];_0xc46ed6[_0xe2d4('0x40')]='dialogflowV2';_0xc46ed6[_0xe2d4('0x41')]=JSON[_0xe2d4('0x26')](_0x5313d3);var _0x4173a6=[];for(var _0x4d8f1d=0x0;_0x4d8f1d<_0x5313d3['length'];_0x4d8f1d++){var _0x51efcd=_0x5313d3[_0x4d8f1d];if(_0x51efcd&&_0x51efcd[_0xe2d4('0x2d')]&&_0x51efcd[_0xe2d4('0x2d')][_0xe2d4('0x42')]){_0x4173a6[_0xe2d4('0x43')](createMessage(_0x2a0cb1,_0x10a392,_0xc46ed6,_0x51efcd['queryResult'][_0xe2d4('0x42')]));}}return Promise[_0xe2d4('0x44')](_0x4173a6)['then'](function(){if(_0x361313){_0x534482[_0xe2d4('0x19')]();}else{_0x534482['stop']();}})[_0xe2d4('0x13')](function(_0x20b4cd){logger[_0xe2d4('0x14')](_0xe2d4('0x12'),_0x2a0cb1,JSON[_0xe2d4('0x26')](_0x20b4cd));_0x534482['next']();});}})[_0xe2d4('0x13')](function(_0xdb2574){logger[_0xe2d4('0x14')](_0xe2d4('0x12'),_0x2a0cb1,_0xdb2574[_0xe2d4('0x2b')],_0xdb2574[_0xe2d4('0x45')]);_0x534482['next']();});}}catch(_0x32819a){logger['error']('[%s][DIALOGFLOWV2]\x20%s',_0x2a0cb1,JSON[_0xe2d4('0x26')](_0x32819a));_0x534482[_0xe2d4('0x28')]();}};};