Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

How will I save inbound call recording after the call from twilio API in the local pc python flask

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)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Twilio developer evangelist here.

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.

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"
    )

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.

First, install and import the requests library. Also import request from Flask

import requests
from flask import Flask, request

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...