b8610365d873bc0a4f071117fefa769388c5c140
[motion2.git] / server / services / agi / scripts / index.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 _0x230f=['utf8','application/x-www-form-urlencoded','f.req=','stringify','jQ1olc','null','generic','slice','parse\x20response\x20failed:\x0a','data','length','lang\x20\x22','generate','goggleTTS\x20path','googleASR','asr','results','map','alternatives','transcript','join','confidence','googleASR\x20confidence','catch','name','message','googleDialogflow','textRequest','result','source','resolvedQuery','action','actionIncomplete','fulfillment','speech','score','code','metadata','endConversation','isFallbackIntent','googleDialogflowV2','replace','SessionsClient','sessionPath','detectIntent','Dialogflow\x20V2\x20response:','queryResult','allRequiredParamsPresent','diagnosticInfo','fields','end_conversation','boolValue','fulfillmentText','intent','displayName','isFallback','queryText','responseId','intentDetectionConfidence','tildeASR','Tilde\x20ASR\x20transcription','Tilde\x20ASR\x20confidence','awsPolly','polly','awsLex','format','%s?apikey=%s&action=convert&text=%s&voice=%s&speed=%s&format=mp3','ispeechASR','model=%s','freeform=3','parse','sestekNDA','text','channelData','AudioFromTts','audiofile','apiai-promise','bluebird','http','https','md5','path','sox','google-tts-api','url','util','./aws','./google','./sestek','agi','https://api.ispeech.org/api/rest','protocol','https:','host','WHAT_EVER','get','statusCode','request\x20to\x20','\x20failed,\x20status\x20code\x20=\x20','statusMessage','createWriteStream','finish','info','finish\x20download\x20file','close','unlink','error','download\x20error','pipe','end','transcode','wav','start','resolve','tmp','.wav','writeFileSync','base64','chmodSync','777','split','readFileSync','toString','https://texttospeech.googleapis.com/v1/','?key=','ssml','input','voice','audioConfig','MP3','text:synthesize','POST','then','.mp3','from','audioContent','writeFile','googleCloudTTS\x20path','https://translate.google.com/_/TranslateWebserverUi/data/batchexecute'];(function(_0x587439,_0x153c32){var _0x138c81=function(_0x36bb26){while(--_0x36bb26){_0x587439['push'](_0x587439['shift']());}};_0x138c81(++_0x153c32);}(_0x230f,0xd7));var _0xf230=function(_0x42f3a5,_0x3a4470){_0x42f3a5=_0x42f3a5-0x0;var _0x1a7d74=_0x230f[_0x42f3a5];return _0x1a7d74;};'use strict';var apiai=require(_0xf230('0x0'));var BPromise=require(_0xf230('0x1'));var dialogflow=require('dialogflow');var fs=require('fs');var http=require(_0xf230('0x2'));var https=require(_0xf230('0x3'));var md5=require(_0xf230('0x4'));var path=require(_0xf230('0x5'));var querystring=require('querystring');var rp=require('request-promise');var rs=require('randomstring');var sox=require(_0xf230('0x6'));var tts=require(_0xf230('0x7'));var urlParse=require(_0xf230('0x8'))['parse'];var util=require(_0xf230('0x9'));var aws=require(_0xf230('0xa'));var google=require(_0xf230('0xb'));var sestek=require(_0xf230('0xc'));var tilde=require('./tilde');var logger=require('../../../config/logger')(_0xf230('0xd'));var ISPEECH_API=_0xf230('0xe');function downloadFile(_0x27ddb7,_0x18c4d6){return new BPromise(function(_0x566cc1,_0x37d73b){var _0x4994ba=urlParse(_0x27ddb7);var _0x537ef8=_0x4994ba[_0xf230('0xf')]===_0xf230('0x10')?https:http;var _0x3d1e7d={'host':_0x4994ba[_0xf230('0x11')],'path':_0x4994ba[_0xf230('0x5')],'headers':{'user-agent':_0xf230('0x12')}};_0x537ef8[_0xf230('0x13')](_0x3d1e7d,function(_0x3cbd88){if(_0x3cbd88[_0xf230('0x14')]!==0xc8){logger['error'](_0xf230('0x15')+_0x27ddb7+_0xf230('0x16')+_0x3cbd88[_0xf230('0x14')]+'\x20('+_0x3cbd88[_0xf230('0x17')]+')');_0x37d73b(new Error(_0xf230('0x15')+_0x27ddb7+'\x20failed,\x20status\x20code\x20=\x20'+_0x3cbd88[_0xf230('0x14')]+'\x20('+_0x3cbd88[_0xf230('0x17')]+')'));return;}var _0x39ecc7=fs[_0xf230('0x18')](_0x18c4d6);_0x39ecc7['on'](_0xf230('0x19'),function(){logger[_0xf230('0x1a')](_0xf230('0x1b'));_0x39ecc7[_0xf230('0x1c')](_0x566cc1);});_0x39ecc7['on']('error',function(_0xa3883f){fs[_0xf230('0x1d')](_0x18c4d6);logger[_0xf230('0x1e')](_0xf230('0x1f'),_0xa3883f);_0x37d73b(_0xa3883f);});_0x3cbd88[_0xf230('0x20')](_0x39ecc7);})['on']('error',function(_0xc3eb4){logger[_0xf230('0x1e')](_0xc3eb4);_0x37d73b(_0xc3eb4);})[_0xf230('0x21')]();});}function uploadFile(_0x10f9e2){return new BPromise(function(_0x251283,_0x206954){var _0x1a196f=urlParse(_0x10f9e2);var _0x385722=_0x1a196f[_0xf230('0xf')]===_0xf230('0x10')?https:http;var _0x14cd15={'host':_0x1a196f[_0xf230('0x11')],'path':_0x1a196f[_0xf230('0x5')],'headers':{'user-agent':_0xf230('0x12')}};_0x385722['get'](_0x14cd15,function(_0x37db95){if(_0x37db95[_0xf230('0x14')]!==0xc8){_0x206954(new Error('request\x20to\x20'+_0x10f9e2+_0xf230('0x16')+_0x37db95[_0xf230('0x14')]+'\x20('+_0x37db95[_0xf230('0x17')]+')'));return;}var _0x4c09e7='';_0x37db95['on']('data',function(_0x100f3a){_0x4c09e7+=_0x100f3a;});_0x37db95['on'](_0xf230('0x21'),function(){_0x251283(_0x4c09e7);});})['on'](_0xf230('0x1e'),function(_0x2e8743){_0x206954(_0x2e8743);})[_0xf230('0x21')]();});}function convertFile(_0x44198f,_0x516b18){return new BPromise(function(_0x49e35d,_0x1e6887){var _0x2e867d=sox[_0xf230('0x22')](_0x44198f,_0x516b18,{'sampleRate':0x1f40,'format':_0xf230('0x23'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x2e867d['on'](_0xf230('0x1e'),function(_0x55ba24){logger['error']('error\x20convert',_0x55ba24);_0x1e6887(_0x55ba24);});_0x2e867d['on'](_0xf230('0x21'),function(){logger[_0xf230('0x1a')]('finish\x20to\x20convert');_0x49e35d();});_0x2e867d[_0xf230('0x24')]();});}function base64Decode(_0x167ea8,_0x2f031a){var _0x1d5cd1=path[_0xf230('0x25')](__dirname,_0xf230('0x26'),rs['generate'](0x4)+_0xf230('0x27'));fs[_0xf230('0x28')](_0x1d5cd1,_0x167ea8,_0xf230('0x29'));return convertFile(_0x1d5cd1,_0x2f031a)['then'](function(){try{fs[_0xf230('0x1d')](_0x1d5cd1);fs[_0xf230('0x2a')](_0x2f031a,_0xf230('0x2b'));}catch(_0x3a5f11){logger[_0xf230('0x1e')](_0x3a5f11);}return _0x2f031a[_0xf230('0x2c')]('.')[0x0];});}function base64Encode(_0x3f264a){var _0x22b1db=fs[_0xf230('0x2d')](_0x3f264a);return new Buffer(_0x22b1db)[_0xf230('0x2e')]('base64');}exports['googleCloudTTS']=function(_0x423413,_0x14bd88,_0x1e1db0,_0xef8d46,_0x8c68e,_0x39676b){var _0x46f3af,_0x4778bf;var _0x1b46c7=_0xf230('0x2f');var _0x209e14=_0xf230('0x30')+_0x423413;var _0x14d572={};if(_0x14bd88===_0xf230('0x31')){_0x14d572[_0xf230('0x32')]={'ssml':_0x1e1db0};}else{_0x14d572[_0xf230('0x32')]={'text':_0x1e1db0};};_0x14d572[_0xf230('0x33')]={'languageCode':_0xef8d46,'ssmlGender':_0x8c68e};_0x14d572[_0xf230('0x34')]={'audioEncoding':_0xf230('0x35'),'sampleRateHertz':0x1f40};var _0x1c42be={'url':_0x1b46c7+_0xf230('0x36')+_0x209e14,'method':_0xf230('0x37'),'json':!![],'body':_0x14d572};return rp(_0x1c42be)[_0xf230('0x38')](function(_0x585087){return new BPromise(function(_0x40dfc8,_0x57fba8){_0x46f3af=path['resolve'](__dirname,_0xf230('0x26'),rs['generate'](0x4)+_0xf230('0x39'));var _0x5cad65=Buffer[_0xf230('0x3a')](_0x585087[_0xf230('0x3b')],_0xf230('0x29'));return fs[_0xf230('0x3c')](_0x46f3af,_0x5cad65,function(_0x1a2a52){_0x57fba8(_0x1a2a52);},function(_0x1db6c5){fs['chmodSync'](_0x46f3af,'777');_0x40dfc8(_0x1db6c5);});});})[_0xf230('0x38')](function(_0x421b69){_0x4778bf=path[_0xf230('0x25')](__dirname,_0xf230('0x26'),rs['generate'](0x4)+_0xf230('0x27'));return convertFile(_0x46f3af,_0x4778bf);})[_0xf230('0x38')](function(){try{fs[_0xf230('0x1d')](_0x46f3af);fs[_0xf230('0x2a')](_0x4778bf,'777');}catch(_0x544897){logger[_0xf230('0x1e')](_0x544897);}logger[_0xf230('0x1a')](_0xf230('0x3d'),_0x4778bf[_0xf230('0x2c')]('.')[0x0]);return _0x4778bf[_0xf230('0x2c')]('.')[0x0];});};exports['googleTTS']=function(_0x233e1c,_0x1ed3c4){var _0x4a8551=_0xf230('0x3e');var _0x386d34,_0x49e1e5;var _0x37e052;var _0x2e1a0c={'encoding':_0xf230('0x3f'),'method':_0xf230('0x37'),'timeout':0x2710,'url':_0x4a8551,'headers':{'Content-Type':_0xf230('0x40')},'body':_0xf230('0x41')+encodeURIComponent(JSON[_0xf230('0x42')]([[[_0xf230('0x43'),JSON['stringify']([_0x233e1c,_0x1ed3c4||'en',![],_0xf230('0x44')]),null,_0xf230('0x45')]]]))};return rp(_0x2e1a0c)[_0xf230('0x38')](function(_0x1eb822){try{_0x37e052=eval(_0x1eb822[_0xf230('0x46')](0x5))[0x0][0x2];}catch(_0x49afad){throw new Error(_0xf230('0x47')+_0x1eb822[_0xf230('0x48')]);}if(!_0x37e052){if(_0x233e1c[_0xf230('0x49')]>0xc8){throw new Error('text\x20length\x20limit\x20reached');}else{throw new Error(_0xf230('0x4a')+_0x1ed3c4+'\x22\x20might\x20not\x20exist');}}try{_0x37e052=eval(_0x37e052)[0x0];}catch(_0x9e46df){throw new Error(_0xf230('0x47')+res[_0xf230('0x48')]);}const _0x125659=_0x37e052;_0x386d34=path[_0xf230('0x25')](__dirname,_0xf230('0x26'),rs[_0xf230('0x4b')](0x4)+_0xf230('0x39'));fs['writeFileSync'](_0x386d34,_0x37e052,{'encoding':_0xf230('0x29')});_0x49e1e5=path['resolve'](__dirname,_0xf230('0x26'),rs[_0xf230('0x4b')](0x4)+_0xf230('0x27'));return convertFile(_0x386d34,_0x49e1e5);})['then'](function(){try{fs[_0xf230('0x1d')](_0x386d34);fs[_0xf230('0x2a')](_0x49e1e5,_0xf230('0x2b'));}catch(_0x35bb67){logger['error'](_0x35bb67);}logger[_0xf230('0x1a')](_0xf230('0x4c'),_0x49e1e5['split']('.')[0x0]);return _0x49e1e5['split']('.')[0x0];});};exports[_0xf230('0x4d')]=function(_0x40dca1,_0x12208c,_0x46a6de){return google[_0xf230('0x4e')](_0x40dca1,_0x12208c,_0x46a6de)[_0xf230('0x38')](function(_0x2d8ee3){logger[_0xf230('0x1a')](_0x2d8ee3['uri']);return rp(_0x2d8ee3);})['then'](function(_0x16a27f){var _0x2d2a0c='';var _0x431727=0x0;try{fs[_0xf230('0x1d')](_0x40dca1);}catch(_0x413481){logger[_0xf230('0x1e')](_0x413481);}if(_0x16a27f&&_0x16a27f['results']){_0x2d2a0c=_0x16a27f[_0xf230('0x4f')][_0xf230('0x50')](function(_0xda8ead){return _0xda8ead&&_0xda8ead[_0xf230('0x51')][0x0]&&_0xda8ead[_0xf230('0x51')][0x0][_0xf230('0x52')]?_0xda8ead[_0xf230('0x51')][0x0][_0xf230('0x52')]:'';})[_0xf230('0x53')]('\x0a');_0x431727=_0x16a27f[_0xf230('0x4f')][_0xf230('0x50')](function(_0x1f79cb){return _0x1f79cb&&_0x1f79cb['alternatives'][0x0]&&_0x1f79cb[_0xf230('0x51')][0x0][_0xf230('0x54')]?_0x1f79cb[_0xf230('0x51')][0x0][_0xf230('0x54')]:0x0;})[_0xf230('0x53')]('\x0a');}logger[_0xf230('0x1a')]('googleASR\x20transcription',_0x2d2a0c);logger[_0xf230('0x1a')](_0xf230('0x55'),_0x431727);return{'transcript':_0x2d2a0c,'confidence':_0x431727};})[_0xf230('0x56')](function(_0x570e22){logger[_0xf230('0x1e')]('[%s]\x20%s:%s',_0x570e22[_0xf230('0x14')],_0x570e22[_0xf230('0x57')],_0x570e22[_0xf230('0x58')]);});};exports[_0xf230('0x59')]=function(_0x6d37c,_0x473105,_0x2d435a,_0x349a14){var _0x6ed8a=apiai(_0x473105,{'language':_0x349a14||'en'});return _0x6ed8a[_0xf230('0x5a')](_0x2d435a,{'sessionId':md5(_0x6d37c)})[_0xf230('0x38')](function(_0x4a38c5){logger[_0xf230('0x1a')](JSON[_0xf230('0x42')](_0x4a38c5));return{'source':_0x4a38c5[_0xf230('0x5b')][_0xf230('0x5c')],'resolvedQuery':_0x4a38c5['result'][_0xf230('0x5d')],'action':_0x4a38c5[_0xf230('0x5b')][_0xf230('0x5e')],'actionIncomplete':_0x4a38c5['result'][_0xf230('0x5f')],'speech':_0x4a38c5[_0xf230('0x5b')][_0xf230('0x60')][_0xf230('0x61')],'score':_0x4a38c5['result'][_0xf230('0x62')],'statusCode':_0x4a38c5['status'][_0xf230('0x63')],'endConversation':_0x4a38c5[_0xf230('0x5b')][_0xf230('0x64')][_0xf230('0x65')]||![],'isFallbackIntent':_0x4a38c5['result']['metadata'][_0xf230('0x66')]};});};exports[_0xf230('0x67')]=function(_0x200936,_0x33ff81,_0x4ae8fe,_0x47dca6,_0xb26eeb,_0xc1d562){var _0x120b85=_0x47dca6[_0xf230('0x68')](/\\n/g,'\x0a');var _0x71e9fb={'credentials':{'private_key':_0x120b85,'client_email':_0x4ae8fe}};var _0x3d3196=new dialogflow[(_0xf230('0x69'))](_0x71e9fb);var _0x37a128=_0x3d3196[_0xf230('0x6a')](_0x33ff81,md5(_0x200936));var _0x37fdc9={'session':_0x37a128,'queryInput':{'text':{'text':_0xb26eeb,'languageCode':_0xc1d562}}};return _0x3d3196[_0xf230('0x6b')](_0x37fdc9)[_0xf230('0x38')](function(_0x38f4a8){if(_0x38f4a8&&_0x38f4a8['length']){logger['info'](_0xf230('0x6c'),JSON[_0xf230('0x42')](_0x38f4a8));var _0x29a347=_0x38f4a8[0x0];if(_0x29a347){var _0x23bd7c={'action':_0x29a347[_0xf230('0x6d')][_0xf230('0x5e')],'allRequiredParamsPresent':_0x29a347[_0xf230('0x6d')][_0xf230('0x6e')],'endConversation':_0x29a347['queryResult'][_0xf230('0x6f')]&&_0x29a347[_0xf230('0x6d')][_0xf230('0x6f')][_0xf230('0x70')]?_0x29a347[_0xf230('0x6d')][_0xf230('0x6f')]['fields'][_0xf230('0x71')][_0xf230('0x72')]:![],'fulfillmentText':_0x29a347[_0xf230('0x6d')][_0xf230('0x73')],'intentName':_0x29a347[_0xf230('0x6d')][_0xf230('0x74')][_0xf230('0x57')],'intentDisplayName':_0x29a347[_0xf230('0x6d')][_0xf230('0x74')][_0xf230('0x75')],'isFallbackIntent':_0x29a347[_0xf230('0x6d')][_0xf230('0x74')][_0xf230('0x76')],'languageCode':_0x29a347[_0xf230('0x6d')]['languageCode'],'queryText':_0x29a347[_0xf230('0x6d')][_0xf230('0x77')],'responseId':_0x29a347[_0xf230('0x78')],'score':_0x29a347[_0xf230('0x6d')][_0xf230('0x79')],'speech':_0x29a347[_0xf230('0x6d')][_0xf230('0x73')],'providerResponse':JSON[_0xf230('0x42')](_0x38f4a8)};return _0x23bd7c;}}});};exports[_0xf230('0x7a')]=function(_0x2f5b72,_0x2e1389,_0x10f47a,_0x4ef7d6){return tilde['asr'](_0x2f5b72,_0x2e1389,_0x10f47a,_0x4ef7d6)[_0xf230('0x38')](function(_0x2260c5){try{fs[_0xf230('0x1d')](_0x2f5b72);}catch(_0x5068c6){logger[_0xf230('0x1e')](_0x5068c6);}logger[_0xf230('0x1a')](_0xf230('0x7b'),_0x2260c5[_0xf230('0x52')]);logger[_0xf230('0x1a')](_0xf230('0x7c'),_0x2260c5['confidence']);return _0x2260c5;});};exports[_0xf230('0x7d')]=function(_0x917dc1,_0x481de9,_0x52671a,_0x3ebb5a,_0x1b382f,_0x5ed1e1){var _0x447ffd=path['resolve'](__dirname,'tmp',rs[_0xf230('0x4b')](0x4)+_0xf230('0x39'));var _0x3ddbaf;return aws[_0xf230('0x7e')](_0x917dc1,_0x481de9,_0x52671a,_0x3ebb5a,_0x1b382f,_0x5ed1e1,_0x447ffd)[_0xf230('0x38')](function(){_0x3ddbaf=path['resolve'](__dirname,_0xf230('0x26'),rs[_0xf230('0x4b')](0x4)+_0xf230('0x27'));return convertFile(_0x447ffd,_0x3ddbaf);})[_0xf230('0x38')](function(){try{fs[_0xf230('0x1d')](_0x447ffd);fs[_0xf230('0x2a')](_0x3ddbaf,_0xf230('0x2b'));}catch(_0x279656){logger['error'](_0x279656);}return _0x3ddbaf[_0xf230('0x2c')]('.')[0x0];});};exports[_0xf230('0x7f')]=function(_0x3990c9,_0x147ed8,_0x520917,_0x332cf7,_0x39ccd5,_0x196490){return aws['lex'](_0x3990c9,_0x147ed8,_0x520917,_0x332cf7,_0x39ccd5,_0x196490)[_0xf230('0x38')](function(_0x10f4e9){logger[_0xf230('0x1a')](JSON['stringify'](_0x10f4e9));return _0x10f4e9;});};exports['ispeechTTS']=function(_0x2f18a2,_0x2b8756,_0x184ad7,_0x53cdf0){var _0x1207ad=util[_0xf230('0x80')](_0xf230('0x81'),ISPEECH_API,_0x2f18a2,encodeURIComponent(_0x2b8756),_0x184ad7,_0x53cdf0);var _0x5dc28a=path['resolve'](__dirname,_0xf230('0x26'),rs[_0xf230('0x4b')](0x4)+_0xf230('0x39'));var _0x2aec9e;return downloadFile(_0x1207ad,_0x5dc28a)['then'](function(){_0x2aec9e=path[_0xf230('0x25')](__dirname,'tmp',rs[_0xf230('0x4b')](0x4)+_0xf230('0x27'));return convertFile(_0x5dc28a,_0x2aec9e);})['then'](function(){try{fs[_0xf230('0x1d')](_0x5dc28a);fs[_0xf230('0x2a')](_0x2aec9e,_0xf230('0x2b'));}catch(_0x6c04f4){logger['error'](_0x6c04f4);}return _0x2aec9e[_0xf230('0x2c')]('.')[0x0];});};exports[_0xf230('0x82')]=function(_0x4d6c44,_0x3a8eab,_0x4b1789,_0x109377){var _0x292fca=util[_0xf230('0x80')]('%s?apikey=%s&action=recognize&locale=%s&content-type=audio/x-wav&audio=%s&%s',ISPEECH_API,_0x3a8eab,_0x4b1789,base64Encode(_0x4d6c44),_0x109377?util[_0xf230('0x80')](_0xf230('0x83'),encodeURIComponent(_0x109377)):_0xf230('0x84'));return uploadFile(_0x292fca)['then'](function(_0x390a7a){return querystring[_0xf230('0x85')](_0x390a7a);});};exports[_0xf230('0x86')]=function(_0x100adf,_0x409a46){return sestek['nda'](_0x100adf,_0x409a46)[_0xf230('0x38')](function(_0x35873d){logger[_0xf230('0x1a')](JSON['stringify'](_0x35873d));var _0x5ef3a7={'speech':_0x35873d[_0xf230('0x87')],'action':_0x35873d['channelData']?_0x35873d['channelData']['CustomAction']:undefined,'actiondata':_0x35873d[_0xf230('0x88')]?_0x35873d[_0xf230('0x88')]['CustomActionData']:undefined};if(_0x35873d['channelData']&&_0x35873d[_0xf230('0x88')][_0xf230('0x89')]){return base64Decode(_0x35873d[_0xf230('0x88')][_0xf230('0x89')]['Data'],path['resolve'](__dirname,'tmp',rs[_0xf230('0x4b')](0x4)+'.wav'))[_0xf230('0x38')](function(_0x4f53b2){_0x5ef3a7[_0xf230('0x8a')]=_0x4f53b2;return _0x5ef3a7;});}return _0x5ef3a7;});};exports['dirName']=function(){return __dirname;};