Built motion from commit 7ab0e143.|2.5.49
[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 _0x9ab9=['transcripts','length','transcript','update','then','[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated','tempSentiment','Sequelize','Sentiment\x20Analysis\x20not\x20requested','sentiment','sentimentRegion','language','bucket','Sentiment\x20Analysis\x20not\x20configured','Transcript\x20not\x20available','sentimentAccountId','CloudProvider','findOne','ValidationError','data1','Cloud\x20Provider\x20not\x20configured','[RECORDING:%d]\x20Sentiment\x20Analysis\x20available','Sentiment','Positive','SentimentScore','Negative','Mixed','Neutral','FAILED','[RECORDING:%d]\x20Transcript\x20Job\x20status\x20not\x20managed','resolve','Cloud\x20Provider\x20not\x20available','[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis','catch','name','inspect','VoiceRecording','findAll','IN_PROGRESS','No\x20transcribe\x20job\x20to\x20check','Setting','Settings\x20not\x20available','Transcribe\x20not\x20configured','transcribeAccountId','forEach','push','all','error','lodash','aws-sdk','request-promise','util','bluebird','../../config/logger','transcribe','TranscribeService','detectSentiment','substring','info','[RECORDING:%d]\x20Get\x20status\x20for\x20transcript\x20job\x20(%s)','transcribeName','data2','transcribeRegion','format','[RECORDING:%d]\x20Status\x20of\x20transcript\x20job\x20(%s):\x20%s','TranscriptionJob','TranscriptionJobStatus','fileText','COMPLETED','Transcript','TranscriptFileUri','[RECORDING:%d]\x20Transcript\x20downloaded','results'];(function(_0x27b4c7,_0xd847e6){var _0x3000b9=function(_0x57ef4b){while(--_0x57ef4b){_0x27b4c7['push'](_0x27b4c7['shift']());}};_0x3000b9(++_0xd847e6);}(_0x9ab9,0x19d));var _0x99ab=function(_0x1e1fc6,_0x588bc1){_0x1e1fc6=_0x1e1fc6-0x0;var _0x283b97=_0x9ab9[_0x1e1fc6];return _0x283b97;};var _=require(_0x99ab('0x0'));var AWS=require(_0x99ab('0x1'));var rp=require(_0x99ab('0x2'));var util=require(_0x99ab('0x3'));var BPromise=require(_0x99ab('0x4'));var db=require('../../mysqldb')['db'];var logger=require(_0x99ab('0x5'))(_0x99ab('0x6'));function getTranscriptionJob(_0x5f28e4,_0x1b1c96,_0x49761a,_0x442e8c){return new Promise(function(_0x1042a3,_0x390d41){var _0x249c1f=new AWS[(_0x99ab('0x7'))]({'accessKeyId':_0x1b1c96,'secretAccessKey':_0x5f28e4,'region':_0x49761a});_0x249c1f['getTranscriptionJob']({'TranscriptionJobName':_0x442e8c},function(_0x5762c1,_0x3f5a2c){if(_0x5762c1){return _0x390d41(_0x5762c1);}else{return _0x1042a3(_0x3f5a2c);}});});}function sentiment(_0x18ca95,_0x4e94f0,_0x14d083,_0xa40f42,_0x1477cc){return new Promise(function(_0xe1660b,_0xe82b20){var _0x23e904=new AWS['Comprehend']({'accessKeyId':_0x4e94f0,'secretAccessKey':_0x18ca95,'region':_0x14d083});_0x23e904[_0x99ab('0x8')]({'Text':_0xa40f42,'LanguageCode':_0x1477cc[_0x99ab('0x9')](0x0,0x2)},function(_0xffdfeb,_0x5bd277){if(_0xffdfeb){return _0xe82b20(_0xffdfeb);}else{return _0xe1660b(_0x5bd277);}});});}function checkTranscribeJob(_0x4e7b4a,_0xed2100,_0x24229c){var _0x3739fb;return new BPromise(function(_0x3b3bba,_0x25f225){logger[_0x99ab('0xa')](util['format'](_0x99ab('0xb'),_0x4e7b4a['id'],_0x4e7b4a[_0x99ab('0xc')]));return getTranscriptionJob(_0x24229c['data1'],_0x24229c[_0x99ab('0xd')],_0xed2100[_0x99ab('0xe')],_0x4e7b4a[_0x99ab('0xc')])['then'](function(_0x4299ae){logger[_0x99ab('0xa')](util[_0x99ab('0xf')](_0x99ab('0x10'),_0x4e7b4a['id'],_0x4e7b4a[_0x99ab('0xc')],_0x4299ae[_0x99ab('0x11')][_0x99ab('0x12')]));if(!_0x4e7b4a[_0x99ab('0x13')]){switch(_0x4299ae['TranscriptionJob'][_0x99ab('0x12')]){case _0x99ab('0x14'):var _0x4193c0={'transcribeStatus':_0x4299ae[_0x99ab('0x11')][_0x99ab('0x12')],'fileUri':_0x4299ae[_0x99ab('0x11')][_0x99ab('0x15')][_0x99ab('0x16')]};logger[_0x99ab('0xa')](util['format']('[RECORDING:%d]\x20Get\x20transcript',_0x4e7b4a['id']));return rp({'uri':_0x4299ae[_0x99ab('0x11')][_0x99ab('0x15')][_0x99ab('0x16')],'json':!![]})['then'](function(_0x2e0464){logger[_0x99ab('0xa')](util[_0x99ab('0xf')](_0x99ab('0x17'),_0x4e7b4a['id']));if(_0x2e0464&&_0x2e0464[_0x99ab('0x18')]&&_0x2e0464[_0x99ab('0x18')][_0x99ab('0x19')]&&_0x2e0464['results'][_0x99ab('0x19')]['length']){_0x4193c0[_0x99ab('0x13')]='';for(var _0x56bdb7=0x0;_0x56bdb7<_0x2e0464['results'][_0x99ab('0x19')][_0x99ab('0x1a')];_0x56bdb7++){_0x4193c0[_0x99ab('0x13')]+=_0x2e0464['results'][_0x99ab('0x19')][_0x56bdb7][_0x99ab('0x1b')]+'';}}return _0x4e7b4a[_0x99ab('0x1c')](_0x4193c0);})[_0x99ab('0x1d')](function(){logger[_0x99ab('0xa')](util[_0x99ab('0xf')](_0x99ab('0x1e'),_0x4e7b4a['id']));if(!_0x4e7b4a[_0x99ab('0x1f')]){throw new db[(_0x99ab('0x20'))]['ValidationError'](_0x99ab('0x21'));}if(!_0xed2100[_0x99ab('0x22')]||!_0xed2100['sentimentAccountId']||!_0xed2100[_0x99ab('0x23')]||!_0xed2100[_0x99ab('0x24')]||!_0xed2100[_0x99ab('0x25')]){throw new db['Sequelize']['ValidationError'](_0x99ab('0x26'));}if(!_0x4193c0[_0x99ab('0x13')]){throw new Error(_0x99ab('0x27'));}if(_0xed2100[_0x99ab('0x28')]===_0x24229c['id']){return _0x24229c;}else{return db[_0x99ab('0x29')][_0x99ab('0x2a')]({'where':{'id':_settings[_0x99ab('0x28')]}});}})[_0x99ab('0x1d')](function(_0x5ba1ea){if(!_0x5ba1ea){throw new db[(_0x99ab('0x20'))][(_0x99ab('0x2b'))]('Cloud\x20Provider\x20not\x20available');}if(!_0x5ba1ea[_0x99ab('0x2c')]||!_0x5ba1ea[_0x99ab('0xd')]){throw new db['Sequelize']['ValidationError'](_0x99ab('0x2d'));}_0x3739fb=_0x5ba1ea;logger[_0x99ab('0xa')](util[_0x99ab('0xf')]('[RECORDING:%d]\x20Requesting\x20sentiment\x20analysis',_0x4e7b4a['id']));return sentiment(_0x3739fb['data1'],_0x3739fb['data2'],_0xed2100['sentimentRegion'],_0x4193c0['fileText'],_0xed2100['language']);})[_0x99ab('0x1d')](function(_0x306721){logger[_0x99ab('0xa')](util['format'](_0x99ab('0x2e'),_0x4e7b4a['id']));return _0x4e7b4a[_0x99ab('0x1c')]({'sentiment':_0x306721[_0x99ab('0x2f')],'sPositive':_0x306721['SentimentScore'][_0x99ab('0x30')],'sNegative':_0x306721[_0x99ab('0x31')][_0x99ab('0x32')],'sMixed':_0x306721[_0x99ab('0x31')][_0x99ab('0x33')],'sNeutral':_0x306721[_0x99ab('0x31')][_0x99ab('0x34')]});})['then'](function(){logger['info'](util[_0x99ab('0xf')](_0x99ab('0x1e'),_0x4e7b4a['id']));_0x3b3bba();});case'FAILED':return _0x4e7b4a[_0x99ab('0x1c')]({'transcribeStatus':_0x99ab('0x35'),'failureReason':_0x4299ae[_0x99ab('0x11')]['FailureReason']})[_0x99ab('0x1d')](function(){logger[_0x99ab('0xa')](util[_0x99ab('0xf')](_0x99ab('0x1e'),_0x4e7b4a['id']));_0x3b3bba();});default:logger[_0x99ab('0xa')](util['format'](_0x99ab('0x36'),_0x4e7b4a['id']));break;}}else{return BPromise[_0x99ab('0x37')]()['then'](function(){if(!_0x4e7b4a[_0x99ab('0x1f')]){throw new db['Sequelize'][(_0x99ab('0x2b'))](_0x99ab('0x21'));}if(!_0xed2100[_0x99ab('0x22')]||!_0xed2100[_0x99ab('0x28')]||!_0xed2100[_0x99ab('0x23')]||!_0xed2100[_0x99ab('0x24')]||!_0xed2100[_0x99ab('0x25')]){throw new db[(_0x99ab('0x20'))][(_0x99ab('0x2b'))](_0x99ab('0x26'));}if(!_0x4e7b4a['fileText']){throw new Error(_0x99ab('0x27'));}if(_0xed2100[_0x99ab('0x28')]===_0x24229c['id']){return _0x24229c;}else{return db['CloudProvider'][_0x99ab('0x2a')]({'where':{'id':_settings['sentimentAccountId']}});}})[_0x99ab('0x1d')](function(_0x5bee18){if(!_0x5bee18){throw new db[(_0x99ab('0x20'))][(_0x99ab('0x2b'))](_0x99ab('0x38'));}if(!_0x5bee18[_0x99ab('0x2c')]||!_0x5bee18['data2']){throw new db[(_0x99ab('0x20'))][(_0x99ab('0x2b'))](_0x99ab('0x2d'));}_0x3739fb=_0x5bee18;logger[_0x99ab('0xa')](util[_0x99ab('0xf')](_0x99ab('0x39'),_0x4e7b4a['id']));return sentiment(_0x3739fb[_0x99ab('0x2c')],_0x3739fb[_0x99ab('0xd')],_0xed2100[_0x99ab('0x23')],_0x4e7b4a[_0x99ab('0x13')],_0xed2100[_0x99ab('0x24')]);})[_0x99ab('0x1d')](function(_0x48648d){logger[_0x99ab('0xa')](util[_0x99ab('0xf')]('[RECORDING:%d]\x20Sentiment\x20Analysis\x20available',_0x4e7b4a['id']));return _0x4e7b4a[_0x99ab('0x1c')]({'sentiment':_0x48648d[_0x99ab('0x2f')],'sPositive':_0x48648d['SentimentScore'][_0x99ab('0x30')],'sNegative':_0x48648d[_0x99ab('0x31')][_0x99ab('0x32')],'sMixed':_0x48648d[_0x99ab('0x31')][_0x99ab('0x33')],'sNeutral':_0x48648d[_0x99ab('0x31')][_0x99ab('0x34')],'transcribeStatus':_0x99ab('0x14')});})[_0x99ab('0x1d')](function(){logger[_0x99ab('0xa')](util[_0x99ab('0xf')]('[RECORDING:%d]\x20Voice\x20Recording\x20entry\x20updated',_0x4e7b4a['id']));_0x3b3bba();});}})[_0x99ab('0x3a')](function(_0x313cd0){if(_0x313cd0){if(!_0x313cd0[_0x99ab('0x3b')]||_0x313cd0[_0x99ab('0x3b')]!=='SequelizeValidationError'){logger['error'](util[_0x99ab('0xf')]('[RECORDING:%d]\x20Error\x20running\x20Transcribe\x20&\x20Sentiment\x20Analysis',_0x4e7b4a['id']),util[_0x99ab('0x3c')](_0x313cd0,{'showHidden':![],'depth':null}));}}_0x3b3bba();});});}function checkTranscribe(){var _0xd44d36;var _0x241345;var _0x3953dc;return db[_0x99ab('0x3d')][_0x99ab('0x3e')]({'where':{'transcribeStatus':_0x99ab('0x3f')}})['then'](function(_0x441d2e){if(!_0x441d2e||!_0x441d2e[_0x99ab('0x1a')]){throw new db[(_0x99ab('0x20'))][(_0x99ab('0x2b'))](_0x99ab('0x40'));}_0xd44d36=_0x441d2e;return db[_0x99ab('0x41')][_0x99ab('0x2a')]({'where':{'id':0x1}});})[_0x99ab('0x1d')](function(_0x3b6e32){if(!_0x3b6e32){throw new db[(_0x99ab('0x20'))]['ValidationError'](_0x99ab('0x42'));}_0x241345=_0x3b6e32;if(!_0x241345[_0x99ab('0x6')]||!_0x241345['transcribeAccountId']||!_0x241345[_0x99ab('0xe')]||!_0x241345[_0x99ab('0x24')]||!_0x241345[_0x99ab('0x25')]){throw new db[(_0x99ab('0x20'))][(_0x99ab('0x2b'))](_0x99ab('0x43'));}return db[_0x99ab('0x29')]['findOne']({'where':{'id':_0x241345[_0x99ab('0x44')]}});})[_0x99ab('0x1d')](function(_0x46814a){if(!_0x46814a){throw new db[(_0x99ab('0x20'))][(_0x99ab('0x2b'))]('Cloud\x20Provider\x20not\x20available');}if(!_0x46814a['data1']||!_0x46814a[_0x99ab('0xd')]){throw new db[(_0x99ab('0x20'))][(_0x99ab('0x2b'))](_0x99ab('0x2d'));}_0x3953dc=_0x46814a;var _0x5af4a6=[];_0xd44d36[_0x99ab('0x45')](function(_0x498078){_0x5af4a6[_0x99ab('0x46')](checkTranscribeJob(_0x498078,_0x241345,_0x3953dc));});return BPromise[_0x99ab('0x47')](_0x5af4a6);})['catch'](function(_0xd93031){if(_0xd93031){if(!_0xd93031[_0x99ab('0x3b')]||_0xd93031[_0x99ab('0x3b')]!=='SequelizeValidationError'){logger[_0x99ab('0x48')](util[_0x99ab('0x3c')](_0xd93031,{'showHidden':![],'depth':null}));}}});}exports['start']=function(){setInterval(function(){return checkTranscribe();},0x3c*0x3e8);return checkTranscribe();};