Skip to content

Commit c87e101

Browse files
author
obvonage
committed
add files
1 parent db7dd36 commit c87e101

File tree

9 files changed

+196
-1
lines changed

9 files changed

+196
-1
lines changed

.env

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Do not use this in production:
2+
FLASK_DEBUG=true
3+
4+
# Replace the following with any random value you like:
5+
FLASK_SECRET_KEY=RANDOM-STRING_CHANGE-THIS-Ea359
6+
7+
# Sender ID (or SMS sender name), VONAGE_BRAND_NAME
8+
VONAGE_NUMBER=Vonage APIs
9+
10+
# Get the following from https://dashboard.nexmo.com/settings
11+
VONAGE_API_KEY=paste-your-api-key
12+
VONAGE_API_SECRET=paste-your-api-secret

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
venv/
2+
.env
Loading

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Vonage Community
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,64 @@
1-
# tutorial-sms-flask-python-sdk
1+
# How to Send SMS Messages with Python, Flask, and Vonage
2+
3+
Using content in this repository, you can send an SMS using the Flask application and Vonage API. Prepared a simple code sample will be an excellent point to start and create your application quickly.
4+
5+
## Requirements
6+
7+
* [Python](https://www.python.org/downloads/)
8+
* [GIT](https://git-scm.com/downloads)
9+
* [Vonage Application](https://developer.vonage.com/application/overview)
10+
11+
Sign in/Sign up for free [developer.vonage.com](https://developer.vonage.com/); to be able to use the [Vonage Messages API](https://developer.vonage.com/en/messages/overview), you'll have to create a [Vonage Application](https://developer.vonage.com/application/overview) from the developer portal.
12+
13+
![](settings.png)
14+
15+
In the left menu [here](https://dashboard.nexmo.com/), click API Settings. Under the API keys tab, you will find your API key and Account secret (API secret). We will use these credentials later.
16+
17+
**Set up a Python**
18+
19+
First, make sure that your laptop/server has [Python 3](https://www.python.org/downloads/) installed. We will then use venv to create an isolated environment with only the necessary packages.
20+
21+
To deploy the application, you need to clone the repo using [git](https://git-scm.com/downloads) and update `.env` with your credentials
22+
23+
## Installation
24+
25+
Clone source code
26+
```bash
27+
git clone https://github.com/obvonage/tutorial-sms-flask-python-sdk
28+
```
29+
30+
Go to the project folder
31+
```bash
32+
cd tutorial-sms-flask-python-sdk
33+
```
34+
35+
Install dependencies
36+
37+
```bash
38+
pip install -r requirements.txt
39+
```
40+
41+
Start your app with the following command.
42+
43+
```bash
44+
FLASK_APP=server.py flask run
45+
```
46+
Expected output
47+
```bash
48+
* Serving Flask app 'server.py'
49+
* Debug mode: on
50+
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
51+
* Running on http://127.0.0.1:5000
52+
Press CTRL+C to quit
53+
* Restarting with stat
54+
* Debugger is active!
55+
* Debugger PIN: 648-303-150
56+
57+
```
58+
## Usage
59+
60+
Open [http://localhost:5000/](http://localhost:5000/) in your browser, and you should see the following web page
61+
62+
![](Flask-App-for-Sending-SMS-Using-Vonage-Python-SDK.png)
63+
64+
Let's send an SMS using this interface. Ensure the number is in international format without the '+' at the start. Hit "Send SMS" and check your phone!

requirements.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
certifi==2022.12.7
2+
cffi==1.15.1
3+
charset-normalizer==3.0.1
4+
click==8.1.3
5+
cryptography==39.0.0
6+
Deprecated==1.2.13
7+
Flask==2.2.2
8+
idna==3.4
9+
importlib-metadata==6.0.0
10+
itsdangerous==2.1.2
11+
Jinja2==3.1.2
12+
MarkupSafe==2.1.2
13+
pycparser==2.21
14+
pydantic==1.10.4
15+
PyJWT==2.6.0
16+
python-dotenv==0.21.1
17+
pytz==2022.7.1
18+
requests==2.28.2
19+
typing_extensions==4.4.0
20+
urllib3==1.26.14
21+
vonage==3.2.2
22+
Werkzeug==2.2.2
23+
wrapt==1.14.1
24+
zipp==3.12.0

server.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from dotenv import load_dotenv
2+
from flask import Flask, flash, redirect, render_template, request, url_for
3+
import vonage
4+
5+
from os import environ as env
6+
7+
# Load environment variables from a .env file:
8+
load_dotenv('.env')
9+
10+
# Load in configuration from environment variables:
11+
VONAGE_API_KEY = env['VONAGE_API_KEY']
12+
VONAGE_API_SECRET = env['VONAGE_API_SECRET']
13+
VONAGE_NUMBER = env['VONAGE_NUMBER']
14+
15+
# Create a new Vonage Client object:
16+
client = vonage.Client(
17+
key=VONAGE_API_KEY, secret=VONAGE_API_SECRET
18+
)
19+
20+
# Initialize Flask:
21+
app = Flask(__name__)
22+
app.config['SECRET_KEY'] = env['FLASK_SECRET_KEY']
23+
24+
@app.route('/')
25+
def index():
26+
""" A view that renders the Send SMS form. """
27+
return render_template('index.html')
28+
29+
@app.route('/send_sms', methods=['POST'])
30+
def send_sms():
31+
""" A POST endpoint that sends an SMS. """
32+
33+
# Extract the form values:
34+
to_number = request.form['to_number']
35+
message = request.form['message']
36+
37+
# Send the SMS message:
38+
result = client.sms.send_message({
39+
'from': VONAGE_NUMBER,
40+
'to': to_number,
41+
'text': message,
42+
})
43+
44+
# Redirect the user back to the form:
45+
return redirect(url_for('index'))

settings.png

141 KB
Loading

templates/index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<!-- Required meta tags -->
5+
<meta charset="utf-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
7+
8+
<!-- Bootstrap CSS -->
9+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
10+
11+
<title>Send an SMS</title>
12+
</head>
13+
<body>
14+
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
15+
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.14.7/dist/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
16+
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.3.1/dist/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
17+
<div class="container">
18+
<h1>Send an SMS</h1>
19+
<form action="/send_sms" method="POST">
20+
<div class="form-group"><label for="destination">Phone Number</label>
21+
<input id="to_number" class="form-control" name="to_number" type="tel" placeholder="Phone Number" /></div>
22+
<div class="form-group"><label for="message">Message</label>
23+
<textarea id="message" class="form-control" name="message" placeholder="Your message goes here"></textarea></div>
24+
<button type="submit" class="btn btn-primary">Send SMS</button>
25+
</form>
26+
</div>
27+
</body>
28+
</html>

0 commit comments

Comments
 (0)