52cb96c11f5c0dd1c048c7c75e29d535b0739937
[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 _0x1f77=['request\x20to\x20','statusMessage','\x20failed,\x20status\x20code\x20=\x20','createWriteStream','finish','info','finish\x20download\x20file','close','unlink','download\x20error','https:','host','end','transcode','error\x20convert','start','tmp','generate','.wav','writeFileSync','base64','then','chmodSync','777','split','readFileSync','toString','googleCloudTTS','?key=','ssml','input','audioConfig','MP3','text:synthesize','.mp3','from','audioContent','resolve','googleCloudTTS\x20path','googleTTS','https://translate.google.com/_/TranslateWebserverUi/data/batchexecute','utf8','POST','application/x-www-form-urlencoded','f.req=','stringify','null','generic','parse\x20response\x20failed:\x0a','data','text\x20length\x20limit\x20reached','lang\x20\x22','\x22\x20might\x20not\x20exist','googleASR','asr','results','map','alternatives','transcript','confidence','join','googleASR\x20transcription','googleASR\x20confidence','textRequest','result','source','resolvedQuery','action','actionIncomplete','fulfillment','speech','status','code','metadata','endConversation','isFallbackIntent','googleDialogflowV2','replace','SessionsClient','sessionPath','detectIntent','Dialogflow\x20V2\x20response:','queryResult','allRequiredParamsPresent','diagnosticInfo','fields','end_conversation','fulfillmentText','name','intent','displayName','isFallback','languageCode','queryText','intentDetectionConfidence','tildeASR','Tilde\x20ASR\x20transcription','polly','awsLex','ispeechTTS','format','%s?apikey=%s&action=convert&text=%s&voice=%s&speed=%s&format=mp3','ispeechASR','%s?apikey=%s&action=recognize&locale=%s&content-type=audio/x-wav&audio=%s&%s','model=%s','sestekNDA','nda','text','channelData','CustomActionData','AudioFromTts','audiofile','bluebird','dialogflow','http','https','md5','querystring','request-promise','randomstring','sox','google-tts-api','url','parse','util','./aws','./google','./sestek','./tilde','protocol','path','WHAT_EVER','get','statusCode','error'];(function(_0x31d02e,_0x3588bb){var _0x1da463=function(_0x1dc3fe){while(--_0x1dc3fe){_0x31d02e['push'](_0x31d02e['shift']());}};_0x1da463(++_0x3588bb);}(_0x1f77,0xf7));var _0x71f7=function(_0x2594ef,_0x41f83b){_0x2594ef=_0x2594ef-0x0;var _0x434df0=_0x1f77[_0x2594ef];return _0x434df0;};'use strict';var apiai=require('apiai-promise');var BPromise=require(_0x71f7('0x0'));var dialogflow=require(_0x71f7('0x1'));var fs=require('fs');var http=require(_0x71f7('0x2'));var https=require(_0x71f7('0x3'));var md5=require(_0x71f7('0x4'));var path=require('path');var querystring=require(_0x71f7('0x5'));var rp=require(_0x71f7('0x6'));var rs=require(_0x71f7('0x7'));var sox=require(_0x71f7('0x8'));var tts=require(_0x71f7('0x9'));var urlParse=require(_0x71f7('0xa'))[_0x71f7('0xb')];var util=require(_0x71f7('0xc'));var aws=require(_0x71f7('0xd'));var google=require(_0x71f7('0xe'));var sestek=require(_0x71f7('0xf'));var tilde=require(_0x71f7('0x10'));var logger=require('../../../config/logger')('agi');var ISPEECH_API='https://api.ispeech.org/api/rest';function downloadFile(_0x1345a2,_0x120f73){return new BPromise(function(_0x47b557,_0x277dce){var _0x5e76eb=urlParse(_0x1345a2);var _0x412d0f=_0x5e76eb[_0x71f7('0x11')]==='https:'?https:http;var _0x342d38={'host':_0x5e76eb['host'],'path':_0x5e76eb[_0x71f7('0x12')],'headers':{'user-agent':_0x71f7('0x13')}};_0x412d0f[_0x71f7('0x14')](_0x342d38,function(_0x436c11){if(_0x436c11[_0x71f7('0x15')]!==0xc8){logger[_0x71f7('0x16')](_0x71f7('0x17')+_0x1345a2+'\x20failed,\x20status\x20code\x20=\x20'+_0x436c11[_0x71f7('0x15')]+'\x20('+_0x436c11[_0x71f7('0x18')]+')');_0x277dce(new Error(_0x71f7('0x17')+_0x1345a2+_0x71f7('0x19')+_0x436c11[_0x71f7('0x15')]+'\x20('+_0x436c11['statusMessage']+')'));return;}var _0x2129c4=fs[_0x71f7('0x1a')](_0x120f73);_0x2129c4['on'](_0x71f7('0x1b'),function(){logger[_0x71f7('0x1c')](_0x71f7('0x1d'));_0x2129c4[_0x71f7('0x1e')](_0x47b557);});_0x2129c4['on'](_0x71f7('0x16'),function(_0x42fe74){fs[_0x71f7('0x1f')](_0x120f73);logger[_0x71f7('0x16')](_0x71f7('0x20'),_0x42fe74);_0x277dce(_0x42fe74);});_0x436c11['pipe'](_0x2129c4);})['on'](_0x71f7('0x16'),function(_0x500811){logger[_0x71f7('0x16')](_0x500811);_0x277dce(_0x500811);})['end']();});}function uploadFile(_0xd1ab24){return new BPromise(function(_0x545a2d,_0x148c47){var _0x314060=urlParse(_0xd1ab24);var _0x2d2dc9=_0x314060['protocol']===_0x71f7('0x21')?https:http;var _0x2c3717={'host':_0x314060[_0x71f7('0x22')],'path':_0x314060[_0x71f7('0x12')],'headers':{'user-agent':_0x71f7('0x13')}};_0x2d2dc9['get'](_0x2c3717,function(_0x51d905){if(_0x51d905[_0x71f7('0x15')]!==0xc8){_0x148c47(new Error(_0x71f7('0x17')+_0xd1ab24+_0x71f7('0x19')+_0x51d905[_0x71f7('0x15')]+'\x20('+_0x51d905[_0x71f7('0x18')]+')'));return;}var _0x49e237='';_0x51d905['on']('data',function(_0x42eace){_0x49e237+=_0x42eace;});_0x51d905['on'](_0x71f7('0x23'),function(){_0x545a2d(_0x49e237);});})['on'](_0x71f7('0x16'),function(_0xf5e6e1){_0x148c47(_0xf5e6e1);})[_0x71f7('0x23')]();});}function convertFile(_0x140342,_0x1bdbd1){return new BPromise(function(_0x11ed74,_0x406f4c){var _0x447537=sox[_0x71f7('0x24')](_0x140342,_0x1bdbd1,{'sampleRate':0x1f40,'format':'wav','channelCount':0x1,'bitRate':0xc0*0x400,'compressionQuality':0x5});_0x447537['on'](_0x71f7('0x16'),function(_0x38dee1){logger['error'](_0x71f7('0x25'),_0x38dee1);_0x406f4c(_0x38dee1);});_0x447537['on'](_0x71f7('0x23'),function(){logger[_0x71f7('0x1c')]('finish\x20to\x20convert');_0x11ed74();});_0x447537[_0x71f7('0x26')]();});}function base64Decode(_0x54936f,_0x59cd7b){var _0x518182=path['resolve'](__dirname,_0x71f7('0x27'),rs[_0x71f7('0x28')](0x4)+_0x71f7('0x29'));fs[_0x71f7('0x2a')](_0x518182,_0x54936f,_0x71f7('0x2b'));return convertFile(_0x518182,_0x59cd7b)[_0x71f7('0x2c')](function(){try{fs[_0x71f7('0x1f')](_0x518182);fs[_0x71f7('0x2d')](_0x59cd7b,_0x71f7('0x2e'));}catch(_0x14c189){logger[_0x71f7('0x16')](_0x14c189);}return _0x59cd7b[_0x71f7('0x2f')]('.')[0x0];});}function base64Encode(_0x183dcd){var _0x2b1a61=fs[_0x71f7('0x30')](_0x183dcd);return new Buffer(_0x2b1a61)[_0x71f7('0x31')](_0x71f7('0x2b'));}exports[_0x71f7('0x32')]=function(_0x2be5a5,_0x62e29b,_0x17fbb2,_0x2cd171,_0x3798ee,_0x22626b){var _0x28be22,_0x5b3471;var _0x506fac='https://texttospeech.googleapis.com/v1/';var _0x54d95b=_0x71f7('0x33')+_0x2be5a5;var _0x342b09={};if(_0x62e29b===_0x71f7('0x34')){_0x342b09[_0x71f7('0x35')]={'ssml':_0x17fbb2};}else{_0x342b09[_0x71f7('0x35')]={'text':_0x17fbb2};};_0x342b09['voice']={'languageCode':_0x2cd171,'ssmlGender':_0x3798ee};_0x342b09[_0x71f7('0x36')]={'audioEncoding':_0x71f7('0x37'),'sampleRateHertz':0x1f40};var _0x4e577d={'url':_0x506fac+_0x71f7('0x38')+_0x54d95b,'method':'POST','json':!![],'body':_0x342b09};return rp(_0x4e577d)[_0x71f7('0x2c')](function(_0x5226c9){return new BPromise(function(_0x5bc1bb,_0x52c68d){_0x28be22=path['resolve'](__dirname,_0x71f7('0x27'),rs['generate'](0x4)+_0x71f7('0x39'));var _0x1195fa=Buffer[_0x71f7('0x3a')](_0x5226c9[_0x71f7('0x3b')],_0x71f7('0x2b'));return fs['writeFile'](_0x28be22,_0x1195fa,function(_0x9002e7){_0x52c68d(_0x9002e7);},function(_0x3cf0ed){fs['chmodSync'](_0x28be22,_0x71f7('0x2e'));_0x5bc1bb(_0x3cf0ed);});});})[_0x71f7('0x2c')](function(_0x473687){_0x5b3471=path[_0x71f7('0x3c')](__dirname,_0x71f7('0x27'),rs['generate'](0x4)+_0x71f7('0x29'));return convertFile(_0x28be22,_0x5b3471);})[_0x71f7('0x2c')](function(){try{fs['unlink'](_0x28be22);fs[_0x71f7('0x2d')](_0x5b3471,_0x71f7('0x2e'));}catch(_0x5ce4e1){logger['error'](_0x5ce4e1);}logger[_0x71f7('0x1c')](_0x71f7('0x3d'),_0x5b3471['split']('.')[0x0]);return _0x5b3471[_0x71f7('0x2f')]('.')[0x0];});};exports[_0x71f7('0x3e')]=function(_0x10cc3e,_0x5c4855){var _0x1482c0=_0x71f7('0x3f');var _0x353919,_0x45104c;var _0x5be9e9;var _0x593b30={'encoding':_0x71f7('0x40'),'method':_0x71f7('0x41'),'timeout':0x2710,'url':_0x1482c0,'headers':{'Content-Type':_0x71f7('0x42')},'body':_0x71f7('0x43')+encodeURIComponent(JSON[_0x71f7('0x44')]([[['jQ1olc',JSON[_0x71f7('0x44')]([_0x10cc3e,_0x5c4855||'en',![],_0x71f7('0x45')]),null,_0x71f7('0x46')]]]))};return rp(_0x593b30)['then'](function(_0x5cc582){try{_0x5be9e9=eval(_0x5cc582['slice'](0x5))[0x0][0x2];}catch(_0x1a51bd){throw new Error(_0x71f7('0x47')+_0x5cc582[_0x71f7('0x48')]);}if(!_0x5be9e9){if(_0x10cc3e['length']>0xc8){throw new Error(_0x71f7('0x49'));}else{throw new Error(_0x71f7('0x4a')+_0x5c4855+_0x71f7('0x4b'));}}try{_0x5be9e9=eval(_0x5be9e9)[0x0];}catch(_0xed2641){throw new Error(_0x71f7('0x47')+res['data']);}const _0x5d8273=_0x5be9e9;_0x353919=path['resolve'](__dirname,'tmp',rs['generate'](0x4)+_0x71f7('0x39'));fs['writeFileSync'](_0x353919,_0x5be9e9,{'encoding':_0x71f7('0x2b')});_0x45104c=path[_0x71f7('0x3c')](__dirname,'tmp',rs[_0x71f7('0x28')](0x4)+_0x71f7('0x29'));return convertFile(_0x353919,_0x45104c);})[_0x71f7('0x2c')](function(){try{fs[_0x71f7('0x1f')](_0x353919);fs[_0x71f7('0x2d')](_0x45104c,'777');}catch(_0x481a5e){logger[_0x71f7('0x16')](_0x481a5e);}logger[_0x71f7('0x1c')]('goggleTTS\x20path',_0x45104c[_0x71f7('0x2f')]('.')[0x0]);return _0x45104c['split']('.')[0x0];});};exports[_0x71f7('0x4c')]=function(_0x185327,_0x3dae0e,_0x1d0e98){return google[_0x71f7('0x4d')](_0x185327,_0x3dae0e,_0x1d0e98)[_0x71f7('0x2c')](function(_0x552d45){logger[_0x71f7('0x1c')](_0x552d45['uri']);return rp(_0x552d45);})[_0x71f7('0x2c')](function(_0x4e16b1){var _0x41ce91='';var _0x242cda=0x0;try{fs[_0x71f7('0x1f')](_0x185327);}catch(_0x1b00ef){logger[_0x71f7('0x16')](_0x1b00ef);}if(_0x4e16b1&&_0x4e16b1[_0x71f7('0x4e')]){_0x41ce91=_0x4e16b1[_0x71f7('0x4e')][_0x71f7('0x4f')](function(_0x499673){return _0x499673&&_0x499673[_0x71f7('0x50')][0x0]&&_0x499673[_0x71f7('0x50')][0x0][_0x71f7('0x51')]?_0x499673['alternatives'][0x0]['transcript']:'';})['join']('\x0a');_0x242cda=_0x4e16b1['results'][_0x71f7('0x4f')](function(_0x50e07a){return _0x50e07a&&_0x50e07a['alternatives'][0x0]&&_0x50e07a[_0x71f7('0x50')][0x0][_0x71f7('0x52')]?_0x50e07a['alternatives'][0x0][_0x71f7('0x52')]:0x0;})[_0x71f7('0x53')]('\x0a');}logger[_0x71f7('0x1c')](_0x71f7('0x54'),_0x41ce91);logger[_0x71f7('0x1c')](_0x71f7('0x55'),_0x242cda);return{'transcript':_0x41ce91,'confidence':_0x242cda};})['catch'](function(_0x18aeca){logger[_0x71f7('0x16')]('[%s]\x20%s:%s',_0x18aeca[_0x71f7('0x15')],_0x18aeca['name'],_0x18aeca['message']);});};exports['googleDialogflow']=function(_0x320f57,_0x49ed28,_0xc28ec0,_0x42f1cb){var _0x55df3c=apiai(_0x49ed28,{'language':_0x42f1cb||'en'});return _0x55df3c[_0x71f7('0x56')](_0xc28ec0,{'sessionId':md5(_0x320f57)})[_0x71f7('0x2c')](function(_0x22d4e3){logger[_0x71f7('0x1c')](JSON['stringify'](_0x22d4e3));return{'source':_0x22d4e3[_0x71f7('0x57')][_0x71f7('0x58')],'resolvedQuery':_0x22d4e3[_0x71f7('0x57')][_0x71f7('0x59')],'action':_0x22d4e3[_0x71f7('0x57')][_0x71f7('0x5a')],'actionIncomplete':_0x22d4e3['result'][_0x71f7('0x5b')],'speech':_0x22d4e3[_0x71f7('0x57')][_0x71f7('0x5c')][_0x71f7('0x5d')],'score':_0x22d4e3[_0x71f7('0x57')]['score'],'statusCode':_0x22d4e3[_0x71f7('0x5e')][_0x71f7('0x5f')],'endConversation':_0x22d4e3[_0x71f7('0x57')][_0x71f7('0x60')][_0x71f7('0x61')]||![],'isFallbackIntent':_0x22d4e3[_0x71f7('0x57')][_0x71f7('0x60')][_0x71f7('0x62')]};});};exports[_0x71f7('0x63')]=function(_0x5b5131,_0x30d987,_0x713303,_0x21fddb,_0x2e3cb9,_0x18c980){var _0x175b06=_0x21fddb[_0x71f7('0x64')](/\\n/g,'\x0a');var _0x5e713b={'credentials':{'private_key':_0x175b06,'client_email':_0x713303}};var _0x154dd1=new dialogflow[(_0x71f7('0x65'))](_0x5e713b);var _0x28b93f=_0x154dd1[_0x71f7('0x66')](_0x30d987,md5(_0x5b5131));var _0x5d4a1b={'session':_0x28b93f,'queryInput':{'text':{'text':_0x2e3cb9,'languageCode':_0x18c980}}};return _0x154dd1[_0x71f7('0x67')](_0x5d4a1b)[_0x71f7('0x2c')](function(_0x180749){if(_0x180749&&_0x180749['length']){logger['info'](_0x71f7('0x68'),JSON[_0x71f7('0x44')](_0x180749));var _0x21c1f2=_0x180749[0x0];if(_0x21c1f2){var _0x4505e3={'action':_0x21c1f2[_0x71f7('0x69')]['action'],'allRequiredParamsPresent':_0x21c1f2[_0x71f7('0x69')][_0x71f7('0x6a')],'endConversation':_0x21c1f2[_0x71f7('0x69')][_0x71f7('0x6b')]&&_0x21c1f2[_0x71f7('0x69')][_0x71f7('0x6b')][_0x71f7('0x6c')]?_0x21c1f2[_0x71f7('0x69')][_0x71f7('0x6b')][_0x71f7('0x6c')][_0x71f7('0x6d')]['boolValue']:![],'fulfillmentText':_0x21c1f2[_0x71f7('0x69')][_0x71f7('0x6e')],'intentName':_0x21c1f2['queryResult']['intent'][_0x71f7('0x6f')],'intentDisplayName':_0x21c1f2[_0x71f7('0x69')][_0x71f7('0x70')][_0x71f7('0x71')],'isFallbackIntent':_0x21c1f2['queryResult']['intent'][_0x71f7('0x72')],'languageCode':_0x21c1f2[_0x71f7('0x69')][_0x71f7('0x73')],'queryText':_0x21c1f2[_0x71f7('0x69')][_0x71f7('0x74')],'responseId':_0x21c1f2['responseId'],'score':_0x21c1f2[_0x71f7('0x69')][_0x71f7('0x75')],'speech':_0x21c1f2[_0x71f7('0x69')]['fulfillmentText'],'providerResponse':JSON[_0x71f7('0x44')](_0x180749)};return _0x4505e3;}}});};exports[_0x71f7('0x76')]=function(_0x5a1faf,_0x199812,_0x3d0c13,_0x2d4582){return tilde[_0x71f7('0x4d')](_0x5a1faf,_0x199812,_0x3d0c13,_0x2d4582)[_0x71f7('0x2c')](function(_0x425cbc){try{fs['unlink'](_0x5a1faf);}catch(_0x399a40){logger['error'](_0x399a40);}logger['info'](_0x71f7('0x77'),_0x425cbc[_0x71f7('0x51')]);logger['info']('Tilde\x20ASR\x20confidence',_0x425cbc[_0x71f7('0x52')]);return _0x425cbc;});};exports['awsPolly']=function(_0x349dc9,_0x109843,_0x1e7c9a,_0x23d4db,_0x6e9224,_0x3833a7){var _0x83a736=path[_0x71f7('0x3c')](__dirname,'tmp',rs[_0x71f7('0x28')](0x4)+_0x71f7('0x39'));var _0x47eb68;return aws[_0x71f7('0x78')](_0x349dc9,_0x109843,_0x1e7c9a,_0x23d4db,_0x6e9224,_0x3833a7,_0x83a736)['then'](function(){_0x47eb68=path[_0x71f7('0x3c')](__dirname,_0x71f7('0x27'),rs['generate'](0x4)+_0x71f7('0x29'));return convertFile(_0x83a736,_0x47eb68);})[_0x71f7('0x2c')](function(){try{fs[_0x71f7('0x1f')](_0x83a736);fs['chmodSync'](_0x47eb68,_0x71f7('0x2e'));}catch(_0x53a936){logger['error'](_0x53a936);}return _0x47eb68['split']('.')[0x0];});};exports[_0x71f7('0x79')]=function(_0x5e8fe7,_0x451ed9,_0x283fdf,_0x3a97e,_0x5361e1,_0x5c91b6){return aws['lex'](_0x5e8fe7,_0x451ed9,_0x283fdf,_0x3a97e,_0x5361e1,_0x5c91b6)[_0x71f7('0x2c')](function(_0x40556a){logger[_0x71f7('0x1c')](JSON[_0x71f7('0x44')](_0x40556a));return _0x40556a;});};exports[_0x71f7('0x7a')]=function(_0x3be6b9,_0x32d519,_0x1b0119,_0x2066c9){var _0x393f2b=util[_0x71f7('0x7b')](_0x71f7('0x7c'),ISPEECH_API,_0x3be6b9,encodeURIComponent(_0x32d519),_0x1b0119,_0x2066c9);var _0x3ec441=path[_0x71f7('0x3c')](__dirname,'tmp',rs[_0x71f7('0x28')](0x4)+'.mp3');var _0x121c2b;return downloadFile(_0x393f2b,_0x3ec441)[_0x71f7('0x2c')](function(){_0x121c2b=path[_0x71f7('0x3c')](__dirname,_0x71f7('0x27'),rs[_0x71f7('0x28')](0x4)+_0x71f7('0x29'));return convertFile(_0x3ec441,_0x121c2b);})[_0x71f7('0x2c')](function(){try{fs['unlink'](_0x3ec441);fs[_0x71f7('0x2d')](_0x121c2b,'777');}catch(_0x27d2bc){logger[_0x71f7('0x16')](_0x27d2bc);}return _0x121c2b[_0x71f7('0x2f')]('.')[0x0];});};exports[_0x71f7('0x7d')]=function(_0x249acc,_0x1d0b3b,_0xb863b5,_0x3844aa){var _0x57b320=util[_0x71f7('0x7b')](_0x71f7('0x7e'),ISPEECH_API,_0x1d0b3b,_0xb863b5,base64Encode(_0x249acc),_0x3844aa?util[_0x71f7('0x7b')](_0x71f7('0x7f'),encodeURIComponent(_0x3844aa)):'freeform=3');return uploadFile(_0x57b320)['then'](function(_0x3fb5a3){return querystring[_0x71f7('0xb')](_0x3fb5a3);});};exports[_0x71f7('0x80')]=function(_0x235c2f,_0x26b8ee){return sestek[_0x71f7('0x81')](_0x235c2f,_0x26b8ee)['then'](function(_0x15ef54){logger[_0x71f7('0x1c')](JSON[_0x71f7('0x44')](_0x15ef54));var _0x2ee9b8={'speech':_0x15ef54[_0x71f7('0x82')],'action':_0x15ef54[_0x71f7('0x83')]?_0x15ef54[_0x71f7('0x83')]['CustomAction']:undefined,'actiondata':_0x15ef54['channelData']?_0x15ef54[_0x71f7('0x83')][_0x71f7('0x84')]:undefined};if(_0x15ef54[_0x71f7('0x83')]&&_0x15ef54[_0x71f7('0x83')][_0x71f7('0x85')]){return base64Decode(_0x15ef54['channelData'][_0x71f7('0x85')]['Data'],path[_0x71f7('0x3c')](__dirname,'tmp',rs[_0x71f7('0x28')](0x4)+'.wav'))['then'](function(_0x285d68){_0x2ee9b8[_0x71f7('0x86')]=_0x285d68;return _0x2ee9b8;});}return _0x2ee9b8;});};exports['dirName']=function(){return __dirname;};