6a52cf50753d7218899c71d961c8c13a40c7d28f
[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 _0xc21e=['phone','ChatWebsiteId','account','upperFirst','InteractionId','created','isEmpty','getMustacheRender','Message','create','then','body','catch','stringify','finally','stop','[%s][DIALOGFLOW]\x20Welcome\x20Message\x20is\x20empty.\x20It\x20will\x20wait\x20for\x20a\x20message\x20from\x20customer!','textRequest','dialogflow','result','fulfillment','providerName','providerResponse','endConversation','error','message','moment','apiai-promise','md5','./utils','../../../../config/logger','routing','../../rpc/openchannelMessage','../../rpc/mailMessage','../../rpc/smsMessage','../../rpc/faxMessage','../../rpc/whatsappMessage','consequence','isNil','interaction','UserId','info','[%s][DIALOGFLOW]\x20%s','next','contact','out','channel','whatsapp'];(function(_0x594235,_0x4a17cf){var _0x4df355=function(_0x4f51d0){while(--_0x4f51d0){_0x594235['push'](_0x594235['shift']());}};_0x4df355(++_0x4a17cf);}(_0xc21e,0x1ca));var _0xec21=function(_0x21a9d9,_0x4d5ac8){_0x21a9d9=_0x21a9d9-0x0;var _0x46adce=_0xc21e[_0x21a9d9];return _0x46adce;};'use strict';var _=require('lodash');var moment=require(_0xec21('0x0'));var apiai=require(_0xec21('0x1'));var md5=require(_0xec21('0x2'));var utils=require(_0xec21('0x3'));var logger=require(_0xec21('0x4'))(_0xec21('0x5'));var rpc={'chatMessage':require('../../rpc/chatMessage'),'openchannelMessage':require(_0xec21('0x6')),'mailMessage':require(_0xec21('0x7')),'smsMessage':require(_0xec21('0x8')),'faxMessage':require(_0xec21('0x9')),'whatsappMessage':require(_0xec21('0xa'))};exports[_0xec21('0xb')]=function(_0x406b66,_0x2f15e7){return function(_0x51f6dc){try{if(!_[_0xec21('0xc')](this[_0xec21('0xd')][_0xec21('0xe')])){logger[_0xec21('0xf')](_0xec21('0x10'),_0x406b66,'Agent\x20is\x20managing\x20the\x20interaction.\x20Go\x20to\x20next\x20application.');return _0x51f6dc[_0xec21('0x11')]();}var _0x1d5e4b={'ContactId':this[_0xec21('0x12')]['id'],'sentAt':moment()['format']('YYYY-MM-DD\x20HH:mm:ss'),'direction':_0xec21('0x13')};if(this[_0xec21('0x14')]==='sms'||this[_0xec21('0x14')]===_0xec21('0x15')){_0x1d5e4b[_0xec21('0x16')]=this[_0xec21('0xd')][_0xec21('0x16')];}var _0x5ee9f6=this[_0xec21('0x14')];if(_0x5ee9f6==='chat'){_0x1d5e4b[_0xec21('0x17')]=this[_0xec21('0x18')]['id'];}else{_0x1d5e4b[_[_0xec21('0x19')](_0x5ee9f6)+'AccountId']=this[_0xec21('0x18')]['id'];}_0x1d5e4b[_[_0xec21('0x19')](_0x5ee9f6)+_0xec21('0x1a')]=this[_0xec21('0xd')]['id'];if(this[_0xec21('0xd')][_0xec21('0x1b')]){var _0x3e538c=_0x2f15e7[0x2];if(!_[_0xec21('0x1c')](_0x3e538c)){_0x1d5e4b['body']=utils[_0xec21('0x1d')](_0x3e538c,this);return rpc[_0x5ee9f6+_0xec21('0x1e')][_0xec21('0x1f')](_0x1d5e4b)[_0xec21('0x20')](function(_0x3ab226){logger[_0xec21('0xf')](_0xec21('0x10'),_0x406b66,_0x1d5e4b[_0xec21('0x21')]);})[_0xec21('0x22')](function(_0x350443){logger['error']('[%s][DIALOGFLOW]\x20%s',_0x406b66,JSON[_0xec21('0x23')](_0x350443));})[_0xec21('0x24')](function(){logger['info']('[%s][DIALOGFLOW]\x20Welcome\x20Message\x20has\x20been\x20sent!',_0x406b66);_0x51f6dc[_0xec21('0x25')]();});}else{logger[_0xec21('0xf')](_0xec21('0x26'),_0x406b66);_0x51f6dc['stop']();}}else{var _0x17d1ac=this;var _0x1f86d2=apiai(_0x2f15e7[0x0],{'language':_0x2f15e7[0x1]||'en'});return _0x1f86d2[_0xec21('0x27')](this['message'][_0xec21('0x21')],{'sessionId':md5(this[_0xec21('0xd')]['id'])})[_0xec21('0x20')](function(_0x46fd9e){_0x17d1ac[_0xec21('0x21')][_0xec21('0x28')]=_0x46fd9e;_0x1d5e4b['body']=_0x46fd9e[_0xec21('0x29')][_0xec21('0x2a')]['speech'];_0x1d5e4b[_0xec21('0x2b')]='dialogflow';_0x1d5e4b[_0xec21('0x2c')]=JSON['stringify'](_0x46fd9e);return rpc[_0x5ee9f6+'Message'][_0xec21('0x1f')](_0x1d5e4b)[_0xec21('0x20')](function(_0x39154a){logger[_0xec21('0xf')](_0xec21('0x10'),_0x406b66,_0x1d5e4b['body']);if(_0x46fd9e[_0xec21('0x29')]['metadata'][_0xec21('0x2d')]){_0x51f6dc[_0xec21('0x11')]();}else{_0x51f6dc[_0xec21('0x25')]();}})[_0xec21('0x22')](function(_0x4dd56b){logger[_0xec21('0x2e')](_0xec21('0x10'),_0x406b66,JSON[_0xec21('0x23')](_0x4dd56b));_0x51f6dc[_0xec21('0x11')]();});})[_0xec21('0x22')](function(_0x1d1afc){logger[_0xec21('0x2e')](_0xec21('0x10'),_0x406b66,_0x1d1afc[_0xec21('0x2f')]);_0x51f6dc[_0xec21('0x11')]();});}}catch(_0x43a2de){logger['error'](_0xec21('0x10'),_0x406b66,JSON[_0xec21('0x23')](_0x43a2de));_0x51f6dc[_0xec21('0x25')]();}};};