我正在构建一个简单的文本转录程序,该程序记录您的语音,然后将其发送到另一个端点,该端点下载音频文件,然后转录它并读出响应。 为了澄清这一点,我想将呼叫从传入呼叫端点移至处理音频端点,因为我希望它处理并读取响应。
目前,它有两个端点,一个“传入呼叫”端点,代码如下:
@app.route('/incoming_call', methods=['POST'])
def handle_call():
response = VoiceResponse()
response.play("./static/audio/intro.mp3")
response.record(action="/handle_audio", recording_status_callback_event="completed",
recording_format='mp3', timeout=3, play_beep=False)
return Response(str(response), 200, mimetype='application/xml')
以及带有以下代码的“handle-audio”端点:
@app.route('/handle_audio', methods=['POST'])
def transcribe_audio():
audio_url = request.values.get('RecordingUrl')
credentials = f"{accSID}:{accAUTH}"
encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
headers = {
'Authorization': f'Basic {encoded_credentials}'
}
print(audio_url)
# Make a GET request to download the audio file
audio_response = requests.get(audio_url+".mp3", headers=headers, stream=True)
audio_response.raise_for_status() # Raise an error for bad responses (e.g., 4xx or 5xx)
当我在 /incoming-call 端点中使用“action=”/handle_audio 参数时,我的程序给出了“requests.exceptions.HTTPError: 404 Client Error: Not Found for url: (Twilio Audio Recording Url)”。但是,当我使用 Recording_status_callback="handle_audio" 时,它使文件请求正常。可能是什么原因导致的以及如何修复它?
在 Twilio 控制台日志上,它向我抛出 11200 错误(链接此处)以及 12200 错误(链接此处),但我仍然无法弄清楚为什么它在我使用 record_status_callback 时起作用,但在使用该操作时不起作用参数。
我感谢所有帮助!
我终于弄清楚了!
第一:记录端点同时具有“action”和“recording_status_callback”方法;两者看起来非常相似。 但是,操作方法会在录音完成后立即调用提供的端点,同时,recording_status_callback 端点仅在录音上传到 Twilio 服务器后调用它,并按照您在“recording_status_callback_event”中指定的方式标记为“已完成” 。因此,如果您(使用操作方法)立即尝试下载文件,Twilio 会抛出 11200 错误,指出资源不可用。这是因为它尚未完成上传,因此还无法访问。为了规避这个问题,我在我创建的名为“download_audio”的函数中使用了 time.sleep() 方法,并按如下方式实现它:
def download_audio(recording_url):
time.sleep(2)
credentials = f"{accSID}:{accAUTH}"
encoded_credentials = base64.b64encode(credentials.encode('utf-8')).decode('utf-8')
headers = {
'Authorization': f'Basic {encoded_credentials}',
}
audio_response = requests.get(headers=headers, url=recording_url)
audio_response.raise_for_status()
return audio_response
希望有帮助!