在本地 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 的新手。
> `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)
我遵循了 link 但无法理解如何使用 flask link 来保存文件。
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 开发人员布道者。
当你使用<Record>
to record a user, you can provide a URL as the recordingStatusCallback
attribute。然后,当录音准备就绪时,Twilio 将向 URL 发出有关录音详细信息的请求。
因此,您可以将 record
TwiML 更新为如下内容:
# Use <Record> to record the caller's message
response.record(
recording_status_callback="/recording-complete",
recording_status_callback_event="completed"
)
然后您将需要一个用于 /recording-complete
的新路由,您可以在其中接收回调并下载文件。 how to download files in response to a webhook 上有一个很好的 post,但它涵盖了 MMS 消息。但是,我们可以把我们从那里学到的东西下载到录音中。
首先,安装并导入 requests
library。同时从 Flask
导入 request
import requests
from flask import Flask, request
然后,创建 /recording-complete
端点。我们将从请求中读取录音 URL。可以看到all the request parameters in the documentation。然后我们将打开一个以录音 SID 作为文件名的文件,使用 requests 下载录音并将录音的内容写入文件。然后我们可以用一个空的 <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)
告诉我你是怎么处理的。
我使用示例代码接收了从一个号码到 twilio 号码的呼叫。 现在我需要将录音保存为 mp3。我不明白该怎么做。我尝试调用各种参数但失败了。我是 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)
我遵循了 link 但无法理解如何使用 flask link 来保存文件。 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 开发人员布道者。
当你使用<Record>
to record a user, you can provide a URL as the recordingStatusCallback
attribute。然后,当录音准备就绪时,Twilio 将向 URL 发出有关录音详细信息的请求。
因此,您可以将 record
TwiML 更新为如下内容:
# Use <Record> to record the caller's message
response.record(
recording_status_callback="/recording-complete",
recording_status_callback_event="completed"
)
然后您将需要一个用于 /recording-complete
的新路由,您可以在其中接收回调并下载文件。 how to download files in response to a webhook 上有一个很好的 post,但它涵盖了 MMS 消息。但是,我们可以把我们从那里学到的东西下载到录音中。
首先,安装并导入 requests
library。同时从 Flask
request
import requests
from flask import Flask, request
然后,创建 /recording-complete
端点。我们将从请求中读取录音 URL。可以看到all the request parameters in the documentation。然后我们将打开一个以录音 SID 作为文件名的文件,使用 requests 下载录音并将录音的内容写入文件。然后我们可以用一个空的 <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)
告诉我你是怎么处理的。