5eb9938c70c7c420d7fa8175cb958b8352a2a70a
[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 _0x0202=['open','message','bluebird','lodash','moment','crypto','../../../../../../config/logger','Tilde\x20URI','wss://runa.tilde.lv/client/ws/speech/LVASR-ONLINE','createHash','sha1','digest','hex','unix','send','createReadStream','data','close','Tilde\x20ASR\x20send\x20error','stringify','terminate','parse','status','result','Tilde\x20ASR\x20hypoteses','hypotheses','transcript','likelihood','Success.','info','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.','isNil','error','isObject'];(function(_0x57b8f0,_0x4b8716){var _0x567d6d=function(_0x36144b){while(--_0x36144b){_0x57b8f0['push'](_0x57b8f0['shift']());}};_0x567d6d(++_0x4b8716);}(_0x0202,0x1ca));var _0x2020=function(_0x3bf6ba,_0x14399f){_0x3bf6ba=_0x3bf6ba-0x0;var _0x34dd73=_0x0202[_0x3bf6ba];return _0x34dd73;};'use strict';var BPromise=require(_0x2020('0x0'));var _=require(_0x2020('0x1'));var fs=require('fs');var WebSocket=require('ws');var moment=require(_0x2020('0x2'));var crypto=require(_0x2020('0x3'));var logger=require(_0x2020('0x4'))('agi');module['exports']=function(_0x2f7ae4,_0x593fd4,_0x49b1f5,_0x4f8deb){return new BPromise(function(_0x43cf7c,_0x499f9c){logger['info'](_0x2020('0x5'),_0x4f8deb||_0x2020('0x6'));var _0x212511=new WebSocket(_0x4f8deb||_0x2020('0x6'),{'perMessageDeflate':![]});var _0x102906='';var _0x109dde='0';var _0x38dfc3='1';var _0x5d71d7='No\x20speech.\x20Sent\x20when\x20the\x20incoming\x20audio\x20contains\x20a\x20large\x20portion\x20of\x20silence\x20or\x20non-speech';function _0x345700(_0x1bebf7){var _0x4f8376=crypto[_0x2020('0x7')](_0x2020('0x8'));_0x4f8376['update'](_0x1bebf7);return _0x4f8376[_0x2020('0x9')](_0x2020('0xa'));}function _0x252799(){try{logger['info']('Open\x20Tilde\x20ASR\x20socket');var _0x3f71ce=moment()[_0x2020('0xb')]();var _0x28b41a=_0x345700(_0x3f71ce+_0x593fd4+_0x49b1f5);_0x212511[_0x2020('0xc')](JSON['stringify']({'appID':_0x593fd4,'timestamp':_0x3f71ce,'appKey':_0x28b41a}));var _0x14e735=fs[_0x2020('0xd')](_0x2f7ae4);_0x14e735['on'](_0x2020('0xe'),function(_0x42c8f5){_0x212511['send'](_0x42c8f5);});_0x14e735['on'](_0x2020('0xf'),function(){_0x212511[_0x2020('0xc')]('EOS');});}catch(_0x1605c7){logger['error'](_0x2020('0x10'),JSON[_0x2020('0x11')](_0x1605c7));_0x212511[_0x2020('0x12')]();}}function _0x552a14(_0x3cddb1){try{if(_0x3cddb1){var _0x41a2a4=JSON[_0x2020('0x13')](_0x3cddb1);switch(_0x41a2a4[_0x2020('0x14')]){case 0x0:if(_0x41a2a4[_0x2020('0x15')]){if(_0x41a2a4[_0x2020('0x15')]['final']){logger['info'](_0x2020('0x16'),JSON[_0x2020('0x11')](_0x41a2a4[_0x2020('0x15')][_0x2020('0x17')]));_0x102906=_0x41a2a4[_0x2020('0x15')][_0x2020('0x17')][0x0][_0x2020('0x18')];_0x109dde=_0x41a2a4[_0x2020('0x15')][_0x2020('0x17')][0x0][_0x2020('0x19')];_0x38dfc3='0';_0x5d71d7=_0x2020('0x1a');_0x212511['terminate']();}}break;case 0x1:logger[_0x2020('0x1b')](_0x2020('0x1c'));_0x38dfc3='1';_0x5d71d7=_0x2020('0x1c');break;case 0x2:logger[_0x2020('0x1b')]('Aborted.\x20Recognition\x20was\x20aborted\x20for\x20some\x20reason.');_0x38dfc3='2';_0x5d71d7=_0x2020('0x1d');break;case 0x9:logger['info'](_0x2020('0x1e'));_0x38dfc3='9';_0x5d71d7=_0x2020('0x1e');break;case 0xa:logger[_0x2020('0x1b')](_0x2020('0x1f'));_0x38dfc3='10';_0x5d71d7=_0x2020('0x1f');break;case 0xb:logger[_0x2020('0x1b')](_0x2020('0x20'));_0x38dfc3='11';_0x5d71d7=_0x2020('0x20');}}}catch(_0x515161){logger['error']('Tilde\x20ASR\x20parse\x20error',JSON[_0x2020('0x11')](_0x515161));_0x212511[_0x2020('0x12')]();}}function _0xb7c5b3(){logger[_0x2020('0x1b')]('Close\x20Tilde\x20ASR\x20socket');_0x43cf7c({'transcript':_0x102906,'confidence':_0x109dde,'status':_0x38dfc3,'status_message':_0x5d71d7});}function _0x5782d9(_0x1f63ad){logger['error']('Error\x20Tilde\x20ASR\x20socket');if(!_[_0x2020('0x21')](_0x1f63ad)){logger[_0x2020('0x22')](_[_0x2020('0x23')](_0x1f63ad)?JSON[_0x2020('0x11')](_0x1f63ad):_0x1f63ad);}}_0x212511['on'](_0x2020('0x24'),_0x252799);_0x212511['on'](_0x2020('0x25'),_0x552a14);_0x212511['on'](_0x2020('0xf'),_0xb7c5b3);_0x212511['on'](_0x2020('0x22'),_0x5782d9);});};