f4e464d5df39a5df024a472d14ddd7487169be75
[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 _0xa596=['TranscriptionJob','TranscriptionJobStatus','fileText','COMPLETED','Transcript','TranscriptFileUri','[RECORDING:%d]\x20Get\x20transcript','results','transcripts','length','update','tempSentiment','ValidationError','Sentiment\x20Analysis\x20not\x20requested','sentiment','sentimentAccountId','sentimentRegion','language','Sentiment\x20Analysis\x20not\x20configured','Transcript\x20not\x20available','CloudProvider','findOne','Sequelize','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','Sentiment','SentimentScore','Negative','Mixed','Neutral','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','FAILED','FailureReason','resolve','bucket','Cloud\x20Provider\x20not\x20available','Cloud\x20Provider\x20not\x20configured','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','name','[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis','inspect','VoiceRecording','IN_PROGRESS','No\x20transcribe\x20job\x20to\x20check','Setting','Settings\x20not\x20available','transcribeAccountId','transcribeRegion','Transcribe\x20not\x20configured','push','all','SequelizeValidationError','start','lodash','aws-sdk','util','bluebird','../../mysqldb','../../config/logger','transcribe','TranscribeService','getTranscriptionJob','detectSentiment','substring','info','format','[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)','transcribeName','data1','data2','then','[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s'];(function(_0x5d26c0,_0x1aa2f7){var _0xec0117=function(_0x21baab){while(--_0x21baab){_0x5d26c0['push'](_0x5d26c0['shift']());}};_0xec0117(++_0x1aa2f7);}(_0xa596,0x7b));var _0x6a59=function(_0x4bed3b,_0x8f48bb){_0x4bed3b=_0x4bed3b-0x0;var _0x459ef0=_0xa596[_0x4bed3b];return _0x459ef0;};var _=require(_0x6a59('0x0'));var AWS=require(_0x6a59('0x1'));var rp=require('request-promise');var util=require(_0x6a59('0x2'));var BPromise=require(_0x6a59('0x3'));var db=require(_0x6a59('0x4'))['db'];var logger=require(_0x6a59('0x5'))(_0x6a59('0x6'));function getTranscriptionJob(_0xf0435f,_0x11cce1,_0x272793,_0x4f42f6){return new Promise(function(_0x37d9ef,_0x3fab57){var _0x21e97f=new AWS[(_0x6a59('0x7'))]({'accessKeyId':_0x11cce1,'secretAccessKey':_0xf0435f,'region':_0x272793});_0x21e97f[_0x6a59('0x8')]({'TranscriptionJobName':_0x4f42f6},function(_0x3c392f,_0x5aa5fe){if(_0x3c392f){return _0x3fab57(_0x3c392f);}else{return _0x37d9ef(_0x5aa5fe);}});});}function sentiment(_0x3f25d8,_0x48ecee,_0x21008c,_0x49be8c,_0x55ef5b){return new Promise(function(_0x3b44b5,_0x6e6ef7){var _0x24041e=new AWS['Comprehend']({'accessKeyId':_0x48ecee,'secretAccessKey':_0x3f25d8,'region':_0x21008c});_0x24041e[_0x6a59('0x9')]({'Text':_0x49be8c,'LanguageCode':_0x55ef5b[_0x6a59('0xa')](0x0,0x2)},function(_0x5a8484,_0x4e2323){if(_0x5a8484){return _0x6e6ef7(_0x5a8484);}else{return _0x3b44b5(_0x4e2323);}});});}function checkTranscribeJob(_0x4bf880,_0x23e9e3,_0x3075ca){var _0x177506;return new BPromise(function(_0x68dd0a,_0x1b0d74){logger[_0x6a59('0xb')](util[_0x6a59('0xc')](_0x6a59('0xd'),_0x4bf880['id'],_0x4bf880[_0x6a59('0xe')]));return getTranscriptionJob(_0x3075ca[_0x6a59('0xf')],_0x3075ca[_0x6a59('0x10')],_0x23e9e3['transcribeRegion'],_0x4bf880[_0x6a59('0xe')])[_0x6a59('0x11')](function(_0x416613){logger[_0x6a59('0xb')](util[_0x6a59('0xc')](_0x6a59('0x12'),_0x4bf880['id'],_0x4bf880['transcribeName'],_0x416613[_0x6a59('0x13')][_0x6a59('0x14')]));if(!_0x4bf880[_0x6a59('0x15')]){switch(_0x416613[_0x6a59('0x13')][_0x6a59('0x14')]){case _0x6a59('0x16'):var _0x4988e2={'transcribeStatus':_0x416613[_0x6a59('0x13')][_0x6a59('0x14')],'fileUri':_0x416613[_0x6a59('0x13')][_0x6a59('0x17')][_0x6a59('0x18')]};logger[_0x6a59('0xb')](util[_0x6a59('0xc')](_0x6a59('0x19'),_0x4bf880['id']));return rp({'uri':_0x416613[_0x6a59('0x13')][_0x6a59('0x17')]['TranscriptFileUri'],'json':!![]})[_0x6a59('0x11')](function(_0x3e068b){logger[_0x6a59('0xb')](util[_0x6a59('0xc')]('[RECORDING:%d]\x20Transcript\x20downloaded',_0x4bf880['id']));if(_0x3e068b&&_0x3e068b['results']&&_0x3e068b[_0x6a59('0x1a')][_0x6a59('0x1b')]&&_0x3e068b[_0x6a59('0x1a')][_0x6a59('0x1b')]['length']){_0x4988e2['fileText']='';for(var _0x344cea=0x0;_0x344cea<_0x3e068b[_0x6a59('0x1a')]['transcripts'][_0x6a59('0x1c')];_0x344cea++){_0x4988e2['fileText']+=_0x3e068b[_0x6a59('0x1a')][_0x6a59('0x1b')][_0x344cea]['transcript']+'';}}return _0x4bf880[_0x6a59('0x1d')](_0x4988e2);})['then'](function(){logger[_0x6a59('0xb')](util[_0x6a59('0xc')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x4bf880['id']));if(!_0x4bf880[_0x6a59('0x1e')]){throw new db['Sequelize'][(_0x6a59('0x1f'))](_0x6a59('0x20'));}if(!_0x23e9e3[_0x6a59('0x21')]||!_0x23e9e3[_0x6a59('0x22')]||!_0x23e9e3[_0x6a59('0x23')]||!_0x23e9e3[_0x6a59('0x24')]||!_0x23e9e3['bucket']){throw new db['Sequelize'][(_0x6a59('0x1f'))](_0x6a59('0x25'));}if(!_0x4988e2['fileText']){throw new Error(_0x6a59('0x26'));}if(_0x23e9e3['sentimentAccountId']===_0x3075ca['id']){return _0x3075ca;}else{return db[_0x6a59('0x27')][_0x6a59('0x28')]({'where':{'id':_settings[_0x6a59('0x22')]}});}})['then'](function(_0x360e16){if(!_0x360e16){throw new db[(_0x6a59('0x29'))][(_0x6a59('0x1f'))]('Cloud\x20Provider\x20not\x20available');}if(!_0x360e16[_0x6a59('0xf')]||!_0x360e16[_0x6a59('0x10')]){throw new db['Sequelize'][(_0x6a59('0x1f'))]('Cloud\x20Provider\x20not\x20configured');}_0x177506=_0x360e16;logger[_0x6a59('0xb')](util[_0x6a59('0xc')]('[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis',_0x4bf880['id']));return sentiment(_0x177506[_0x6a59('0xf')],_0x177506[_0x6a59('0x10')],_0x23e9e3['sentimentRegion'],_0x4988e2['fileText'],_0x23e9e3['language']);})['then'](function(_0x49542a){logger[_0x6a59('0xb')](util[_0x6a59('0xc')](_0x6a59('0x2a'),_0x4bf880['id']));return _0x4bf880['update']({'sentiment':_0x49542a[_0x6a59('0x2b')],'sPositive':_0x49542a[_0x6a59('0x2c')]['Positive'],'sNegative':_0x49542a['SentimentScore'][_0x6a59('0x2d')],'sMixed':_0x49542a['SentimentScore'][_0x6a59('0x2e')],'sNeutral':_0x49542a[_0x6a59('0x2c')][_0x6a59('0x2f')]});})[_0x6a59('0x11')](function(){logger[_0x6a59('0xb')](util[_0x6a59('0xc')](_0x6a59('0x30'),_0x4bf880['id']));_0x68dd0a();});case _0x6a59('0x31'):return _0x4bf880[_0x6a59('0x1d')]({'transcribeStatus':_0x6a59('0x31'),'failureReason':_0x416613[_0x6a59('0x13')][_0x6a59('0x32')]})[_0x6a59('0x11')](function(){logger[_0x6a59('0xb')](util[_0x6a59('0xc')](_0x6a59('0x30'),_0x4bf880['id']));_0x68dd0a();});default:logger['info'](util[_0x6a59('0xc')]('[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed',_0x4bf880['id']));break;}}else{return BPromise[_0x6a59('0x33')]()[_0x6a59('0x11')](function(){if(!_0x4bf880[_0x6a59('0x1e')]){throw new db[(_0x6a59('0x29'))][(_0x6a59('0x1f'))](_0x6a59('0x20'));}if(!_0x23e9e3[_0x6a59('0x21')]||!_0x23e9e3[_0x6a59('0x22')]||!_0x23e9e3[_0x6a59('0x23')]||!_0x23e9e3['language']||!_0x23e9e3[_0x6a59('0x34')]){throw new db[(_0x6a59('0x29'))][(_0x6a59('0x1f'))]('Sentiment\x20Analysis\x20not\x20configured');}if(!_0x4bf880['fileText']){throw new Error(_0x6a59('0x26'));}if(_0x23e9e3[_0x6a59('0x22')]===_0x3075ca['id']){return _0x3075ca;}else{return db[_0x6a59('0x27')]['findOne']({'where':{'id':_settings[_0x6a59('0x22')]}});}})[_0x6a59('0x11')](function(_0x399532){if(!_0x399532){throw new db['Sequelize']['ValidationError'](_0x6a59('0x35'));}if(!_0x399532[_0x6a59('0xf')]||!_0x399532[_0x6a59('0x10')]){throw new db[(_0x6a59('0x29'))][(_0x6a59('0x1f'))](_0x6a59('0x36'));}_0x177506=_0x399532;logger['info'](util['format'](_0x6a59('0x37'),_0x4bf880['id']));return sentiment(_0x177506['data1'],_0x177506[_0x6a59('0x10')],_0x23e9e3[_0x6a59('0x23')],_0x4bf880['fileText'],_0x23e9e3[_0x6a59('0x24')]);})[_0x6a59('0x11')](function(_0x267a4b){logger[_0x6a59('0xb')](util[_0x6a59('0xc')]('[RECORDING:%d]\x20Sentiment\x20Analysis\x20available',_0x4bf880['id']));return _0x4bf880[_0x6a59('0x1d')]({'sentiment':_0x267a4b['Sentiment'],'sPositive':_0x267a4b['SentimentScore']['Positive'],'sNegative':_0x267a4b[_0x6a59('0x2c')][_0x6a59('0x2d')],'sMixed':_0x267a4b[_0x6a59('0x2c')]['Mixed'],'sNeutral':_0x267a4b[_0x6a59('0x2c')][_0x6a59('0x2f')],'transcribeStatus':_0x6a59('0x16')});})['then'](function(){logger['info'](util['format'](_0x6a59('0x30'),_0x4bf880['id']));_0x68dd0a();});}})['catch'](function(_0xa17ed9){if(_0xa17ed9){if(!_0xa17ed9[_0x6a59('0x38')]||_0xa17ed9['name']!=='SequelizeValidationError'){logger['error'](util[_0x6a59('0xc')](_0x6a59('0x39'),_0x4bf880['id']),util[_0x6a59('0x3a')](_0xa17ed9,{'showHidden':![],'depth':null}));}}_0x68dd0a();});});}function checkTranscribe(){var _0x180dc4;var _0x1509e2;var _0x11715b;return db[_0x6a59('0x3b')]['findAll']({'where':{'transcribeStatus':_0x6a59('0x3c')}})[_0x6a59('0x11')](function(_0xbc5b1a){if(!_0xbc5b1a||!_0xbc5b1a[_0x6a59('0x1c')]){throw new db[(_0x6a59('0x29'))]['ValidationError'](_0x6a59('0x3d'));}_0x180dc4=_0xbc5b1a;return db[_0x6a59('0x3e')][_0x6a59('0x28')]({'where':{'id':0x1}});})[_0x6a59('0x11')](function(_0x278e7a){if(!_0x278e7a){throw new db[(_0x6a59('0x29'))]['ValidationError'](_0x6a59('0x3f'));}_0x1509e2=_0x278e7a;if(!_0x1509e2['transcribe']||!_0x1509e2[_0x6a59('0x40')]||!_0x1509e2[_0x6a59('0x41')]||!_0x1509e2[_0x6a59('0x24')]||!_0x1509e2['bucket']){throw new db['Sequelize']['ValidationError'](_0x6a59('0x42'));}return db[_0x6a59('0x27')]['findOne']({'where':{'id':_0x1509e2[_0x6a59('0x40')]}});})[_0x6a59('0x11')](function(_0x31aa4e){if(!_0x31aa4e){throw new db[(_0x6a59('0x29'))]['ValidationError']('Cloud\x20Provider\x20not\x20available');}if(!_0x31aa4e[_0x6a59('0xf')]||!_0x31aa4e[_0x6a59('0x10')]){throw new db[(_0x6a59('0x29'))][(_0x6a59('0x1f'))](_0x6a59('0x36'));}_0x11715b=_0x31aa4e;var _0x574c24=[];_0x180dc4['forEach'](function(_0x1a118e){_0x574c24[_0x6a59('0x43')](checkTranscribeJob(_0x1a118e,_0x1509e2,_0x11715b));});return BPromise[_0x6a59('0x44')](_0x574c24);})['catch'](function(_0x328e78){if(_0x328e78){if(!_0x328e78['name']||_0x328e78[_0x6a59('0x38')]!==_0x6a59('0x45')){logger['error'](util[_0x6a59('0x3a')](_0x328e78,{'showHidden':![],'depth':null}));}}});}exports[_0x6a59('0x46')]=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};