Skip to content

Instantly share code, notes, and snippets.

@mattmakai
Created May 30, 2016 18:13
Show Gist options
  • Save mattmakai/8ab434ccb604d3ba5bde817a183e0bde to your computer and use it in GitHub Desktop.
Save mattmakai/8ab434ccb604d3ba5bde817a183e0bde to your computer and use it in GitHub Desktop.
Simple Python & Flask app to respond to incoming SMS text messages
from flask import Flask, Response, request
from twilio import twiml
app = Flask(__name__)
@app.route("/")
def check_app():
# returns a simple string stating the app is working
return Response("It works!"), 200
@app.route("/twilio", methods=["POST"])
def inbound_sms():
response = twiml.Response()
# we get the SMS message from the request. we could also get the
# "To" and the "From" phone number as well
inbound_message = request.form.get("Body")
# we can now use the incoming message text in our Python application
if inbound_message == "Hello":
response.message("Hello back to you!")
else:
response.message("Hi! Not quite sure what you meant, but okay.")
# we return back the mimetype because Twilio needs an XML response
return Response(str(response), mimetype="application/xml"), 200
if __name__ == "__main__":
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment