df96f014e802d090ff66bf81b40fec1045b2a0e2
[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 _0x7bc9=['routing','../../rpc/chatMessage','../../rpc/mailMessage','../../rpc/smsMessage','../../rpc/faxMessage','../../rpc/whatsappMessage','consequence','isNil','interaction','info','[%s][DIALOGFLOW]\x20%s','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','next','contact','YYYY-MM-DD\x20HH:mm:ss','out','channel','sms','whatsapp','phone','chat','account','upperFirst','AccountId','InteractionId','created','isEmpty','body','getMustacheRender','Message','create','error','stringify','[%s][DIALOGFLOW]\x20Welcome\x20Message\x20has\x20been\x20sent!','stop','textRequest','message','then','result','speech','providerName','dialogflow','providerResponse','metadata','endConversation','catch','moment','md5','./utils','../../../../config/logger'];(function(_0x218d9d,_0x51c72b){var _0x30912d=function(_0x4f35ad){while(--_0x4f35ad){_0x218d9d['push'](_0x218d9d['shift']());}};_0x30912d(++_0x51c72b);}(_0x7bc9,0x128));var _0x97bc=function(_0xbc568c,_0x425cae){_0xbc568c=_0xbc568c-0x0;var _0x472040=_0x7bc9[_0xbc568c];return _0x472040;};'use strict';var _=require('lodash');var moment=require(_0x97bc('0x0'));var apiai=require('apiai-promise');var md5=require(_0x97bc('0x1'));var utils=require(_0x97bc('0x2'));var logger=require(_0x97bc('0x3'))(_0x97bc('0x4'));var rpc={'chatMessage':require(_0x97bc('0x5')),'openchannelMessage':require('../../rpc/openchannelMessage'),'mailMessage':require(_0x97bc('0x6')),'smsMessage':require(_0x97bc('0x7')),'faxMessage':require(_0x97bc('0x8')),'whatsappMessage':require(_0x97bc('0x9'))};exports[_0x97bc('0xa')]=function(_0x18be89,_0x437f44){return function(_0x4799e6){try{if(!_[_0x97bc('0xb')](this[_0x97bc('0xc')]['UserId'])){logger[_0x97bc('0xd')](_0x97bc('0xe'),_0x18be89,_0x97bc('0xf'));return _0x4799e6[_0x97bc('0x10')]();}var _0x21d3a5={'ContactId':this[_0x97bc('0x11')]['id'],'sentAt':moment()['format'](_0x97bc('0x12')),'direction':_0x97bc('0x13')};if(this[_0x97bc('0x14')]===_0x97bc('0x15')||this[_0x97bc('0x14')]===_0x97bc('0x16')){_0x21d3a5[_0x97bc('0x17')]=this['interaction'][_0x97bc('0x17')];}var _0x1a9222=this[_0x97bc('0x14')];if(_0x1a9222===_0x97bc('0x18')){_0x21d3a5['ChatWebsiteId']=this[_0x97bc('0x19')]['id'];}else{_0x21d3a5[_[_0x97bc('0x1a')](_0x1a9222)+_0x97bc('0x1b')]=this[_0x97bc('0x19')]['id'];}_0x21d3a5[_[_0x97bc('0x1a')](_0x1a9222)+_0x97bc('0x1c')]=this[_0x97bc('0xc')]['id'];if(this['interaction'][_0x97bc('0x1d')]){var _0x2f06ca=_0x437f44[0x2];if(!_[_0x97bc('0x1e')](_0x2f06ca)){_0x21d3a5[_0x97bc('0x1f')]=utils[_0x97bc('0x20')](_0x2f06ca,this);return rpc[_0x1a9222+_0x97bc('0x21')][_0x97bc('0x22')](_0x21d3a5)['then'](function(_0x2b1de1){logger[_0x97bc('0xd')](_0x97bc('0xe'),_0x18be89,_0x21d3a5[_0x97bc('0x1f')]);})['catch'](function(_0x417492){logger[_0x97bc('0x23')]('[%s][DIALOGFLOW]\x20%s',_0x18be89,JSON[_0x97bc('0x24')](_0x417492));})['finally'](function(){logger[_0x97bc('0xd')](_0x97bc('0x25'),_0x18be89);_0x4799e6[_0x97bc('0x26')]();});}else{logger[_0x97bc('0xd')]('[%s][DIALOGFLOW]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!',_0x18be89);_0x4799e6[_0x97bc('0x26')]();}}else{var _0x165a9b=this;var _0x3c376b=apiai(_0x437f44[0x0],{'language':_0x437f44[0x1]||'en'});return _0x3c376b[_0x97bc('0x27')](this[_0x97bc('0x28')][_0x97bc('0x1f')],{'sessionId':md5(this[_0x97bc('0xc')]['id'])})[_0x97bc('0x29')](function(_0x5a2af5){_0x165a9b['body']['dialogflow']=_0x5a2af5;_0x21d3a5[_0x97bc('0x1f')]=_0x5a2af5[_0x97bc('0x2a')]['fulfillment'][_0x97bc('0x2b')];_0x21d3a5[_0x97bc('0x2c')]=_0x97bc('0x2d');_0x21d3a5[_0x97bc('0x2e')]=JSON[_0x97bc('0x24')](_0x5a2af5);return rpc[_0x1a9222+_0x97bc('0x21')][_0x97bc('0x22')](_0x21d3a5)[_0x97bc('0x29')](function(_0x6d3d11){logger['info'](_0x97bc('0xe'),_0x18be89,_0x21d3a5[_0x97bc('0x1f')]);if(_0x5a2af5[_0x97bc('0x2a')][_0x97bc('0x2f')][_0x97bc('0x30')]){_0x4799e6['next']();}else{_0x4799e6['stop']();}})[_0x97bc('0x31')](function(_0x569735){logger[_0x97bc('0x23')](_0x97bc('0xe'),_0x18be89,JSON['stringify'](_0x569735));_0x4799e6[_0x97bc('0x10')]();});})[_0x97bc('0x31')](function(_0x45826e){logger[_0x97bc('0x23')](_0x97bc('0xe'),_0x18be89,_0x45826e[_0x97bc('0x28')]);_0x4799e6['next']();});}}catch(_0x3958f3){logger['error']('[%s][DIALOGFLOW]\x20%s',_0x18be89,JSON[_0x97bc('0x24')](_0x3958f3));_0x4799e6[_0x97bc('0x26')]();}};};