Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
59 lines (41 sloc) 1.5 KB
##
## main.py
##
# Note: We don't need to call app.run() since our application is embedded within
# the App Engine WSGI application server.
# Twilio docs here: https://www.twilio.com/docs/quickstart/python/sms/replying-to-sms-messages
from flask import Flask, request, redirect
import twilio.twiml
import socket
import ssl
from getmail import getmail
app = Flask(__name__)
# config.py stores secrets:
app.config.from_object('config')
callers = app.config.get('CALLERS')
email_address = app.config.get('EMAIL')
password = app.config.get('PASSWORD')
url = "/" + app.config.get('SECRET_URL')
# url is long random string to prevent a brute force attack
@app.route(url, methods=["GET", "POST"])
def mail_response():
"""Respond to incoming text with email content, if authorized"""
from_number = request.values.get("From", None)
if from_number in callers:
resp = twilio.twiml.Response()
content = getmail(email_address, password)
resp.message(content)
return str(resp)
else:
return 'Sorry, Nothing at this URL.', 404
#TODO:
#Allow user to input 10-20 in the body of the text, respond with corresponding messages
#set max response to ~ 30, default is first 5 messages
@app.errorhandler(404)
def page_not_found(e):
"""Return a custom 404 error."""
return 'Sorry, Nothing at this URL.', 404
@app.errorhandler(500)
def application_error(e):
"""Return a custom 500 error."""
return 'Sorry, unexpected error: {}'.format(e), 500