24f654616bdd5da0bdeb1632d2e7364a56b5d79c
[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 _0xc08e=['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','detectIntent','then','length','queryResult','diagnosticInfo','end_conversation','DIALOGFLOWV2_ACTION','action','DIALOGFLOWV2_ALLREQUIREDPARAMSPRESENT','allRequiredParamsPresent','DIALOGFLOWV2_ENDCONVERSATION','fields','boolValue','DIALOGFLOWV2_INTENTNAME','name','DIALOGFLOWV2_INTENTDISPLAYNAME','intent','DIALOGFLOWV2_ISFALLBACKINTENT','isFallback','DIALOGFLOWV2_LANGUAGECODE','languageCode','queryText','DIALOGFLOWV2_SCORE','intentDetectionConfidence','dialogflowV2','providerResponse','fulfillmentText','push','all','lodash','dialogflow','md5','bluebird','routing','./utils','../../rpc/chatMessage','../../rpc/openchannelMessage','../../rpc/mailMessage','clone','body','Message','create','info','[%s][DIALOGFLOWV2]\x20%s','catch','stringify','isNil','interaction','UserId','next','contact','format','YYYY-MM-DD\x20HH:mm:ss','channel','sms','phone','chat','ChatWebsiteId','upperFirst','AccountId','account','InteractionId','created','isEmpty','error'];(function(_0x1eabda,_0x5164e5){var _0x101654=function(_0x2a3a9e){while(--_0x2a3a9e){_0x1eabda['push'](_0x1eabda['shift']());}};_0x101654(++_0x5164e5);}(_0xc08e,0xb4));var _0xec08=function(_0x3d05c2,_0x2abc6e){_0x3d05c2=_0x3d05c2-0x0;var _0x2d98e4=_0xc08e[_0x3d05c2];return _0x2d98e4;};'use strict';var _=require(_0xec08('0x0'));var dialogflow=require(_0xec08('0x1'));var md5=require(_0xec08('0x2'));var moment=require('moment');var Promise=require(_0xec08('0x3'));var logger=require('../../../../config/logger')(_0xec08('0x4'));var utils=require(_0xec08('0x5'));var rpc={'chatMessage':require(_0xec08('0x6')),'openchannelMessage':require(_0xec08('0x7')),'mailMessage':require(_0xec08('0x8')),'smsMessage':require('../../rpc/smsMessage'),'faxMessage':require('../../rpc/faxMessage'),'whatsappMessage':require('../../rpc/whatsappMessage')};function createMessage(_0x118d80,_0x5603b1,_0x1ee390,_0x23d8d2){return new Promise(function(_0x29ce4e,_0x1cdc31){var _0x566b26=_[_0xec08('0x9')](_0x1ee390);_0x566b26[_0xec08('0xa')]=_0x23d8d2;return rpc[_0x5603b1+_0xec08('0xb')][_0xec08('0xc')](_0x566b26)['then'](function(){logger[_0xec08('0xd')](_0xec08('0xe'),_0x118d80,_0x566b26['body']);_0x29ce4e();})[_0xec08('0xf')](function(_0x474d58){logger['error']('[%s][DIALOGFLOWV2]\x20%s',_0x118d80,JSON[_0xec08('0x10')](_0x474d58));_0x1cdc31(_0x474d58);});});}exports['consequence']=function(_0x4361ee,_0xbd3337){return function(_0x4e18c1){try{if(!_[_0xec08('0x11')](this[_0xec08('0x12')][_0xec08('0x13')])){logger[_0xec08('0xd')](_0xec08('0xe'),_0x4361ee,'Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.');return _0x4e18c1[_0xec08('0x14')]();}var _0x5cbc55={'ContactId':this[_0xec08('0x15')]['id'],'sentAt':moment()[_0xec08('0x16')](_0xec08('0x17')),'direction':'out'};if(this[_0xec08('0x18')]===_0xec08('0x19')||this[_0xec08('0x18')]==='whatsapp'){_0x5cbc55[_0xec08('0x1a')]=this['interaction']['phone'];}var _0x51174f=this['channel'];if(_0x51174f===_0xec08('0x1b')){_0x5cbc55[_0xec08('0x1c')]=this['account']['id'];}else{_0x5cbc55[_[_0xec08('0x1d')](_0x51174f)+_0xec08('0x1e')]=this[_0xec08('0x1f')]['id'];}_0x5cbc55[_['upperFirst'](_0x51174f)+_0xec08('0x20')]=this[_0xec08('0x12')]['id'];if(this[_0xec08('0x12')][_0xec08('0x21')]){var _0x3fea59=_0xbd3337[0x4];if(!_[_0xec08('0x22')](_0x3fea59)){_0x5cbc55[_0xec08('0xa')]=utils['getMustacheRender'](_0x3fea59,this);return rpc[_0x51174f+_0xec08('0xb')]['create'](_0x5cbc55)['then'](function(_0x296283){logger[_0xec08('0xd')](_0xec08('0xe'),_0x4361ee,_0x5cbc55[_0xec08('0xa')]);})['catch'](function(_0x24fbe9){logger[_0xec08('0x23')](_0xec08('0xe'),_0x4361ee,JSON[_0xec08('0x10')](_0x24fbe9));})[_0xec08('0x24')](function(){logger[_0xec08('0xd')](_0xec08('0x25'),_0x4361ee);_0x4e18c1[_0xec08('0x26')]();});}else{logger['info'](_0xec08('0x27'),_0x4361ee);_0x4e18c1[_0xec08('0x26')]();}}else{var _0x4562e2=this;var _0x1adb93=_0xbd3337[0x2][_0xec08('0x28')](/\\n/g,'\x0a');var _0x2d5ef5={'credentials':{'private_key':_0x1adb93,'client_email':_0xbd3337[0x1]}};var _0x93a4bf=new dialogflow[(_0xec08('0x29'))](_0x2d5ef5);var _0x23eb81=_0x93a4bf[_0xec08('0x2a')](_0xbd3337[0x0],md5(_0x4562e2[_0xec08('0x12')]['id']));var _0x570ad3={'session':_0x23eb81,'queryInput':{'text':{'text':_0x4562e2['message']['body'],'languageCode':_0xbd3337[0x3]||'en'}}};return _0x93a4bf[_0xec08('0x2b')](_0x570ad3)[_0xec08('0x2c')](function(_0x292387){if(_0x292387&&_0x292387[_0xec08('0x2d')]){_0x4562e2[_0xec08('0xa')]['dialogflowV2']=_0x292387;var _0x2e1b2b=_0x292387[0x0];var _0x4f51ed=_0x2e1b2b[_0xec08('0x2e')][_0xec08('0x2f')]&&_0x2e1b2b[_0xec08('0x2e')]['diagnosticInfo']['fields']?_0x2e1b2b[_0xec08('0x2e')][_0xec08('0x2f')]['fields'][_0xec08('0x30')]['boolValue']:![];_0x4562e2[_0xec08('0xa')][_0xec08('0x31')]=_0x2e1b2b['queryResult'][_0xec08('0x32')];_0x4562e2[_0xec08('0xa')][_0xec08('0x33')]=_0x2e1b2b[_0xec08('0x2e')][_0xec08('0x34')];_0x4562e2[_0xec08('0xa')][_0xec08('0x35')]=_0x2e1b2b['queryResult'][_0xec08('0x2f')]&&_0x2e1b2b[_0xec08('0x2e')][_0xec08('0x2f')]['fields']?_0x2e1b2b['queryResult']['diagnosticInfo'][_0xec08('0x36')]['end_conversation'][_0xec08('0x37')]:![];_0x4562e2['body'][_0xec08('0x38')]=_0x2e1b2b[_0xec08('0x2e')]['intent'][_0xec08('0x39')];_0x4562e2[_0xec08('0xa')][_0xec08('0x3a')]=_0x2e1b2b['queryResult'][_0xec08('0x3b')]['displayName'];_0x4562e2[_0xec08('0xa')][_0xec08('0x3c')]=_0x2e1b2b[_0xec08('0x2e')][_0xec08('0x3b')][_0xec08('0x3d')];_0x4562e2[_0xec08('0xa')][_0xec08('0x3e')]=_0x2e1b2b['queryResult'][_0xec08('0x3f')];_0x4562e2['body']['DIALOGFLOWV2_QUERYTEXT']=_0x2e1b2b[_0xec08('0x2e')][_0xec08('0x40')];_0x4562e2['body'][_0xec08('0x41')]=_0x2e1b2b['queryResult'][_0xec08('0x42')];_0x5cbc55['providerName']=_0xec08('0x43');_0x5cbc55[_0xec08('0x44')]=JSON[_0xec08('0x10')](_0x292387);var _0x103941=[];for(var _0x4ca712=0x0;_0x4ca712<_0x292387[_0xec08('0x2d')];_0x4ca712++){var _0x62849b=_0x292387[_0x4ca712];if(_0x62849b&&_0x62849b[_0xec08('0x2e')]&&_0x62849b[_0xec08('0x2e')][_0xec08('0x45')]){_0x103941[_0xec08('0x46')](createMessage(_0x4361ee,_0x51174f,_0x5cbc55,_0x62849b[_0xec08('0x2e')][_0xec08('0x45')]));}}return Promise[_0xec08('0x47')](_0x103941)[_0xec08('0x2c')](function(){if(_0x4f51ed){_0x4e18c1[_0xec08('0x14')]();}else{_0x4e18c1['stop']();}})[_0xec08('0xf')](function(_0x3496a8){logger[_0xec08('0x23')](_0xec08('0xe'),_0x4361ee,JSON[_0xec08('0x10')](_0x3496a8));_0x4e18c1[_0xec08('0x14')]();});}})['catch'](function(_0x56f1b4){logger[_0xec08('0x23')](_0xec08('0xe'),_0x4361ee,_0x56f1b4['message'],_0x56f1b4['stack']);_0x4e18c1[_0xec08('0x14')]();});}}catch(_0x208efb){logger[_0xec08('0x23')](_0xec08('0xe'),_0x4361ee,JSON[_0xec08('0x10')](_0x208efb));_0x4e18c1[_0xec08('0x26')]();}};};