Python Quickstart: Greet Caller by Name

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.

Ok, that was fun. Seriously, it was. But let's raise the stakes a bit. When someone calls, we are going to try to greet the caller by name.
from flask import Flask, request, redirect
from twilio.twiml.voice_response import VoiceResponse

app = Flask(__name__)

# Try adding your own number to this list!
callers = {
    "+14158675309": "Curious George",
    "+14158675310": "Boots",
    "+14158675311": "Virgil",

@app.route("/", methods=['GET', 'POST'])
def hello_monkey():
    # Get the caller's phone number from the incoming Twilio request
    from_number = request.values.get('From', None)
    resp = VoiceResponse()

    # if the caller is someone we know:
    if from_number in callers:
        # Greet the caller by name
        resp.say("Hello " + callers[from_number])
        resp.say("Hello Monkey")

    return str(resp)

if __name__ == "__main__":

When Twilio makes a request to your URL, information about the call is included along with the request, such as the number the call is coming from and the time of the call. We then check to see if the caller is known looking up the number in the callers dictionary to extract their name. We then <Say> their name, or the word 'Monkey' if their Caller ID isn't known.

If you add your phone number and name to the callers dictionary, you can listen to Twilio greet you by name.

Note that the phone numbers in the callers dictionary have +1 prepended to them. '1' is the international country code for the US and Canada and the '+' formats the number in E.164 format, which is used by Twilio when passing you phone numbers.

Next: Play mp3 for Caller »

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.