a99c030c29a5b9744102f11aea1ef5caade99145
[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 _0xc14a=['isNil','UserId','info','[%s][DIALOGFLOW]\x20%s','Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.','format','YYYY-MM-DD\x20HH:mm:ss','sms','channel','phone','interaction','chat','ChatWebsiteId','account','InteractionId','created','isEmpty','body','Message','create','then','error','stringify','finally','stop','message','fulfillment','speech','dialogflow','providerResponse','metadata','endConversation','next','catch','lodash','moment','apiai-promise','md5','./utils','routing','../../rpc/chatMessage','../../rpc/mailMessage','../../rpc/faxMessage','../../rpc/whatsappMessage','consequence'];(function(_0x4dfe9d,_0x1bbd61){var _0x2d8540=function(_0x434fc7){while(--_0x434fc7){_0x4dfe9d['push'](_0x4dfe9d['shift']());}};_0x2d8540(++_0x1bbd61);}(_0xc14a,0xa9));var _0xac14=function(_0x3947c9,_0x4d6c97){_0x3947c9=_0x3947c9-0x0;var _0x2e7871=_0xc14a[_0x3947c9];return _0x2e7871;};'use strict';var _=require(_0xac14('0x0'));var moment=require(_0xac14('0x1'));var apiai=require(_0xac14('0x2'));var md5=require(_0xac14('0x3'));var utils=require(_0xac14('0x4'));var logger=require('../../../../config/logger')(_0xac14('0x5'));var rpc={'chatMessage':require(_0xac14('0x6')),'openchannelMessage':require('../../rpc/openchannelMessage'),'mailMessage':require(_0xac14('0x7')),'smsMessage':require('../../rpc/smsMessage'),'faxMessage':require(_0xac14('0x8')),'whatsappMessage':require(_0xac14('0x9'))};exports[_0xac14('0xa')]=function(_0x59a627,_0x1fff1f){return function(_0x1783be){try{if(!_[_0xac14('0xb')](this['interaction'][_0xac14('0xc')])){logger[_0xac14('0xd')](_0xac14('0xe'),_0x59a627,_0xac14('0xf'));return _0x1783be['next']();}var _0x86118d={'ContactId':this['contact']['id'],'sentAt':moment()[_0xac14('0x10')](_0xac14('0x11')),'direction':'out'};if(this['channel']===_0xac14('0x12')||this[_0xac14('0x13')]==='whatsapp'){_0x86118d[_0xac14('0x14')]=this[_0xac14('0x15')]['phone'];}var _0x5dc164=this['channel'];if(_0x5dc164===_0xac14('0x16')){_0x86118d[_0xac14('0x17')]=this[_0xac14('0x18')]['id'];}else{_0x86118d[_['upperFirst'](_0x5dc164)+'AccountId']=this[_0xac14('0x18')]['id'];}_0x86118d[_['upperFirst'](_0x5dc164)+_0xac14('0x19')]=this['interaction']['id'];if(this[_0xac14('0x15')][_0xac14('0x1a')]){var _0x1023e2=_0x1fff1f[0x2];if(!_[_0xac14('0x1b')](_0x1023e2)){_0x86118d[_0xac14('0x1c')]=utils['getMustacheRender'](_0x1023e2,this);return rpc[_0x5dc164+_0xac14('0x1d')][_0xac14('0x1e')](_0x86118d)[_0xac14('0x1f')](function(_0x1682b1){logger[_0xac14('0xd')](_0xac14('0xe'),_0x59a627,_0x86118d[_0xac14('0x1c')]);})['catch'](function(_0x429390){logger[_0xac14('0x20')](_0xac14('0xe'),_0x59a627,JSON[_0xac14('0x21')](_0x429390));})[_0xac14('0x22')](function(){logger['info']('[%s][DIALOGFLOW]\x20Welcome\x20Message\x20has\x20been\x20sent!',_0x59a627);_0x1783be[_0xac14('0x23')]();});}else{logger[_0xac14('0xd')]('[%s][DIALOGFLOW]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!',_0x59a627);_0x1783be[_0xac14('0x23')]();}}else{var _0x492f37=this;var _0x528406=apiai(_0x1fff1f[0x0],{'language':_0x1fff1f[0x1]||'en'});return _0x528406['textRequest'](this[_0xac14('0x24')]['body'],{'sessionId':md5(this[_0xac14('0x15')]['id'])})[_0xac14('0x1f')](function(_0x272b46){_0x492f37[_0xac14('0x1c')]['dialogflow']=_0x272b46;_0x86118d[_0xac14('0x1c')]=_0x272b46['result'][_0xac14('0x25')][_0xac14('0x26')];_0x86118d['providerName']=_0xac14('0x27');_0x86118d[_0xac14('0x28')]=JSON[_0xac14('0x21')](_0x272b46);return rpc[_0x5dc164+_0xac14('0x1d')][_0xac14('0x1e')](_0x86118d)[_0xac14('0x1f')](function(_0x5d033d){logger[_0xac14('0xd')]('[%s][DIALOGFLOW]\x20%s',_0x59a627,_0x86118d['body']);if(_0x272b46['result'][_0xac14('0x29')][_0xac14('0x2a')]){_0x1783be[_0xac14('0x2b')]();}else{_0x1783be['stop']();}})['catch'](function(_0x506837){logger['error']('[%s][DIALOGFLOW]\x20%s',_0x59a627,JSON[_0xac14('0x21')](_0x506837));_0x1783be[_0xac14('0x2b')]();});})[_0xac14('0x2c')](function(_0x464ae9){logger[_0xac14('0x20')]('[%s][DIALOGFLOW]\x20%s',_0x59a627,_0x464ae9[_0xac14('0x24')]);_0x1783be['next']();});}}catch(_0x30dc81){logger[_0xac14('0x20')](_0xac14('0xe'),_0x59a627,JSON[_0xac14('0x21')](_0x30dc81));_0x1783be['stop']();}};};