8319b32c850148c730f79daef1d86ddec9231bf2
[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 _0xf380=['whatsapp','phone','chat','ChatWebsiteId','account','upperFirst','created','isEmpty','getMustacheRender','Message','stringify','finally','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20has\x20been\x20sent!','stop','[%s][DIALOGFLOWV2]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!','replace','sessionPath','detectIntent','dialogflowV2','queryResult','diagnosticInfo','fields','end_conversation','DIALOGFLOWV2_ACTION','action','DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT','allRequiredParamsPresent','DIALOGFLOWV2_ENDCONVERSATION','boolValue','intent','name','isFallback','DIALOGFLOWV2_LANGUAGECODE','languageCode','DIALOGFLOWV2_QUERYTEXT','queryText','intentDetectionConfidence','providerName','providerResponse','fulfillmentText','push','all','message','dialogflow','moment','../../../../config/logger','routing','./utils','../../rpc/chatMessage','../../rpc/openchannelMessage','../../rpc/mailMessage','../../rpc/smsMessage','../../rpc/faxMessage','../../rpc/whatsappMessage','body','create','then','info','[%s][DIALOGFLOWV2]\x20%s','catch','error','consequence','interaction','UserId','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','next','contact','format','YYYY-MM-DD\x20HH:mm:ss','out','channel'];(function(_0x1203f,_0x554bbc){var _0x365f0a=function(_0x3531d0){while(--_0x3531d0){_0x1203f['push'](_0x1203f['shift']());}};_0x365f0a(++_0x554bbc);}(_0xf380,0xb9));var _0x0f38=function(_0x24371e,_0x43c47b){_0x24371e=_0x24371e-0x0;var _0x581a8e=_0xf380[_0x24371e];return _0x581a8e;};'use strict';var _=require('lodash');var dialogflow=require(_0x0f38('0x0'));var md5=require('md5');var moment=require(_0x0f38('0x1'));var Promise=require('bluebird');var logger=require(_0x0f38('0x2'))(_0x0f38('0x3'));var utils=require(_0x0f38('0x4'));var rpc={'chatMessage':require(_0x0f38('0x5')),'openchannelMessage':require(_0x0f38('0x6')),'mailMessage':require(_0x0f38('0x7')),'smsMessage':require(_0x0f38('0x8')),'faxMessage':require(_0x0f38('0x9')),'whatsappMessage':require(_0x0f38('0xa'))};function createMessage(_0x556f8c,_0x170ef9,_0x43626c,_0x476511){return new Promise(function(_0x31f4f0,_0x5e7afc){var _0x15a0f8=_['clone'](_0x43626c);_0x15a0f8[_0x0f38('0xb')]=_0x476511;return rpc[_0x170ef9+'Message'][_0x0f38('0xc')](_0x15a0f8)[_0x0f38('0xd')](function(){logger[_0x0f38('0xe')](_0x0f38('0xf'),_0x556f8c,_0x15a0f8[_0x0f38('0xb')]);_0x31f4f0();})[_0x0f38('0x10')](function(_0x42551d){logger[_0x0f38('0x11')](_0x0f38('0xf'),_0x556f8c,JSON['stringify'](_0x42551d));_0x5e7afc(_0x42551d);});});}exports[_0x0f38('0x12')]=function(_0x1bdd86,_0x44766d){return function(_0x3a1fb){try{if(!_['isNil'](this[_0x0f38('0x13')][_0x0f38('0x14')])){logger['info']('[%s][DIALOGFLOWV2]\x20%s',_0x1bdd86,_0x0f38('0x15'));return _0x3a1fb[_0x0f38('0x16')]();}var _0x44de14={'ContactId':this[_0x0f38('0x17')]['id'],'sentAt':moment()[_0x0f38('0x18')](_0x0f38('0x19')),'direction':_0x0f38('0x1a')};if(this[_0x0f38('0x1b')]==='sms'||this[_0x0f38('0x1b')]===_0x0f38('0x1c')){_0x44de14[_0x0f38('0x1d')]=this['interaction'][_0x0f38('0x1d')];}var _0x2eac3a=this[_0x0f38('0x1b')];if(_0x2eac3a===_0x0f38('0x1e')){_0x44de14[_0x0f38('0x1f')]=this[_0x0f38('0x20')]['id'];}else{_0x44de14[_[_0x0f38('0x21')](_0x2eac3a)+'AccountId']=this['account']['id'];}_0x44de14[_[_0x0f38('0x21')](_0x2eac3a)+'InteractionId']=this[_0x0f38('0x13')]['id'];if(this[_0x0f38('0x13')][_0x0f38('0x22')]){var _0x5f92a7=_0x44766d[0x4];if(!_[_0x0f38('0x23')](_0x5f92a7)){_0x44de14['body']=utils[_0x0f38('0x24')](_0x5f92a7,this);return rpc[_0x2eac3a+_0x0f38('0x25')][_0x0f38('0xc')](_0x44de14)[_0x0f38('0xd')](function(_0x4e2396){logger[_0x0f38('0xe')](_0x0f38('0xf'),_0x1bdd86,_0x44de14[_0x0f38('0xb')]);})[_0x0f38('0x10')](function(_0x3ac114){logger[_0x0f38('0x11')](_0x0f38('0xf'),_0x1bdd86,JSON[_0x0f38('0x26')](_0x3ac114));})[_0x0f38('0x27')](function(){logger[_0x0f38('0xe')](_0x0f38('0x28'),_0x1bdd86);_0x3a1fb[_0x0f38('0x29')]();});}else{logger[_0x0f38('0xe')](_0x0f38('0x2a'),_0x1bdd86);_0x3a1fb['stop']();}}else{var _0x2e1432=this;var _0x2cd6bb=_0x44766d[0x2][_0x0f38('0x2b')](/\\n/g,'\x0a');var _0x2c3504={'credentials':{'private_key':_0x2cd6bb,'client_email':_0x44766d[0x1]}};var _0x201058=new dialogflow['SessionsClient'](_0x2c3504);var _0x38bead=_0x201058[_0x0f38('0x2c')](_0x44766d[0x0],md5(_0x2e1432[_0x0f38('0x13')]['id']));var _0x4554ad={'session':_0x38bead,'queryInput':{'text':{'text':_0x2e1432['message'][_0x0f38('0xb')],'languageCode':_0x44766d[0x3]||'en'}}};return _0x201058[_0x0f38('0x2d')](_0x4554ad)[_0x0f38('0xd')](function(_0x3dc3ef){if(_0x3dc3ef&&_0x3dc3ef['length']){_0x2e1432['body'][_0x0f38('0x2e')]=_0x3dc3ef;var _0x5b107d=_0x3dc3ef[0x0];var _0x358c61=_0x5b107d[_0x0f38('0x2f')][_0x0f38('0x30')]&&_0x5b107d[_0x0f38('0x2f')]['diagnosticInfo'][_0x0f38('0x31')]?_0x5b107d[_0x0f38('0x2f')][_0x0f38('0x30')]['fields'][_0x0f38('0x32')]['boolValue']:![];_0x2e1432[_0x0f38('0xb')][_0x0f38('0x33')]=_0x5b107d['queryResult'][_0x0f38('0x34')];_0x2e1432[_0x0f38('0xb')][_0x0f38('0x35')]=_0x5b107d[_0x0f38('0x2f')][_0x0f38('0x36')];_0x2e1432[_0x0f38('0xb')][_0x0f38('0x37')]=_0x5b107d[_0x0f38('0x2f')][_0x0f38('0x30')]&&_0x5b107d[_0x0f38('0x2f')][_0x0f38('0x30')][_0x0f38('0x31')]?_0x5b107d[_0x0f38('0x2f')]['diagnosticInfo']['fields'][_0x0f38('0x32')][_0x0f38('0x38')]:![];_0x2e1432[_0x0f38('0xb')]['DIALOGFLOWV2_INTENTNAME']=_0x5b107d['queryResult'][_0x0f38('0x39')][_0x0f38('0x3a')];_0x2e1432['body']['DIALOGFLOWV2_INTENTDISPLAYNAME']=_0x5b107d['queryResult']['intent']['displayName'];_0x2e1432[_0x0f38('0xb')]['DIALOGFLOWV2_ISFALLBACKINTENT']=_0x5b107d['queryResult'][_0x0f38('0x39')][_0x0f38('0x3b')];_0x2e1432[_0x0f38('0xb')][_0x0f38('0x3c')]=_0x5b107d[_0x0f38('0x2f')][_0x0f38('0x3d')];_0x2e1432[_0x0f38('0xb')][_0x0f38('0x3e')]=_0x5b107d[_0x0f38('0x2f')][_0x0f38('0x3f')];_0x2e1432['body']['DIALOGFLOWV2_SCORE']=_0x5b107d[_0x0f38('0x2f')][_0x0f38('0x40')];_0x44de14[_0x0f38('0x41')]=_0x0f38('0x2e');_0x44de14[_0x0f38('0x42')]=JSON[_0x0f38('0x26')](_0x3dc3ef);var _0x3728ec=[];for(var _0x3791e5=0x0;_0x3791e5<_0x3dc3ef['length'];_0x3791e5++){var _0x519637=_0x3dc3ef[_0x3791e5];if(_0x519637&&_0x519637['queryResult']&&_0x519637[_0x0f38('0x2f')][_0x0f38('0x43')]){_0x3728ec[_0x0f38('0x44')](createMessage(_0x1bdd86,_0x2eac3a,_0x44de14,_0x519637[_0x0f38('0x2f')][_0x0f38('0x43')]));}}return Promise[_0x0f38('0x45')](_0x3728ec)[_0x0f38('0xd')](function(){if(_0x358c61){_0x3a1fb[_0x0f38('0x16')]();}else{_0x3a1fb[_0x0f38('0x29')]();}})[_0x0f38('0x10')](function(_0x2b1a08){logger[_0x0f38('0x11')](_0x0f38('0xf'),_0x1bdd86,JSON[_0x0f38('0x26')](_0x2b1a08));_0x3a1fb[_0x0f38('0x16')]();});}})[_0x0f38('0x10')](function(_0xd1fb45){logger[_0x0f38('0x11')](_0x0f38('0xf'),_0x1bdd86,_0xd1fb45[_0x0f38('0x46')]);_0x3a1fb['next']();});}}catch(_0x32ff0f){logger['error'](_0x0f38('0xf'),_0x1bdd86,JSON[_0x0f38('0x26')](_0x32ff0f));_0x3a1fb[_0x0f38('0x29')]();}};};