Skip to content

Commit 8b8f840

Browse files
committed
add server and messenger snippets
1 parent 9c8cad9 commit 8b8f840

27 files changed

+288
-411
lines changed

.env.dist

+3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ WHATSAPP_NUMBER='YOUR_WHATSAPP_NUMBER'
1515
CATALOG_ID='YOUR_CATALOG_ID'
1616
PRODUCT_RETAILER_ID='YOUR_PRODUCT_RETAILER_ID'
1717
VIBER_SERVICE_MESSAGE_ID='YOUR_VIBER_SERVICE_MESSAGE_ID'
18+
AUDIO_URL='EXAMPLE_AUDIO_URL'
1819
IMAGE_URL='EXAMPLE_IMAGE_URL'
20+
FILE_URL='EXAMPLE_FILE_URL'
21+
VIDEO_URL='EXAMPLE_VIDEO_URL'
1922
WHATSAPP_TEMPLATE_REPLACEMENT_TEXT='EXAMPLE_TEMPLATE_REPLACEMENT_TEXT'
2023
RCS_SENDER_ID='YOUR_RCS_SENDER_ID'
2124

decode-jwt/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Verifying Signed Webhooks Demo
2+
3+
This quick demo shows how to verify an incoming Webhook signature by decoding the incoming JWT sent by Vonage.
4+
5+
For signed incoming SMS signatures through the Messaging API, please see the snippet for verifying a signed incoming SMS message instead.
6+
7+
## Usage
8+
9+
You may want to use a localhost tunnel agent such as [ngrok](https://ngrok.com/) for local testing.
10+
11+
### Set Up Your Enviroment
12+
13+
Install dependencies with `pip` in a virtual environment:
14+
15+
```bash
16+
python3 -m venv venv
17+
. ./venv/bin/activate
18+
19+
# Point to the requirements file in the root of the python-code-snippets repo
20+
pip install -r requirements.txt
21+
```
22+
23+
### Set Up an Incoming Webhook
24+
1. Start ngrok with `ngrok http 8000`. ngrok will give you a forwarding address you can now use to recieve event webhooks.
25+
1. Go to the [Customer Dashboard](https://dashboard.nexmo.com/sign-in).
26+
1. Click on ["Applications"](https://dashboard.nexmo.com/applications).
27+
1. Click the three dots and select "Edit" for the application you are using.
28+
1. Under "Capabilities", enter `https://your-ngrok-url/events` for the Voice capability.
29+
1. Click "Save".
30+
31+
### Start the FastAPI Server
32+
33+
Run the FastAPI server with
34+
35+
```bash
36+
fastapi dev decode-jwt/main.py
37+
```
38+
39+
You can now create a voice call using the voice sample in this repo. This sample app will verify the incoming signature.

decode-jwt/main.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
11+
VONAGE_SIGNATURE = os.getenv('VONAGE_SIGNATURE')
12+
13+
from fastapi import FastAPI, Request
14+
from vonage_jwt.verify_jwt import verify_signature
15+
16+
app = FastAPI()
17+
18+
19+
@app.get('/events')
20+
async def verify_signed_webhook(request: Request):
21+
# Need to get the JWT after "Bearer " in the authorization header
22+
auth_header = request.headers["authorization"].split()
23+
token = auth_header[1].strip()
24+
25+
if verify_signature(token, VONAGE_SIGNATURE):
26+
print('Valid signature')
27+
else:
28+
print('Invalid signature')

initialize/application.py

-15
This file was deleted.

initialize/basic.py

-12
This file was deleted.

initialize/full.py

-19
This file was deleted.

jwt/decode-jwt/.env.dist

-8
This file was deleted.

jwt/decode-jwt/Pipfile

-14
This file was deleted.

jwt/decode-jwt/README.md

-33
This file was deleted.

jwt/decode-jwt/app.py

-30
This file was deleted.

jwt/decode-jwt/requirements.txt

-19
This file was deleted.

messages/inbound-message.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
#!/usr/bin/env python3
21
from pprint import pprint
3-
from flask import Flask, request
2+
from fastapi import FastAPI, Request
43

5-
app = Flask(__name__)
4+
app = FastAPI()
65

76

8-
@app.route("/webhooks/inbound-message", methods=["POST"])
9-
def inbound_message():
10-
data = request.get_json()
7+
@app.post('/inbound')
8+
async def inbound_message(request: Request):
9+
data = await request.json()
1110
pprint(data)
12-
return "200"
13-
14-
15-
if __name__ == "__main__":
16-
app.run(host="www.example.org", port=3000)

messages/message-status.py

+5-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
#!/usr/bin/env python3
21
from pprint import pprint
3-
from flask import Flask, request
2+
from fastapi import FastAPI, Request, status
43

5-
app = Flask(__name__)
4+
app = FastAPI()
65

76

8-
@app.route("/webhooks/message-status", methods=["POST"])
9-
def message_status():
10-
data = request.get_json()
7+
@app.post('/status', status_code=status.HTTP_200_OK)
8+
async def status_message(request: Request):
9+
data = await request.json()
1110
pprint(data)
12-
return "200"
13-
14-
15-
if __name__ == "__main__":
16-
app.run(host="www.example.org", port=3000)

messages/messenger/send-audio.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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_APPLICATION_ID = os.environ.get("VONAGE_APPLICATION_ID")
9+
VONAGE_APPLICATION_PRIVATE_KEY_PATH = os.environ.get(
10+
"VONAGE_APPLICATION_PRIVATE_KEY_PATH"
11+
)
12+
13+
VONAGE_FB_SENDER_ID = os.environ.get("VONAGE_FB_SENDER_ID")
14+
FB_RECIPIENT_ID = os.environ.get("FB_RECIPIENT_ID")
15+
AUDIO_URL = os.environ.get("AUDIO_URL")
16+
17+
from vonage import Auth, Vonage
18+
from vonage_messages.models import MessengerAudio, MessengerResource
19+
20+
client = Vonage(
21+
Auth(
22+
application_id=VONAGE_APPLICATION_ID,
23+
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
24+
)
25+
)
26+
27+
message = MessengerAudio(
28+
to=FB_RECIPIENT_ID,
29+
from_=VONAGE_FB_SENDER_ID,
30+
audio=MessengerResource(url=AUDIO_URL),
31+
)
32+
33+
response = client.messages.send(message)
34+
print(response)

messages/messenger/send-file.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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_APPLICATION_ID = os.environ.get("VONAGE_APPLICATION_ID")
9+
VONAGE_APPLICATION_PRIVATE_KEY_PATH = os.environ.get(
10+
"VONAGE_APPLICATION_PRIVATE_KEY_PATH"
11+
)
12+
13+
VONAGE_FB_SENDER_ID = os.environ.get("VONAGE_FB_SENDER_ID")
14+
FB_RECIPIENT_ID = os.environ.get("FB_RECIPIENT_ID")
15+
FILE_URL = os.environ.get("FILE_URL")
16+
17+
from vonage import Auth, Vonage
18+
from vonage_messages.models import MessengerFile, MessengerResource
19+
20+
client = Vonage(
21+
Auth(
22+
application_id=VONAGE_APPLICATION_ID,
23+
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
24+
)
25+
)
26+
27+
message = MessengerFile(
28+
to=FB_RECIPIENT_ID,
29+
from_=VONAGE_FB_SENDER_ID,
30+
file=MessengerResource(url=FILE_URL),
31+
)
32+
33+
response = client.messages.send(message)
34+
print(response)

messages/messenger/send-image.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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_APPLICATION_ID = os.environ.get("VONAGE_APPLICATION_ID")
9+
VONAGE_APPLICATION_PRIVATE_KEY_PATH = os.environ.get(
10+
"VONAGE_APPLICATION_PRIVATE_KEY_PATH"
11+
)
12+
13+
VONAGE_FB_SENDER_ID = os.environ.get("VONAGE_FB_SENDER_ID")
14+
FB_RECIPIENT_ID = os.environ.get("FB_RECIPIENT_ID")
15+
IMAGE_URL = os.environ.get("IMAGE_URL")
16+
17+
from vonage import Auth, Vonage
18+
from vonage_messages.models import MessengerImage, MessengerResource
19+
20+
client = Vonage(
21+
Auth(
22+
application_id=VONAGE_APPLICATION_ID,
23+
private_key=VONAGE_APPLICATION_PRIVATE_KEY_PATH,
24+
)
25+
)
26+
27+
message = MessengerImage(
28+
to=FB_RECIPIENT_ID,
29+
from_=VONAGE_FB_SENDER_ID,
30+
image=MessengerResource(url=IMAGE_URL),
31+
)
32+
33+
response = client.messages.send(message)
34+
print(response)

0 commit comments

Comments
 (0)