#!/usr/bin/env python
"""
Demo Flask-Twilio application
"""

# Imports.
import os
from flask import flash, Flask, render_template, request
from flask_twilio import Twilio, Response
from twilio.base.exceptions import TwilioRestException
from jinja2 import DictLoader

# A standard test number.
# See https://www.twilio.com/docs/api/rest/test-credentials.
DEFAULT_NUMBER = '+15005550006'

# Application configuration.
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(24)
app.config['TWILIO_ACCOUNT_SID'] = os.environ['TWILIO_ACCOUNT_SID']
app.config['TWILIO_AUTH_TOKEN'] = os.environ['TWILIO_AUTH_TOKEN']
app.config['TWILIO_FROM'] = os.environ.get('TWILIO_FROM', DEFAULT_NUMBER)
twilio = Twilio(app)

# Main form template.
app.jinja_loader = DictLoader({'example.html': '''\
<!doctype html>
<title>Flask-Twilio Test</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<br>
<div class=container>
    <div class=jumbotron>
        <h2>Flask-Twilio Test</h2>
    </div>
    {% with messages = get_flashed_messages(with_categories=true) %}
        {% for category, message in messages %}
            <div class="alert alert-{{ category }}">{{ message }}</div>
        {% endfor %}
    {% endwith %}
    <form method=post>
        <div class=input-group>
            <input type=hidden id=say name=say value=1>
            <input type=hidden id=sms name=sms value=1>
            <input autofocus class=form-control type=tel name=to value="{{ to }}">
            <div class=input-group-btn>
                <button class="btn btn-default" type=submit>
                    Say + SMS
                </button>
                <button type=button class="btn btn-default dropdown-toggle" data-toggle=dropdown aria-haspopup=true aria-expanded=false>
                    <span class=caret></span>
                </button>
                <ul class="dropdown-menu dropdown-menu-right">
                    <li><a href="#" onclick="$('form').submit();">Say + SMS</a></li>
                    <li><a href="#" onclick="$('#sms').val(0); $('form').submit();">Say only</a></li>
                    <li><a href="#" onclick="$('#say').val(0); $('form').submit();">SMS only</a></li>
                </ul>
            </div>
        </div>
    </form>
</div>
'''})


@app.route('/twiml')
@twilio.twiml
def test_call():
    """View for producing the TwiML document."""
    say = int(request.values.get('say', 1))
    sms = int(request.values.get('sms', 1))
    resp = Response()
    if say:
        resp.say('This is a voice call from Twilio!', voice='female')
    if sms:
        resp.sms('This is an SMS message from Twilio!')
    return resp


@app.route('/', methods=['GET'])
def index_get():
    """Main form view."""
    return render_template(
        'example.html',
        to=request.values.get('to', DEFAULT_NUMBER))


@app.route('/', methods=['POST'])
def index_post():
    """Main form action."""
    try:
        say = int(request.values.get('say', 1))
        sms = int(request.values.get('sms', 1))
        to = request.values.get('to', None)
        if say:
            twilio.call_for('test_call', to, say=say, sms=sms)
        else:
            twilio.message('This is an SMS message from Twilio!', to)
        flash('Request was successfully sent to Twilio.', 'success')
    except TwilioRestException as e:
        flash('Failed to make call: ' + e.msg, 'danger')
    return index_get()


# Start application.
app.run('0.0.0.0')

@@ -58,26 +58,25 @@
 %22%22%0A%0A
-# I
+i
 mport
+ o
 s
-.
 %0Aimport 
 os%0Af
@@ -71,18 +71,23 @@
 %0Aimport 
-o
 s
+ocket%0A
 %0Afrom fl
@@ -233,16 +233,112 @@
 ception%0A
+from twilio.twiml.messaging_response import Message%0Afrom twilio.twiml.voice_response import Say%0A
 from jin
@@ -573,16 +573,71 @@
 dom(24)%0A
+app.config%5B'SERVER_NAME'%5D = socket.getfqdn() + ':5000'%0A
 app.conf
@@ -2961,17 +2961,24 @@
    resp.
-s
+append(S
 ay('This
@@ -3024,16 +3024,17 @@
 female')
+)
 %0A    if 
@@ -3051,19 +3051,30 @@
    resp.
-sms
+append(Message
 ('This i
@@ -3104,16 +3104,17 @@
 wilio!')
+)
 %0A    ret
@@ -3862,16 +3862,68 @@
 anger')%0A
+        app.logger.exception('Failed to make call')%0A
     retu

Update example.py [ci skip]