Python Quickstart: Record a Message from the Caller

Easy so far... how about letting the caller record her own monkey howl? Sounds like fun.

This tutorial assumes you are running Flask on a server available over the Internet, and have the twilio-python library installed. If not, see our guide to setting up your local development environment.
run.py
from flask import Flask, request, redirect
from __future__ import with_statement   # Only necessary for Python 2.5
from twilio.twiml.voice_response import VoiceResponse

app = Flask(__name__)

callers = {
    "+14158675309": "Curious George",
    "+14158675310": "Boots",
    "+14158675311": "Virgil",
}

@app.route("/", methods=['GET', 'POST'])
def hello_monkey():
    from_number = request.values.get('From', None)
    if from_number in callers:
        caller = callers[from_number]
    else:
        caller = "Monkey"

    resp = VoiceResponse()
    # Greet the caller by name
    resp.say("Hello " + caller)
    # Play an mp3
    resp.play("http://demo.twilio.com/hellomonkey/monkey.mp3")

    # Gather digits.
    with resp.gather(numDigits=1, action="/handle-key", method="POST") as g:
        g.say("""To speak to a real monkey, press 1. 
                 Press 2 to record your own monkey howl.
                 Press any other key to start over.""")

    return str(resp)

@app.route("/handle-key", methods=['GET', 'POST'])
def handle_key():
    """Handle key press from a user."""

    digit_pressed = request.values.get('Digits', None)
    if digit_pressed == "1":
        resp = VoiceResponse()
        # Dial (310) 555-1212 - connect that number to the incoming caller.
        resp.dial("+13105551212")
        # If the dial fails:
        resp.say("The call failed, or the remote party hung up. Goodbye.")

        return str(resp)

    elif digit_pressed == "2":
        resp = VoiceResponse()
        resp.say("Record your monkey howl after the tone.")
        resp.record(maxLength="30", action="/handle-recording")
        return str(resp)

    # If the caller pressed anything but 1, redirect them to the homepage.
    else:
        return redirect("/")

@app.route("/handle-recording", methods=['GET', 'POST'])
def handle_recording():
    """Play back the caller's recording."""

    recording_url = request.values.get("RecordingUrl", None)

    resp = VoiceResponse()
    resp.say("Thanks for howling... take a listen to what you howled.")
    resp.play(recording_url)
    resp.say("Goodbye.")
    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)

The handler now outputs a new response asking for your hoooooooooowl. The <Record> tag lets you record audio from the caller.

The maxLength parameter tells Twilio to record up to 30 seconds of audio. The action parameter tells Twilio what to do after the caller is done recording. In this case we pass off to the /handle-recording page.

The URL of the recording is passed to the handler in the request as the RecordingUrl parameter. The URL is externally accessible so you can send the URL of the howl to your friends or reference it in a webpage. Check out the Voice Recorder demo for another example of making voice recordings.

That's the end of this tutorial! If you still have questions, please contact our support team.

Next tutorial: Make outgoing calls »

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.