Built motion from commit (unavailable).|2.5.4
[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 _0x94ae=['SequelizeValidationError','error','[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis','VoiceRecording','IN_PROGRESS','Setting','Transcribe\x20not\x20configured','transcribeAccountId','forEach','push','name','inspect','lodash','aws-sdk','request-promise','util','bluebird','transcribe','TranscribeService','detectSentiment','substring','format','[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)','transcribeName','data1','data2','info','TranscriptionJobStatus','TranscriptionJob','Transcript','TranscriptFileUri','[RECORDING:%d]\x20Get\x20transcript','results','transcripts','length','fileText','update','Sequelize','ValidationError','Sentiment\x20Analysis\x20not\x20requested','sentimentAccountId','sentimentRegion','bucket','Sentiment\x20Analysis\x20not\x20configured','Transcript\x20not\x20available','CloudProvider','findOne','then','Cloud\x20Provider\x20not\x20available','Cloud\x20Provider\x20not\x20configured','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','language','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','Sentiment','SentimentScore','Positive','Mixed','FAILED','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed','resolve','Negative','COMPLETED','catch'];(function(_0x5e4375,_0x756e2b){var _0x4bd4cd=function(_0x51ed6d){while(--_0x51ed6d){_0x5e4375['push'](_0x5e4375['shift']());}};_0x4bd4cd(++_0x756e2b);}(_0x94ae,0xcc));var _0xe94a=function(_0x5b0c87,_0x358c21){_0x5b0c87=_0x5b0c87-0x0;var _0x147089=_0x94ae[_0x5b0c87];return _0x147089;};var _=require(_0xe94a('0x0'));var AWS=require(_0xe94a('0x1'));var rp=require(_0xe94a('0x2'));var util=require(_0xe94a('0x3'));var BPromise=require(_0xe94a('0x4'));var db=require('../../mysqldb')['db'];var logger=require('../../config/logger')(_0xe94a('0x5'));function getTranscriptionJob(_0x3c0d95,_0x4944b0,_0x33c62d,_0x279fbc){return new Promise(function(_0x2f3a5e,_0x49cd75){var _0x31fc9f=new AWS[(_0xe94a('0x6'))]({'accessKeyId':_0x4944b0,'secretAccessKey':_0x3c0d95,'region':_0x33c62d});_0x31fc9f['getTranscriptionJob']({'TranscriptionJobName':_0x279fbc},function(_0x67faf3,_0x599f99){if(_0x67faf3){return _0x49cd75(_0x67faf3);}else{return _0x2f3a5e(_0x599f99);}});});}function sentiment(_0x1630c0,_0x5b1338,_0x45e3f2,_0x19f1ba,_0x58d4e2){return new Promise(function(_0x827438,_0x5ad65e){var _0x215f93=new AWS['Comprehend']({'accessKeyId':_0x5b1338,'secretAccessKey':_0x1630c0,'region':_0x45e3f2});_0x215f93[_0xe94a('0x7')]({'Text':_0x19f1ba,'LanguageCode':_0x58d4e2[_0xe94a('0x8')](0x0,0x2)},function(_0x86ba68,_0x6dc17f){if(_0x86ba68){return _0x5ad65e(_0x86ba68);}else{return _0x827438(_0x6dc17f);}});});}function checkTranscribeJob(_0x4f4f5c,_0x5170ea,_0x267b5e){var _0x2f6eb6;return new BPromise(function(_0x41f652,_0x3b6fb0){logger['info'](util[_0xe94a('0x9')](_0xe94a('0xa'),_0x4f4f5c['id'],_0x4f4f5c[_0xe94a('0xb')]));return getTranscriptionJob(_0x267b5e[_0xe94a('0xc')],_0x267b5e[_0xe94a('0xd')],_0x5170ea['transcribeRegion'],_0x4f4f5c[_0xe94a('0xb')])['then'](function(_0x11bc17){logger[_0xe94a('0xe')](util[_0xe94a('0x9')]('[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s',_0x4f4f5c['id'],_0x4f4f5c['transcribeName'],_0x11bc17['TranscriptionJob'][_0xe94a('0xf')]));if(!_0x4f4f5c['fileText']){switch(_0x11bc17['TranscriptionJob'][_0xe94a('0xf')]){case'COMPLETED':var _0x51cadd={'transcribeStatus':_0x11bc17[_0xe94a('0x10')]['TranscriptionJobStatus'],'fileUri':_0x11bc17[_0xe94a('0x10')][_0xe94a('0x11')][_0xe94a('0x12')]};logger[_0xe94a('0xe')](util[_0xe94a('0x9')](_0xe94a('0x13'),_0x4f4f5c['id']));return rp({'uri':_0x11bc17['TranscriptionJob'][_0xe94a('0x11')]['TranscriptFileUri'],'json':!![]})['then'](function(_0x32add5){logger[_0xe94a('0xe')](util['format']('[RECORDING:%d]\x20Transcript\x20downloaded',_0x4f4f5c['id']));if(_0x32add5&&_0x32add5[_0xe94a('0x14')]&&_0x32add5[_0xe94a('0x14')]['transcripts']&&_0x32add5['results'][_0xe94a('0x15')][_0xe94a('0x16')]){_0x51cadd[_0xe94a('0x17')]='';for(var _0x165ccd=0x0;_0x165ccd<_0x32add5[_0xe94a('0x14')][_0xe94a('0x15')]['length'];_0x165ccd++){_0x51cadd['fileText']+=_0x32add5['results']['transcripts'][_0x165ccd]['transcript']+'';}}return _0x4f4f5c[_0xe94a('0x18')](_0x51cadd);})['then'](function(){logger[_0xe94a('0xe')](util[_0xe94a('0x9')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x4f4f5c['id']));if(!_0x4f4f5c['tempSentiment']){throw new db[(_0xe94a('0x19'))][(_0xe94a('0x1a'))](_0xe94a('0x1b'));}if(!_0x5170ea['sentiment']||!_0x5170ea[_0xe94a('0x1c')]||!_0x5170ea[_0xe94a('0x1d')]||!_0x5170ea['language']||!_0x5170ea[_0xe94a('0x1e')]){throw new db[(_0xe94a('0x19'))][(_0xe94a('0x1a'))](_0xe94a('0x1f'));}if(!_0x51cadd[_0xe94a('0x17')]){throw new Error(_0xe94a('0x20'));}if(_0x5170ea[_0xe94a('0x1c')]===_0x267b5e['id']){return _0x267b5e;}else{return db[_0xe94a('0x21')][_0xe94a('0x22')]({'where':{'id':_settings['sentimentAccountId']}});}})[_0xe94a('0x23')](function(_0x384765){if(!_0x384765){throw new db[(_0xe94a('0x19'))][(_0xe94a('0x1a'))](_0xe94a('0x24'));}if(!_0x384765[_0xe94a('0xc')]||!_0x384765[_0xe94a('0xd')]){throw new db[(_0xe94a('0x19'))][(_0xe94a('0x1a'))](_0xe94a('0x25'));}_0x2f6eb6=_0x384765;logger[_0xe94a('0xe')](util[_0xe94a('0x9')](_0xe94a('0x26'),_0x4f4f5c['id']));return sentiment(_0x2f6eb6[_0xe94a('0xc')],_0x2f6eb6['data2'],_0x5170ea['sentimentRegion'],_0x51cadd[_0xe94a('0x17')],_0x5170ea[_0xe94a('0x27')]);})[_0xe94a('0x23')](function(_0x4963d5){logger[_0xe94a('0xe')](util['format'](_0xe94a('0x28'),_0x4f4f5c['id']));return _0x4f4f5c[_0xe94a('0x18')]({'sentiment':_0x4963d5[_0xe94a('0x29')],'sPositive':_0x4963d5[_0xe94a('0x2a')][_0xe94a('0x2b')],'sNegative':_0x4963d5['SentimentScore']['Negative'],'sMixed':_0x4963d5['SentimentScore'][_0xe94a('0x2c')],'sNeutral':_0x4963d5[_0xe94a('0x2a')]['Neutral']});})['then'](function(){logger[_0xe94a('0xe')](util[_0xe94a('0x9')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x4f4f5c['id']));_0x41f652();});case _0xe94a('0x2d'):return _0x4f4f5c[_0xe94a('0x18')]({'transcribeStatus':_0xe94a('0x2d'),'failureReason':_0x11bc17['TranscriptionJob']['FailureReason']})[_0xe94a('0x23')](function(){logger['info'](util['format'](_0xe94a('0x2e'),_0x4f4f5c['id']));_0x41f652();});default:logger[_0xe94a('0xe')](util[_0xe94a('0x9')](_0xe94a('0x2f'),_0x4f4f5c['id']));break;}}else{return BPromise[_0xe94a('0x30')]()['then'](function(){if(!_0x4f4f5c['tempSentiment']){throw new db[(_0xe94a('0x19'))]['ValidationError'](_0xe94a('0x1b'));}if(!_0x5170ea['sentiment']||!_0x5170ea['sentimentAccountId']||!_0x5170ea[_0xe94a('0x1d')]||!_0x5170ea[_0xe94a('0x27')]||!_0x5170ea[_0xe94a('0x1e')]){throw new db[(_0xe94a('0x19'))]['ValidationError'](_0xe94a('0x1f'));}if(!_0x4f4f5c[_0xe94a('0x17')]){throw new Error('Transcript\x20not\x20available');}if(_0x5170ea[_0xe94a('0x1c')]===_0x267b5e['id']){return _0x267b5e;}else{return db[_0xe94a('0x21')][_0xe94a('0x22')]({'where':{'id':_settings[_0xe94a('0x1c')]}});}})['then'](function(_0x5315a4){if(!_0x5315a4){throw new db['Sequelize'][(_0xe94a('0x1a'))](_0xe94a('0x24'));}if(!_0x5315a4[_0xe94a('0xc')]||!_0x5315a4['data2']){throw new db[(_0xe94a('0x19'))]['ValidationError']('Cloud\x20Provider\x20not\x20configured');}_0x2f6eb6=_0x5315a4;logger[_0xe94a('0xe')](util['format'](_0xe94a('0x26'),_0x4f4f5c['id']));return sentiment(_0x2f6eb6[_0xe94a('0xc')],_0x2f6eb6[_0xe94a('0xd')],_0x5170ea[_0xe94a('0x1d')],_0x4f4f5c[_0xe94a('0x17')],_0x5170ea['language']);})[_0xe94a('0x23')](function(_0x27ab0d){logger[_0xe94a('0xe')](util[_0xe94a('0x9')](_0xe94a('0x28'),_0x4f4f5c['id']));return _0x4f4f5c['update']({'sentiment':_0x27ab0d[_0xe94a('0x29')],'sPositive':_0x27ab0d['SentimentScore'][_0xe94a('0x2b')],'sNegative':_0x27ab0d[_0xe94a('0x2a')][_0xe94a('0x31')],'sMixed':_0x27ab0d[_0xe94a('0x2a')][_0xe94a('0x2c')],'sNeutral':_0x27ab0d['SentimentScore']['Neutral'],'transcribeStatus':_0xe94a('0x32')});})[_0xe94a('0x23')](function(){logger[_0xe94a('0xe')](util[_0xe94a('0x9')](_0xe94a('0x2e'),_0x4f4f5c['id']));_0x41f652();});}})[_0xe94a('0x33')](function(_0x57b6dd){if(_0x57b6dd){if(!_0x57b6dd['name']||_0x57b6dd['name']!==_0xe94a('0x34')){logger[_0xe94a('0x35')](util[_0xe94a('0x9')](_0xe94a('0x36'),_0x4f4f5c['id']),util['inspect'](_0x57b6dd,{'showHidden':![],'depth':null}));}}_0x41f652();});});}function checkTranscribe(){var _0x4fe373;var _0x488631;var _0x42eab9;return db[_0xe94a('0x37')]['findAll']({'where':{'transcribeStatus':_0xe94a('0x38')}})['then'](function(_0x287509){if(!_0x287509||!_0x287509['length']){throw new db[(_0xe94a('0x19'))][(_0xe94a('0x1a'))]('No\x20transcribe\x20job\x20to\x20check');}_0x4fe373=_0x287509;return db[_0xe94a('0x39')]['findOne']({'where':{'id':0x1}});})[_0xe94a('0x23')](function(_0x8d7898){if(!_0x8d7898){throw new db['Sequelize'][(_0xe94a('0x1a'))]('Settings\x20not\x20available');}_0x488631=_0x8d7898;if(!_0x488631[_0xe94a('0x5')]||!_0x488631['transcribeAccountId']||!_0x488631['transcribeRegion']||!_0x488631[_0xe94a('0x27')]||!_0x488631['bucket']){throw new db[(_0xe94a('0x19'))][(_0xe94a('0x1a'))](_0xe94a('0x3a'));}return db['CloudProvider'][_0xe94a('0x22')]({'where':{'id':_0x488631[_0xe94a('0x3b')]}});})['then'](function(_0x1c6efb){if(!_0x1c6efb){throw new db[(_0xe94a('0x19'))][(_0xe94a('0x1a'))](_0xe94a('0x24'));}if(!_0x1c6efb[_0xe94a('0xc')]||!_0x1c6efb['data2']){throw new db[(_0xe94a('0x19'))][(_0xe94a('0x1a'))](_0xe94a('0x25'));}_0x42eab9=_0x1c6efb;var _0x1c69ec=[];_0x4fe373[_0xe94a('0x3c')](function(_0x3ddfe4){_0x1c69ec[_0xe94a('0x3d')](checkTranscribeJob(_0x3ddfe4,_0x488631,_0x42eab9));});return BPromise['all'](_0x1c69ec);})[_0xe94a('0x33')](function(_0x192ff0){if(_0x192ff0){if(!_0x192ff0[_0xe94a('0x3e')]||_0x192ff0[_0xe94a('0x3e')]!==_0xe94a('0x34')){logger[_0xe94a('0x35')](util[_0xe94a('0x3f')](_0x192ff0,{'showHidden':![],'depth':null}));}}});}exports['start']=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};