You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Twilio中用户输入数字及#后立即播放MP3延迟问题问询

问题描述

我希望用户输入数字并按下#后立即播放一段MP3(例如“请稍候”提示音),但实际使用中用户需要等待数秒才能听到下一段音频。执行操作后,预期后续音频能立即播放,却出现了数秒延迟。

相关代码

handleCall

const axios = require('axios');
const Twilio = require('twilio');
const twiml = new Twilio.twiml.VoiceResponse();


exports.handler = async function(context, event, callback) {
    console.log("Received incoming call");

    // Warm up the APIs
    try {
        const warmUpResponse = await axios.get('https://bobarke.com/_functions/warmup', { timeout: 60000 });
        console.log('Warm-up response:', warmUpResponse.data);
    } catch (error) {
        console.error('Error during warm-up:', error);
    }

    // Create a <Gather> verb with a nested <Play> verb and a timeout of 3 seconds
    const gather = twiml.gather({
        action: '/processZip',
        method: 'POST',
        numDigits: 5,
        timeout: 3,
        finishOnKey: '#'
    });

    // Nest the <Play> verb within the <Gather> verb
    gather.play('https://hmp3.s3.us-west-1.amazonaws.com/audio-d500e540-f05f-4e0d-8b25-323d15cc5aa6.mp3');


    console.log("Sending response to gather zip code");
    return callback(null, twiml);
};

processZip

const axios = require('axios');
const Twilio = require('twilio');
const twiml = new Twilio.twiml.VoiceResponse();

twiml.play('https://hmp3.s3.us-west-1.amazonaws.com/audio-6f45b4e0-1837-4b43-a229-3cf94d23eac1.mp3');

exports.handler = async function(context, event, callback) {
    console.log("Received zip code input");

    const zipCode = event.Digits;
    console.log("Digits received:", zipCode);

    if (!zipCode) {
        console.error("No zip code provided");
        twiml.say("Invalid input. Please try again.");
        return callback(null, twiml);
    }

    try {
        console.log("Fetching weather audio for zip code:", zipCode);
        const response = await axios.post('https://bobarke.com/_functions/processZip', null, {
            params: { Digits: zipCode },
            timeout: 60000 // Set timeout to 60 seconds
        });

        const audioUrl = response.data.audioUrl;
        console.log("Audio URL received:", audioUrl);

        if (!audioUrl) {
            console.error("No audio URL received from the response");
            twiml.say("Error retrieving weather forecast. Please try again later.");
            return callback(null, twiml);
        }

        // Use the <Play> verb to play the MP3 file directly
        twiml.play(audioUrl);
        console.log("Playing audio");
        
        twiml.pause({ length: 0.5 });
        
        twiml.play('https://hmp3.s3.us-west-1.amazonaws.com/audio-e3218baf-c1b6-49ed-8734-87d3c3316eae.mp3');

        return callback(null, twiml);
    } catch (error) {
        if (error.code === 'ECONNABORTED') {
            console.error("Request timed out:", error);
            twiml.say("The request took too long. Please try again later.");
        } else {
            console.error("Error fetching weather audio:", error);
            twiml.say("Error retrieving weather forecast. Please try again later.");
        }

        return callback(null, twiml);
    }
};
问题分析与修复方案

延迟的核心原因是processZip handler中,异步请求(axios调用)在构建返回给Twilio的Twiml之前执行——用户按下#后,Twilio会调用processZip,但此时代码要先完成对外部接口的POST请求,才能返回包含“请稍候”音频的Twiml,请求耗时就是延迟的来源。

修复步骤

  1. 将“请稍候”音频的播放逻辑提前到异步请求之前,同时把Twiml实例移到handler内部,避免并发请求时的复用问题:
const axios = require('axios');
const Twilio = require('twilio');

exports.handler = async function(context, event, callback) {
    const twiml = new Twilio.twiml.VoiceResponse(); // 移到handler内部创建
    console.log("Received zip code input");

    // 先播放"请稍候"提示音,确保Twilio立即收到并播放
    twiml.play('https://hmp3.s3.us-west-1.amazonaws.com/audio-6f45b4e0-1837-4b43-a229-3cf94d23eac1.mp3');

    const zipCode = event.Digits;
    console.log("Digits received:", zipCode);

    if (!zipCode) {
        console.error("No zip code provided");
        twiml.say("Invalid input. Please try again.");
        return callback(null, twiml);
    }

    try {
        console.log("Fetching weather audio for zip code:", zipCode);
        const response = await axios.post('https://bobarke.com/_functions/processZip', null, {
            params: { Digits: zipCode },
            timeout: 60000
        });

        const audioUrl = response.data.audioUrl;
        console.log("Audio URL received:", audioUrl);

        if (!audioUrl) {
            console.error("No audio URL received from the response");
            twiml.say("Error retrieving weather forecast. Please try again later.");
            return callback(null, twiml);
        }

        twiml.play(audioUrl);
        console.log("Playing audio");
        
        twiml.pause({ length: 0.5 });
        
        twiml.play('https://hmp3.s3.us-west-1.amazonaws.com/audio-e3218baf-c1b6-49ed-8734-87d3c3316eae.mp3');

        return callback(null, twiml);
    } catch (error) {
        if (error.code === 'ECONNABORTED') {
            console.error("Request timed out:", error);
            twiml.say("The request took too long. Please try again later.");
        } else {
            console.error("Error fetching weather audio:", error);
            twiml.say("Error retrieving weather forecast. Please try again later.");
        }

        return callback(null, twiml);
    }
};
  1. 额外优化建议
    如果外部接口请求耗时过长,可以结合Twilio的异步处理能力(比如用Webhook后续推送音频),但优先保证“请稍候”音频先返回播放,再处理后台逻辑,就能彻底消除用户感知到的延迟。

内容的提问来源于stack exchange,提问作者bobarke

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 03:02:06