Skip to content

Commit

Permalink
update sms samples
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed Nov 4, 2024
1 parent 37e4022 commit f06c854
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 129 deletions.
2 changes: 1 addition & 1 deletion decode-jwt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ For signed incoming SMS signatures through the Messaging API, please see the sni

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

### Set Up Your Enviroment
### Set Up Your Environment

Install dependencies with `pip` in a virtual environment:

Expand Down
17 changes: 0 additions & 17 deletions server.py

This file was deleted.

5 changes: 0 additions & 5 deletions sms/__init__.py

This file was deleted.

30 changes: 15 additions & 15 deletions sms/send-an-sms-with-unicode.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import os
from os.path import join, dirname
from dotenv import load_dotenv
import vonage

dotenv_path = join(dirname(__file__), '../.env')
dotenv_path = join(dirname(__file__), "../.env")
load_dotenv(dotenv_path)

VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
TO_NUMBER = os.getenv('TO_NUMBER')
VONAGE_BRAND_NAME = os.getenv('VONAGE_BRAND_NAME')
VONAGE_BRAND_NAME = os.getenv("VONAGE_BRAND_NAME")
TO_NUMBER = os.getenv("TO_NUMBER")

client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)
from vonage import Auth, Vonage
from vonage_sms import SmsMessage, SmsResponse

response = client.sms.send_message({
'from': VONAGE_BRAND_NAME,
'to': TO_NUMBER,
'text': 'こんにちは世界',
'type': 'unicode',
})
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))

if response["messages"][0]["status"] == "0":
print("Message sent successfully.")
else:
print(f"Message failed with error: {response['messages'][0]['error-text']}")
message = SmsMessage(
to=TO_NUMBER,
from_=VONAGE_BRAND_NAME,
text='こんにちは世界',
type='unicode',
)

response: SmsResponse = client.sms.send(message)
print(response)
25 changes: 11 additions & 14 deletions sms/send-an-sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,21 @@
dotenv_path = join(dirname(__file__), "../.env")
load_dotenv(dotenv_path)

VONAGE_API_KEY = os.getenv("VONAGE_API_KEY")
VONAGE_API_SECRET = os.getenv("VONAGE_API_SECRET")
VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
VONAGE_BRAND_NAME = os.getenv("VONAGE_BRAND_NAME")
TO_NUMBER = os.getenv("TO_NUMBER")

import vonage
from vonage import Auth, Vonage
from vonage_sms import SmsMessage, SmsResponse

client = vonage.Client(key=VONAGE_API_KEY, secret=VONAGE_API_SECRET)
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))

responseData = client.sms.send_message(
{
"from": VONAGE_BRAND_NAME,
"to": TO_NUMBER,
"text": "A text message sent using the Vonage SMS API",
}
message = SmsMessage(
to=TO_NUMBER,
from_=VONAGE_BRAND_NAME,
text="A text message sent using the Vonage SMS API.",
)

if responseData["messages"][0]["status"] == "0":
print("Message sent successfully.")
else:
print(f"Message failed with error: {responseData['messages'][0]['error-text']}")
response: SmsResponse = client.sms.send(message)
print(response)
42 changes: 17 additions & 25 deletions sms/send-signed-sms.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
# Import dependencies
import os
from os.path import join, dirname
from dotenv import load_dotenv
import vonage

# Load the environment
envpath = join(dirname(__file__), '../.env')
load_dotenv(envpath)
dotenv_path = join(dirname(__file__), "../.env")
load_dotenv(dotenv_path)

# Initialise the client
client = vonage.Client(
key=os.getenv('VONAGE_API_KEY'),
signature_secret=os.getenv('VONAGE_SIGNATURE_SECRET'),
signature_method='md5' # MD5 HMAC, need to select this option in the developer dashboard
)
VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
VONAGE_SIGNATURE_SECRET = os.getenv('VONAGE_SIGNATURE_SECRET')
VONAGE_BRAND_NAME = os.getenv("VONAGE_BRAND_NAME")
TO_NUMBER = os.getenv("TO_NUMBER")

from vonage import Auth, Vonage
from vonage_sms import SmsMessage, SmsResponse

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

# Sending the sms
response = client.sms.send_message({
"from": from_number,
"to": to_number,
"text": text
})
message = SmsMessage(
to=TO_NUMBER,
from_=VONAGE_BRAND_NAME,
text="A text message sent using the Vonage SMS API.",
)

if response["messages"][0]["status"] == "0":
print("Message sent successfully.")
else:
print(f"Message failed with error: {response['messages'][0]['error-text']}")
response: SmsResponse = client.sms.send(message)
print(response)
24 changes: 24 additions & 0 deletions sms/submit-sms-conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
from os.path import join, dirname
from dotenv import load_dotenv

dotenv_path = join(dirname(__file__), "../.env")
load_dotenv(dotenv_path)

VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')

from vonage import Auth, Vonage

client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))

client.sms.submit_sms_conversion(
message_id='MESSAGE_ID',
delivered=True,
timestamp='2020-01-01T12:00:00Z',
)

if client.http_client.last_response.status_code == 200:
print('Conversion submitted successfully.')
else:
print('Conversion not submitted.')
3 changes: 0 additions & 3 deletions sms/verify-signed-sms/.flaskenv

This file was deleted.

54 changes: 30 additions & 24 deletions sms/verify-signed-sms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,33 @@ Note: you must have enabled signed webhooks. To do this, please contact support.

## Usage

Install dependencies inside a virtual environment, run the Flask application and set up a webhook.

### Set up a virtual environment
1. Run `python3 -m venv venv`
1. Run `source venv/bin/activate`
1. Run `pip install -r requirements.txt`

### Set up and run the Flask application
1. Copy `.env.dist` to `.env` in the root directory of this repo and fill in your values for:
* `VONAGE_API_KEY`
* `VONAGE_SIGNATURE_SECRET`
* `VONAGE_SIGNATURE_SECRET_METHOD`
1. Copy `.flaskenv.dist` to `.flaskenv` and fill in your credentials
1. Run `flask run`

### Set up incoming webhook
1. Start ngrok with `ngrok http 3000`. ngrok will give you a forwarding address you can now use for your delivery receipts.
1. Copy this URL and go to your [customer dashboard](https://dashboard.nexmo.com/sign-in)
1. Click on "Numbers", and then ["Your Numbers"](https://dashboard.nexmo.com/your-numbers)
1. Click the "Edit" icon on the number to use, and change the "Inbound Webhook URL" option to `https://your-ngrok-url`
1. Click "Save"

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.
You may want to use a localhost tunnel agent such as [ngrok](https://ngrok.com/) for local testing.

### Set Up Your Environment

Install dependencies with `pip` in a virtual environment:

```bash
python3 -m venv venv
. ./venv/bin/activate

# Point to the requirements file in the root of the python-code-snippets repo
pip install -r requirements.txt
```

### Set Up an Incoming Webhook
1. Start ngrok with `ngrok http 8000`. ngrok will give you a forwarding address you can now use to recieve event webhooks.
1. Go to the [Customer Dashboard](https://dashboard.nexmo.com/sign-in).
1. Go to ["API Settings"](https://dashboard.nexmo.com/settings).
1. Under "SMS Settings", choose the SMS API and paste in your ngrok URL in the "Delivery Receipts" section.
1. Click "Save Changes".

### Start the FastAPI Server

Run the FastAPI server with

```bash
fastapi dev sms/verify-signed-sms/main.py
```

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.
25 changes: 0 additions & 25 deletions sms/verify-signed-sms/app.py

This file was deleted.

35 changes: 35 additions & 0 deletions sms/verify-signed-sms/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import os
from os.path import join, dirname

from dotenv import load_dotenv

# Load the environment
envpath = join(dirname(__file__), '../.env')
load_dotenv(envpath)

VONAGE_API_KEY = os.getenv("VONAGE_API_KEY")
VONAGE_SIGNATURE_SECRET = os.getenv("VONAGE_SIGNATURE_SECRET")
VONAGE_SIGNATURE_SECRET_METHOD = os.getenv("VONAGE_SIGNATURE_SECRET_METHOD")

from fastapi import FastAPI, Request
from vonage import Auth, Vonage

client = Vonage(
Auth(
api_key=VONAGE_API_KEY,
signature_secret=VONAGE_SIGNATURE_SECRET,
signature_method=VONAGE_SIGNATURE_SECRET_METHOD,
)
)

app = FastAPI()


@app.get('/')
async def verify_signed_webhook(request: Request):
data = await request.json()

if client.http_client.auth.check_signature(data):
print('Valid signature')
else:
print('Invalid signature')

0 comments on commit f06c854

Please sign in to comment.