Built motion from commit 3c2ce842.|2.6.31
[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 _0x95c6=['goggleTTS\x20path','googleASR','asr','uri','results','alternatives','transcript','join','map','confidence','googleASR\x20transcription','googleASR\x20confidence','catch','name','textRequest','result','resolvedQuery','action','actionIncomplete','status','code','metadata','isFallbackIntent','googleDialogflowV2','replace','SessionsClient','sessionPath','detectIntent','queryResult','allRequiredParamsPresent','diagnosticInfo','fields','end_conversation','boolValue','fulfillmentText','intent','displayName','isFallback','queryText','responseId','intentDetectionConfidence','tildeASR','Tilde\x20ASR\x20transcription','Tilde\x20ASR\x20confidence','polly','awsLex','ispeechTTS','%s?apikey=%s&action=convert&text=%s&voice=%s&speed=%s&format=mp3','ispeechASR','format','%s?apikey=%s&action=recognize&locale=%s&content-type=audio/x-wav&audio=%s&%s','model=%s','freeform=3','text','channelData','CustomActionData','AudioFromTts','Data','audiofile','dirName','bluebird','dialogflow','http','https','md5','querystring','request-promise','randomstring','sox','google-tts-api','url','parse','./sestek','./tilde','../../../config/logger','agi','https://api.ispeech.org/api/rest','protocol','https:','host','WHAT_EVER','statusCode','error','statusMessage','request\x20to\x20','\x20failed,\x20status\x20code\x20=\x20','createWriteStream','finish','info','finish\x20download\x20file','close','unlink','download\x20error','pipe','get','data','end','transcode','wav','error\x20convert','finish\x20to\x20convert','tmp','writeFileSync','then','chmodSync','777','split','toString','googleCloudTTS','?key=','ssml','input','voice','audioConfig','MP3','text:synthesize','resolve','generate','.mp3','from','audioContent','base64','writeFile','googleCloudTTS\x20path','utf8','POST','application/x-www-form-urlencoded','f.req=','jQ1olc','stringify','null','slice','parse\x20response\x20failed:\x0a','length','\x22\x20might\x20not\x20exist','.wav'];(function(_0x5d8c7d,_0x3e6c01){var _0x326595=function(_0x57f7b1){while(--_0x57f7b1){_0x5d8c7d['push'](_0x5d8c7d['shift']());}};_0x326595(++_0x3e6c01);}(_0x95c6,0x14c));var _0x695c=function(_0x258fe1,_0x40a300){_0x258fe1=_0x258fe1-0x0;var _0x597fab=_0x95c6[_0x258fe1];return _0x597fab;};'use strict';var apiai=require('apiai-promise');var BPromise=require(_0x695c('0x0'));var dialogflow=require(_0x695c('0x1'));var fs=require('fs');var http=require(_0x695c('0x2'));var https=require(_0x695c('0x3'));var md5=require(_0x695c('0x4'));var path=require('path');var querystring=require(_0x695c('0x5'));var rp=require(_0x695c('0x6'));var rs=require(_0x695c('0x7'));var sox=require(_0x695c('0x8'));var tts=require(_0x695c('0x9'));var urlParse=require(_0x695c('0xa'))[_0x695c('0xb')];var util=require('util');var aws=require('./aws');var google=require('./google');var sestek=require(_0x695c('0xc'));var tilde=require(_0x695c('0xd'));var logger=require(_0x695c('0xe'))(_0x695c('0xf'));var ISPEECH_API=_0x695c('0x10');function downloadFile(_0x18249f,_0x1eb7df){return new BPromise(function(_0x47c621,_0x154688){var _0x462525=urlParse(_0x18249f);var _0x501628=_0x462525[_0x695c('0x11')]===_0x695c('0x12')?https:http;var _0x2d3a6c={'host':_0x462525[_0x695c('0x13')],'path':_0x462525['path'],'headers':{'user-agent':_0x695c('0x14')}};_0x501628['get'](_0x2d3a6c,function(_0x1a2c62){if(_0x1a2c62[_0x695c('0x15')]!==0xc8){logger[_0x695c('0x16')]('request\x20to\x20'+_0x18249f+'\x20failed,\x20status\x20code\x20=\x20'+_0x1a2c62[_0x695c('0x15')]+'\x20('+_0x1a2c62[_0x695c('0x17')]+')');_0x154688(new Error(_0x695c('0x18')+_0x18249f+_0x695c('0x19')+_0x1a2c62[_0x695c('0x15')]+'\x20('+_0x1a2c62[_0x695c('0x17')]+')'));return;}var _0x427a1a=fs[_0x695c('0x1a')](_0x1eb7df);_0x427a1a['on'](_0x695c('0x1b'),function(){logger[_0x695c('0x1c')](_0x695c('0x1d'));_0x427a1a[_0x695c('0x1e')](_0x47c621);});_0x427a1a['on'](_0x695c('0x16'),function(_0x3d3573){fs[_0x695c('0x1f')](_0x1eb7df);logger[_0x695c('0x16')](_0x695c('0x20'),_0x3d3573);_0x154688(_0x3d3573);});_0x1a2c62[_0x695c('0x21')](_0x427a1a);})['on'](_0x695c('0x16'),function(_0x1e5468){logger[_0x695c('0x16')](_0x1e5468);_0x154688(_0x1e5468);})['end']();});}function uploadFile(_0x4c794d){return new BPromise(function(_0x3397d0,_0x46b267){var _0x446319=urlParse(_0x4c794d);var _0x5605e4=_0x446319[_0x695c('0x11')]===_0x695c('0x12')?https:http;var _0x127017={'host':_0x446319[_0x695c('0x13')],'path':_0x446319['path'],'headers':{'user-agent':_0x695c('0x14')}};_0x5605e4[_0x695c('0x22')](_0x127017,function(_0x54ec48){if(_0x54ec48['statusCode']!==0xc8){_0x46b267(new Error(_0x695c('0x18')+_0x4c794d+_0x695c('0x19')+_0x54ec48[_0x695c('0x15')]+'\x20('+_0x54ec48['statusMessage']+')'));return;}var _0x34d6d6='';_0x54ec48['on'](_0x695c('0x23'),function(_0x43c75c){_0x34d6d6+=_0x43c75c;});_0x54ec48['on'](_0x695c('0x24'),function(){_0x3397d0(_0x34d6d6);});})['on']('error',function(_0x2cab31){_0x46b267(_0x2cab31);})[_0x695c('0x24')]();});}function convertFile(_0xa81491,_0x115682){return new BPromise(function(_0xbffa1c,_0x1ff9ca){var _0x28362c=sox[_0x695c('0x25')](_0xa81491,_0x115682,{'sampleRate':0x1f40,'format':_0x695c('0x26'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x28362c['on'](_0x695c('0x16'),function(_0x435413){logger[_0x695c('0x16')](_0x695c('0x27'),_0x435413);_0x1ff9ca(_0x435413);});_0x28362c['on'](_0x695c('0x24'),function(){logger[_0x695c('0x1c')](_0x695c('0x28'));_0xbffa1c();});_0x28362c['start']();});}function base64Decode(_0x48514d,_0x111ed0){var _0x3be1ea=path['resolve'](__dirname,_0x695c('0x29'),rs['generate'](0x4)+'.wav');fs[_0x695c('0x2a')](_0x3be1ea,_0x48514d,'base64');return convertFile(_0x3be1ea,_0x111ed0)[_0x695c('0x2b')](function(){try{fs[_0x695c('0x1f')](_0x3be1ea);fs[_0x695c('0x2c')](_0x111ed0,_0x695c('0x2d'));}catch(_0x2dccd5){logger[_0x695c('0x16')](_0x2dccd5);}return _0x111ed0[_0x695c('0x2e')]('.')[0x0];});}function base64Encode(_0x29419d){var _0x2850dc=fs['readFileSync'](_0x29419d);return new Buffer(_0x2850dc)[_0x695c('0x2f')]('base64');}exports[_0x695c('0x30')]=function(_0x5d69f2,_0x57e86f,_0x3bc068,_0x25d01c,_0xc54fe9,_0x14da48){var _0x1856e7,_0x7a607e;var _0x11b7d7='https://texttospeech.googleapis.com/v1/';var _0x3ff1a0=_0x695c('0x31')+_0x5d69f2;var _0x138032={};if(_0x57e86f===_0x695c('0x32')){_0x138032[_0x695c('0x33')]={'ssml':_0x3bc068};}else{_0x138032[_0x695c('0x33')]={'text':_0x3bc068};};_0x138032[_0x695c('0x34')]={'languageCode':_0x25d01c,'ssmlGender':_0xc54fe9};_0x138032[_0x695c('0x35')]={'audioEncoding':_0x695c('0x36'),'sampleRateHertz':0x1f40};var _0x445fef={'url':_0x11b7d7+_0x695c('0x37')+_0x3ff1a0,'method':'POST','json':!![],'body':_0x138032};return rp(_0x445fef)[_0x695c('0x2b')](function(_0x163dc0){return new BPromise(function(_0x2b8fe5,_0x2841d5){_0x1856e7=path[_0x695c('0x38')](__dirname,_0x695c('0x29'),rs[_0x695c('0x39')](0x4)+_0x695c('0x3a'));var _0x27e7a1=Buffer[_0x695c('0x3b')](_0x163dc0[_0x695c('0x3c')],_0x695c('0x3d'));return fs[_0x695c('0x3e')](_0x1856e7,_0x27e7a1,function(_0x218663){_0x2841d5(_0x218663);},function(_0x3d7ea8){fs[_0x695c('0x2c')](_0x1856e7,_0x695c('0x2d'));_0x2b8fe5(_0x3d7ea8);});});})[_0x695c('0x2b')](function(_0x39c5a0){_0x7a607e=path['resolve'](__dirname,'tmp',rs[_0x695c('0x39')](0x4)+'.wav');return convertFile(_0x1856e7,_0x7a607e);})['then'](function(){try{fs['unlink'](_0x1856e7);fs[_0x695c('0x2c')](_0x7a607e,_0x695c('0x2d'));}catch(_0x29fa25){logger[_0x695c('0x16')](_0x29fa25);}logger['info'](_0x695c('0x3f'),_0x7a607e[_0x695c('0x2e')]('.')[0x0]);return _0x7a607e[_0x695c('0x2e')]('.')[0x0];});};exports['googleTTS']=function(_0xeaa767,_0x266184){var _0x16035d='https://translate.google.com/_/TranslateWebserverUi/data/batchexecute';var _0x4ab8a9,_0x5175c6;var _0x2dbea6;var _0x25428c={'encoding':_0x695c('0x40'),'method':_0x695c('0x41'),'timeout':0x2710,'url':_0x16035d,'headers':{'Content-Type':_0x695c('0x42')},'body':_0x695c('0x43')+encodeURIComponent(JSON['stringify']([[[_0x695c('0x44'),JSON[_0x695c('0x45')]([_0xeaa767,_0x266184||'en',![],_0x695c('0x46')]),null,'generic']]]))};return rp(_0x25428c)[_0x695c('0x2b')](function(_0x1b1432){try{_0x2dbea6=eval(_0x1b1432[_0x695c('0x47')](0x5))[0x0][0x2];}catch(_0x2e46c1){throw new Error(_0x695c('0x48')+_0x1b1432[_0x695c('0x23')]);}if(!_0x2dbea6){if(_0xeaa767[_0x695c('0x49')]>0xc8){throw new Error('text\x20length\x20limit\x20reached');}else{throw new Error('lang\x20\x22'+_0x266184+_0x695c('0x4a'));}}try{_0x2dbea6=eval(_0x2dbea6)[0x0];}catch(_0x2a5298){throw new Error(_0x695c('0x48')+res[_0x695c('0x23')]);}const _0xb3e65=_0x2dbea6;_0x4ab8a9=path[_0x695c('0x38')](__dirname,_0x695c('0x29'),rs[_0x695c('0x39')](0x4)+_0x695c('0x3a'));fs['writeFileSync'](_0x4ab8a9,_0x2dbea6,{'encoding':_0x695c('0x3d')});_0x5175c6=path['resolve'](__dirname,_0x695c('0x29'),rs['generate'](0x4)+_0x695c('0x4b'));return convertFile(_0x4ab8a9,_0x5175c6);})[_0x695c('0x2b')](function(){try{fs[_0x695c('0x1f')](_0x4ab8a9);fs[_0x695c('0x2c')](_0x5175c6,_0x695c('0x2d'));}catch(_0x5d2acc){logger[_0x695c('0x16')](_0x5d2acc);}logger[_0x695c('0x1c')](_0x695c('0x4c'),_0x5175c6[_0x695c('0x2e')]('.')[0x0]);return _0x5175c6[_0x695c('0x2e')]('.')[0x0];});};exports[_0x695c('0x4d')]=function(_0x183009,_0x1c0ff8,_0x4d7841){return google[_0x695c('0x4e')](_0x183009,_0x1c0ff8,_0x4d7841)['then'](function(_0x36d213){logger[_0x695c('0x1c')](_0x36d213[_0x695c('0x4f')]);return rp(_0x36d213);})['then'](function(_0x5d9631){var _0x1111da='';var _0x209ec7=0x0;try{fs[_0x695c('0x1f')](_0x183009);}catch(_0x4514fa){logger['error'](_0x4514fa);}if(_0x5d9631&&_0x5d9631[_0x695c('0x50')]){_0x1111da=_0x5d9631['results']['map'](function(_0x4a7a19){return _0x4a7a19&&_0x4a7a19[_0x695c('0x51')][0x0]&&_0x4a7a19[_0x695c('0x51')][0x0]['transcript']?_0x4a7a19['alternatives'][0x0][_0x695c('0x52')]:'';})[_0x695c('0x53')]('\x0a');_0x209ec7=_0x5d9631[_0x695c('0x50')][_0x695c('0x54')](function(_0x1580f2){return _0x1580f2&&_0x1580f2[_0x695c('0x51')][0x0]&&_0x1580f2[_0x695c('0x51')][0x0][_0x695c('0x55')]?_0x1580f2['alternatives'][0x0][_0x695c('0x55')]:0x0;})[_0x695c('0x53')]('\x0a');}logger[_0x695c('0x1c')](_0x695c('0x56'),_0x1111da);logger[_0x695c('0x1c')](_0x695c('0x57'),_0x209ec7);return{'transcript':_0x1111da,'confidence':_0x209ec7};})[_0x695c('0x58')](function(_0x7015f9){logger[_0x695c('0x16')]('[%s]\x20%s:%s',_0x7015f9[_0x695c('0x15')],_0x7015f9[_0x695c('0x59')],_0x7015f9['message']);});};exports['googleDialogflow']=function(_0x5f4129,_0x10043e,_0x165f79,_0xeb3570){var _0x5ba1f6=apiai(_0x10043e,{'language':_0xeb3570||'en'});return _0x5ba1f6[_0x695c('0x5a')](_0x165f79,{'sessionId':md5(_0x5f4129)})[_0x695c('0x2b')](function(_0x766c5c){logger['info'](JSON[_0x695c('0x45')](_0x766c5c));return{'source':_0x766c5c[_0x695c('0x5b')]['source'],'resolvedQuery':_0x766c5c[_0x695c('0x5b')][_0x695c('0x5c')],'action':_0x766c5c[_0x695c('0x5b')][_0x695c('0x5d')],'actionIncomplete':_0x766c5c[_0x695c('0x5b')][_0x695c('0x5e')],'speech':_0x766c5c[_0x695c('0x5b')]['fulfillment']['speech'],'score':_0x766c5c[_0x695c('0x5b')]['score'],'statusCode':_0x766c5c[_0x695c('0x5f')][_0x695c('0x60')],'endConversation':_0x766c5c['result'][_0x695c('0x61')]['endConversation']||![],'isFallbackIntent':_0x766c5c[_0x695c('0x5b')][_0x695c('0x61')][_0x695c('0x62')]};});};exports[_0x695c('0x63')]=function(_0x51b945,_0x14612c,_0x44f5f9,_0x3924e5,_0x368fbe,_0x15d6d6){var _0x2fbec9=_0x3924e5[_0x695c('0x64')](/\\n/g,'\x0a');var _0x4537d8={'credentials':{'private_key':_0x2fbec9,'client_email':_0x44f5f9}};var _0x1bcbb9=new dialogflow[(_0x695c('0x65'))](_0x4537d8);var _0x277547=_0x1bcbb9[_0x695c('0x66')](_0x14612c,md5(_0x51b945));var _0x3c718e={'session':_0x277547,'queryInput':{'text':{'text':_0x368fbe,'languageCode':_0x15d6d6}}};return _0x1bcbb9[_0x695c('0x67')](_0x3c718e)[_0x695c('0x2b')](function(_0x3f52a8){if(_0x3f52a8&&_0x3f52a8[_0x695c('0x49')]){logger[_0x695c('0x1c')]('Dialogflow\x20V2\x20response:',JSON[_0x695c('0x45')](_0x3f52a8));var _0x1a13be=_0x3f52a8[0x0];if(_0x1a13be){var _0x4254c9={'action':_0x1a13be[_0x695c('0x68')][_0x695c('0x5d')],'allRequiredParamsPresent':_0x1a13be[_0x695c('0x68')][_0x695c('0x69')],'endConversation':_0x1a13be[_0x695c('0x68')][_0x695c('0x6a')]&&_0x1a13be[_0x695c('0x68')][_0x695c('0x6a')][_0x695c('0x6b')]?_0x1a13be[_0x695c('0x68')][_0x695c('0x6a')][_0x695c('0x6b')][_0x695c('0x6c')][_0x695c('0x6d')]:![],'fulfillmentText':_0x1a13be[_0x695c('0x68')][_0x695c('0x6e')],'intentName':_0x1a13be[_0x695c('0x68')][_0x695c('0x6f')]['name'],'intentDisplayName':_0x1a13be[_0x695c('0x68')][_0x695c('0x6f')][_0x695c('0x70')],'isFallbackIntent':_0x1a13be[_0x695c('0x68')][_0x695c('0x6f')][_0x695c('0x71')],'languageCode':_0x1a13be['queryResult']['languageCode'],'queryText':_0x1a13be['queryResult'][_0x695c('0x72')],'responseId':_0x1a13be[_0x695c('0x73')],'score':_0x1a13be[_0x695c('0x68')][_0x695c('0x74')],'speech':_0x1a13be[_0x695c('0x68')][_0x695c('0x6e')],'providerResponse':JSON['stringify'](_0x3f52a8)};return _0x4254c9;}}});};exports[_0x695c('0x75')]=function(_0x20d175,_0x280b45,_0x2c24d9,_0xb05a3){return tilde[_0x695c('0x4e')](_0x20d175,_0x280b45,_0x2c24d9,_0xb05a3)[_0x695c('0x2b')](function(_0x2fad99){try{fs[_0x695c('0x1f')](_0x20d175);}catch(_0x579835){logger[_0x695c('0x16')](_0x579835);}logger[_0x695c('0x1c')](_0x695c('0x76'),_0x2fad99[_0x695c('0x52')]);logger['info'](_0x695c('0x77'),_0x2fad99['confidence']);return _0x2fad99;});};exports['awsPolly']=function(_0x118c4d,_0x1afe4b,_0x2557a5,_0x5b08fa,_0x45f9cf,_0xe1a21e){var _0x4bac12=path[_0x695c('0x38')](__dirname,_0x695c('0x29'),rs['generate'](0x4)+_0x695c('0x3a'));var _0x356265;return aws[_0x695c('0x78')](_0x118c4d,_0x1afe4b,_0x2557a5,_0x5b08fa,_0x45f9cf,_0xe1a21e,_0x4bac12)[_0x695c('0x2b')](function(){_0x356265=path[_0x695c('0x38')](__dirname,_0x695c('0x29'),rs[_0x695c('0x39')](0x4)+_0x695c('0x4b'));return convertFile(_0x4bac12,_0x356265);})['then'](function(){try{fs['unlink'](_0x4bac12);fs[_0x695c('0x2c')](_0x356265,_0x695c('0x2d'));}catch(_0x40c974){logger['error'](_0x40c974);}return _0x356265[_0x695c('0x2e')]('.')[0x0];});};exports[_0x695c('0x79')]=function(_0x5d47c2,_0x3f9edd,_0x324ac2,_0x842b8b,_0x49c3b4,_0x35c93c){return aws['lex'](_0x5d47c2,_0x3f9edd,_0x324ac2,_0x842b8b,_0x49c3b4,_0x35c93c)['then'](function(_0x481465){logger[_0x695c('0x1c')](JSON[_0x695c('0x45')](_0x481465));return _0x481465;});};exports[_0x695c('0x7a')]=function(_0x410e75,_0x28f2db,_0x2457d6,_0x2960d8){var _0x300878=util['format'](_0x695c('0x7b'),ISPEECH_API,_0x410e75,encodeURIComponent(_0x28f2db),_0x2457d6,_0x2960d8);var _0x286b69=path[_0x695c('0x38')](__dirname,'tmp',rs['generate'](0x4)+'.mp3');var _0x3d64cc;return downloadFile(_0x300878,_0x286b69)['then'](function(){_0x3d64cc=path[_0x695c('0x38')](__dirname,_0x695c('0x29'),rs[_0x695c('0x39')](0x4)+'.wav');return convertFile(_0x286b69,_0x3d64cc);})[_0x695c('0x2b')](function(){try{fs[_0x695c('0x1f')](_0x286b69);fs[_0x695c('0x2c')](_0x3d64cc,_0x695c('0x2d'));}catch(_0x50de99){logger[_0x695c('0x16')](_0x50de99);}return _0x3d64cc[_0x695c('0x2e')]('.')[0x0];});};exports[_0x695c('0x7c')]=function(_0x174edb,_0x390e33,_0x12b973,_0x5f219f){var _0x14d70a=util[_0x695c('0x7d')](_0x695c('0x7e'),ISPEECH_API,_0x390e33,_0x12b973,base64Encode(_0x174edb),_0x5f219f?util['format'](_0x695c('0x7f'),encodeURIComponent(_0x5f219f)):_0x695c('0x80'));return uploadFile(_0x14d70a)['then'](function(_0x6610cf){return querystring[_0x695c('0xb')](_0x6610cf);});};exports['sestekNDA']=function(_0x12d22f,_0x390eb4){return sestek['nda'](_0x12d22f,_0x390eb4)['then'](function(_0x50e9f9){logger[_0x695c('0x1c')](JSON[_0x695c('0x45')](_0x50e9f9));var _0x316f89={'speech':_0x50e9f9[_0x695c('0x81')],'action':_0x50e9f9[_0x695c('0x82')]?_0x50e9f9[_0x695c('0x82')]['CustomAction']:undefined,'actiondata':_0x50e9f9['channelData']?_0x50e9f9[_0x695c('0x82')][_0x695c('0x83')]:undefined};if(_0x50e9f9[_0x695c('0x82')]&&_0x50e9f9[_0x695c('0x82')]['AudioFromTts']){return base64Decode(_0x50e9f9[_0x695c('0x82')][_0x695c('0x84')][_0x695c('0x85')],path[_0x695c('0x38')](__dirname,'tmp',rs[_0x695c('0x39')](0x4)+_0x695c('0x4b')))[_0x695c('0x2b')](function(_0x29b52a){_0x316f89[_0x695c('0x86')]=_0x29b52a;return _0x316f89;});}return _0x316f89;});};exports[_0x695c('0x87')]=function(){return __dirname;};