Built motion from commit 753c950e.|2.5.42
[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 _0x7033=['Neutral','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','FAILED','tempSentiment','SequelizeValidationError','error','inspect','VoiceRecording','IN_PROGRESS','Setting','Settings\x20not\x20available','transcribeAccountId','Transcribe\x20not\x20configured','forEach','all','catch','name','start','lodash','aws-sdk','request-promise','util','../../mysqldb','../../config/logger','transcribe','substring','info','format','[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)','transcribeName','data1','data2','transcribeRegion','then','[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s','TranscriptionJob','fileText','TranscriptionJobStatus','Transcript','TranscriptFileUri','transcripts','results','length','transcript','update','Sequelize','Sentiment\x20Analysis\x20not\x20requested','sentiment','language','bucket','ValidationError','Sentiment\x20Analysis\x20not\x20configured','Transcript\x20not\x20available','sentimentAccountId','CloudProvider','findOne','Cloud\x20Provider\x20not\x20available','Cloud\x20Provider\x20not\x20configured','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','sentimentRegion','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','Sentiment','Positive','Negative','SentimentScore','Mixed'];(function(_0x2e6b70,_0x555b6e){var _0x181f91=function(_0x179a73){while(--_0x179a73){_0x2e6b70['push'](_0x2e6b70['shift']());}};_0x181f91(++_0x555b6e);}(_0x7033,0x19e));var _0x3703=function(_0x4b369d,_0x265eb8){_0x4b369d=_0x4b369d-0x0;var _0x571a98=_0x7033[_0x4b369d];return _0x571a98;};var _=require(_0x3703('0x0'));var AWS=require(_0x3703('0x1'));var rp=require(_0x3703('0x2'));var util=require(_0x3703('0x3'));var BPromise=require('bluebird');var db=require(_0x3703('0x4'))['db'];var logger=require(_0x3703('0x5'))(_0x3703('0x6'));function getTranscriptionJob(_0x2f080e,_0x4102da,_0xab35c3,_0x3fb885){return new Promise(function(_0x283af9,_0x1170b6){var _0xb3a377=new AWS['TranscribeService']({'accessKeyId':_0x4102da,'secretAccessKey':_0x2f080e,'region':_0xab35c3});_0xb3a377['getTranscriptionJob']({'TranscriptionJobName':_0x3fb885},function(_0x4ca371,_0x12c111){if(_0x4ca371){return _0x1170b6(_0x4ca371);}else{return _0x283af9(_0x12c111);}});});}function sentiment(_0x16d296,_0x481d8c,_0x41ba6e,_0x97fbf8,_0x5b252d){return new Promise(function(_0x6acdb7,_0x367148){var _0x28e18d=new AWS['Comprehend']({'accessKeyId':_0x481d8c,'secretAccessKey':_0x16d296,'region':_0x41ba6e});_0x28e18d['detectSentiment']({'Text':_0x97fbf8,'LanguageCode':_0x5b252d[_0x3703('0x7')](0x0,0x2)},function(_0x5b898d,_0x3074ab){if(_0x5b898d){return _0x367148(_0x5b898d);}else{return _0x6acdb7(_0x3074ab);}});});}function checkTranscribeJob(_0x21361e,_0x31c0df,_0x41af55){var _0x520ce3;return new BPromise(function(_0x5b13ee,_0x3fcc9b){logger[_0x3703('0x8')](util[_0x3703('0x9')](_0x3703('0xa'),_0x21361e['id'],_0x21361e[_0x3703('0xb')]));return getTranscriptionJob(_0x41af55[_0x3703('0xc')],_0x41af55[_0x3703('0xd')],_0x31c0df[_0x3703('0xe')],_0x21361e[_0x3703('0xb')])[_0x3703('0xf')](function(_0x3a8a89){logger[_0x3703('0x8')](util[_0x3703('0x9')](_0x3703('0x10'),_0x21361e['id'],_0x21361e['transcribeName'],_0x3a8a89[_0x3703('0x11')]['TranscriptionJobStatus']));if(!_0x21361e[_0x3703('0x12')]){switch(_0x3a8a89['TranscriptionJob'][_0x3703('0x13')]){case'COMPLETED':var _0x176ddc={'transcribeStatus':_0x3a8a89[_0x3703('0x11')]['TranscriptionJobStatus'],'fileUri':_0x3a8a89[_0x3703('0x11')][_0x3703('0x14')][_0x3703('0x15')]};logger[_0x3703('0x8')](util[_0x3703('0x9')]('[RECORDING:%d]\x20Get\x20transcript',_0x21361e['id']));return rp({'uri':_0x3a8a89[_0x3703('0x11')][_0x3703('0x14')][_0x3703('0x15')],'json':!![]})['then'](function(_0x386c4a){logger[_0x3703('0x8')](util[_0x3703('0x9')]('[RECORDING:%d]\x20Transcript\x20downloaded',_0x21361e['id']));if(_0x386c4a&&_0x386c4a['results']&&_0x386c4a['results'][_0x3703('0x16')]&&_0x386c4a[_0x3703('0x17')][_0x3703('0x16')][_0x3703('0x18')]){_0x176ddc['fileText']='';for(var _0x4fe6dd=0x0;_0x4fe6dd<_0x386c4a[_0x3703('0x17')]['transcripts']['length'];_0x4fe6dd++){_0x176ddc[_0x3703('0x12')]+=_0x386c4a[_0x3703('0x17')]['transcripts'][_0x4fe6dd][_0x3703('0x19')]+'';}}return _0x21361e[_0x3703('0x1a')](_0x176ddc);})[_0x3703('0xf')](function(){logger[_0x3703('0x8')](util[_0x3703('0x9')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x21361e['id']));if(!_0x21361e['tempSentiment']){throw new db[(_0x3703('0x1b'))]['ValidationError'](_0x3703('0x1c'));}if(!_0x31c0df[_0x3703('0x1d')]||!_0x31c0df['sentimentAccountId']||!_0x31c0df['sentimentRegion']||!_0x31c0df[_0x3703('0x1e')]||!_0x31c0df[_0x3703('0x1f')]){throw new db[(_0x3703('0x1b'))][(_0x3703('0x20'))](_0x3703('0x21'));}if(!_0x176ddc[_0x3703('0x12')]){throw new Error(_0x3703('0x22'));}if(_0x31c0df[_0x3703('0x23')]===_0x41af55['id']){return _0x41af55;}else{return db[_0x3703('0x24')][_0x3703('0x25')]({'where':{'id':_settings[_0x3703('0x23')]}});}})['then'](function(_0x244f74){if(!_0x244f74){throw new db[(_0x3703('0x1b'))][(_0x3703('0x20'))](_0x3703('0x26'));}if(!_0x244f74[_0x3703('0xc')]||!_0x244f74[_0x3703('0xd')]){throw new db[(_0x3703('0x1b'))][(_0x3703('0x20'))](_0x3703('0x27'));}_0x520ce3=_0x244f74;logger[_0x3703('0x8')](util[_0x3703('0x9')](_0x3703('0x28'),_0x21361e['id']));return sentiment(_0x520ce3[_0x3703('0xc')],_0x520ce3[_0x3703('0xd')],_0x31c0df[_0x3703('0x29')],_0x176ddc[_0x3703('0x12')],_0x31c0df[_0x3703('0x1e')]);})[_0x3703('0xf')](function(_0x33134a){logger[_0x3703('0x8')](util[_0x3703('0x9')](_0x3703('0x2a'),_0x21361e['id']));return _0x21361e['update']({'sentiment':_0x33134a[_0x3703('0x2b')],'sPositive':_0x33134a['SentimentScore'][_0x3703('0x2c')],'sNegative':_0x33134a['SentimentScore'][_0x3703('0x2d')],'sMixed':_0x33134a[_0x3703('0x2e')][_0x3703('0x2f')],'sNeutral':_0x33134a[_0x3703('0x2e')][_0x3703('0x30')]});})['then'](function(){logger[_0x3703('0x8')](util['format'](_0x3703('0x31'),_0x21361e['id']));_0x5b13ee();});case _0x3703('0x32'):return _0x21361e[_0x3703('0x1a')]({'transcribeStatus':'FAILED','failureReason':_0x3a8a89[_0x3703('0x11')]['FailureReason']})[_0x3703('0xf')](function(){logger[_0x3703('0x8')](util[_0x3703('0x9')](_0x3703('0x31'),_0x21361e['id']));_0x5b13ee();});default:logger[_0x3703('0x8')](util[_0x3703('0x9')]('[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed',_0x21361e['id']));break;}}else{return BPromise['resolve']()[_0x3703('0xf')](function(){if(!_0x21361e[_0x3703('0x33')]){throw new db['Sequelize'][(_0x3703('0x20'))](_0x3703('0x1c'));}if(!_0x31c0df[_0x3703('0x1d')]||!_0x31c0df[_0x3703('0x23')]||!_0x31c0df[_0x3703('0x29')]||!_0x31c0df[_0x3703('0x1e')]||!_0x31c0df[_0x3703('0x1f')]){throw new db[(_0x3703('0x1b'))][(_0x3703('0x20'))]('Sentiment\x20Analysis\x20not\x20configured');}if(!_0x21361e['fileText']){throw new Error(_0x3703('0x22'));}if(_0x31c0df[_0x3703('0x23')]===_0x41af55['id']){return _0x41af55;}else{return db['CloudProvider'][_0x3703('0x25')]({'where':{'id':_settings[_0x3703('0x23')]}});}})[_0x3703('0xf')](function(_0x333c51){if(!_0x333c51){throw new db[(_0x3703('0x1b'))][(_0x3703('0x20'))](_0x3703('0x26'));}if(!_0x333c51[_0x3703('0xc')]||!_0x333c51[_0x3703('0xd')]){throw new db['Sequelize']['ValidationError'](_0x3703('0x27'));}_0x520ce3=_0x333c51;logger['info'](util['format'](_0x3703('0x28'),_0x21361e['id']));return sentiment(_0x520ce3[_0x3703('0xc')],_0x520ce3[_0x3703('0xd')],_0x31c0df[_0x3703('0x29')],_0x21361e[_0x3703('0x12')],_0x31c0df[_0x3703('0x1e')]);})[_0x3703('0xf')](function(_0x47dcb1){logger[_0x3703('0x8')](util[_0x3703('0x9')](_0x3703('0x2a'),_0x21361e['id']));return _0x21361e[_0x3703('0x1a')]({'sentiment':_0x47dcb1['Sentiment'],'sPositive':_0x47dcb1['SentimentScore'][_0x3703('0x2c')],'sNegative':_0x47dcb1[_0x3703('0x2e')][_0x3703('0x2d')],'sMixed':_0x47dcb1[_0x3703('0x2e')][_0x3703('0x2f')],'sNeutral':_0x47dcb1[_0x3703('0x2e')][_0x3703('0x30')],'transcribeStatus':'COMPLETED'});})[_0x3703('0xf')](function(){logger[_0x3703('0x8')](util[_0x3703('0x9')](_0x3703('0x31'),_0x21361e['id']));_0x5b13ee();});}})['catch'](function(_0x59a06e){if(_0x59a06e){if(!_0x59a06e['name']||_0x59a06e['name']!==_0x3703('0x34')){logger[_0x3703('0x35')](util[_0x3703('0x9')]('[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis',_0x21361e['id']),util[_0x3703('0x36')](_0x59a06e,{'showHidden':![],'depth':null}));}}_0x5b13ee();});});}function checkTranscribe(){var _0x4d824d;var _0x31e455;var _0x2ab470;return db[_0x3703('0x37')]['findAll']({'where':{'transcribeStatus':_0x3703('0x38')}})[_0x3703('0xf')](function(_0x2df518){if(!_0x2df518||!_0x2df518[_0x3703('0x18')]){throw new db['Sequelize']['ValidationError']('No\x20transcribe\x20job\x20to\x20check');}_0x4d824d=_0x2df518;return db[_0x3703('0x39')]['findOne']({'where':{'id':0x1}});})[_0x3703('0xf')](function(_0x528db8){if(!_0x528db8){throw new db[(_0x3703('0x1b'))]['ValidationError'](_0x3703('0x3a'));}_0x31e455=_0x528db8;if(!_0x31e455[_0x3703('0x6')]||!_0x31e455[_0x3703('0x3b')]||!_0x31e455['transcribeRegion']||!_0x31e455[_0x3703('0x1e')]||!_0x31e455[_0x3703('0x1f')]){throw new db[(_0x3703('0x1b'))][(_0x3703('0x20'))](_0x3703('0x3c'));}return db['CloudProvider'][_0x3703('0x25')]({'where':{'id':_0x31e455['transcribeAccountId']}});})[_0x3703('0xf')](function(_0x4a09f8){if(!_0x4a09f8){throw new db[(_0x3703('0x1b'))][(_0x3703('0x20'))](_0x3703('0x26'));}if(!_0x4a09f8[_0x3703('0xc')]||!_0x4a09f8['data2']){throw new db['Sequelize'][(_0x3703('0x20'))](_0x3703('0x27'));}_0x2ab470=_0x4a09f8;var _0x168437=[];_0x4d824d[_0x3703('0x3d')](function(_0x38ef4d){_0x168437['push'](checkTranscribeJob(_0x38ef4d,_0x31e455,_0x2ab470));});return BPromise[_0x3703('0x3e')](_0x168437);})[_0x3703('0x3f')](function(_0x3ad43a){if(_0x3ad43a){if(!_0x3ad43a[_0x3703('0x40')]||_0x3ad43a[_0x3703('0x40')]!==_0x3703('0x34')){logger['error'](util[_0x3703('0x36')](_0x3ad43a,{'showHidden':![],'depth':null}));}}});}exports[_0x3703('0x41')]=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};