Skip to content

Commit f06c854

Browse files
committed
update sms samples
1 parent 37e4022 commit f06c854

11 files changed

+133
-129
lines changed

decode-jwt/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ For signed incoming SMS signatures through the Messaging API, please see the sni
88

99
You may want to use a localhost tunnel agent such as [ngrok](https://ngrok.com/) for local testing.
1010

11-
### Set Up Your Enviroment
11+
### Set Up Your Environment
1212

1313
Install dependencies with `pip` in a virtual environment:
1414

server.py

-17
This file was deleted.

sms/__init__.py

-5
This file was deleted.

sms/send-an-sms-with-unicode.py

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import os
22
from os.path import join, dirname
33
from dotenv import load_dotenv
4-
import vonage
54

6-
dotenv_path = join(dirname(__file__), '../.env')
5+
dotenv_path = join(dirname(__file__), "../.env")
76
load_dotenv(dotenv_path)
87

98
VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
109
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
11-
TO_NUMBER = os.getenv('TO_NUMBER')
12-
VONAGE_BRAND_NAME = os.getenv('VONAGE_BRAND_NAME')
10+
VONAGE_BRAND_NAME = os.getenv("VONAGE_BRAND_NAME")
11+
TO_NUMBER = os.getenv("TO_NUMBER")
1312

14-
client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)
13+
from vonage import Auth, Vonage
14+
from vonage_sms import SmsMessage, SmsResponse
1515

16-
response = client.sms.send_message({
17-
'from': VONAGE_BRAND_NAME,
18-
'to': TO_NUMBER,
19-
'text': 'こんにちは世界',
20-
'type': 'unicode',
21-
})
16+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
2217

23-
if response["messages"][0]["status"] == "0":
24-
print("Message sent successfully.")
25-
else:
26-
print(f"Message failed with error: {response['messages'][0]['error-text']}")
18+
message = SmsMessage(
19+
to=TO_NUMBER,
20+
from_=VONAGE_BRAND_NAME,
21+
text='こんにちは世界',
22+
type='unicode',
23+
)
24+
25+
response: SmsResponse = client.sms.send(message)
26+
print(response)

sms/send-an-sms.py

+11-14
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,21 @@
55
dotenv_path = join(dirname(__file__), "../.env")
66
load_dotenv(dotenv_path)
77

8-
VONAGE_API_KEY = os.getenv("VONAGE_API_KEY")
9-
VONAGE_API_SECRET = os.getenv("VONAGE_API_SECRET")
8+
VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
9+
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
1010
VONAGE_BRAND_NAME = os.getenv("VONAGE_BRAND_NAME")
1111
TO_NUMBER = os.getenv("TO_NUMBER")
1212

13-
import vonage
13+
from vonage import Auth, Vonage
14+
from vonage_sms import SmsMessage, SmsResponse
1415

15-
client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)
16+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
1617

17-
responseData = client.sms.send_message(
18-
{
19-
"from": VONAGE_BRAND_NAME,
20-
"to": TO_NUMBER,
21-
"text": "A text message sent using the Vonage SMS API",
22-
}
18+
message = SmsMessage(
19+
to=TO_NUMBER,
20+
from_=VONAGE_BRAND_NAME,
21+
text="A text message sent using the Vonage SMS API.",
2322
)
2423

25-
if responseData["messages"][0]["status"] == "0":
26-
print("Message sent successfully.")
27-
else:
28-
print(f"Message failed with error: {responseData['messages'][0]['error-text']}")
24+
response: SmsResponse = client.sms.send(message)
25+
print(response)

sms/send-signed-sms.py

+17-25
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
1-
# Import dependencies
21
import os
32
from os.path import join, dirname
43
from dotenv import load_dotenv
5-
import vonage
64

7-
# Load the environment
8-
envpath = join(dirname(__file__), '../.env')
9-
load_dotenv(envpath)
5+
dotenv_path = join(dirname(__file__), "../.env")
6+
load_dotenv(dotenv_path)
107

11-
# Initialise the client
12-
client = vonage.Client(
13-
key=os.getenv('VONAGE_API_KEY'),
14-
signature_secret=os.getenv('VONAGE_SIGNATURE_SECRET'),
15-
signature_method='md5' # MD5 HMAC, need to select this option in the developer dashboard
16-
)
8+
VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
9+
VONAGE_SIGNATURE_SECRET = os.getenv('VONAGE_SIGNATURE_SECRET')
10+
VONAGE_BRAND_NAME = os.getenv("VONAGE_BRAND_NAME")
11+
TO_NUMBER = os.getenv("TO_NUMBER")
12+
13+
from vonage import Auth, Vonage
14+
from vonage_sms import SmsMessage, SmsResponse
1715

18-
# Define variables - replace FROM_NUMBER and TO_NUMBER with actual numbers
19-
from_number = os.getenv('FROM_NUMBER')
20-
to_number = os.getenv('TO_NUMBER')
21-
text = 'A text message sent using the Vonage SMS API'
16+
client = Vonage(Auth(api_key=VONAGE_API_KEY, signature_secret=VONAGE_SIGNATURE_SECRET))
2217

23-
# Sending the sms
24-
response = client.sms.send_message({
25-
"from": from_number,
26-
"to": to_number,
27-
"text": text
28-
})
18+
message = SmsMessage(
19+
to=TO_NUMBER,
20+
from_=VONAGE_BRAND_NAME,
21+
text="A text message sent using the Vonage SMS API.",
22+
)
2923

30-
if response["messages"][0]["status"] == "0":
31-
print("Message sent successfully.")
32-
else:
33-
print(f"Message failed with error: {response['messages'][0]['error-text']}")
24+
response: SmsResponse = client.sms.send(message)
25+
print(response)

sms/submit-sms-conversion.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
5+
dotenv_path = join(dirname(__file__), "../.env")
6+
load_dotenv(dotenv_path)
7+
8+
VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
9+
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
10+
11+
from vonage import Auth, Vonage
12+
13+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
14+
15+
client.sms.submit_sms_conversion(
16+
message_id='MESSAGE_ID',
17+
delivered=True,
18+
timestamp='2020-01-01T12:00:00Z',
19+
)
20+
21+
if client.http_client.last_response.status_code == 200:
22+
print('Conversion submitted successfully.')
23+
else:
24+
print('Conversion not submitted.')

sms/verify-signed-sms/.flaskenv

-3
This file was deleted.

sms/verify-signed-sms/README.md

+30-24
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,33 @@ Note: you must have enabled signed webhooks. To do this, please contact support.
77

88
## Usage
99

10-
Install dependencies inside a virtual environment, run the Flask application and set up a webhook.
11-
12-
### Set up a virtual environment
13-
1. Run `python3 -m venv venv`
14-
1. Run `source venv/bin/activate`
15-
1. Run `pip install -r requirements.txt`
16-
17-
### Set up and run the Flask application
18-
1. Copy `.env.dist` to `.env` in the root directory of this repo and fill in your values for:
19-
* `VONAGE_API_KEY`
20-
* `VONAGE_SIGNATURE_SECRET`
21-
* `VONAGE_SIGNATURE_SECRET_METHOD`
22-
1. Copy `.flaskenv.dist` to `.flaskenv` and fill in your credentials
23-
1. Run `flask run`
24-
25-
### Set up incoming webhook
26-
1. Start ngrok with `ngrok http 3000`. ngrok will give you a forwarding address you can now use for your delivery receipts.
27-
1. Copy this URL and go to your [customer dashboard](https://dashboard.nexmo.com/sign-in)
28-
1. Click on "Numbers", and then ["Your Numbers"](https://dashboard.nexmo.com/your-numbers)
29-
1. Click the "Edit" icon on the number to use, and change the "Inbound Webhook URL" option to `https://your-ngrok-url`
30-
1. Click "Save"
31-
32-
You can now send an SMS and the incoming message will be sent to your webhook URL. You can check the console output of
33-
the application to see the results of the signature check.
10+
You may want to use a localhost tunnel agent such as [ngrok](https://ngrok.com/) for local testing.
11+
12+
### Set Up Your Environment
13+
14+
Install dependencies with `pip` in a virtual environment:
15+
16+
```bash
17+
python3 -m venv venv
18+
. ./venv/bin/activate
19+
20+
# Point to the requirements file in the root of the python-code-snippets repo
21+
pip install -r requirements.txt
22+
```
23+
24+
### Set Up an Incoming Webhook
25+
1. Start ngrok with `ngrok http 8000`. ngrok will give you a forwarding address you can now use to recieve event webhooks.
26+
1. Go to the [Customer Dashboard](https://dashboard.nexmo.com/sign-in).
27+
1. Go to ["API Settings"](https://dashboard.nexmo.com/settings).
28+
1. Under "SMS Settings", choose the SMS API and paste in your ngrok URL in the "Delivery Receipts" section.
29+
1. Click "Save Changes".
30+
31+
### Start the FastAPI Server
32+
33+
Run the FastAPI server with
34+
35+
```bash
36+
fastapi dev sms/verify-signed-sms/main.py
37+
```
38+
39+
You can now send an SMS and the incoming message will be sent to your webhook URL. You can check the console output of the application to see the results of the signature check.

sms/verify-signed-sms/app.py

-25
This file was deleted.

sms/verify-signed-sms/main.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
from os.path import join, dirname
3+
4+
from dotenv import load_dotenv
5+
6+
# Load the environment
7+
envpath = join(dirname(__file__), '../.env')
8+
load_dotenv(envpath)
9+
10+
VONAGE_API_KEY = os.getenv("VONAGE_API_KEY")
11+
VONAGE_SIGNATURE_SECRET = os.getenv("VONAGE_SIGNATURE_SECRET")
12+
VONAGE_SIGNATURE_SECRET_METHOD = os.getenv("VONAGE_SIGNATURE_SECRET_METHOD")
13+
14+
from fastapi import FastAPI, Request
15+
from vonage import Auth, Vonage
16+
17+
client = Vonage(
18+
Auth(
19+
api_key=VONAGE_API_KEY,
20+
signature_secret=VONAGE_SIGNATURE_SECRET,
21+
signature_method=VONAGE_SIGNATURE_SECRET_METHOD,
22+
)
23+
)
24+
25+
app = FastAPI()
26+
27+
28+
@app.get('/')
29+
async def verify_signed_webhook(request: Request):
30+
data = await request.json()
31+
32+
if client.http_client.auth.check_signature(data):
33+
print('Valid signature')
34+
else:
35+
print('Invalid signature')

0 commit comments

Comments
 (0)