我正在开发一个语音机器人,它使用twilio媒体流(Google STT),处理文本,并使用TwiML Say对象向用户返回响应。我使用的端点在用户开始呼叫时触发(状态呼叫正在振铃):
@app.route("/twilio_call", methods=['POST'])
def voice(request):
    """Respond to incoming phone calls with a greet message"""
    call_status = request.form.get("CallStatus")
    if call_status == "ringing":
       voice_respond = VoiceResponse()
       voice_respond.say("hello! how can I help!", voice='women')
       return response.text(str(voice_response), content_type="text/xml")在此消息传递给用户后,我想用媒体流直接触发websocket服务器。
@app.websockets('/media')
def transcribe_media(request, ws):
    while True:
        message = ws.recv()
        if message is None:
            continue
    data = json.loads(message)
    if data['event'] == "media":
                    ....
#here I'm sending data to google and getting the transcription back我不能像这样在文档中修改正在进行的调用:https://www.twilio.com/docs/voice/tutorials/how-to-modify-calls-in-progress-python
我已经尝试过了:
client = Client(ACCOUNT_SID, AUTH_TOKEN)
        call = client.calls(conversation_id).update(
            twiml='<Response><Say voice="woman" language="de-DE">' + msg_text + '</Say></Response>')然而,我得到了一个错误,状态调用不在进行中(它是“振铃”)..
我也尝试了TwiML“流”对象,但当它与TwiML "Say“对象一起使用时,它不会启动服务器(当我只传递流时,它会触发服务器):
 voice_response = VoiceResponse()
 start = Start()
 start.stream(url='wss://<NGROK_IP>.ngrok.io/webhooks/twilio_voice/media')
 voice_response.say("Hello, how can I help?", language="en-GB")
 voice_response.append(start)
response.text(str(voice_response), content_type="text/xml")有人知道我该如何解决这个问题吗?Twiml"Say“对象传给用户后,如何触发websocket服务器?
发布于 2021-09-21 03:58:19
Twilio开发者的布道者在这里。
实现这一点的正确方法是通过Stream TwiML element。我建议将流放在TwiML响应的开头,这样它就可以及时建立,以便您可以开始接收用户的语音。此外,一旦TwiML完成,Twilio将挂断呼叫,即使有实时流也是如此。因此,您应该使用pause来等待用户的语音响应。
因此,我将您的webhook端点更改为:
@app.route("/twilio_call", methods=['POST'])
def voice(request):
"""Respond to incoming phone calls with a greet message"""
call_status = request.form.get("CallStatus")
voice_respond = VoiceResponse()
start = Start()
start.stream(url='wss://<NGROK_IP>.ngrok.io/webhooks/twilio_voice/media')
voice_respond.append(start)
voice_respond.say("hello! how can I help!", voice='women')
voice_respond.pause(length=60)
return response.text(str(voice_response), content_type="text/xml")现在您的流应该连接到您的websocket端点,您的用户将听到问候语。调用不会挂断,因为有60秒的暂停,当用户说话时,您可以使用websocket端点将语音发送到STT服务,当您获得响应时,重定向调用并使用<Say>说出结果。
https://stackoverflow.com/questions/69227137
复制相似问题