Leaving A Message For A Rockstar with Python and Twilio

May 12, 2017
Written by

ZBB

You know when you’re selling out stadiums and want to connect with your fans one-on-one but can’t exactly give out your personal phone number? Yeah, common problem — for Zac Brown & his band.
Zac Brown Band has about 1.67 million Twitter followers. The ZBB squad wanted to reach out to every fan before dropping their new record (which came out today). Dialing up 1.67 million people is tricky, so the ZBB team let the fans come to them.

Now, all of them can call up Zac Brown Band’s Twilio number and leave a message relaying their favorite memory, song, or moment from an album.

Here’s an initial prototype of the app I built for Zac’s management crew to get ideas flowing. The entire app runs on about 60 lines of Python.

The goal is to do the following things in order:

  • Greet the caller with a friendly message
  • Allow them to record a message
  • Tell Twilio when they’re done recording a message and bid them adieu.

In this prototype, we set a max length of 30 seconds to ensure the team can get through everyone’s messages easily, and to ward off any would-be-butt-dial voicemails.

Here’s all the code you need to make it happen.

 

from flask import Flask, request
from twilio.twiml.voice_response import VoiceResponse, Gather


app = Flask(__name__)


@app.route("/voice", methods=['GET', 'POST'])
def welcome():
    """
    Twilio voice webhook for incoming calls.
    Respond with a welcome message and ask them to press 1
    to record a message for the band.
    """
    # Start our TwiML response
    resp = VoiceResponse()

    # <Say> a welcome message
    resp.say("Welcome to the band's line")
    resp.say("Press 1 to record a message for the band")

    # <Gather> a response from the caller
    resp.gather(numDigits=1, action='/start-recording')

    return str(resp)

@app.route('/start-recording', methods=['GET', 'POST'])
def start_recording():
    """Processes the caller's <Gather> input from the welcome view"""
    # Start our TwiML response
    resp = VoiceResponse()

    # If the caller pressed one, start the recording
    if 'Digits' in request.values and request.values['Digits'] == '1':
        resp.say('Awesome. Leave a message after the tone.')
        resp.record(max_length="30", action="/end-call")

    # Otherwise, say we didn't understand the caller and ask them to try again
    else:
        resp.say("Sorry, I didn't understand that.")
        resp.say("Press 1 to record a message for the band")
        resp.gather(numDigits=1, action='/start-recording')

    return str(resp)

@app.route('/end-call', methods=['GET', 'POST'])
def end_call():
    """Thanks a caller for their recording and hangs up"""
    # Start our TwiML response
    resp = VoiceResponse()

    # <Say> thanks to the caller
    # (Add your signoff as you see fit)
    resp.say("Thanks for your message. The band will be stoked.")
    resp.hangup()

    return str(resp)

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

If you’ve got any ideas for what you can build in Python, keep me posted – kylekellyyahner.