d4462e02f3cc1a4902f21fef2275c3267919bd6f
[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 _0x2179=['TranscriptionJobStatus','fileText','COMPLETED','Transcript','TranscriptFileUri','[RECORDING:%d]\x20Get\x20transcript','[RECORDING:%d]\x20Transcript\x20downloaded','results','transcripts','length','transcript','update','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','tempSentiment','Sequelize','ValidationError','sentiment','sentimentAccountId','bucket','Sentiment\x20Analysis\x20not\x20configured','findOne','Cloud\x20Provider\x20not\x20available','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','SentimentScore','Positive','Neutral','FAILED','[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed','resolve','Sentiment\x20Analysis\x20not\x20requested','sentimentRegion','language','CloudProvider','Cloud\x20Provider\x20not\x20configured','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','Sentiment','Negative','Mixed','name','SequelizeValidationError','error','inspect','VoiceRecording','findAll','Settings\x20not\x20available','transcribeAccountId','Transcribe\x20not\x20configured','forEach','push','start','lodash','aws-sdk','request-promise','util','bluebird','../../mysqldb','../../config/logger','transcribe','getTranscriptionJob','Comprehend','substring','info','[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)','transcribeName','data1','data2','transcribeRegion','then','format','[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s','TranscriptionJob'];(function(_0x222332,_0x52ce1b){var _0x5d944a=function(_0xd80b48){while(--_0xd80b48){_0x222332['push'](_0x222332['shift']());}};_0x5d944a(++_0x52ce1b);}(_0x2179,0xc0));var _0x9217=function(_0x4279e1,_0x39cda4){_0x4279e1=_0x4279e1-0x0;var _0x1e33ea=_0x2179[_0x4279e1];return _0x1e33ea;};var _=require(_0x9217('0x0'));var AWS=require(_0x9217('0x1'));var rp=require(_0x9217('0x2'));var util=require(_0x9217('0x3'));var BPromise=require(_0x9217('0x4'));var db=require(_0x9217('0x5'))['db'];var logger=require(_0x9217('0x6'))(_0x9217('0x7'));function getTranscriptionJob(_0xe708c9,_0x2e397c,_0x3a5922,_0x757f88){return new Promise(function(_0x471e4c,_0x17a107){var _0x1ed3f7=new AWS['TranscribeService']({'accessKeyId':_0x2e397c,'secretAccessKey':_0xe708c9,'region':_0x3a5922});_0x1ed3f7[_0x9217('0x8')]({'TranscriptionJobName':_0x757f88},function(_0x38127f,_0x23b2e7){if(_0x38127f){return _0x17a107(_0x38127f);}else{return _0x471e4c(_0x23b2e7);}});});}function sentiment(_0x2da50f,_0x2286b8,_0x5e7d99,_0x496fb7,_0x25bd97){return new Promise(function(_0x4fe267,_0x1baed8){var _0x3304cb=new AWS[(_0x9217('0x9'))]({'accessKeyId':_0x2286b8,'secretAccessKey':_0x2da50f,'region':_0x5e7d99});_0x3304cb['detectSentiment']({'Text':_0x496fb7,'LanguageCode':_0x25bd97[_0x9217('0xa')](0x0,0x2)},function(_0x2c9f23,_0x322e8c){if(_0x2c9f23){return _0x1baed8(_0x2c9f23);}else{return _0x4fe267(_0x322e8c);}});});}function checkTranscribeJob(_0x23b7aa,_0x203aa8,_0xb21639){var _0x11c99d;return new BPromise(function(_0x1f69bb,_0x5e5bed){logger[_0x9217('0xb')](util['format'](_0x9217('0xc'),_0x23b7aa['id'],_0x23b7aa[_0x9217('0xd')]));return getTranscriptionJob(_0xb21639[_0x9217('0xe')],_0xb21639[_0x9217('0xf')],_0x203aa8[_0x9217('0x10')],_0x23b7aa[_0x9217('0xd')])[_0x9217('0x11')](function(_0x13c330){logger[_0x9217('0xb')](util[_0x9217('0x12')](_0x9217('0x13'),_0x23b7aa['id'],_0x23b7aa[_0x9217('0xd')],_0x13c330[_0x9217('0x14')][_0x9217('0x15')]));if(!_0x23b7aa[_0x9217('0x16')]){switch(_0x13c330[_0x9217('0x14')][_0x9217('0x15')]){case _0x9217('0x17'):var _0x2465c7={'transcribeStatus':_0x13c330[_0x9217('0x14')][_0x9217('0x15')],'fileUri':_0x13c330[_0x9217('0x14')][_0x9217('0x18')][_0x9217('0x19')]};logger[_0x9217('0xb')](util[_0x9217('0x12')](_0x9217('0x1a'),_0x23b7aa['id']));return rp({'uri':_0x13c330[_0x9217('0x14')][_0x9217('0x18')][_0x9217('0x19')],'json':!![]})['then'](function(_0x78e5c){logger[_0x9217('0xb')](util[_0x9217('0x12')](_0x9217('0x1b'),_0x23b7aa['id']));if(_0x78e5c&&_0x78e5c[_0x9217('0x1c')]&&_0x78e5c[_0x9217('0x1c')]['transcripts']&&_0x78e5c[_0x9217('0x1c')][_0x9217('0x1d')][_0x9217('0x1e')]){_0x2465c7['fileText']='';for(var _0x160584=0x0;_0x160584<_0x78e5c[_0x9217('0x1c')][_0x9217('0x1d')][_0x9217('0x1e')];_0x160584++){_0x2465c7[_0x9217('0x16')]+=_0x78e5c[_0x9217('0x1c')]['transcripts'][_0x160584][_0x9217('0x1f')]+'';}}return _0x23b7aa[_0x9217('0x20')](_0x2465c7);})['then'](function(){logger['info'](util['format'](_0x9217('0x21'),_0x23b7aa['id']));if(!_0x23b7aa[_0x9217('0x22')]){throw new db[(_0x9217('0x23'))][(_0x9217('0x24'))]('Sentiment\x20Analysis\x20not\x20requested');}if(!_0x203aa8[_0x9217('0x25')]||!_0x203aa8[_0x9217('0x26')]||!_0x203aa8['sentimentRegion']||!_0x203aa8['language']||!_0x203aa8[_0x9217('0x27')]){throw new db[(_0x9217('0x23'))][(_0x9217('0x24'))](_0x9217('0x28'));}if(!_0x2465c7['fileText']){throw new Error('Transcript\x20not\x20available');}if(_0x203aa8[_0x9217('0x26')]===_0xb21639['id']){return _0xb21639;}else{return db['CloudProvider'][_0x9217('0x29')]({'where':{'id':_settings[_0x9217('0x26')]}});}})[_0x9217('0x11')](function(_0x330006){if(!_0x330006){throw new db[(_0x9217('0x23'))][(_0x9217('0x24'))](_0x9217('0x2a'));}if(!_0x330006['data1']||!_0x330006[_0x9217('0xf')]){throw new db[(_0x9217('0x23'))][(_0x9217('0x24'))]('Cloud\x20Provider\x20not\x20configured');}_0x11c99d=_0x330006;logger[_0x9217('0xb')](util[_0x9217('0x12')]('[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis',_0x23b7aa['id']));return sentiment(_0x11c99d[_0x9217('0xe')],_0x11c99d['data2'],_0x203aa8['sentimentRegion'],_0x2465c7[_0x9217('0x16')],_0x203aa8['language']);})['then'](function(_0x462c22){logger['info'](util[_0x9217('0x12')](_0x9217('0x2b'),_0x23b7aa['id']));return _0x23b7aa[_0x9217('0x20')]({'sentiment':_0x462c22['Sentiment'],'sPositive':_0x462c22[_0x9217('0x2c')][_0x9217('0x2d')],'sNegative':_0x462c22[_0x9217('0x2c')]['Negative'],'sMixed':_0x462c22[_0x9217('0x2c')]['Mixed'],'sNeutral':_0x462c22[_0x9217('0x2c')][_0x9217('0x2e')]});})[_0x9217('0x11')](function(){logger['info'](util['format'](_0x9217('0x21'),_0x23b7aa['id']));_0x1f69bb();});case _0x9217('0x2f'):return _0x23b7aa[_0x9217('0x20')]({'transcribeStatus':'FAILED','failureReason':_0x13c330[_0x9217('0x14')]['FailureReason']})[_0x9217('0x11')](function(){logger[_0x9217('0xb')](util['format'](_0x9217('0x21'),_0x23b7aa['id']));_0x1f69bb();});default:logger['info'](util[_0x9217('0x12')](_0x9217('0x30'),_0x23b7aa['id']));break;}}else{return BPromise[_0x9217('0x31')]()['then'](function(){if(!_0x23b7aa[_0x9217('0x22')]){throw new db[(_0x9217('0x23'))][(_0x9217('0x24'))](_0x9217('0x32'));}if(!_0x203aa8['sentiment']||!_0x203aa8[_0x9217('0x26')]||!_0x203aa8[_0x9217('0x33')]||!_0x203aa8[_0x9217('0x34')]||!_0x203aa8[_0x9217('0x27')]){throw new db['Sequelize'][(_0x9217('0x24'))]('Sentiment\x20Analysis\x20not\x20configured');}if(!_0x23b7aa[_0x9217('0x16')]){throw new Error('Transcript\x20not\x20available');}if(_0x203aa8[_0x9217('0x26')]===_0xb21639['id']){return _0xb21639;}else{return db[_0x9217('0x35')][_0x9217('0x29')]({'where':{'id':_settings['sentimentAccountId']}});}})['then'](function(_0x355c46){if(!_0x355c46){throw new db[(_0x9217('0x23'))][(_0x9217('0x24'))](_0x9217('0x2a'));}if(!_0x355c46['data1']||!_0x355c46['data2']){throw new db[(_0x9217('0x23'))][(_0x9217('0x24'))](_0x9217('0x36'));}_0x11c99d=_0x355c46;logger[_0x9217('0xb')](util[_0x9217('0x12')](_0x9217('0x37'),_0x23b7aa['id']));return sentiment(_0x11c99d[_0x9217('0xe')],_0x11c99d[_0x9217('0xf')],_0x203aa8['sentimentRegion'],_0x23b7aa[_0x9217('0x16')],_0x203aa8['language']);})[_0x9217('0x11')](function(_0x4e892a){logger['info'](util[_0x9217('0x12')](_0x9217('0x2b'),_0x23b7aa['id']));return _0x23b7aa[_0x9217('0x20')]({'sentiment':_0x4e892a[_0x9217('0x38')],'sPositive':_0x4e892a['SentimentScore'][_0x9217('0x2d')],'sNegative':_0x4e892a[_0x9217('0x2c')][_0x9217('0x39')],'sMixed':_0x4e892a['SentimentScore'][_0x9217('0x3a')],'sNeutral':_0x4e892a[_0x9217('0x2c')][_0x9217('0x2e')],'transcribeStatus':_0x9217('0x17')});})[_0x9217('0x11')](function(){logger[_0x9217('0xb')](util[_0x9217('0x12')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x23b7aa['id']));_0x1f69bb();});}})['catch'](function(_0x5438e2){if(_0x5438e2){if(!_0x5438e2[_0x9217('0x3b')]||_0x5438e2[_0x9217('0x3b')]!==_0x9217('0x3c')){logger[_0x9217('0x3d')](util['format']('[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis',_0x23b7aa['id']),util[_0x9217('0x3e')](_0x5438e2,{'showHidden':![],'depth':null}));}}_0x1f69bb();});});}function checkTranscribe(){var _0x1c0a63;var _0xb705f5;var _0x15c4e6;return db[_0x9217('0x3f')][_0x9217('0x40')]({'where':{'transcribeStatus':'IN_PROGRESS'}})[_0x9217('0x11')](function(_0x2f4ce){if(!_0x2f4ce||!_0x2f4ce[_0x9217('0x1e')]){throw new db[(_0x9217('0x23'))]['ValidationError']('No\x20transcribe\x20job\x20to\x20check');}_0x1c0a63=_0x2f4ce;return db['Setting']['findOne']({'where':{'id':0x1}});})['then'](function(_0x9188eb){if(!_0x9188eb){throw new db[(_0x9217('0x23'))][(_0x9217('0x24'))](_0x9217('0x41'));}_0xb705f5=_0x9188eb;if(!_0xb705f5[_0x9217('0x7')]||!_0xb705f5[_0x9217('0x42')]||!_0xb705f5[_0x9217('0x10')]||!_0xb705f5[_0x9217('0x34')]||!_0xb705f5[_0x9217('0x27')]){throw new db[(_0x9217('0x23'))][(_0x9217('0x24'))](_0x9217('0x43'));}return db[_0x9217('0x35')]['findOne']({'where':{'id':_0xb705f5['transcribeAccountId']}});})[_0x9217('0x11')](function(_0x2f9670){if(!_0x2f9670){throw new db[(_0x9217('0x23'))]['ValidationError'](_0x9217('0x2a'));}if(!_0x2f9670[_0x9217('0xe')]||!_0x2f9670[_0x9217('0xf')]){throw new db[(_0x9217('0x23'))]['ValidationError']('Cloud\x20Provider\x20not\x20configured');}_0x15c4e6=_0x2f9670;var _0xea35b1=[];_0x1c0a63[_0x9217('0x44')](function(_0x2c7f65){_0xea35b1[_0x9217('0x45')](checkTranscribeJob(_0x2c7f65,_0xb705f5,_0x15c4e6));});return BPromise['all'](_0xea35b1);})['catch'](function(_0x5c239a){if(_0x5c239a){if(!_0x5c239a[_0x9217('0x3b')]||_0x5c239a['name']!==_0x9217('0x3c')){logger[_0x9217('0x3d')](util[_0x9217('0x3e')](_0x5c239a,{'showHidden':![],'depth':null}));}}});}exports[_0x9217('0x46')]=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};