Twilio媒体流- Twilio没有连接到我的WebSocket服务器

ohfgkhjo  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(114)

我目前正在开发一个应用程序,使用Twilio Media Streams来实时处理音频数据。然而,我遇到了一个问题,在向/voice端点发出POST请求后,呼叫立即断开。
以下是我的设置细节:
1.我使用Flask来托管我的应用程序,使用Flask-Sockets来处理WebSocket连接。

  1. My /voice端点返回TwiML响应,其中包含启动媒体流的动词。TwiML响应中的WebSocket URL是wss:my-ngrok-subdomain.ngrok.io/stream。
    1.我的WebSocket服务器正在运行,可以从Internet访问。我已经通过使用WebSocket客户端独立于Twilio测试WebSocket连接证实了这一点。
    1.我已经在我的应用程序和WebSocket服务器中启用了详细的日志记录,但我没有发现任何错误或问题,可以解释为什么调用断开/为什么Twilio没有连接到我的WebSocket。
    尽管采取了这些措施,但呼叫仍然会在向/voice端点发出POST请求后立即断开连接。我已经检查了Twilio日志,它们以向/voice发出请求结束。没有日志表明Twilio试图连接到我的WebSocket。
    下面是我的WebSocket服务器代码的相关部分:
def handle_audio(ws):
    logging.info('Handling audio')  # Added logging
    while not ws.closed:
        message = ws.receive()
        if message is None:
            logging.info("No message received...")
            continue

        # Messages are a JSON encoded string
        data = json.loads(message)

        # Using the event type you can determine what type of message you are receiving
        if data['event'] == "connected":
            logging.info("Connected Message received: {}".format(message))
        elif data['event'] == "start":
            logging.info("Start Message received: {}".format(message))
        elif data['event'] == "media":
            logging.info("Media message: {}".format(message))
            payload = data['media']['payload']
            logging.info("Payload is: {}".format(payload))
            audio_data = base64.b64decode(payload)
            logging.info("That's {} bytes".format(len(audio_data)))

            # Split the audio data on silence
            audio_chunks = whisper_handler.split_on_silence(audio_data)
            logging.info('Split audio data into %d chunks', len(audio_chunks))  # Added logging

            # Transcribe each audio chunk
            transcriptions = [whisper_handler.transcribe_audio(chunk) for chunk in audio_chunks]
            logging.info('Transcribed audio chunks: %s', transcriptions)  # Added logging
        elif data['event'] == "closed":
            logging.info("Closed Message received: {}".format(message))
            break

我将感谢您在解决此问题方面提供的任何帮助。如果您需要任何其他信息,请告诉我。

3bygqnnd

3bygqnnd1#

显然,我需要补充:

response.say("hello! how can I help!", voice='women')
response.pause(length=60)

所以我的代码现在看起来像这样:

@app.route('/voice', methods=['GET', 'POST'])
def voice():
    response = VoiceResponse()

    # Use the <Start> verb to start a Media Stream
    start = Start()
    start.stream(name='My Audio Stream', url='wss://subdomain.ngrok-free.app/stream')  # Added name
    
    response.append(start)  # Append the Start object to the VoiceResponse object

    response.say("hello! how can I help!", voice='women')
    response.pause(length=60)

    # Return a Response object with the Content-Type header set to application/xml
    return Response(str(response), mimetype='application/xml')

相关问题