Python Quickstart: Replying to SMS and MMS Messages

Let's walk through creating your first SMS application: Mobile Monkey.

You can also use Twilio to respond to incoming text messages to your Twilio number. We are going to use Flask to set up a Python web server, so we can tell Twilio what to do when we receive an incoming SMS.

First set up the local Flask server:

Loading Code Samples...
Language
SDK Version:
  • 5.x
  • 6.x
Format:
  • TwiML
# /usr/bin/env python
# Download the twilio-python library from twilio.com/docs/libraries/python
from flask import Flask
from twilio import twiml

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def hello_monkey():
    """Respond to incoming calls with a simple text message."""

    resp = twiml.Response()
    resp.message("Hello, Mobile Monkey")
    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)
# /usr/bin/env python
# Download the twilio-python library from twilio.com/docs/libraries/python
from flask import Flask
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def hello_monkey():
    """Respond to incoming calls with a simple text message."""

    resp = MessagingResponse()
    resp.message("Hello, Mobile Monkey")
    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        Hello, Mobile Monkey
    </Message>
</Response>
Reply SMS using Twilio

Your server will return TwiML, instructions that tell Twilio what to do when you receive incoming messages. For more information see the documentation on responding to incoming messages with TwiML.

Now, start your Flask server from the terminal:

python run.py
* Running on http://127.0.0.1:5000/

You should be able to open a web browser to http://localhost:5000. If you view the page source code, you should see the following text:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        Hello, Mobile Monkey
    </Message>
</Response>

Let Twilio find your server

At this point in the tutorial, you will need to find a way to expose your server to the public Internet.

Here are some tutorials that may be able to help you:

For the rest of the tutorial, we will assume your server is available at example.com.

Try it out

Copy and paste the URL of your server into the "SMS" URL of a number on the Numbers page of your Twilio Account.

Now text message your Twilio number! If everything went according to plan, you should receive a text message in response saying, "Hello, Mobile Monkey".

Picture of Incoming SMS

Respond with an MMS

Now modify your run.py on your server as follows specifying a URL of an image. If necessary, restart your server, then text your Twilio number again. You should receive a text message in response saying, "Hello, Mobile Monkey", but this time an image will be included!

MMS messages can only be sent and received by numbers having MMS capability. You can check the capabilities of numbers in the account portal or query the Available Phone Numbers resource to search for Twilio numbers that are MMS enabled.
Loading Code Samples...
Language
SDK Version:
  • 5.x
  • 6.x
Format:
  • TwiML
# /usr/bin/env python
# Download the twilio-python library from twilio.com/docs/libraries/python
from flask import Flask
from twilio import twiml

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def hello_monkey():
    """Respond to incoming calls with a simple text message."""

    resp = twiml.Response()
    msg = resp.message("Hello, Mobile Monkey")
    msg.media("https://demo.twilio.com/owl.png")

    return str(resp)

    if __name__ == "__main__":
        app.run(debug=True)
# /usr/bin/env python
# Download the twilio-python library from twilio.com/docs/libraries/python
from flask import Flask
from twilio.twiml.messaging_response import MessagingResponse, Message

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def hello_monkey():
    """Respond to incoming calls with a simple text message."""

    resp = MessagingResponse()
    msg = Message()\
        .body("Hello, Mobile Monkey")\
        .media("https://demo.twilio.com/owl.png")
    resp.append(msg)

    return str(resp)

    if __name__ == "__main__":
        app.run(debug=True)
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        <Body>Hello, Mobile Monkey</Body>
        <Media>https://demo.twilio.com/owl.png</Media>
    </Message>
</Response>
Reply MMS using Twilio

You can even send multiple images by adding more media elements to your response. Check out the official docs for more details.

Next: Replying to Messages »

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.

1 / 1
Loading Code Samples...
SDK Version:
  • 5.x
  • 6.x
Format:
  • TwiML
# /usr/bin/env python
# Download the twilio-python library from twilio.com/docs/libraries/python
from flask import Flask
from twilio import twiml

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def hello_monkey():
    """Respond to incoming calls with a simple text message."""

    resp = twiml.Response()
    resp.message("Hello, Mobile Monkey")
    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)
# /usr/bin/env python
# Download the twilio-python library from twilio.com/docs/libraries/python
from flask import Flask
from twilio.twiml.messaging_response import MessagingResponse

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def hello_monkey():
    """Respond to incoming calls with a simple text message."""

    resp = MessagingResponse()
    resp.message("Hello, Mobile Monkey")
    return str(resp)

if __name__ == "__main__":
    app.run(debug=True)
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        Hello, Mobile Monkey
    </Message>
</Response>
SDK Version:
  • 5.x
  • 6.x
Format:
  • TwiML
# /usr/bin/env python
# Download the twilio-python library from twilio.com/docs/libraries/python
from flask import Flask
from twilio import twiml

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def hello_monkey():
    """Respond to incoming calls with a simple text message."""

    resp = twiml.Response()
    msg = resp.message("Hello, Mobile Monkey")
    msg.media("https://demo.twilio.com/owl.png")

    return str(resp)

    if __name__ == "__main__":
        app.run(debug=True)
# /usr/bin/env python
# Download the twilio-python library from twilio.com/docs/libraries/python
from flask import Flask
from twilio.twiml.messaging_response import MessagingResponse, Message

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def hello_monkey():
    """Respond to incoming calls with a simple text message."""

    resp = MessagingResponse()
    msg = Message()\
        .body("Hello, Mobile Monkey")\
        .media("https://demo.twilio.com/owl.png")
    resp.append(msg)

    return str(resp)

    if __name__ == "__main__":
        app.run(debug=True)
<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Message>
        <Body>Hello, Mobile Monkey</Body>
        <Media>https://demo.twilio.com/owl.png</Media>
    </Message>
</Response>