7d66be9c06f15851507428902747f8526a53af6b
[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 _0x3f18=['message','close','bluebird','crypto','info','Tilde\x20URI','wss://runa.tilde.lv/client/ws/speech/LVASR-ONLINE','createHash','update','digest','hex','Open\x20Tilde\x20ASR\x20socket','unix','createReadStream','data','EOS','error','Tilde\x20ASR\x20send\x20error','stringify','terminate','parse','status','result','final','Tilde\x20ASR\x20hypoteses','hypotheses','transcript','likelihood','Success.','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.','Tilde\x20ASR\x20parse\x20error','isNil','isObject','open'];(function(_0x9b7542,_0x26ab8f){var _0x5287e6=function(_0xcfb1a8){while(--_0xcfb1a8){_0x9b7542['push'](_0x9b7542['shift']());}};_0x5287e6(++_0x26ab8f);}(_0x3f18,0x9a));var _0x83f1=function(_0x2641f9,_0x26715f){_0x2641f9=_0x2641f9-0x0;var _0x585741=_0x3f18[_0x2641f9];return _0x585741;};'use strict';var BPromise=require(_0x83f1('0x0'));var _=require('lodash');var fs=require('fs');var WebSocket=require('ws');var moment=require('moment');var crypto=require(_0x83f1('0x1'));var logger=require('../../../../../../config/logger')('agi');module['exports']=function(_0x5b5158,_0x80ce75,_0x292a50,_0x24d85d){return new BPromise(function(_0xf32161,_0x2d8024){logger[_0x83f1('0x2')](_0x83f1('0x3'),_0x24d85d||_0x83f1('0x4'));var _0x20951f=new WebSocket(_0x24d85d||_0x83f1('0x4'),{'perMessageDeflate':![]});var _0x59cb25='';var _0x3da8dc='0';var _0x43ad73='1';var _0x2646f5='No\x20speech.\x20Sent\x20when\x20the\x20incoming\x20audio\x20contains\x20a\x20large\x20portion\x20of\x20silence\x20or\x20non-speech';function _0x319b4b(_0x244e74){var _0x1ab90a=crypto[_0x83f1('0x5')]('sha1');_0x1ab90a[_0x83f1('0x6')](_0x244e74);return _0x1ab90a[_0x83f1('0x7')](_0x83f1('0x8'));}function _0x38dc31(){try{logger[_0x83f1('0x2')](_0x83f1('0x9'));var _0x110939=moment()[_0x83f1('0xa')]();var _0x476a6e=_0x319b4b(_0x110939+_0x80ce75+_0x292a50);_0x20951f['send'](JSON['stringify']({'appID':_0x80ce75,'timestamp':_0x110939,'appKey':_0x476a6e}));var _0x214c94=fs[_0x83f1('0xb')](_0x5b5158);_0x214c94['on'](_0x83f1('0xc'),function(_0x452ecd){_0x20951f['send'](_0x452ecd);});_0x214c94['on']('close',function(){_0x20951f['send'](_0x83f1('0xd'));});}catch(_0x407839){logger[_0x83f1('0xe')](_0x83f1('0xf'),JSON[_0x83f1('0x10')](_0x407839));_0x20951f[_0x83f1('0x11')]();}}function _0x283afc(_0x5dfd9b){try{if(_0x5dfd9b){var _0x5dac3c=JSON[_0x83f1('0x12')](_0x5dfd9b);switch(_0x5dac3c[_0x83f1('0x13')]){case 0x0:if(_0x5dac3c[_0x83f1('0x14')]){if(_0x5dac3c[_0x83f1('0x14')][_0x83f1('0x15')]){logger[_0x83f1('0x2')](_0x83f1('0x16'),JSON[_0x83f1('0x10')](_0x5dac3c['result'][_0x83f1('0x17')]));_0x59cb25=_0x5dac3c[_0x83f1('0x14')][_0x83f1('0x17')][0x0][_0x83f1('0x18')];_0x3da8dc=_0x5dac3c[_0x83f1('0x14')][_0x83f1('0x17')][0x0][_0x83f1('0x19')];_0x43ad73='0';_0x2646f5=_0x83f1('0x1a');_0x20951f['terminate']();}}break;case 0x1:logger['info'](_0x83f1('0x1b'));_0x43ad73='1';_0x2646f5=_0x83f1('0x1b');break;case 0x2:logger[_0x83f1('0x2')](_0x83f1('0x1c'));_0x43ad73='2';_0x2646f5=_0x83f1('0x1c');break;case 0x9:logger[_0x83f1('0x2')](_0x83f1('0x1d'));_0x43ad73='9';_0x2646f5=_0x83f1('0x1d');break;case 0xa:logger['info'](_0x83f1('0x1e'));_0x43ad73='10';_0x2646f5=_0x83f1('0x1e');break;case 0xb:logger[_0x83f1('0x2')]('All\x20recognition\x20workers\x20are\x20currently\x20in\x20use\x20and\x20real-time\x20recognition\x20is\x20not\x20possible.');_0x43ad73='11';_0x2646f5=_0x83f1('0x1f');}}}catch(_0x5ed700){logger[_0x83f1('0xe')](_0x83f1('0x20'),JSON[_0x83f1('0x10')](_0x5ed700));_0x20951f['terminate']();}}function _0x350bb9(){logger[_0x83f1('0x2')]('Close\x20Tilde\x20ASR\x20socket');_0xf32161({'transcript':_0x59cb25,'confidence':_0x3da8dc,'status':_0x43ad73,'status_message':_0x2646f5});}function _0x4d4ebc(_0xa48457){logger['error']('Error\x20Tilde\x20ASR\x20socket');if(!_[_0x83f1('0x21')](_0xa48457)){logger['error'](_[_0x83f1('0x22')](_0xa48457)?JSON[_0x83f1('0x10')](_0xa48457):_0xa48457);}}_0x20951f['on'](_0x83f1('0x23'),_0x38dc31);_0x20951f['on'](_0x83f1('0x24'),_0x283afc);_0x20951f['on'](_0x83f1('0x25'),_0x350bb9);_0x20951f['on'](_0x83f1('0xe'),_0x4d4ebc);});};