-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.py
124 lines (100 loc) · 3.79 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os
from flask import Flask
from flask import render_template
from flask import url_for
from flask import request
from flask import redirect
from twilio import twiml
from twilio.rest import TwilioRestClient
from twilio import TwilioRestException
from forms import SMSInviteForm
'''
Configure App
Configures the Flask app with the options set in local_settings.py.
'''
# Declare and configure application
app = Flask(__name__, static_url_path='/static')
app.config.from_pyfile('local_settings.py')
# Instantiate Twilio REST client to use for sending SMS.
if app.config['TWILIO_ACCOUNT_SID'] and app.config['TWILIO_AUTH_TOKEN']:
app.twilio_client = TwilioRestClient(app.config['TWILIO_ACCOUNT_SID'],
app.config['TWILIO_AUTH_TOKEN'])
'''
Index Page
Show a simple web form to submit your phone number and receive an
SMS message.
'''
@app.route('/', methods=['GET', 'POST'])
def index():
# Make sure we have this host configured properly.
config_errors = []
for option in ['TWILIO_ACCOUNT_SID', 'TWILIO_AUTH_TOKEN']:
if not app.config[option]:
config_errors.append("%s is not configured for this host."
% option)
# Define important links
params = {
'sms_request_url': url_for('.sms', _external=True),
'invite_uri': url_for('.invite', _external=True),
'config_errors': config_errors}
# Check if this is a submitted form, if so send the submitted
# phone number an SMS invite.
if request.method == 'POST':
form = SMSInviteForm(request.form)
# Check if form validates, if so send text message.
if form.validate():
try:
app.twilio_client.sms.messages.create(
from_=app.config['TWILIO_CALLER_ID'],
to=form.e164, body="Download Plants vs. "\
"Zombies now using this link: %s" % \
url_for('.invite', _external=True))
except TwilioRestException as e:
# If we encounter a Twilio error, invalidate the form and
# ask user to enter again.
form.phone_number.errors = [unicode(e.msg)]
return render_template('index.html', params=params,
form=form)
# Render Thank You page.
params = {
'phone_number': request.form['phone_number']}
return render_template('thankyou.html', params=params)
else:
# If it's not a post, just create a blank form.
form = SMSInviteForm()
# If not a submission, render form.
return render_template('index.html', params=params, form=form)
'''
Invite URI
This is the link users are set to redirect them to the app in their platform's
app store.
'''
@app.route('/invite')
def invite():
# Get user-agent
user_agent = request.headers.get('User-Agent')
# If user is on iPhone/iPad, send to iTunes App Store
if "iPhone" in user_agent:
return redirect(app.config['IOS_URI'])
# If user is on Android, send to Google Marketplace
elif "Android" in user_agent:
return redirect(app.config['ANDROID_URI'])
# Anything else, given them the web experience
else:
return redirect(app.config['WEB_URI'])
'''
SMS Request URL
This accepts incoming text messages and replies with a link to the app.
'''
@app.route('/sms', methods=['GET', 'POST'])
def sms():
# Respond to any text inbound text message with a link to the app!
response = twiml.Response()
response.sms("Download Plants vs. Zombies now using this " \
"link: http://mad.brooklynhacker.com/invite")
return str(response)
if __name__ == '__main__':
port = int(os.environ.get("PORT", 5000))
if port == 5000:
app.debug = True
app.run(host='0.0.0.0', port=port)