如何在本地 pc python flask 中从 twilio API 调用后保存入站通话录音 [英] How will I save inbound call recording after the call from twilio API in the local pc python flask
问题描述
我使用示例代码接收从一个号码到 twilio 号码的呼叫.现在我需要将录音保存为 mp3.我不明白该怎么做.我试图调用各种参数但失败了.我是 twilio 的新手.
I used the sample code to receive a call from a number to twilio number. Now I need to save the recording as mp3. I cant understand how to do it. I tried to call various parameters but failed. I am new to twilio.
> `from flask import Flask
from twilio.twiml.voice_response import VoiceResponse
app = Flask(__name__)
@app.route("/record", methods=['GET', 'POST'])
def record():
"""Returns TwiML which prompts the caller to record a message"""
# Start our TwiML response
response = VoiceResponse()
# Use <Say> to give the caller some instructions
response.say('Hello. Please leave a message after the beep.')
# Use <Record> to record the caller's message
response.record()
# End the call with <Hangup>
response.hangup()
return str(response)
def record(response):
# function to save file to .wav format
if __name__ == "__main__":
app.run(debug = True)
我按照链接操作,但无法理解如何将其与烧瓶链接以保存文件.https://www.twilio.com/docs/voice/api/recording?code-sample=code-filter-recordings-with-range-match&code-language=Python&code-sdk-version=6.x
I followed the link but cant understand how to link it with flask to save the file. https://www.twilio.com/docs/voice/api/recording?code-sample=code-filter-recordings-with-range-match&code-language=Python&code-sdk-version=6.x
推荐答案
Twilio 开发人员布道者在这里.
Twilio developer evangelist here.
当您使用
要记录用户,您可以提供一个 URL 作为 recordingStatusCallback
属性.然后,当录音准备好时,Twilio 将向该 URL 发出请求,并提供有关录音的详细信息.
When you use <Record>
to record a user, you can provide a URL as the recordingStatusCallback
attribute. Then, when the recording is ready, Twilio will make a request to that URL with the details about the recording.
因此,您可以将 record
TwiML 更新为如下所示:
So, you can update your record
TwiML to something like this:
# Use <Record> to record the caller's message
response.record(
recording_status_callback="/recording-complete",
recording_status_callback_event="completed"
)
然后您将需要一个用于 /recording-complete
的新路径,您可以在其中接收回调并下载文件.如何下载文件以响应 webhook 但它涵盖了彩信.但是,我们可以从那里学到什么来下载录音.
Then you will need a new route for /recording-complete
in which you receive the callback and download the file. There is a good post on how to download files in response to a webhook but it covers MMS messages. However, we can take what we learn from there to download the recording.
首先,安装并导入requests
库.同时从 Flask 导入 request
import requests
from flask import Flask, request
然后,创建 /recording-complete
端点.我们将从请求中读取记录 URL.你可以在文档中看到所有的请求参数.然后我们将使用录音 SID 作为文件名打开一个文件,使用请求下载录音并将录音内容写入文件.然后我们可以用一个空的 <Response/>
来响应.
Then, create the /recording-complete
endpoint. We'll read the recording URL from the request. You can see all the request parameters in the documentation. Then we'll open a file using the recording SID as the file name, download the recording using requests and write the contents of the recording to the file. We can then respond with an empty <Response/>
.
@app.route("/recording-complete", methods=['GET', 'POST'])
def recording_complete():
response = VoiceResponse()
# The recording url will return a wav file by default, or an mp3 if you add .mp3
recording_url = request.values['RecordingUrl'] + '.mp3'
filename = request.values['RecordingSid'] + '.mp3'
with open('{}/{}'.format("directory/to/download/to", filename), 'wb') as f:
f.write(requests.get(recording_url).content)
return str(resp)
告诉我你是如何处理的.
Let me know how you get on with that.
这篇关于如何在本地 pc python flask 中从 twilio API 调用后保存入站通话录音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!