fix: voice STT path fix + execSync error handling + fetch download
This commit is contained in:
@@ -1088,21 +1088,36 @@ export async function initBot(config, api, tools, skills, agents) {
|
||||
const url = `https://api.telegram.org/file/bot${botToken}/${file.file_path}`;
|
||||
const oggPath = `/tmp/zcode-voice-${Date.now()}.ogg`;
|
||||
|
||||
// Download voice file
|
||||
// Download voice file via fetch (faster than curl subprocess)
|
||||
const { execSync } = await import('child_process');
|
||||
execSync(`curl -sL "${url}" -o "${oggPath}"`, { timeout: 15000 });
|
||||
const voiceResp = await fetch(url);
|
||||
if (!voiceResp.ok) throw new Error(`Download failed: ${voiceResp.status}`);
|
||||
const { writeFileSync, unlinkSync } = await import('fs');
|
||||
writeFileSync(oggPath, Buffer.from(await voiceResp.arrayBuffer()));
|
||||
logger.info(`Voice downloaded: ${oggPath}`);
|
||||
|
||||
// Run Vosk STT via Python script
|
||||
const sttScript = new URL('../scripts/stt.py', import.meta.url).pathname;
|
||||
const result = execSync(
|
||||
`python3 "${sttScript}" "${oggPath}" 2>/dev/null`,
|
||||
{ timeout: 30000, encoding: 'utf-8' }
|
||||
);
|
||||
const parsed = JSON.parse(result.trim());
|
||||
|
||||
// Cleanup
|
||||
execSync(`rm -f "${oggPath}"`);
|
||||
// Run Vosk STT — path is ../../scripts/stt.py from src/bot/
|
||||
const sttScript = new URL('../../scripts/stt.py', import.meta.url).pathname;
|
||||
let parsed;
|
||||
try {
|
||||
const result = execSync(
|
||||
`python3 "${sttScript}" "${oggPath}"`,
|
||||
{ timeout: 30000, encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] }
|
||||
);
|
||||
parsed = JSON.parse(result.trim());
|
||||
} catch (e) {
|
||||
// exit code 1 = no speech detected, stdout still has JSON
|
||||
const stdout = e.stdout?.trim();
|
||||
if (stdout) {
|
||||
try { parsed = JSON.parse(stdout); } catch { parsed = null; }
|
||||
}
|
||||
if (!parsed || !parsed.text) {
|
||||
await ctx.api.editMessageText(ctx.chat.id, statusMsg.message_id,
|
||||
'🎤 Could not detect speech in the voice message.');
|
||||
return;
|
||||
}
|
||||
}
|
||||
unlinkSync(oggPath);
|
||||
|
||||
if (parsed.error) {
|
||||
logger.error(`STT error: ${parsed.error}`);
|
||||
|
||||
Reference in New Issue
Block a user