Built motion from commit d69f4380.|2.6.29
[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 _0xab26=['lodash','aws-sdk','request-promise','bluebird','../../mysqldb','TranscribeService','Comprehend','substring','info','format','[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)','transcribeName','data2','then','[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s','TranscriptionJob','TranscriptionJobStatus','fileText','COMPLETED','TranscriptFileUri','[RECORDING:%d]\x20Get\x20transcript','[RECORDING:%d]\x20Transcript\x20downloaded','results','transcripts','transcript','Sequelize','ValidationError','Sentiment\x20Analysis\x20not\x20requested','sentiment','sentimentAccountId','sentimentRegion','language','bucket','Sentiment\x20Analysis\x20not\x20configured','findOne','data1','Cloud\x20Provider\x20not\x20configured','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','update','Sentiment','SentimentScore','Negative','Mixed','FAILED','FailureReason','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed','tempSentiment','Transcript\x20not\x20available','CloudProvider','Cloud\x20Provider\x20not\x20available','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','Neutral','name','error','VoiceRecording','IN_PROGRESS','length','No\x20transcribe\x20job\x20to\x20check','Setting','Settings\x20not\x20available','transcribe','transcribeAccountId','transcribeRegion','forEach','push','all','catch','SequelizeValidationError','inspect'];(function(_0x4e6558,_0x417981){var _0x595c71=function(_0x46b1c6){while(--_0x46b1c6){_0x4e6558['push'](_0x4e6558['shift']());}};_0x595c71(++_0x417981);}(_0xab26,0xd2));var _0x6ab2=function(_0x20a584,_0xe384a5){_0x20a584=_0x20a584-0x0;var _0x4b41a8=_0xab26[_0x20a584];return _0x4b41a8;};var _=require(_0x6ab2('0x0'));var AWS=require(_0x6ab2('0x1'));var rp=require(_0x6ab2('0x2'));var util=require('util');var BPromise=require(_0x6ab2('0x3'));var db=require(_0x6ab2('0x4'))['db'];var logger=require('../../config/logger')('transcribe');function getTranscriptionJob(_0x4c8cf,_0x51bdcd,_0x332458,_0xb15094){return new Promise(function(_0x31c9f1,_0x56dc80){var _0x41643a=new AWS[(_0x6ab2('0x5'))]({'accessKeyId':_0x51bdcd,'secretAccessKey':_0x4c8cf,'region':_0x332458});_0x41643a['getTranscriptionJob']({'TranscriptionJobName':_0xb15094},function(_0x140cc1,_0x3157f1){if(_0x140cc1){return _0x56dc80(_0x140cc1);}else{return _0x31c9f1(_0x3157f1);}});});}function sentiment(_0x442ae2,_0xf9b141,_0x3bb68b,_0x3964fd,_0x217b42){return new Promise(function(_0x448216,_0x459538){var _0x2e765a=new AWS[(_0x6ab2('0x6'))]({'accessKeyId':_0xf9b141,'secretAccessKey':_0x442ae2,'region':_0x3bb68b});_0x2e765a['detectSentiment']({'Text':_0x3964fd,'LanguageCode':_0x217b42[_0x6ab2('0x7')](0x0,0x2)},function(_0x2db481,_0x3c4612){if(_0x2db481){return _0x459538(_0x2db481);}else{return _0x448216(_0x3c4612);}});});}function checkTranscribeJob(_0x5b7e6d,_0x5b6f7c,_0x10c3b1){var _0x2bb464;return new BPromise(function(_0x431d63,_0x8d1dc8){logger[_0x6ab2('0x8')](util[_0x6ab2('0x9')](_0x6ab2('0xa'),_0x5b7e6d['id'],_0x5b7e6d[_0x6ab2('0xb')]));return getTranscriptionJob(_0x10c3b1['data1'],_0x10c3b1[_0x6ab2('0xc')],_0x5b6f7c['transcribeRegion'],_0x5b7e6d[_0x6ab2('0xb')])[_0x6ab2('0xd')](function(_0x5ced24){logger['info'](util['format'](_0x6ab2('0xe'),_0x5b7e6d['id'],_0x5b7e6d[_0x6ab2('0xb')],_0x5ced24[_0x6ab2('0xf')][_0x6ab2('0x10')]));if(!_0x5b7e6d[_0x6ab2('0x11')]){switch(_0x5ced24[_0x6ab2('0xf')]['TranscriptionJobStatus']){case _0x6ab2('0x12'):var _0x2aac55={'transcribeStatus':_0x5ced24['TranscriptionJob'][_0x6ab2('0x10')],'fileUri':_0x5ced24['TranscriptionJob']['Transcript'][_0x6ab2('0x13')]};logger[_0x6ab2('0x8')](util[_0x6ab2('0x9')](_0x6ab2('0x14'),_0x5b7e6d['id']));return rp({'uri':_0x5ced24[_0x6ab2('0xf')]['Transcript'][_0x6ab2('0x13')],'json':!![]})[_0x6ab2('0xd')](function(_0x3b7a1a){logger[_0x6ab2('0x8')](util[_0x6ab2('0x9')](_0x6ab2('0x15'),_0x5b7e6d['id']));if(_0x3b7a1a&&_0x3b7a1a[_0x6ab2('0x16')]&&_0x3b7a1a[_0x6ab2('0x16')][_0x6ab2('0x17')]&&_0x3b7a1a[_0x6ab2('0x16')]['transcripts']['length']){_0x2aac55['fileText']='';for(var _0x11e287=0x0;_0x11e287<_0x3b7a1a[_0x6ab2('0x16')][_0x6ab2('0x17')]['length'];_0x11e287++){_0x2aac55[_0x6ab2('0x11')]+=_0x3b7a1a[_0x6ab2('0x16')][_0x6ab2('0x17')][_0x11e287][_0x6ab2('0x18')]+'';}}return _0x5b7e6d['update'](_0x2aac55);})['then'](function(){logger['info'](util[_0x6ab2('0x9')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x5b7e6d['id']));if(!_0x5b7e6d['tempSentiment']){throw new db[(_0x6ab2('0x19'))][(_0x6ab2('0x1a'))](_0x6ab2('0x1b'));}if(!_0x5b6f7c[_0x6ab2('0x1c')]||!_0x5b6f7c[_0x6ab2('0x1d')]||!_0x5b6f7c[_0x6ab2('0x1e')]||!_0x5b6f7c[_0x6ab2('0x1f')]||!_0x5b6f7c[_0x6ab2('0x20')]){throw new db[(_0x6ab2('0x19'))]['ValidationError'](_0x6ab2('0x21'));}if(!_0x2aac55[_0x6ab2('0x11')]){throw new Error('Transcript\x20not\x20available');}if(_0x5b6f7c[_0x6ab2('0x1d')]===_0x10c3b1['id']){return _0x10c3b1;}else{return db['CloudProvider'][_0x6ab2('0x22')]({'where':{'id':_settings[_0x6ab2('0x1d')]}});}})[_0x6ab2('0xd')](function(_0x151367){if(!_0x151367){throw new db[(_0x6ab2('0x19'))][(_0x6ab2('0x1a'))]('Cloud\x20Provider\x20not\x20available');}if(!_0x151367[_0x6ab2('0x23')]||!_0x151367[_0x6ab2('0xc')]){throw new db[(_0x6ab2('0x19'))][(_0x6ab2('0x1a'))](_0x6ab2('0x24'));}_0x2bb464=_0x151367;logger[_0x6ab2('0x8')](util[_0x6ab2('0x9')]('[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis',_0x5b7e6d['id']));return sentiment(_0x2bb464[_0x6ab2('0x23')],_0x2bb464[_0x6ab2('0xc')],_0x5b6f7c['sentimentRegion'],_0x2aac55['fileText'],_0x5b6f7c[_0x6ab2('0x1f')]);})[_0x6ab2('0xd')](function(_0x309165){logger[_0x6ab2('0x8')](util[_0x6ab2('0x9')](_0x6ab2('0x25'),_0x5b7e6d['id']));return _0x5b7e6d[_0x6ab2('0x26')]({'sentiment':_0x309165[_0x6ab2('0x27')],'sPositive':_0x309165['SentimentScore']['Positive'],'sNegative':_0x309165[_0x6ab2('0x28')][_0x6ab2('0x29')],'sMixed':_0x309165[_0x6ab2('0x28')][_0x6ab2('0x2a')],'sNeutral':_0x309165[_0x6ab2('0x28')]['Neutral']});})[_0x6ab2('0xd')](function(){logger[_0x6ab2('0x8')](util[_0x6ab2('0x9')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x5b7e6d['id']));_0x431d63();});case _0x6ab2('0x2b'):return _0x5b7e6d[_0x6ab2('0x26')]({'transcribeStatus':'FAILED','failureReason':_0x5ced24[_0x6ab2('0xf')][_0x6ab2('0x2c')]})[_0x6ab2('0xd')](function(){logger[_0x6ab2('0x8')](util[_0x6ab2('0x9')](_0x6ab2('0x2d'),_0x5b7e6d['id']));_0x431d63();});default:logger[_0x6ab2('0x8')](util['format'](_0x6ab2('0x2e'),_0x5b7e6d['id']));break;}}else{return BPromise['resolve']()[_0x6ab2('0xd')](function(){if(!_0x5b7e6d[_0x6ab2('0x2f')]){throw new db[(_0x6ab2('0x19'))]['ValidationError']('Sentiment\x20Analysis\x20not\x20requested');}if(!_0x5b6f7c[_0x6ab2('0x1c')]||!_0x5b6f7c['sentimentAccountId']||!_0x5b6f7c['sentimentRegion']||!_0x5b6f7c[_0x6ab2('0x1f')]||!_0x5b6f7c[_0x6ab2('0x20')]){throw new db[(_0x6ab2('0x19'))][(_0x6ab2('0x1a'))](_0x6ab2('0x21'));}if(!_0x5b7e6d['fileText']){throw new Error(_0x6ab2('0x30'));}if(_0x5b6f7c['sentimentAccountId']===_0x10c3b1['id']){return _0x10c3b1;}else{return db[_0x6ab2('0x31')][_0x6ab2('0x22')]({'where':{'id':_settings[_0x6ab2('0x1d')]}});}})[_0x6ab2('0xd')](function(_0x507fdc){if(!_0x507fdc){throw new db[(_0x6ab2('0x19'))][(_0x6ab2('0x1a'))](_0x6ab2('0x32'));}if(!_0x507fdc[_0x6ab2('0x23')]||!_0x507fdc[_0x6ab2('0xc')]){throw new db[(_0x6ab2('0x19'))][(_0x6ab2('0x1a'))](_0x6ab2('0x24'));}_0x2bb464=_0x507fdc;logger[_0x6ab2('0x8')](util[_0x6ab2('0x9')](_0x6ab2('0x33'),_0x5b7e6d['id']));return sentiment(_0x2bb464[_0x6ab2('0x23')],_0x2bb464['data2'],_0x5b6f7c[_0x6ab2('0x1e')],_0x5b7e6d[_0x6ab2('0x11')],_0x5b6f7c[_0x6ab2('0x1f')]);})[_0x6ab2('0xd')](function(_0x1b38ae){logger[_0x6ab2('0x8')](util['format'](_0x6ab2('0x25'),_0x5b7e6d['id']));return _0x5b7e6d[_0x6ab2('0x26')]({'sentiment':_0x1b38ae[_0x6ab2('0x27')],'sPositive':_0x1b38ae[_0x6ab2('0x28')]['Positive'],'sNegative':_0x1b38ae['SentimentScore'][_0x6ab2('0x29')],'sMixed':_0x1b38ae[_0x6ab2('0x28')][_0x6ab2('0x2a')],'sNeutral':_0x1b38ae[_0x6ab2('0x28')][_0x6ab2('0x34')],'transcribeStatus':'COMPLETED'});})[_0x6ab2('0xd')](function(){logger[_0x6ab2('0x8')](util[_0x6ab2('0x9')](_0x6ab2('0x2d'),_0x5b7e6d['id']));_0x431d63();});}})['catch'](function(_0x538532){if(_0x538532){if(!_0x538532[_0x6ab2('0x35')]||_0x538532[_0x6ab2('0x35')]!=='SequelizeValidationError'){logger[_0x6ab2('0x36')](util['format']('[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis',_0x5b7e6d['id']),util['inspect'](_0x538532,{'showHidden':![],'depth':null}));}}_0x431d63();});});}function checkTranscribe(){var _0x1a7e42;var _0x2bee34;var _0x4fffb4;return db[_0x6ab2('0x37')]['findAll']({'where':{'transcribeStatus':_0x6ab2('0x38')}})[_0x6ab2('0xd')](function(_0xe385cf){if(!_0xe385cf||!_0xe385cf[_0x6ab2('0x39')]){throw new db['Sequelize'][(_0x6ab2('0x1a'))](_0x6ab2('0x3a'));}_0x1a7e42=_0xe385cf;return db[_0x6ab2('0x3b')][_0x6ab2('0x22')]({'where':{'id':0x1}});})[_0x6ab2('0xd')](function(_0x301270){if(!_0x301270){throw new db[(_0x6ab2('0x19'))]['ValidationError'](_0x6ab2('0x3c'));}_0x2bee34=_0x301270;if(!_0x2bee34[_0x6ab2('0x3d')]||!_0x2bee34[_0x6ab2('0x3e')]||!_0x2bee34[_0x6ab2('0x3f')]||!_0x2bee34[_0x6ab2('0x1f')]||!_0x2bee34[_0x6ab2('0x20')]){throw new db['Sequelize'][(_0x6ab2('0x1a'))]('Transcribe\x20not\x20configured');}return db[_0x6ab2('0x31')][_0x6ab2('0x22')]({'where':{'id':_0x2bee34[_0x6ab2('0x3e')]}});})[_0x6ab2('0xd')](function(_0x145428){if(!_0x145428){throw new db[(_0x6ab2('0x19'))][(_0x6ab2('0x1a'))](_0x6ab2('0x32'));}if(!_0x145428[_0x6ab2('0x23')]||!_0x145428['data2']){throw new db[(_0x6ab2('0x19'))]['ValidationError']('Cloud\x20Provider\x20not\x20configured');}_0x4fffb4=_0x145428;var _0x3dde6b=[];_0x1a7e42[_0x6ab2('0x40')](function(_0x12eab2){_0x3dde6b[_0x6ab2('0x41')](checkTranscribeJob(_0x12eab2,_0x2bee34,_0x4fffb4));});return BPromise[_0x6ab2('0x42')](_0x3dde6b);})[_0x6ab2('0x43')](function(_0x7f1000){if(_0x7f1000){if(!_0x7f1000[_0x6ab2('0x35')]||_0x7f1000[_0x6ab2('0x35')]!==_0x6ab2('0x44')){logger[_0x6ab2('0x36')](util[_0x6ab2('0x45')](_0x7f1000,{'showHidden':![],'depth':null}));}}});}exports['start']=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};