Initiate a call, record it, and play the recording back
<p>I am putting together a POC for a client that wants to do phone based testing. In the POC, we simply want to let the user enter a phone# on a web page. We would then display a question and call their number. We would record their response to the question and play it back to them.</p>

<p>I can initiate the call, but can't figure out how to indicate that I want to record it. Ideally, I would like to say something and start recording after the beep.</p>

<p>I have all of 3 hours of experience with Twilio, so forgive my ignorance.</p>

<p>Here is my code so far:</p>

<pre><code>import logging

# [START imports]
from flask import Flask, render_template, request
import twilio.twiml
from twilio.rest import TwilioRestClient
# [END imports]

app = Flask(__name__)


# [START form]
@app.route('/form')
def form():
    return render_template('form.html')
# [END form]


# [START submitted]
@app.route('/submitted', methods=['POST'])
def submitted_form():
    phone = request.form['phone']

    account_sid = "AC60***********************"
    auth_token = "27ea************************"
    client = TwilioRestClient(account_sid, auth_token)
    call = client.calls.create(to=phone,  # Any phone number
        from_="+160#######", # Must be a valid Twilio number
        url="https://my-host/static/prompt.xml")

    call.record(maxLength="30", action="/handle-recording")

    return render_template(
        'submitted_form.html',
        phone=phone)
    # [END render_template]

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

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

    resp = twilio.twiml.Response()
    resp.say("Thanks for your response... take a listen to what you responded.")
    resp.play(recording_url)
    resp.say("Goodbye.")
    return str(resp)

@app.errorhandler(500)
def server_error(e):
    # Log the error and stacktrace.
    logging.exception('An error occurred during a request.')
    return 'An internal error occurred.', 500
# [END app]
</code></pre>

<p>Twilio developer evangelist here.</p>

<p>When you create the call, you pass a URL to the call. That URL will be the one called when the user answers the phone. The response to that request should be the <a href="https://www.twilio.com/docs/api/twiml" rel="nofollow">TwiML</a> to instruct Twilio to <a href="https://www.twilio.com/docs/api/twiml/say" rel="nofollow">say the message</a> and <a href="https://www.twilio.com/docs/api/twiml/record" rel="nofollow">record</a> the response. Like so:</p>

<pre><code>@app.route("/handle-call", methods=['GET', 'POST'])
def handle_call():
    resp = twilio.twiml.Response()
    resp.say("Please leave your message after the beep")
    resp.record(action="/handle-recording", method="POST")
    return str(resp)
</code></pre>

<p>Then you just need to update your call creation to point to that URL</p>

<pre><code>call = client.calls.create(to=phone,  # Any phone number
        from_="+160#######", # Must be a valid Twilio number
        url="https://my-host/handle-call")
</code></pre>

<p>Your <code>/handle-recording</code> path looks as though it will do what you want already.</p>

<p>Just a quick tip, as you're new to Twilio, when developing using webhooks I recommend using <a href="http://ngrok.com" rel="nofollow">ngrok</a> to tunnel to your dev machine and expose your application to Twilio. I wrote a <a href="https://www.twilio.com/blog/2015/09/6-awesome-reasons-to-use-ngrok-when-testing-webhooks.html" rel="nofollow">blog post about how to use ngrok</a> and some of the features I like too.</p>

<p>Let me know if this helps at all.</p>

python twilio