function buildAudioPayload(data, sid, endFlag, languageType) { let customParams = { botId: Constant.INTERACT_BOT_ID, orgCode: Constant.INTERACT_ORG_CODE, sid: sid, deviceId: '123', location: Constant.INTERACT_LOCATION, languageType: languageType === '2' ? 'cn' : languageType === '3' ? 'en' : 'cn', }; let iatParams = { sid: sid, aue: 'raw', dwa: 'wpgs', pgs: 'apd', endFlag: true, vad_switch: 'false', eos: '60000', rse: 'utf-8', rst: 'json', engine_param: `pproc_param_puncproc=false;wdec_param_LanguageTypeChoice=${languageType || '1'}`, }; let payload = { data: toBase64(data), appid: Constant.INTERACT_APP_ID, scene: Constant.INTERACT_SCENE, debug: true, sessionParams: { id: sid, traceId: sid, reset: false, abilityList: [ { abilityCode: 'iat', serviceName: 'iat', param: JSON.stringify(iatParams), }, ], }, }; return JSON.stringify(payload); } function buildTextPayload(data, sid) { let customParams = { botId: 'INTERACT_BOT_ID', orgCode: 'INTERACT_ORG_CODE', sid: sid, location: '双龙站', deviceId: '123', }; let ttsParams = { audio_coding: 'raw', sample_rate: '16000', sid: sid, volume: '20', }; let payload = { data: btoa(data), appid: 'INTERACT_APPID', scene: 'INTERACT_SCENE', debug: true, sessionParams: { id: sid, traceId: sid, reset: false, abilityList: [ { abilityCode: 'dics', param: JSON.stringify(customParams), }, { abilityCode: 'tts', param: JSON.stringify(ttsParams), }, ], }, }; return JSON.stringify(payload); } function toBase64(buffer) { var binary = ''; var bytes = new Uint8Array(buffer); var len = bytes.byteLength; for (var i = 0; i < len; i++) { binary += String.fromCharCode(bytes[i]); } return btoa(binary); } let iatText = ''; let iatAppendText = ''; function decodeMessage(messageData) { let aiuiResult = new AIUIResult(AIUI_TYPE.ERROR, messageData); try { const result = JSON.parse(messageData); if (result) { const json = JSON.parse(result.result); let sub = json[0].sub; let data = json[0].data; if (sub === 'iat' && data && data !== '') { let iat = JSON.parse(data); let pgs = iat.pgs; let ls = iat.ls; if (ls) { aiuiResult = new AIUIResult(AIUI_TYPE.IAT_FINAL, messageData); aiuiResult.setFinalText(iatText); iatText = ''; return aiuiResult; } if (pgs === 'rpl') { iatText = ''; iatText += iatAppendText; } else if (pgs === 'apd') { iatAppendText = ''; if (iatText != null) { iatAppendText += iatText; } iatText = ''; } if (iat.ws && iat.ws.length > 0) { iat.ws.forEach((item) => { iatText += item.cw[0].w; }); } aiuiResult = new AIUIResult(AIUI_TYPE.IAT_REALTIME, messageData); aiuiResult.setRealtimeText(iatText); return aiuiResult; } } } catch (e) { console.error(e); } return aiuiResult; } const AIUI_TYPE = { IAT_REALTIME: 'iat_realtime', IAT_FINAL: 'iat_final', ERROR: 'error', }; class AIUIResult { constructor(type, json) { this.type = type; this.json = json; } setRealtimeText(text) { this.realtimeText = text; } setFinalText(text) { this.finalText = text; } getReal() { return this.realtimeText; } getFinal() { return this.finalText; } getJSON() { return this.json; } }