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

Twilio语音流报错Error 11100:媒体流URL格式无效排查求助

问题:Twilio语音流服务返回Error 11100无效URL格式错误

基于Flask框架结合Twilio开发语音流服务,预期实现音频流接收并触发控制台打印‘Audio is streaming’,但Twilio控制台返回Error 11100,提示「无效URL格式」。尝试改用https协议后仍出现相同报错,疑惑是否需要导入特定库以支持wss协议。

Twilio报错详情

  • 提示信息:无效URL
  • 错误描述:Error - 11100 无效URL格式,提供的URL格式无效
  • 可能解决方案:确保提交包含协议(http://或https://)、主机名、文件路径、正确URL编码查询参数的完整URL,且Twilio可通过公网访问该URL
  • 可能原因:电话号码配置的URL错误、呼出请求传递的URL错误、Play/Redirect动词内容中的URL错误、动词action属性的URL错误、修改通话时Record动词未提供action URL、URL认证部分包含不支持字符

相关代码

from flask import Flask, request, Response
import requests
from twilio.twiml.voice_response import VoiceResponse, Play, Start, Stream

from twilio.rest import Client
import time
import os
from google.cloud import speech_v1 as speech
from google.cloud.speech_v1 import types
import threading
import queue
import base64

app = Flask(__name__)

#Google Cloud Credentials environment variable
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = './google_credentials.json'

# Twilio credentials
account_sid = ['TWILIO_SID']
auth_token = ['TWILIO_AUTH']
twilio_client = Client(account_sid, auth_token)
speech_client = speech.SpeechClient()

# Create a queue to store audio chunks
audio_chunks_queue = queue.Queue()

# A simple in-memory structure to store call associations
# In production, you might want to use a database
call_associations = {}

response = None

@app.route("/incoming_call", methods=['POST'])
def handle_incoming_call():
    """Responds to incoming calls and sets up segmented recording of inbound audio."""
    response = VoiceResponse()

    start = Start()
    stream = Stream(url='wss://b32e-2603-8081-4c00-3a7c-2d64-d183-b6e6-f523.ngrok-free.app/stream_audio')
    start.append(stream)
    response.append(start)

    twiml_response_str = str(response)
    print("Generated TwiML Response:\n", twiml_response_str)

         
    print("incoming call")
    return Response(str(response), mimetype='text/xml')

@app.route("/stream_audio", methods=['POST'])
def stream_audio():
    """Receive streamed audio from Twilio and add it to the queue."""
    print("Audio is streaming")
    audio_data = request.get_data()
    audio_chunks_queue.put(audio_data)
    return ('', 204)

问题分析与解决方法

  1. Stream动词URL协议错误
    Twilio的<Stream>动词仅支持http或https协议,因为Twilio是通过HTTP POST请求推送音频流数据,而非WebSocket连接。代码中使用wss://开头的URL是核心错误,直接替换为https://即可。

  2. 修正后的关键代码
    修改handle_incoming_call函数中的Stream URL:

    stream = Stream(url='https://b32e-2603-8081-4c00-3a7c-2d64-d183-b6e6-f523.ngrok-free.app/stream_audio')
    
  3. 无需额外导入wss相关库
    该场景下Twilio语音流基于HTTP推送,不需要WebSocket支持,保持现有Flask和Twilio依赖库即可。

  4. 额外验证步骤

    • 确认ngrok地址处于有效运行状态,用curl测试/stream_audio端点:
      curl -X POST https://b32e-2603-8081-4c00-3a7c-2d64-d183-b6e6-f523.ngrok-free.app/stream_audio
      
      返回204状态码即为正常。
    • 检查Twilio电话号码配置的Webhook URL为https://你的ngrok地址/incoming_call,请求方法设置为POST。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 04:47:12