f26e51b7facf9338263097584faebe8f6ffcf521
[motion2.git] / server / config / schedule / transcribe.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 _0x276c=['data1','data2','transcribeRegion','then','transcribeName','TranscriptionJob','TranscriptionJobStatus','COMPLETED','TranscriptFileUri','[RECORDING:%d]\x20Get\x20transcript','Transcript','[RECORDING:%d]\x20Transcript\x20downloaded','results','transcripts','length','fileText','transcript','update','tempSentiment','ValidationError','Sentiment\x20Analysis\x20not\x20requested','sentiment','sentimentAccountId','language','bucket','Sequelize','Sentiment\x20Analysis\x20not\x20configured','Transcript\x20not\x20available','CloudProvider','findOne','Cloud\x20Provider\x20not\x20available','Cloud\x20Provider\x20not\x20configured','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','sentimentRegion','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','Sentiment','SentimentScore','Positive','Negative','Mixed','Neutral','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','FAILED','FailureReason','[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed','catch','SequelizeValidationError','error','VoiceRecording','findAll','No\x20transcribe\x20job\x20to\x20check','Setting','Settings\x20not\x20available','transcribeAccountId','forEach','name','inspect','start','request-promise','util','bluebird','getTranscriptionJob','substring','info','format','[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)'];(function(_0x9d639c,_0x524dbe){var _0x1671de=function(_0x27888e){while(--_0x27888e){_0x9d639c['push'](_0x9d639c['shift']());}};_0x1671de(++_0x524dbe);}(_0x276c,0x1c6));var _0xc276=function(_0x120c96,_0x3434b9){_0x120c96=_0x120c96-0x0;var _0x173922=_0x276c[_0x120c96];return _0x173922;};var _=require('lodash');var AWS=require('aws-sdk');var rp=require(_0xc276('0x0'));var util=require(_0xc276('0x1'));var BPromise=require(_0xc276('0x2'));var db=require('../../mysqldb')['db'];var logger=require('../../config/logger')('transcribe');function getTranscriptionJob(_0x3ffb43,_0xd06f2d,_0x5a61a4,_0x2deb3d){return new Promise(function(_0x4be9b0,_0x39782e){var _0x48b335=new AWS['TranscribeService']({'accessKeyId':_0xd06f2d,'secretAccessKey':_0x3ffb43,'region':_0x5a61a4});_0x48b335[_0xc276('0x3')]({'TranscriptionJobName':_0x2deb3d},function(_0x101737,_0x3b99ed){if(_0x101737){return _0x39782e(_0x101737);}else{return _0x4be9b0(_0x3b99ed);}});});}function sentiment(_0x10f26e,_0x2a14ec,_0x4e7771,_0x12c765,_0x29d903){return new Promise(function(_0x50cc23,_0x5f2c7c){var _0x2e2748=new AWS['Comprehend']({'accessKeyId':_0x2a14ec,'secretAccessKey':_0x10f26e,'region':_0x4e7771});_0x2e2748['detectSentiment']({'Text':_0x12c765,'LanguageCode':_0x29d903[_0xc276('0x4')](0x0,0x2)},function(_0x5097d6,_0x24176c){if(_0x5097d6){return _0x5f2c7c(_0x5097d6);}else{return _0x50cc23(_0x24176c);}});});}function checkTranscribeJob(_0x1384bf,_0x1e94fd,_0x2336f9){var _0x55c9d4;return new BPromise(function(_0x19c1f9,_0x598a91){logger[_0xc276('0x5')](util[_0xc276('0x6')](_0xc276('0x7'),_0x1384bf['id'],_0x1384bf['transcribeName']));return getTranscriptionJob(_0x2336f9[_0xc276('0x8')],_0x2336f9[_0xc276('0x9')],_0x1e94fd[_0xc276('0xa')],_0x1384bf['transcribeName'])[_0xc276('0xb')](function(_0x21752e){logger[_0xc276('0x5')](util[_0xc276('0x6')]('[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s',_0x1384bf['id'],_0x1384bf[_0xc276('0xc')],_0x21752e[_0xc276('0xd')][_0xc276('0xe')]));if(!_0x1384bf['fileText']){switch(_0x21752e[_0xc276('0xd')][_0xc276('0xe')]){case _0xc276('0xf'):var _0x5dcfa0={'transcribeStatus':_0x21752e[_0xc276('0xd')][_0xc276('0xe')],'fileUri':_0x21752e[_0xc276('0xd')]['Transcript'][_0xc276('0x10')]};logger[_0xc276('0x5')](util['format'](_0xc276('0x11'),_0x1384bf['id']));return rp({'uri':_0x21752e[_0xc276('0xd')][_0xc276('0x12')]['TranscriptFileUri'],'json':!![]})[_0xc276('0xb')](function(_0x4fa826){logger[_0xc276('0x5')](util[_0xc276('0x6')](_0xc276('0x13'),_0x1384bf['id']));if(_0x4fa826&&_0x4fa826[_0xc276('0x14')]&&_0x4fa826['results']['transcripts']&&_0x4fa826[_0xc276('0x14')][_0xc276('0x15')][_0xc276('0x16')]){_0x5dcfa0[_0xc276('0x17')]='';for(var _0x37f713=0x0;_0x37f713<_0x4fa826['results'][_0xc276('0x15')]['length'];_0x37f713++){_0x5dcfa0[_0xc276('0x17')]+=_0x4fa826['results'][_0xc276('0x15')][_0x37f713][_0xc276('0x18')]+'';}}return _0x1384bf[_0xc276('0x19')](_0x5dcfa0);})[_0xc276('0xb')](function(){logger[_0xc276('0x5')](util[_0xc276('0x6')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x1384bf['id']));if(!_0x1384bf[_0xc276('0x1a')]){throw new db['Sequelize'][(_0xc276('0x1b'))](_0xc276('0x1c'));}if(!_0x1e94fd[_0xc276('0x1d')]||!_0x1e94fd[_0xc276('0x1e')]||!_0x1e94fd['sentimentRegion']||!_0x1e94fd[_0xc276('0x1f')]||!_0x1e94fd[_0xc276('0x20')]){throw new db[(_0xc276('0x21'))][(_0xc276('0x1b'))](_0xc276('0x22'));}if(!_0x5dcfa0[_0xc276('0x17')]){throw new Error(_0xc276('0x23'));}if(_0x1e94fd[_0xc276('0x1e')]===_0x2336f9['id']){return _0x2336f9;}else{return db[_0xc276('0x24')][_0xc276('0x25')]({'where':{'id':_settings[_0xc276('0x1e')]}});}})['then'](function(_0x50fa88){if(!_0x50fa88){throw new db[(_0xc276('0x21'))]['ValidationError'](_0xc276('0x26'));}if(!_0x50fa88[_0xc276('0x8')]||!_0x50fa88[_0xc276('0x9')]){throw new db['Sequelize'][(_0xc276('0x1b'))](_0xc276('0x27'));}_0x55c9d4=_0x50fa88;logger[_0xc276('0x5')](util['format'](_0xc276('0x28'),_0x1384bf['id']));return sentiment(_0x55c9d4[_0xc276('0x8')],_0x55c9d4['data2'],_0x1e94fd[_0xc276('0x29')],_0x5dcfa0[_0xc276('0x17')],_0x1e94fd[_0xc276('0x1f')]);})[_0xc276('0xb')](function(_0x61b53f){logger[_0xc276('0x5')](util['format'](_0xc276('0x2a'),_0x1384bf['id']));return _0x1384bf[_0xc276('0x19')]({'sentiment':_0x61b53f[_0xc276('0x2b')],'sPositive':_0x61b53f[_0xc276('0x2c')][_0xc276('0x2d')],'sNegative':_0x61b53f[_0xc276('0x2c')][_0xc276('0x2e')],'sMixed':_0x61b53f[_0xc276('0x2c')][_0xc276('0x2f')],'sNeutral':_0x61b53f[_0xc276('0x2c')][_0xc276('0x30')]});})[_0xc276('0xb')](function(){logger['info'](util[_0xc276('0x6')](_0xc276('0x31'),_0x1384bf['id']));_0x19c1f9();});case _0xc276('0x32'):return _0x1384bf[_0xc276('0x19')]({'transcribeStatus':_0xc276('0x32'),'failureReason':_0x21752e[_0xc276('0xd')][_0xc276('0x33')]})[_0xc276('0xb')](function(){logger[_0xc276('0x5')](util[_0xc276('0x6')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x1384bf['id']));_0x19c1f9();});default:logger[_0xc276('0x5')](util[_0xc276('0x6')](_0xc276('0x34'),_0x1384bf['id']));break;}}else{return BPromise['resolve']()[_0xc276('0xb')](function(){if(!_0x1384bf[_0xc276('0x1a')]){throw new db[(_0xc276('0x21'))]['ValidationError'](_0xc276('0x1c'));}if(!_0x1e94fd[_0xc276('0x1d')]||!_0x1e94fd['sentimentAccountId']||!_0x1e94fd['sentimentRegion']||!_0x1e94fd[_0xc276('0x1f')]||!_0x1e94fd[_0xc276('0x20')]){throw new db['Sequelize'][(_0xc276('0x1b'))]('Sentiment\x20Analysis\x20not\x20configured');}if(!_0x1384bf[_0xc276('0x17')]){throw new Error(_0xc276('0x23'));}if(_0x1e94fd[_0xc276('0x1e')]===_0x2336f9['id']){return _0x2336f9;}else{return db[_0xc276('0x24')][_0xc276('0x25')]({'where':{'id':_settings['sentimentAccountId']}});}})[_0xc276('0xb')](function(_0xd356e3){if(!_0xd356e3){throw new db[(_0xc276('0x21'))]['ValidationError']('Cloud\x20Provider\x20not\x20available');}if(!_0xd356e3['data1']||!_0xd356e3[_0xc276('0x9')]){throw new db['Sequelize']['ValidationError'](_0xc276('0x27'));}_0x55c9d4=_0xd356e3;logger[_0xc276('0x5')](util['format'](_0xc276('0x28'),_0x1384bf['id']));return sentiment(_0x55c9d4['data1'],_0x55c9d4[_0xc276('0x9')],_0x1e94fd[_0xc276('0x29')],_0x1384bf[_0xc276('0x17')],_0x1e94fd[_0xc276('0x1f')]);})['then'](function(_0x320b47){logger['info'](util[_0xc276('0x6')]('[RECORDING:%d]\x20Sentiment\x20Analysis\x20available',_0x1384bf['id']));return _0x1384bf[_0xc276('0x19')]({'sentiment':_0x320b47[_0xc276('0x2b')],'sPositive':_0x320b47[_0xc276('0x2c')][_0xc276('0x2d')],'sNegative':_0x320b47[_0xc276('0x2c')][_0xc276('0x2e')],'sMixed':_0x320b47['SentimentScore'][_0xc276('0x2f')],'sNeutral':_0x320b47[_0xc276('0x2c')]['Neutral'],'transcribeStatus':_0xc276('0xf')});})[_0xc276('0xb')](function(){logger[_0xc276('0x5')](util[_0xc276('0x6')](_0xc276('0x31'),_0x1384bf['id']));_0x19c1f9();});}})[_0xc276('0x35')](function(_0x4f30af){if(_0x4f30af){if(!_0x4f30af['name']||_0x4f30af['name']!==_0xc276('0x36')){logger[_0xc276('0x37')](util['format']('[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis',_0x1384bf['id']),util['inspect'](_0x4f30af,{'showHidden':![],'depth':null}));}}_0x19c1f9();});});}function checkTranscribe(){var _0x495a1b;var _0x30c36f;var _0x1da595;return db[_0xc276('0x38')][_0xc276('0x39')]({'where':{'transcribeStatus':'IN_PROGRESS'}})[_0xc276('0xb')](function(_0x51a0db){if(!_0x51a0db||!_0x51a0db[_0xc276('0x16')]){throw new db[(_0xc276('0x21'))][(_0xc276('0x1b'))](_0xc276('0x3a'));}_0x495a1b=_0x51a0db;return db[_0xc276('0x3b')]['findOne']({'where':{'id':0x1}});})['then'](function(_0x12667c){if(!_0x12667c){throw new db['Sequelize'][(_0xc276('0x1b'))](_0xc276('0x3c'));}_0x30c36f=_0x12667c;if(!_0x30c36f['transcribe']||!_0x30c36f['transcribeAccountId']||!_0x30c36f[_0xc276('0xa')]||!_0x30c36f[_0xc276('0x1f')]||!_0x30c36f[_0xc276('0x20')]){throw new db['Sequelize']['ValidationError']('Transcribe\x20not\x20configured');}return db[_0xc276('0x24')][_0xc276('0x25')]({'where':{'id':_0x30c36f[_0xc276('0x3d')]}});})[_0xc276('0xb')](function(_0xf232f0){if(!_0xf232f0){throw new db['Sequelize'][(_0xc276('0x1b'))](_0xc276('0x26'));}if(!_0xf232f0[_0xc276('0x8')]||!_0xf232f0['data2']){throw new db['Sequelize'][(_0xc276('0x1b'))]('Cloud\x20Provider\x20not\x20configured');}_0x1da595=_0xf232f0;var _0x3263c3=[];_0x495a1b[_0xc276('0x3e')](function(_0xa43ac8){_0x3263c3['push'](checkTranscribeJob(_0xa43ac8,_0x30c36f,_0x1da595));});return BPromise['all'](_0x3263c3);})[_0xc276('0x35')](function(_0x5d8490){if(_0x5d8490){if(!_0x5d8490[_0xc276('0x3f')]||_0x5d8490[_0xc276('0x3f')]!==_0xc276('0x36')){logger[_0xc276('0x37')](util[_0xc276('0x40')](_0x5d8490,{'showHidden':![],'depth':null}));}}});}exports[_0xc276('0x41')]=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};