Handle the Form Post

Add the following function to the bottom of your python file, to accept the POST request from the form:

@app.route('/send_sms', methods=['POST'])
def send_sms():
    """ A POST endpoint that sends an SMS. """
 
    # Extract the form values:
    to_number = request.form['to_number']
    message = request.form['message']
 
    # Send the SMS message:
    result = nexmo_client.send_message({
        'from': VONAGE_NUMBER,
        'to': to_number,
        'text': message,
    })
 
    # Redirect the user back to the form:
    return redirect(url_for('index'))

If your FLASK_DEBUG flag is set to true, then your changes should automatically be reloaded into the running server. Refresh your form, fill in your phone number and a message. Make sure the number is in international format without the '+' at the start. Hit "Send SMS" and check your phone.

If the application did not work, check out the extra lines in the sample code in server.py and index.html that use Flask’s flash message mechanism to report errors to the user.

How to Send SMS Messages with Python, Flask and Nexmo

This tutorial introduces you to sending SMS with Python, making use of the Nexmo Python library. It starts by showing how to send SMS from the REPL, then goes on to show you how to build a simple flask app with SMS capabilities.

手順
1
Introduction to this tutorial
2
Prerequisites
3
Install the Vonage Python Server SDK
4
Send an SMS from the Python REPL
5
Set up an SMS Sending Flask App
6
Add a Send SMS View
7
Run the Flask Server
8
Handle the Form Post
9
What's next?