94a161c31e2110188510c00da28d256a0659094a
[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 _0xfb37=['transcribe','transcribeAccountId','transcribeRegion','Transcribe\x20not\x20configured','forEach','push','all','lodash','util','bluebird','../../mysqldb','TranscribeService','Comprehend','detectSentiment','substring','info','format','transcribeName','data1','then','[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s','TranscriptionJob','TranscriptionJobStatus','COMPLETED','Transcript','TranscriptFileUri','[RECORDING:%d]\x20Transcript\x20downloaded','results','transcripts','length','fileText','transcript','update','tempSentiment','Sequelize','ValidationError','Sentiment\x20Analysis\x20not\x20requested','sentiment','language','bucket','Sentiment\x20Analysis\x20not\x20configured','sentimentAccountId','CloudProvider','findOne','Cloud\x20Provider\x20not\x20available','data2','Cloud\x20Provider\x20not\x20configured','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','sentimentRegion','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','Sentiment','SentimentScore','Positive','Mixed','Neutral','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed','resolve','Transcript\x20not\x20available','Negative','catch','name','SequelizeValidationError','error','[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis','inspect','VoiceRecording','No\x20transcribe\x20job\x20to\x20check','Setting','Settings\x20not\x20available'];(function(_0xdf666a,_0x177888){var _0x426cc0=function(_0x48e012){while(--_0x48e012){_0xdf666a['push'](_0xdf666a['shift']());}};_0x426cc0(++_0x177888);}(_0xfb37,0xd9));var _0x7fb3=function(_0x22093a,_0xa7acf1){_0x22093a=_0x22093a-0x0;var _0x2f4989=_0xfb37[_0x22093a];return _0x2f4989;};var _=require(_0x7fb3('0x0'));var AWS=require('aws-sdk');var rp=require('request-promise');var util=require(_0x7fb3('0x1'));var BPromise=require(_0x7fb3('0x2'));var db=require(_0x7fb3('0x3'))['db'];var logger=require('../../config/logger')('transcribe');function getTranscriptionJob(_0x35a297,_0x1ebfa9,_0x26126e,_0x2fbafd){return new Promise(function(_0x89b90c,_0x5ab2cd){var _0x1241c9=new AWS[(_0x7fb3('0x4'))]({'accessKeyId':_0x1ebfa9,'secretAccessKey':_0x35a297,'region':_0x26126e});_0x1241c9['getTranscriptionJob']({'TranscriptionJobName':_0x2fbafd},function(_0x4d1758,_0x4cf6b5){if(_0x4d1758){return _0x5ab2cd(_0x4d1758);}else{return _0x89b90c(_0x4cf6b5);}});});}function sentiment(_0x458b07,_0xce0779,_0x5bd4df,_0x195692,_0x59bc02){return new Promise(function(_0x1d005d,_0x3c6c24){var _0x527d6f=new AWS[(_0x7fb3('0x5'))]({'accessKeyId':_0xce0779,'secretAccessKey':_0x458b07,'region':_0x5bd4df});_0x527d6f[_0x7fb3('0x6')]({'Text':_0x195692,'LanguageCode':_0x59bc02[_0x7fb3('0x7')](0x0,0x2)},function(_0x2899c2,_0x506e9a){if(_0x2899c2){return _0x3c6c24(_0x2899c2);}else{return _0x1d005d(_0x506e9a);}});});}function checkTranscribeJob(_0x790217,_0x4f4612,_0x1f946d){var _0x3ab550;return new BPromise(function(_0x411ee0,_0x144733){logger[_0x7fb3('0x8')](util[_0x7fb3('0x9')]('[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)',_0x790217['id'],_0x790217[_0x7fb3('0xa')]));return getTranscriptionJob(_0x1f946d[_0x7fb3('0xb')],_0x1f946d['data2'],_0x4f4612['transcribeRegion'],_0x790217[_0x7fb3('0xa')])[_0x7fb3('0xc')](function(_0x1f46a0){logger[_0x7fb3('0x8')](util[_0x7fb3('0x9')](_0x7fb3('0xd'),_0x790217['id'],_0x790217[_0x7fb3('0xa')],_0x1f46a0[_0x7fb3('0xe')][_0x7fb3('0xf')]));if(!_0x790217['fileText']){switch(_0x1f46a0[_0x7fb3('0xe')][_0x7fb3('0xf')]){case _0x7fb3('0x10'):var _0x4bc0df={'transcribeStatus':_0x1f46a0[_0x7fb3('0xe')]['TranscriptionJobStatus'],'fileUri':_0x1f46a0[_0x7fb3('0xe')][_0x7fb3('0x11')][_0x7fb3('0x12')]};logger[_0x7fb3('0x8')](util['format']('[RECORDING:%d]\x20Get\x20transcript',_0x790217['id']));return rp({'uri':_0x1f46a0[_0x7fb3('0xe')][_0x7fb3('0x11')]['TranscriptFileUri'],'json':!![]})[_0x7fb3('0xc')](function(_0x232da1){logger[_0x7fb3('0x8')](util[_0x7fb3('0x9')](_0x7fb3('0x13'),_0x790217['id']));if(_0x232da1&&_0x232da1['results']&&_0x232da1[_0x7fb3('0x14')]['transcripts']&&_0x232da1[_0x7fb3('0x14')][_0x7fb3('0x15')][_0x7fb3('0x16')]){_0x4bc0df['fileText']='';for(var _0x287757=0x0;_0x287757<_0x232da1[_0x7fb3('0x14')][_0x7fb3('0x15')]['length'];_0x287757++){_0x4bc0df[_0x7fb3('0x17')]+=_0x232da1[_0x7fb3('0x14')][_0x7fb3('0x15')][_0x287757][_0x7fb3('0x18')]+'';}}return _0x790217[_0x7fb3('0x19')](_0x4bc0df);})[_0x7fb3('0xc')](function(){logger[_0x7fb3('0x8')](util[_0x7fb3('0x9')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x790217['id']));if(!_0x790217[_0x7fb3('0x1a')]){throw new db[(_0x7fb3('0x1b'))][(_0x7fb3('0x1c'))](_0x7fb3('0x1d'));}if(!_0x4f4612[_0x7fb3('0x1e')]||!_0x4f4612['sentimentAccountId']||!_0x4f4612['sentimentRegion']||!_0x4f4612[_0x7fb3('0x1f')]||!_0x4f4612[_0x7fb3('0x20')]){throw new db['Sequelize'][(_0x7fb3('0x1c'))](_0x7fb3('0x21'));}if(!_0x4bc0df[_0x7fb3('0x17')]){throw new Error('Transcript\x20not\x20available');}if(_0x4f4612[_0x7fb3('0x22')]===_0x1f946d['id']){return _0x1f946d;}else{return db[_0x7fb3('0x23')][_0x7fb3('0x24')]({'where':{'id':_settings[_0x7fb3('0x22')]}});}})[_0x7fb3('0xc')](function(_0x2355f){if(!_0x2355f){throw new db[(_0x7fb3('0x1b'))][(_0x7fb3('0x1c'))](_0x7fb3('0x25'));}if(!_0x2355f[_0x7fb3('0xb')]||!_0x2355f[_0x7fb3('0x26')]){throw new db['Sequelize']['ValidationError'](_0x7fb3('0x27'));}_0x3ab550=_0x2355f;logger['info'](util[_0x7fb3('0x9')](_0x7fb3('0x28'),_0x790217['id']));return sentiment(_0x3ab550[_0x7fb3('0xb')],_0x3ab550[_0x7fb3('0x26')],_0x4f4612[_0x7fb3('0x29')],_0x4bc0df[_0x7fb3('0x17')],_0x4f4612[_0x7fb3('0x1f')]);})[_0x7fb3('0xc')](function(_0x4ef5d7){logger['info'](util['format'](_0x7fb3('0x2a'),_0x790217['id']));return _0x790217[_0x7fb3('0x19')]({'sentiment':_0x4ef5d7[_0x7fb3('0x2b')],'sPositive':_0x4ef5d7[_0x7fb3('0x2c')][_0x7fb3('0x2d')],'sNegative':_0x4ef5d7[_0x7fb3('0x2c')]['Negative'],'sMixed':_0x4ef5d7['SentimentScore'][_0x7fb3('0x2e')],'sNeutral':_0x4ef5d7[_0x7fb3('0x2c')][_0x7fb3('0x2f')]});})[_0x7fb3('0xc')](function(){logger[_0x7fb3('0x8')](util['format']('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x790217['id']));_0x411ee0();});case'FAILED':return _0x790217[_0x7fb3('0x19')]({'transcribeStatus':'FAILED','failureReason':_0x1f46a0[_0x7fb3('0xe')]['FailureReason']})[_0x7fb3('0xc')](function(){logger[_0x7fb3('0x8')](util[_0x7fb3('0x9')](_0x7fb3('0x30'),_0x790217['id']));_0x411ee0();});default:logger[_0x7fb3('0x8')](util[_0x7fb3('0x9')](_0x7fb3('0x31'),_0x790217['id']));break;}}else{return BPromise[_0x7fb3('0x32')]()[_0x7fb3('0xc')](function(){if(!_0x790217['tempSentiment']){throw new db[(_0x7fb3('0x1b'))][(_0x7fb3('0x1c'))](_0x7fb3('0x1d'));}if(!_0x4f4612[_0x7fb3('0x1e')]||!_0x4f4612[_0x7fb3('0x22')]||!_0x4f4612['sentimentRegion']||!_0x4f4612['language']||!_0x4f4612[_0x7fb3('0x20')]){throw new db[(_0x7fb3('0x1b'))][(_0x7fb3('0x1c'))]('Sentiment\x20Analysis\x20not\x20configured');}if(!_0x790217[_0x7fb3('0x17')]){throw new Error(_0x7fb3('0x33'));}if(_0x4f4612[_0x7fb3('0x22')]===_0x1f946d['id']){return _0x1f946d;}else{return db[_0x7fb3('0x23')][_0x7fb3('0x24')]({'where':{'id':_settings[_0x7fb3('0x22')]}});}})[_0x7fb3('0xc')](function(_0x12d477){if(!_0x12d477){throw new db[(_0x7fb3('0x1b'))]['ValidationError'](_0x7fb3('0x25'));}if(!_0x12d477[_0x7fb3('0xb')]||!_0x12d477[_0x7fb3('0x26')]){throw new db[(_0x7fb3('0x1b'))][(_0x7fb3('0x1c'))](_0x7fb3('0x27'));}_0x3ab550=_0x12d477;logger[_0x7fb3('0x8')](util['format'](_0x7fb3('0x28'),_0x790217['id']));return sentiment(_0x3ab550['data1'],_0x3ab550[_0x7fb3('0x26')],_0x4f4612[_0x7fb3('0x29')],_0x790217[_0x7fb3('0x17')],_0x4f4612[_0x7fb3('0x1f')]);})[_0x7fb3('0xc')](function(_0x22aa4e){logger[_0x7fb3('0x8')](util['format'](_0x7fb3('0x2a'),_0x790217['id']));return _0x790217[_0x7fb3('0x19')]({'sentiment':_0x22aa4e[_0x7fb3('0x2b')],'sPositive':_0x22aa4e[_0x7fb3('0x2c')]['Positive'],'sNegative':_0x22aa4e['SentimentScore'][_0x7fb3('0x34')],'sMixed':_0x22aa4e[_0x7fb3('0x2c')][_0x7fb3('0x2e')],'sNeutral':_0x22aa4e[_0x7fb3('0x2c')][_0x7fb3('0x2f')],'transcribeStatus':'COMPLETED'});})[_0x7fb3('0xc')](function(){logger[_0x7fb3('0x8')](util[_0x7fb3('0x9')](_0x7fb3('0x30'),_0x790217['id']));_0x411ee0();});}})[_0x7fb3('0x35')](function(_0x32bd8f){if(_0x32bd8f){if(!_0x32bd8f[_0x7fb3('0x36')]||_0x32bd8f[_0x7fb3('0x36')]!==_0x7fb3('0x37')){logger[_0x7fb3('0x38')](util[_0x7fb3('0x9')](_0x7fb3('0x39'),_0x790217['id']),util[_0x7fb3('0x3a')](_0x32bd8f,{'showHidden':![],'depth':null}));}}_0x411ee0();});});}function checkTranscribe(){var _0x3037bb;var _0x329ea3;var _0x4007fa;return db[_0x7fb3('0x3b')]['findAll']({'where':{'transcribeStatus':'IN_PROGRESS'}})['then'](function(_0x300c0a){if(!_0x300c0a||!_0x300c0a[_0x7fb3('0x16')]){throw new db['Sequelize'][(_0x7fb3('0x1c'))](_0x7fb3('0x3c'));}_0x3037bb=_0x300c0a;return db[_0x7fb3('0x3d')][_0x7fb3('0x24')]({'where':{'id':0x1}});})[_0x7fb3('0xc')](function(_0x5e0ccd){if(!_0x5e0ccd){throw new db[(_0x7fb3('0x1b'))]['ValidationError'](_0x7fb3('0x3e'));}_0x329ea3=_0x5e0ccd;if(!_0x329ea3[_0x7fb3('0x3f')]||!_0x329ea3[_0x7fb3('0x40')]||!_0x329ea3[_0x7fb3('0x41')]||!_0x329ea3[_0x7fb3('0x1f')]||!_0x329ea3['bucket']){throw new db[(_0x7fb3('0x1b'))]['ValidationError'](_0x7fb3('0x42'));}return db[_0x7fb3('0x23')][_0x7fb3('0x24')]({'where':{'id':_0x329ea3[_0x7fb3('0x40')]}});})[_0x7fb3('0xc')](function(_0x3129f6){if(!_0x3129f6){throw new db[(_0x7fb3('0x1b'))]['ValidationError']('Cloud\x20Provider\x20not\x20available');}if(!_0x3129f6[_0x7fb3('0xb')]||!_0x3129f6['data2']){throw new db[(_0x7fb3('0x1b'))][(_0x7fb3('0x1c'))](_0x7fb3('0x27'));}_0x4007fa=_0x3129f6;var _0x543153=[];_0x3037bb[_0x7fb3('0x43')](function(_0x334fe7){_0x543153[_0x7fb3('0x44')](checkTranscribeJob(_0x334fe7,_0x329ea3,_0x4007fa));});return BPromise[_0x7fb3('0x45')](_0x543153);})[_0x7fb3('0x35')](function(_0x2a7f09){if(_0x2a7f09){if(!_0x2a7f09[_0x7fb3('0x36')]||_0x2a7f09[_0x7fb3('0x36')]!==_0x7fb3('0x37')){logger[_0x7fb3('0x38')](util[_0x7fb3('0x3a')](_0x2a7f09,{'showHidden':![],'depth':null}));}}});}exports['start']=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};