Built motion from commit (unavailable).|2.5.4
[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 _0xf250=['freeform=3','sestekNDA','text','channelData','CustomAction','AudioFromTts','Data','audiofile','dirName','http','parse','bluebird','randomstring','util','querystring','apiai-promise','./google','./sestek','../../../config/logger','agi','https://api.ispeech.org/api/rest','protocol','https:','host','path','WHAT_EVER','get','statusCode','error','request\x20to\x20','\x20failed,\x20status\x20code\x20=\x20','statusMessage','info','finish\x20download\x20file','close','unlink','pipe','end','data','transcode','wav','error\x20convert','finish\x20to\x20convert','start','tmp','generate','.wav','writeFileSync','base64','then','chmodSync','777','toString','https://texttospeech.googleapis.com/v1/','?key=','ssml','input','voice','text:synthesize','POST','resolve','.mp3','from','writeFile','split','googleTTS','goggleTTS\x20path','googleASR','asr','results','map','alternatives','transcript','join','confidence','catch','[%s]\x20%s:%s','googleDialogflow','stringify','result','resolvedQuery','action','actionIncomplete','fulfillment','status','code','endConversation','metadata','isFallbackIntent','tildeASR','Tilde\x20ASR\x20transcription','Tilde\x20ASR\x20confidence','awsPolly','polly','awsLex','ispeechTTS','format','%s?apikey=%s&action=convert&text=%s&voice=%s&speed=%s&format=mp3','ispeechASR','model=%s'];(function(_0x4b8435,_0x442eab){var _0x3aac28=function(_0x327f04){while(--_0x327f04){_0x4b8435['push'](_0x4b8435['shift']());}};_0x3aac28(++_0x442eab);}(_0xf250,0x135));var _0x0f25=function(_0x3ccdb0,_0x4adc30){_0x3ccdb0=_0x3ccdb0-0x0;var _0x58aa5b=_0xf250[_0x3ccdb0];return _0x58aa5b;};'use strict';var fs=require('fs');var path=require('path');var http=require(_0x0f25('0x0'));var https=require('https');var urlParse=require('url')[_0x0f25('0x1')];var BPromise=require(_0x0f25('0x2'));var rs=require(_0x0f25('0x3'));var sox=require('sox');var rp=require('request-promise');var util=require(_0x0f25('0x4'));var querystring=require(_0x0f25('0x5'));var tts=require('google-tts-api');var apiai=require(_0x0f25('0x6'));var md5=require('md5');var google=require(_0x0f25('0x7'));var aws=require('./aws');var tilde=require('./tilde');var sestek=require(_0x0f25('0x8'));var logger=require(_0x0f25('0x9'))(_0x0f25('0xa'));var ISPEECH_API=_0x0f25('0xb');function downloadFile(_0xe4a5ae,_0x20ac2a){return new BPromise(function(_0x537cf3,_0x11b72d){var _0x5e6ea1=urlParse(_0xe4a5ae);var _0x46dc0a=_0x5e6ea1[_0x0f25('0xc')]===_0x0f25('0xd')?https:http;var _0x30dcf3={'host':_0x5e6ea1[_0x0f25('0xe')],'path':_0x5e6ea1[_0x0f25('0xf')],'headers':{'user-agent':_0x0f25('0x10')}};_0x46dc0a[_0x0f25('0x11')](_0x30dcf3,function(_0x545895){if(_0x545895[_0x0f25('0x12')]!==0xc8){logger[_0x0f25('0x13')](_0x0f25('0x14')+_0xe4a5ae+_0x0f25('0x15')+_0x545895[_0x0f25('0x12')]+'\x20('+_0x545895[_0x0f25('0x16')]+')');_0x11b72d(new Error(_0x0f25('0x14')+_0xe4a5ae+'\x20failed,\x20status\x20code\x20=\x20'+_0x545895[_0x0f25('0x12')]+'\x20('+_0x545895[_0x0f25('0x16')]+')'));return;}var _0x11a7cf=fs['createWriteStream'](_0x20ac2a);_0x11a7cf['on']('finish',function(){logger[_0x0f25('0x17')](_0x0f25('0x18'));_0x11a7cf[_0x0f25('0x19')](_0x537cf3);});_0x11a7cf['on']('error',function(_0x122ab1){fs[_0x0f25('0x1a')](_0x20ac2a);logger['error']('download\x20error',_0x122ab1);_0x11b72d(_0x122ab1);});_0x545895[_0x0f25('0x1b')](_0x11a7cf);})['on']('error',function(_0x4b8b9e){logger['error'](_0x4b8b9e);_0x11b72d(_0x4b8b9e);})[_0x0f25('0x1c')]();});}function uploadFile(_0x1d3399){return new BPromise(function(_0x2fe703,_0x2c0366){var _0x523dcf=urlParse(_0x1d3399);var _0x23af9f=_0x523dcf['protocol']===_0x0f25('0xd')?https:http;var _0x666d11={'host':_0x523dcf['host'],'path':_0x523dcf[_0x0f25('0xf')],'headers':{'user-agent':_0x0f25('0x10')}};_0x23af9f[_0x0f25('0x11')](_0x666d11,function(_0x333e05){if(_0x333e05[_0x0f25('0x12')]!==0xc8){_0x2c0366(new Error(_0x0f25('0x14')+_0x1d3399+_0x0f25('0x15')+_0x333e05[_0x0f25('0x12')]+'\x20('+_0x333e05[_0x0f25('0x16')]+')'));return;}var _0x18bb90='';_0x333e05['on'](_0x0f25('0x1d'),function(_0x27a191){_0x18bb90+=_0x27a191;});_0x333e05['on'](_0x0f25('0x1c'),function(){_0x2fe703(_0x18bb90);});})['on'](_0x0f25('0x13'),function(_0x12cdec){_0x2c0366(_0x12cdec);})[_0x0f25('0x1c')]();});}function convertFile(_0x3e6057,_0x22c934){return new BPromise(function(_0x59f20e,_0x261c9c){var _0x44b839=sox[_0x0f25('0x1e')](_0x3e6057,_0x22c934,{'sampleRate':0x1f40,'format':_0x0f25('0x1f'),'channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x44b839['on']('error',function(_0x259db5){logger[_0x0f25('0x13')](_0x0f25('0x20'),_0x259db5);_0x261c9c(_0x259db5);});_0x44b839['on'](_0x0f25('0x1c'),function(){logger[_0x0f25('0x17')](_0x0f25('0x21'));_0x59f20e();});_0x44b839[_0x0f25('0x22')]();});}function base64Decode(_0x19bd8f,_0x2c43e7){var _0xb76631=path['resolve'](__dirname,_0x0f25('0x23'),rs[_0x0f25('0x24')](0x4)+_0x0f25('0x25'));fs[_0x0f25('0x26')](_0xb76631,_0x19bd8f,_0x0f25('0x27'));return convertFile(_0xb76631,_0x2c43e7)[_0x0f25('0x28')](function(){try{fs[_0x0f25('0x1a')](_0xb76631);fs[_0x0f25('0x29')](_0x2c43e7,_0x0f25('0x2a'));}catch(_0x27da15){logger['error'](_0x27da15);}return _0x2c43e7['split']('.')[0x0];});}function base64Encode(_0x261a6f){var _0xafeecf=fs['readFileSync'](_0x261a6f);return new Buffer(_0xafeecf)[_0x0f25('0x2b')]('base64');}exports['googleCloudTTS']=function(_0x132a13,_0x17923a,_0x2c0fc6,_0x4ac456,_0x167c78,_0x59941e){var _0x23a841,_0x1dffcf;var _0x4935af=_0x0f25('0x2c');var _0x274812=_0x0f25('0x2d')+_0x132a13;var _0x9ef54={};if(_0x17923a===_0x0f25('0x2e')){_0x9ef54[_0x0f25('0x2f')]={'ssml':_0x2c0fc6};}else{_0x9ef54[_0x0f25('0x2f')]={'text':_0x2c0fc6};};_0x9ef54[_0x0f25('0x30')]={'languageCode':_0x4ac456,'ssmlGender':_0x167c78};_0x9ef54['audioConfig']={'audioEncoding':'MP3','sampleRateHertz':0x1f40};var _0xeea0a7={'url':_0x4935af+_0x0f25('0x31')+_0x274812,'method':_0x0f25('0x32'),'json':!![],'body':_0x9ef54};return rp(_0xeea0a7)['then'](function(_0x4def12){return new BPromise(function(_0x20c379,_0x58dce4){_0x23a841=path[_0x0f25('0x33')](__dirname,_0x0f25('0x23'),rs[_0x0f25('0x24')](0x4)+_0x0f25('0x34'));var _0x8534f0=Buffer[_0x0f25('0x35')](_0x4def12['audioContent'],_0x0f25('0x27'));return fs[_0x0f25('0x36')](_0x23a841,_0x8534f0,function(_0x4df40d){_0x58dce4(_0x4df40d);},function(_0x2c1a99){fs[_0x0f25('0x29')](_0x23a841,_0x0f25('0x2a'));_0x20c379(_0x2c1a99);});});})[_0x0f25('0x28')](function(_0xb4ef7b){_0x1dffcf=path[_0x0f25('0x33')](__dirname,_0x0f25('0x23'),rs[_0x0f25('0x24')](0x4)+_0x0f25('0x25'));return convertFile(_0x23a841,_0x1dffcf);})[_0x0f25('0x28')](function(){try{fs['unlink'](_0x23a841);fs['chmodSync'](_0x1dffcf,'777');}catch(_0x475b4){logger[_0x0f25('0x13')](_0x475b4);}logger[_0x0f25('0x17')]('googleCloudTTS\x20path',_0x1dffcf[_0x0f25('0x37')]('.')[0x0]);return _0x1dffcf['split']('.')[0x0];});};exports[_0x0f25('0x38')]=function(_0x122e60,_0x8fb8ef){var _0x326bc8,_0x2ab44a;return tts(_0x122e60,_0x8fb8ef||'en',0x1)[_0x0f25('0x28')](function(_0x59e785){logger['info'](_0x59e785);_0x326bc8=path[_0x0f25('0x33')](__dirname,_0x0f25('0x23'),rs[_0x0f25('0x24')](0x4)+'.mp3');return downloadFile(_0x59e785,_0x326bc8);})['then'](function(){_0x2ab44a=path[_0x0f25('0x33')](__dirname,_0x0f25('0x23'),rs[_0x0f25('0x24')](0x4)+_0x0f25('0x25'));return convertFile(_0x326bc8,_0x2ab44a);})[_0x0f25('0x28')](function(){try{fs[_0x0f25('0x1a')](_0x326bc8);fs[_0x0f25('0x29')](_0x2ab44a,'777');}catch(_0x41c516){logger[_0x0f25('0x13')](_0x41c516);}logger[_0x0f25('0x17')](_0x0f25('0x39'),_0x2ab44a['split']('.')[0x0]);return _0x2ab44a[_0x0f25('0x37')]('.')[0x0];});};exports[_0x0f25('0x3a')]=function(_0x42427a,_0x3a8bc1,_0x3b8166){return google[_0x0f25('0x3b')](_0x42427a,_0x3a8bc1,_0x3b8166)[_0x0f25('0x28')](function(_0x2c022c){logger[_0x0f25('0x17')](_0x2c022c['uri']);return rp(_0x2c022c);})['then'](function(_0x4c303f){var _0x54da78='';var _0xcdd6e=0x0;try{fs[_0x0f25('0x1a')](_0x42427a);}catch(_0x2b8d7f){logger[_0x0f25('0x13')](_0x2b8d7f);}if(_0x4c303f&&_0x4c303f[_0x0f25('0x3c')]){_0x54da78=_0x4c303f['results'][_0x0f25('0x3d')](function(_0x3a1192){return _0x3a1192&&_0x3a1192[_0x0f25('0x3e')][0x0]&&_0x3a1192[_0x0f25('0x3e')][0x0]['transcript']?_0x3a1192[_0x0f25('0x3e')][0x0][_0x0f25('0x3f')]:'';})[_0x0f25('0x40')]('\x0a');_0xcdd6e=_0x4c303f['results'][_0x0f25('0x3d')](function(_0x4c9a95){return _0x4c9a95&&_0x4c9a95[_0x0f25('0x3e')][0x0]&&_0x4c9a95[_0x0f25('0x3e')][0x0]['confidence']?_0x4c9a95['alternatives'][0x0][_0x0f25('0x41')]:0x0;})['join']('\x0a');}logger['info']('googleASR\x20transcription',_0x54da78);logger['info']('googleASR\x20confidence',_0xcdd6e);return{'transcript':_0x54da78,'confidence':_0xcdd6e};})[_0x0f25('0x42')](function(_0xae8b8){logger['error'](_0x0f25('0x43'),_0xae8b8[_0x0f25('0x12')],_0xae8b8['name'],_0xae8b8['message']);});};exports[_0x0f25('0x44')]=function(_0x1cf922,_0x16cf4b,_0x3bd3c8,_0x46fc68){var _0x411cd5=apiai(_0x16cf4b,{'language':_0x46fc68||'en'});return _0x411cd5['textRequest'](_0x3bd3c8,{'sessionId':md5(_0x1cf922)})[_0x0f25('0x28')](function(_0x197a7b){logger[_0x0f25('0x17')](JSON[_0x0f25('0x45')](_0x197a7b));return{'source':_0x197a7b[_0x0f25('0x46')]['source'],'resolvedQuery':_0x197a7b[_0x0f25('0x46')][_0x0f25('0x47')],'action':_0x197a7b[_0x0f25('0x46')][_0x0f25('0x48')],'actionIncomplete':_0x197a7b[_0x0f25('0x46')][_0x0f25('0x49')],'speech':_0x197a7b[_0x0f25('0x46')][_0x0f25('0x4a')]['speech'],'score':_0x197a7b[_0x0f25('0x46')]['score'],'statusCode':_0x197a7b[_0x0f25('0x4b')][_0x0f25('0x4c')],'endConversation':_0x197a7b[_0x0f25('0x46')]['metadata'][_0x0f25('0x4d')]||![],'isFallbackIntent':_0x197a7b[_0x0f25('0x46')][_0x0f25('0x4e')][_0x0f25('0x4f')]};});};exports[_0x0f25('0x50')]=function(_0x45478f,_0x34cc4a,_0x328b0d,_0x2e28a9){return tilde['asr'](_0x45478f,_0x34cc4a,_0x328b0d,_0x2e28a9)['then'](function(_0x5b4b31){try{fs[_0x0f25('0x1a')](_0x45478f);}catch(_0xaed1c7){logger[_0x0f25('0x13')](_0xaed1c7);}logger[_0x0f25('0x17')](_0x0f25('0x51'),_0x5b4b31['transcript']);logger['info'](_0x0f25('0x52'),_0x5b4b31[_0x0f25('0x41')]);return _0x5b4b31;});};exports[_0x0f25('0x53')]=function(_0x2adf77,_0x3d689a,_0x31dcc2,_0x4526b3,_0x385074,_0x2b7aee){var _0x11e2db=path['resolve'](__dirname,'tmp',rs[_0x0f25('0x24')](0x4)+_0x0f25('0x34'));var _0x53afae;return aws[_0x0f25('0x54')](_0x2adf77,_0x3d689a,_0x31dcc2,_0x4526b3,_0x385074,_0x2b7aee,_0x11e2db)[_0x0f25('0x28')](function(){_0x53afae=path[_0x0f25('0x33')](__dirname,_0x0f25('0x23'),rs[_0x0f25('0x24')](0x4)+_0x0f25('0x25'));return convertFile(_0x11e2db,_0x53afae);})[_0x0f25('0x28')](function(){try{fs[_0x0f25('0x1a')](_0x11e2db);fs[_0x0f25('0x29')](_0x53afae,_0x0f25('0x2a'));}catch(_0x51e425){logger[_0x0f25('0x13')](_0x51e425);}return _0x53afae[_0x0f25('0x37')]('.')[0x0];});};exports[_0x0f25('0x55')]=function(_0x35e93b,_0x255544,_0x3141ad,_0x8fe7da,_0x2f8870,_0x5f3924){return aws['lex'](_0x35e93b,_0x255544,_0x3141ad,_0x8fe7da,_0x2f8870,_0x5f3924)['then'](function(_0x130f10){logger[_0x0f25('0x17')](JSON['stringify'](_0x130f10));return _0x130f10;});};exports[_0x0f25('0x56')]=function(_0x64ca26,_0xf643a5,_0x7170dc,_0x4b7f8f){var _0x57e059=util[_0x0f25('0x57')](_0x0f25('0x58'),ISPEECH_API,_0x64ca26,encodeURIComponent(_0xf643a5),_0x7170dc,_0x4b7f8f);var _0x2237fd=path[_0x0f25('0x33')](__dirname,_0x0f25('0x23'),rs[_0x0f25('0x24')](0x4)+'.mp3');var _0x2701e6;return downloadFile(_0x57e059,_0x2237fd)[_0x0f25('0x28')](function(){_0x2701e6=path['resolve'](__dirname,_0x0f25('0x23'),rs[_0x0f25('0x24')](0x4)+_0x0f25('0x25'));return convertFile(_0x2237fd,_0x2701e6);})[_0x0f25('0x28')](function(){try{fs[_0x0f25('0x1a')](_0x2237fd);fs[_0x0f25('0x29')](_0x2701e6,_0x0f25('0x2a'));}catch(_0x3aa277){logger['error'](_0x3aa277);}return _0x2701e6[_0x0f25('0x37')]('.')[0x0];});};exports[_0x0f25('0x59')]=function(_0x75b169,_0x23b21f,_0x2206d9,_0x5c4f10){var _0xaa1578=util['format']('%s?apikey=%s&action=recognize&locale=%s&content-type=audio/x-wav&audio=%s&%s',ISPEECH_API,_0x23b21f,_0x2206d9,base64Encode(_0x75b169),_0x5c4f10?util[_0x0f25('0x57')](_0x0f25('0x5a'),encodeURIComponent(_0x5c4f10)):_0x0f25('0x5b'));return uploadFile(_0xaa1578)['then'](function(_0x18b95e){return querystring[_0x0f25('0x1')](_0x18b95e);});};exports[_0x0f25('0x5c')]=function(_0x5688df,_0x310c6c){return sestek['nda'](_0x5688df,_0x310c6c)[_0x0f25('0x28')](function(_0x4b156e){logger[_0x0f25('0x17')](JSON['stringify'](_0x4b156e));var _0x22c0ff={'speech':_0x4b156e[_0x0f25('0x5d')],'action':_0x4b156e[_0x0f25('0x5e')]?_0x4b156e[_0x0f25('0x5e')][_0x0f25('0x5f')]:undefined,'actiondata':_0x4b156e[_0x0f25('0x5e')]?_0x4b156e[_0x0f25('0x5e')]['CustomActionData']:undefined};if(_0x4b156e['channelData']&&_0x4b156e['channelData'][_0x0f25('0x60')]){return base64Decode(_0x4b156e['channelData']['AudioFromTts'][_0x0f25('0x61')],path[_0x0f25('0x33')](__dirname,'tmp',rs[_0x0f25('0x24')](0x4)+_0x0f25('0x25')))[_0x0f25('0x28')](function(_0x326f24){_0x22c0ff[_0x0f25('0x62')]=_0x326f24;return _0x22c0ff;});}return _0x22c0ff;});};exports[_0x0f25('0x63')]=function(){return __dirname;};