Built motion from commit d5e4af8c.|2.6.23
[motion2.git] / server / services / routing / server / applications / dialogflow.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 _0xdbfa=['catch','message','md5','../../../../config/logger','routing','../../rpc/openchannelMessage','../../rpc/mailMessage','../../rpc/smsMessage','../../rpc/faxMessage','../../rpc/whatsappMessage','isNil','UserId','info','[%s][DIALOGFLOW]\x20%s','next','contact','format','YYYY-MM-DD\x20HH:mm:ss','out','channel','sms','whatsapp','phone','ChatWebsiteId','upperFirst','AccountId','account','InteractionId','interaction','created','isEmpty','body','getMustacheRender','create','then','error','stringify','finally','[%s][DIALOGFLOW]\x20Welcome\x20Message\x20has\x20been\x20sent!','stop','textRequest','fulfillment','speech','providerName','dialogflow','providerResponse','result','metadata','endConversation'];(function(_0x563066,_0x376721){var _0x347618=function(_0x1cd119){while(--_0x1cd119){_0x563066['push'](_0x563066['shift']());}};_0x347618(++_0x376721);}(_0xdbfa,0x128));var _0xadbf=function(_0x44651d,_0x1e6710){_0x44651d=_0x44651d-0x0;var _0x49c068=_0xdbfa[_0x44651d];return _0x49c068;};'use strict';var _=require('lodash');var moment=require('moment');var apiai=require('apiai-promise');var md5=require(_0xadbf('0x0'));var utils=require('./utils');var logger=require(_0xadbf('0x1'))(_0xadbf('0x2'));var rpc={'chatMessage':require('../../rpc/chatMessage'),'openchannelMessage':require(_0xadbf('0x3')),'mailMessage':require(_0xadbf('0x4')),'smsMessage':require(_0xadbf('0x5')),'faxMessage':require(_0xadbf('0x6')),'whatsappMessage':require(_0xadbf('0x7'))};exports['consequence']=function(_0x1204f4,_0x30f643){return function(_0x16704e){try{if(!_[_0xadbf('0x8')](this['interaction'][_0xadbf('0x9')])){logger[_0xadbf('0xa')](_0xadbf('0xb'),_0x1204f4,'Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.');return _0x16704e[_0xadbf('0xc')]();}var _0x38f6e7={'ContactId':this[_0xadbf('0xd')]['id'],'sentAt':moment()[_0xadbf('0xe')](_0xadbf('0xf')),'direction':_0xadbf('0x10')};if(this[_0xadbf('0x11')]===_0xadbf('0x12')||this['channel']===_0xadbf('0x13')){_0x38f6e7[_0xadbf('0x14')]=this['interaction'][_0xadbf('0x14')];}var _0x4484a9=this['channel'];if(_0x4484a9==='chat'){_0x38f6e7[_0xadbf('0x15')]=this['account']['id'];}else{_0x38f6e7[_[_0xadbf('0x16')](_0x4484a9)+_0xadbf('0x17')]=this[_0xadbf('0x18')]['id'];}_0x38f6e7[_[_0xadbf('0x16')](_0x4484a9)+_0xadbf('0x19')]=this[_0xadbf('0x1a')]['id'];if(this[_0xadbf('0x1a')][_0xadbf('0x1b')]){var _0x21ee0f=_0x30f643[0x2];if(!_[_0xadbf('0x1c')](_0x21ee0f)){_0x38f6e7[_0xadbf('0x1d')]=utils[_0xadbf('0x1e')](_0x21ee0f,this);return rpc[_0x4484a9+'Message'][_0xadbf('0x1f')](_0x38f6e7)[_0xadbf('0x20')](function(_0x38e3f2){logger[_0xadbf('0xa')](_0xadbf('0xb'),_0x1204f4,_0x38f6e7[_0xadbf('0x1d')]);})['catch'](function(_0x1a1125){logger[_0xadbf('0x21')](_0xadbf('0xb'),_0x1204f4,JSON[_0xadbf('0x22')](_0x1a1125));})[_0xadbf('0x23')](function(){logger[_0xadbf('0xa')](_0xadbf('0x24'),_0x1204f4);_0x16704e[_0xadbf('0x25')]();});}else{logger[_0xadbf('0xa')]('[%s][DIALOGFLOW]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!',_0x1204f4);_0x16704e[_0xadbf('0x25')]();}}else{var _0x252d51=this;var _0x1c871f=apiai(_0x30f643[0x0],{'language':_0x30f643[0x1]||'en'});return _0x1c871f[_0xadbf('0x26')](this['message'][_0xadbf('0x1d')],{'sessionId':md5(this[_0xadbf('0x1a')]['id'])})[_0xadbf('0x20')](function(_0x34f042){_0x252d51[_0xadbf('0x1d')]['dialogflow']=_0x34f042;_0x38f6e7['body']=_0x34f042['result'][_0xadbf('0x27')][_0xadbf('0x28')];_0x38f6e7[_0xadbf('0x29')]=_0xadbf('0x2a');_0x38f6e7[_0xadbf('0x2b')]=JSON[_0xadbf('0x22')](_0x34f042);return rpc[_0x4484a9+'Message'][_0xadbf('0x1f')](_0x38f6e7)[_0xadbf('0x20')](function(_0x3989dc){logger['info'](_0xadbf('0xb'),_0x1204f4,_0x38f6e7[_0xadbf('0x1d')]);if(_0x34f042[_0xadbf('0x2c')][_0xadbf('0x2d')][_0xadbf('0x2e')]){_0x16704e[_0xadbf('0xc')]();}else{_0x16704e[_0xadbf('0x25')]();}})['catch'](function(_0x4d1015){logger[_0xadbf('0x21')](_0xadbf('0xb'),_0x1204f4,JSON[_0xadbf('0x22')](_0x4d1015));_0x16704e['next']();});})[_0xadbf('0x2f')](function(_0x41fdb6){logger[_0xadbf('0x21')]('[%s][DIALOGFLOW]\x20%s',_0x1204f4,_0x41fdb6[_0xadbf('0x30')]);_0x16704e[_0xadbf('0xc')]();});}}catch(_0x57a391){logger[_0xadbf('0x21')](_0xadbf('0xb'),_0x1204f4,JSON[_0xadbf('0x22')](_0x57a391));_0x16704e[_0xadbf('0x25')]();}};};