f8794c5a05b3e3b64c196107ee7d9b69ec2fbc75
[motion2.git] / server / services / agi / scripts / tilde / asr / lib / api.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 _0xce88=['Error\x20Tilde\x20ASR\x20socket','isObject','open','message','bluebird','lodash','Tilde\x20URI','wss://runa.tilde.lv/client/ws/speech/LVASR-ONLINE','createHash','sha1','update','info','Open\x20Tilde\x20ASR\x20socket','unix','send','stringify','createReadStream','close','EOS','Tilde\x20ASR\x20send\x20error','terminate','parse','status','final','Tilde\x20ASR\x20hypoteses','result','hypotheses','likelihood','No\x20speech.\x20Sent\x20when\x20the\x20incoming\x20audio\x20contains\x20a\x20large\x20portion\x20of\x20silence\x20or\x20non-speech','Aborted.\x20Recognition\x20was\x20aborted\x20for\x20some\x20reason.','Not\x20available.\x20Max\x20load\x20limit\x20reached.','Authentication\x20failed.','All\x20recognition\x20workers\x20are\x20currently\x20in\x20use\x20and\x20real-time\x20recognition\x20is\x20not\x20possible.','error'];(function(_0x578b6c,_0x2838aa){var _0x24a715=function(_0x522826){while(--_0x522826){_0x578b6c['push'](_0x578b6c['shift']());}};_0x24a715(++_0x2838aa);}(_0xce88,0x17a));var _0x8ce8=function(_0x465a0c,_0x19d4d5){_0x465a0c=_0x465a0c-0x0;var _0x1e202e=_0xce88[_0x465a0c];return _0x1e202e;};'use strict';var BPromise=require(_0x8ce8('0x0'));var _=require(_0x8ce8('0x1'));var fs=require('fs');var WebSocket=require('ws');var moment=require('moment');var crypto=require('crypto');var logger=require('../../../../../../config/logger')('agi');module['exports']=function(_0x36654a,_0x3161bf,_0x283fee,_0x4851a6){return new BPromise(function(_0xaace86,_0x2e64a8){logger['info'](_0x8ce8('0x2'),_0x4851a6||_0x8ce8('0x3'));var _0x3b0f82=new WebSocket(_0x4851a6||_0x8ce8('0x3'),{'perMessageDeflate':![]});var _0xfae368='';var _0x2677f2='0';var _0x5d0f41='1';var _0x3f123a='No\x20speech.\x20Sent\x20when\x20the\x20incoming\x20audio\x20contains\x20a\x20large\x20portion\x20of\x20silence\x20or\x20non-speech';function _0x21cab2(_0x4613b2){var _0x15d7a4=crypto[_0x8ce8('0x4')](_0x8ce8('0x5'));_0x15d7a4[_0x8ce8('0x6')](_0x4613b2);return _0x15d7a4['digest']('hex');}function _0x5e9063(){try{logger[_0x8ce8('0x7')](_0x8ce8('0x8'));var _0x50a012=moment()[_0x8ce8('0x9')]();var _0x5e1e67=_0x21cab2(_0x50a012+_0x3161bf+_0x283fee);_0x3b0f82[_0x8ce8('0xa')](JSON[_0x8ce8('0xb')]({'appID':_0x3161bf,'timestamp':_0x50a012,'appKey':_0x5e1e67}));var _0x3a36dc=fs[_0x8ce8('0xc')](_0x36654a);_0x3a36dc['on']('data',function(_0x59b76c){_0x3b0f82['send'](_0x59b76c);});_0x3a36dc['on'](_0x8ce8('0xd'),function(){_0x3b0f82[_0x8ce8('0xa')](_0x8ce8('0xe'));});}catch(_0x3a399d){logger['error'](_0x8ce8('0xf'),JSON['stringify'](_0x3a399d));_0x3b0f82[_0x8ce8('0x10')]();}}function _0x2cfbd5(_0x487695){try{if(_0x487695){var _0x497dcc=JSON[_0x8ce8('0x11')](_0x487695);switch(_0x497dcc[_0x8ce8('0x12')]){case 0x0:if(_0x497dcc['result']){if(_0x497dcc['result'][_0x8ce8('0x13')]){logger['info'](_0x8ce8('0x14'),JSON[_0x8ce8('0xb')](_0x497dcc[_0x8ce8('0x15')][_0x8ce8('0x16')]));_0xfae368=_0x497dcc[_0x8ce8('0x15')]['hypotheses'][0x0]['transcript'];_0x2677f2=_0x497dcc['result'][_0x8ce8('0x16')][0x0][_0x8ce8('0x17')];_0x5d0f41='0';_0x3f123a='Success.';_0x3b0f82[_0x8ce8('0x10')]();}}break;case 0x1:logger[_0x8ce8('0x7')](_0x8ce8('0x18'));_0x5d0f41='1';_0x3f123a='No\x20speech.\x20Sent\x20when\x20the\x20incoming\x20audio\x20contains\x20a\x20large\x20portion\x20of\x20silence\x20or\x20non-speech';break;case 0x2:logger['info'](_0x8ce8('0x19'));_0x5d0f41='2';_0x3f123a=_0x8ce8('0x19');break;case 0x9:logger['info'](_0x8ce8('0x1a'));_0x5d0f41='9';_0x3f123a=_0x8ce8('0x1a');break;case 0xa:logger['info'](_0x8ce8('0x1b'));_0x5d0f41='10';_0x3f123a=_0x8ce8('0x1b');break;case 0xb:logger[_0x8ce8('0x7')](_0x8ce8('0x1c'));_0x5d0f41='11';_0x3f123a=_0x8ce8('0x1c');}}}catch(_0x5dd751){logger[_0x8ce8('0x1d')]('Tilde\x20ASR\x20parse\x20error',JSON[_0x8ce8('0xb')](_0x5dd751));_0x3b0f82['terminate']();}}function _0x46dfb3(){logger[_0x8ce8('0x7')]('Close\x20Tilde\x20ASR\x20socket');_0xaace86({'transcript':_0xfae368,'confidence':_0x2677f2,'status':_0x5d0f41,'status_message':_0x3f123a});}function _0xd324be(_0x9239e){logger[_0x8ce8('0x1d')](_0x8ce8('0x1e'));if(!_['isNil'](_0x9239e)){logger[_0x8ce8('0x1d')](_[_0x8ce8('0x1f')](_0x9239e)?JSON['stringify'](_0x9239e):_0x9239e);}}_0x3b0f82['on'](_0x8ce8('0x20'),_0x5e9063);_0x3b0f82['on'](_0x8ce8('0x21'),_0x2cfbd5);_0x3b0f82['on'](_0x8ce8('0xd'),_0x46dfb3);_0x3b0f82['on'](_0x8ce8('0x1d'),_0xd324be);});};