Built motion from commit (unavailable).|2.5.0
[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 _0x27c9=['catch','[%s]\x20%s:%s','name','message','googleDialogflow','result','source','resolvedQuery','action','actionIncomplete','fulfillment','speech','status','metadata','endConversation','isFallbackIntent','tildeASR','Tilde\x20ASR\x20transcription','Tilde\x20ASR\x20confidence','awsPolly','polly','awsLex','stringify','ispeechTTS','format','ispeechASR','%s?apikey=%s&action=recognize&locale=%s&content-type=audio/x-wav&audio=%s&%s','model=%s','freeform=3','text','channelData','CustomAction','CustomActionData','AudioFromTts','Data','path','http','https','parse','bluebird','randomstring','sox','request-promise','util','querystring','google-tts-api','apiai-promise','./google','./aws','./tilde','./sestek','../../../config/logger','agi','https://api.ispeech.org/api/rest','protocol','https:','host','WHAT_EVER','get','statusCode','error','request\x20to\x20','\x20failed,\x20status\x20code\x20=\x20','statusMessage','createWriteStream','finish','finish\x20download\x20file','close','unlink','download\x20error','pipe','end','data','error\x20convert','info','finish\x20to\x20convert','start','resolve','tmp','generate','.wav','writeFileSync','base64','then','chmodSync','777','split','readFileSync','toString','https://texttospeech.googleapis.com/v1/','?key=','ssml','input','voice','audioConfig','MP3','POST','.mp3','from','audioContent','writeFile','googleCloudTTS\x20path','googleTTS','goggleTTS\x20path','googleASR','asr','map','alternatives','transcript','join','results','confidence','googleASR\x20confidence'];(function(_0x14add3,_0x13ce8d){var _0x4a174f=function(_0x2b3ed7){while(--_0x2b3ed7){_0x14add3['push'](_0x14add3['shift']());}};_0x4a174f(++_0x13ce8d);}(_0x27c9,0x1e7));var _0x927c=function(_0x465028,_0x4570ac){_0x465028=_0x465028-0x0;var _0xb783d4=_0x27c9[_0x465028];return _0xb783d4;};'use strict';var fs=require('fs');var path=require(_0x927c('0x0'));var http=require(_0x927c('0x1'));var https=require(_0x927c('0x2'));var urlParse=require('url')[_0x927c('0x3')];var BPromise=require(_0x927c('0x4'));var rs=require(_0x927c('0x5'));var sox=require(_0x927c('0x6'));var rp=require(_0x927c('0x7'));var util=require(_0x927c('0x8'));var querystring=require(_0x927c('0x9'));var tts=require(_0x927c('0xa'));var apiai=require(_0x927c('0xb'));var md5=require('md5');var google=require(_0x927c('0xc'));var aws=require(_0x927c('0xd'));var tilde=require(_0x927c('0xe'));var sestek=require(_0x927c('0xf'));var logger=require(_0x927c('0x10'))(_0x927c('0x11'));var ISPEECH_API=_0x927c('0x12');function downloadFile(_0x53608a,_0x1bbf19){return new BPromise(function(_0x413876,_0x29839f){var _0x37be84=urlParse(_0x53608a);var _0x7a4fbd=_0x37be84[_0x927c('0x13')]===_0x927c('0x14')?https:http;var _0x24b575={'host':_0x37be84[_0x927c('0x15')],'path':_0x37be84[_0x927c('0x0')],'headers':{'user-agent':_0x927c('0x16')}};_0x7a4fbd[_0x927c('0x17')](_0x24b575,function(_0x2bb0ae){if(_0x2bb0ae[_0x927c('0x18')]!==0xc8){logger[_0x927c('0x19')](_0x927c('0x1a')+_0x53608a+_0x927c('0x1b')+_0x2bb0ae[_0x927c('0x18')]+'\x20('+_0x2bb0ae[_0x927c('0x1c')]+')');_0x29839f(new Error(_0x927c('0x1a')+_0x53608a+_0x927c('0x1b')+_0x2bb0ae[_0x927c('0x18')]+'\x20('+_0x2bb0ae['statusMessage']+')'));return;}var _0x4e60b6=fs[_0x927c('0x1d')](_0x1bbf19);_0x4e60b6['on'](_0x927c('0x1e'),function(){logger['info'](_0x927c('0x1f'));_0x4e60b6[_0x927c('0x20')](_0x413876);});_0x4e60b6['on'](_0x927c('0x19'),function(_0x2b9008){fs[_0x927c('0x21')](_0x1bbf19);logger[_0x927c('0x19')](_0x927c('0x22'),_0x2b9008);_0x29839f(_0x2b9008);});_0x2bb0ae[_0x927c('0x23')](_0x4e60b6);})['on'](_0x927c('0x19'),function(_0x5a46d4){logger[_0x927c('0x19')](_0x5a46d4);_0x29839f(_0x5a46d4);})[_0x927c('0x24')]();});}function uploadFile(_0x574405){return new BPromise(function(_0x450f0b,_0xeac4c9){var _0x57c762=urlParse(_0x574405);var _0x451f05=_0x57c762[_0x927c('0x13')]===_0x927c('0x14')?https:http;var _0x2e56a9={'host':_0x57c762['host'],'path':_0x57c762[_0x927c('0x0')],'headers':{'user-agent':_0x927c('0x16')}};_0x451f05[_0x927c('0x17')](_0x2e56a9,function(_0x13bdde){if(_0x13bdde[_0x927c('0x18')]!==0xc8){_0xeac4c9(new Error('request\x20to\x20'+_0x574405+_0x927c('0x1b')+_0x13bdde[_0x927c('0x18')]+'\x20('+_0x13bdde[_0x927c('0x1c')]+')'));return;}var _0x35f9ba='';_0x13bdde['on'](_0x927c('0x25'),function(_0xde7048){_0x35f9ba+=_0xde7048;});_0x13bdde['on'](_0x927c('0x24'),function(){_0x450f0b(_0x35f9ba);});})['on'](_0x927c('0x19'),function(_0x3e89a9){_0xeac4c9(_0x3e89a9);})['end']();});}function convertFile(_0x5dd7be,_0x32670f){return new BPromise(function(_0x10b11b,_0x5bb058){var _0x37eac7=sox['transcode'](_0x5dd7be,_0x32670f,{'sampleRate':0x1f40,'format':'wav','channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x37eac7['on'](_0x927c('0x19'),function(_0x524a90){logger[_0x927c('0x19')](_0x927c('0x26'),_0x524a90);_0x5bb058(_0x524a90);});_0x37eac7['on']('end',function(){logger[_0x927c('0x27')](_0x927c('0x28'));_0x10b11b();});_0x37eac7[_0x927c('0x29')]();});}function base64Decode(_0x367d94,_0x15985b){var _0x1e4c13=path[_0x927c('0x2a')](__dirname,_0x927c('0x2b'),rs[_0x927c('0x2c')](0x4)+_0x927c('0x2d'));fs[_0x927c('0x2e')](_0x1e4c13,_0x367d94,_0x927c('0x2f'));return convertFile(_0x1e4c13,_0x15985b)[_0x927c('0x30')](function(){try{fs[_0x927c('0x21')](_0x1e4c13);fs[_0x927c('0x31')](_0x15985b,_0x927c('0x32'));}catch(_0x42b005){logger[_0x927c('0x19')](_0x42b005);}return _0x15985b[_0x927c('0x33')]('.')[0x0];});}function base64Encode(_0x38ae98){var _0x463d58=fs[_0x927c('0x34')](_0x38ae98);return new Buffer(_0x463d58)[_0x927c('0x35')](_0x927c('0x2f'));}exports['googleCloudTTS']=function(_0x17f0cb,_0x5cc176,_0x230d6d,_0x413d64,_0x408183,_0x49eb28){var _0x44510c,_0x253839;var _0x2c9bc9=_0x927c('0x36');var _0x47929=_0x927c('0x37')+_0x17f0cb;var _0x13b3a3={};if(_0x5cc176===_0x927c('0x38')){_0x13b3a3[_0x927c('0x39')]={'ssml':_0x230d6d};}else{_0x13b3a3[_0x927c('0x39')]={'text':_0x230d6d};};_0x13b3a3[_0x927c('0x3a')]={'languageCode':_0x413d64,'ssmlGender':_0x408183};_0x13b3a3[_0x927c('0x3b')]={'audioEncoding':_0x927c('0x3c'),'sampleRateHertz':0x1f40};var _0x3d9440={'url':_0x2c9bc9+'text:synthesize'+_0x47929,'method':_0x927c('0x3d'),'json':!![],'body':_0x13b3a3};return rp(_0x3d9440)[_0x927c('0x30')](function(_0x234cfc){return new BPromise(function(_0x3c5655,_0xbef4a3){_0x44510c=path[_0x927c('0x2a')](__dirname,_0x927c('0x2b'),rs[_0x927c('0x2c')](0x4)+_0x927c('0x3e'));var _0x36bf63=Buffer[_0x927c('0x3f')](_0x234cfc[_0x927c('0x40')],_0x927c('0x2f'));return fs[_0x927c('0x41')](_0x44510c,_0x36bf63,function(_0x4341fd){_0xbef4a3(_0x4341fd);},function(_0x2c36c6){fs[_0x927c('0x31')](_0x44510c,'777');_0x3c5655(_0x2c36c6);});});})[_0x927c('0x30')](function(_0x2014c6){_0x253839=path[_0x927c('0x2a')](__dirname,_0x927c('0x2b'),rs[_0x927c('0x2c')](0x4)+'.wav');return convertFile(_0x44510c,_0x253839);})['then'](function(){try{fs[_0x927c('0x21')](_0x44510c);fs['chmodSync'](_0x253839,'777');}catch(_0x179b8d){logger[_0x927c('0x19')](_0x179b8d);}logger['info'](_0x927c('0x42'),_0x253839[_0x927c('0x33')]('.')[0x0]);return _0x253839[_0x927c('0x33')]('.')[0x0];});};exports[_0x927c('0x43')]=function(_0x5c9c4b,_0x1e2e5f){var _0x5db3ed,_0xef7c01;return tts(_0x5c9c4b,_0x1e2e5f||'en',0x1)['then'](function(_0x3692d1){logger['info'](_0x3692d1);_0x5db3ed=path[_0x927c('0x2a')](__dirname,_0x927c('0x2b'),rs['generate'](0x4)+_0x927c('0x3e'));return downloadFile(_0x3692d1,_0x5db3ed);})[_0x927c('0x30')](function(){_0xef7c01=path['resolve'](__dirname,'tmp',rs[_0x927c('0x2c')](0x4)+_0x927c('0x2d'));return convertFile(_0x5db3ed,_0xef7c01);})[_0x927c('0x30')](function(){try{fs[_0x927c('0x21')](_0x5db3ed);fs[_0x927c('0x31')](_0xef7c01,_0x927c('0x32'));}catch(_0x37efbe){logger[_0x927c('0x19')](_0x37efbe);}logger[_0x927c('0x27')](_0x927c('0x44'),_0xef7c01[_0x927c('0x33')]('.')[0x0]);return _0xef7c01['split']('.')[0x0];});};exports[_0x927c('0x45')]=function(_0x38b0df,_0xd72e77,_0x14250b){return google[_0x927c('0x46')](_0x38b0df,_0xd72e77,_0x14250b)['then'](function(_0x49c89b){logger[_0x927c('0x27')](_0x49c89b['uri']);return rp(_0x49c89b);})[_0x927c('0x30')](function(_0x506def){var _0x33cd84='';var _0x972d81=0x0;try{fs[_0x927c('0x21')](_0x38b0df);}catch(_0x514af3){logger[_0x927c('0x19')](_0x514af3);}if(_0x506def&&_0x506def['results']){_0x33cd84=_0x506def['results'][_0x927c('0x47')](function(_0x1d2144){return _0x1d2144&&_0x1d2144['alternatives'][0x0]&&_0x1d2144[_0x927c('0x48')][0x0][_0x927c('0x49')]?_0x1d2144['alternatives'][0x0][_0x927c('0x49')]:'';})[_0x927c('0x4a')]('\x0a');_0x972d81=_0x506def[_0x927c('0x4b')][_0x927c('0x47')](function(_0xc40a0c){return _0xc40a0c&&_0xc40a0c[_0x927c('0x48')][0x0]&&_0xc40a0c['alternatives'][0x0][_0x927c('0x4c')]?_0xc40a0c[_0x927c('0x48')][0x0][_0x927c('0x4c')]:0x0;})[_0x927c('0x4a')]('\x0a');}logger[_0x927c('0x27')]('googleASR\x20transcription',_0x33cd84);logger[_0x927c('0x27')](_0x927c('0x4d'),_0x972d81);return{'transcript':_0x33cd84,'confidence':_0x972d81};})[_0x927c('0x4e')](function(_0x437b71){logger['error'](_0x927c('0x4f'),_0x437b71[_0x927c('0x18')],_0x437b71[_0x927c('0x50')],_0x437b71[_0x927c('0x51')]);});};exports[_0x927c('0x52')]=function(_0x45535a,_0x51471d,_0x2ddc07,_0x2621ba){var _0x22578e=apiai(_0x51471d,{'language':_0x2621ba||'en'});return _0x22578e['textRequest'](_0x2ddc07,{'sessionId':md5(_0x45535a)})[_0x927c('0x30')](function(_0x203893){logger[_0x927c('0x27')](JSON['stringify'](_0x203893));return{'source':_0x203893[_0x927c('0x53')][_0x927c('0x54')],'resolvedQuery':_0x203893['result'][_0x927c('0x55')],'action':_0x203893[_0x927c('0x53')][_0x927c('0x56')],'actionIncomplete':_0x203893['result'][_0x927c('0x57')],'speech':_0x203893[_0x927c('0x53')][_0x927c('0x58')][_0x927c('0x59')],'score':_0x203893[_0x927c('0x53')]['score'],'statusCode':_0x203893[_0x927c('0x5a')]['code'],'endConversation':_0x203893[_0x927c('0x53')][_0x927c('0x5b')][_0x927c('0x5c')]||![],'isFallbackIntent':_0x203893['result'][_0x927c('0x5b')][_0x927c('0x5d')]};});};exports[_0x927c('0x5e')]=function(_0x14bf54,_0x11bcde,_0xa82a9b,_0x306460){return tilde[_0x927c('0x46')](_0x14bf54,_0x11bcde,_0xa82a9b,_0x306460)[_0x927c('0x30')](function(_0x3fe387){try{fs[_0x927c('0x21')](_0x14bf54);}catch(_0x151c9f){logger['error'](_0x151c9f);}logger[_0x927c('0x27')](_0x927c('0x5f'),_0x3fe387[_0x927c('0x49')]);logger[_0x927c('0x27')](_0x927c('0x60'),_0x3fe387[_0x927c('0x4c')]);return _0x3fe387;});};exports[_0x927c('0x61')]=function(_0x50d51e,_0x15b3cb,_0x39226a,_0x3536ce,_0x40f464,_0x5eeea0){var _0xfb9399=path[_0x927c('0x2a')](__dirname,_0x927c('0x2b'),rs['generate'](0x4)+_0x927c('0x3e'));var _0x4a113d;return aws[_0x927c('0x62')](_0x50d51e,_0x15b3cb,_0x39226a,_0x3536ce,_0x40f464,_0x5eeea0,_0xfb9399)['then'](function(){_0x4a113d=path['resolve'](__dirname,'tmp',rs[_0x927c('0x2c')](0x4)+_0x927c('0x2d'));return convertFile(_0xfb9399,_0x4a113d);})[_0x927c('0x30')](function(){try{fs[_0x927c('0x21')](_0xfb9399);fs[_0x927c('0x31')](_0x4a113d,_0x927c('0x32'));}catch(_0x4a3b1c){logger[_0x927c('0x19')](_0x4a3b1c);}return _0x4a113d[_0x927c('0x33')]('.')[0x0];});};exports[_0x927c('0x63')]=function(_0xacf376,_0x5d080b,_0x53f052,_0x89590a,_0x26ec8b,_0x15c7b6){return aws['lex'](_0xacf376,_0x5d080b,_0x53f052,_0x89590a,_0x26ec8b,_0x15c7b6)[_0x927c('0x30')](function(_0x1eb11d){logger['info'](JSON[_0x927c('0x64')](_0x1eb11d));return _0x1eb11d;});};exports[_0x927c('0x65')]=function(_0x47cd8e,_0x5e8583,_0x5379a0,_0x3e1065){var _0x198ebd=util[_0x927c('0x66')]('%s?apikey=%s&action=convert&text=%s&voice=%s&speed=%s&format=mp3',ISPEECH_API,_0x47cd8e,encodeURIComponent(_0x5e8583),_0x5379a0,_0x3e1065);var _0xb150c=path[_0x927c('0x2a')](__dirname,_0x927c('0x2b'),rs['generate'](0x4)+_0x927c('0x3e'));var _0x11a6aa;return downloadFile(_0x198ebd,_0xb150c)['then'](function(){_0x11a6aa=path[_0x927c('0x2a')](__dirname,_0x927c('0x2b'),rs[_0x927c('0x2c')](0x4)+_0x927c('0x2d'));return convertFile(_0xb150c,_0x11a6aa);})[_0x927c('0x30')](function(){try{fs[_0x927c('0x21')](_0xb150c);fs[_0x927c('0x31')](_0x11a6aa,_0x927c('0x32'));}catch(_0x38d0da){logger[_0x927c('0x19')](_0x38d0da);}return _0x11a6aa[_0x927c('0x33')]('.')[0x0];});};exports[_0x927c('0x67')]=function(_0x260d79,_0x1cd07a,_0x5a12b3,_0x1f4c33){var _0x493269=util[_0x927c('0x66')](_0x927c('0x68'),ISPEECH_API,_0x1cd07a,_0x5a12b3,base64Encode(_0x260d79),_0x1f4c33?util['format'](_0x927c('0x69'),encodeURIComponent(_0x1f4c33)):_0x927c('0x6a'));return uploadFile(_0x493269)[_0x927c('0x30')](function(_0x3c3b8){return querystring['parse'](_0x3c3b8);});};exports['sestekNDA']=function(_0x42ffa6,_0x24e3cb){return sestek['nda'](_0x42ffa6,_0x24e3cb)[_0x927c('0x30')](function(_0x2beab6){logger['info'](JSON[_0x927c('0x64')](_0x2beab6));var _0x4665f8={'speech':_0x2beab6[_0x927c('0x6b')],'action':_0x2beab6[_0x927c('0x6c')]?_0x2beab6[_0x927c('0x6c')][_0x927c('0x6d')]:undefined,'actiondata':_0x2beab6[_0x927c('0x6c')]?_0x2beab6[_0x927c('0x6c')][_0x927c('0x6e')]:undefined};if(_0x2beab6[_0x927c('0x6c')]&&_0x2beab6[_0x927c('0x6c')][_0x927c('0x6f')]){return base64Decode(_0x2beab6['channelData'][_0x927c('0x6f')][_0x927c('0x70')],path[_0x927c('0x2a')](__dirname,_0x927c('0x2b'),rs[_0x927c('0x2c')](0x4)+_0x927c('0x2d')))[_0x927c('0x30')](function(_0x188f82){_0x4665f8['audiofile']=_0x188f82;return _0x4665f8;});}return _0x4665f8;});};exports['dirName']=function(){return __dirname;};