Skip to content

Commit b99c5de

Browse files
committed
renamed verify and verifyv2, add numbers and number insight snippets
1 parent 14ff0e6 commit b99c5de

32 files changed

+128
-134
lines changed

number-insight/async-callback/.env.dist

-4
This file was deleted.

number-insight/async-callback/Pipfile

-13
This file was deleted.
+30-16
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,41 @@
1-
This quick demo shows how to accept a Number Insight Async Webhook
1+
# Verifying Signed Webhooks Demo
2+
3+
This quick demo shows how to recieve an incoming Number Insight webhook.
24

35
## Usage
46

5-
### Setup
7+
You may want to use a localhost tunnel agent such as [ngrok](https://ngrok.com/) for local testing.
8+
9+
### Set Up Your Environment
10+
11+
Install dependencies with `pip` in a virtual environment:
12+
13+
```bash
14+
python3 -m venv venv
15+
. ./venv/bin/activate
16+
17+
# Point to the requirements file in the root of the python-code-snippets repo
18+
pip install -r requirements.txt
19+
```
20+
21+
### Start Your Localhost Tunnel
22+
23+
Start ngrok with `ngrok http 8000`. ngrok will give you a forwarding address you can now use to recieve event webhooks.
24+
25+
### Start the FastAPI Server
626

7-
Install using either `pipenv` or `virtualenv`, and then set up the webhooks.
27+
Run the FastAPI server with
828

9-
#### `pipenv`
10-
1. Run `pipenv install`
11-
1. Copy `.env.dist` to `.env` and fill in your credentials
12-
1. Run `pipenv run flask`
29+
```bash
30+
fastapi dev number-insight/async-callback/main.py
31+
```
1332

14-
#### `virtualenv`
15-
1. Run `virtualenv env`
16-
1. Run `source env/bin/activate`
17-
1. Run `pip install -r requirements.txt`
18-
1. Copy `.env.dist` to `.env` and fill in your credentials
19-
1. Run `flask run`
33+
### Trigger the Lookup
2034

21-
#### Trigger the lookup
22-
1. Start ngrok with `ngrok http 3000`. ngrok will give you a forwarding address you can now use for your delivery receipts.
35+
1. Edit the `ni-advanced-async-trigger.py` script to add the number to return insights for.
36+
1. Add your ngrok URL as the callback to the `number_insight.get_advanced_info_async` method.
2337
1. Run the trigger script with:
2438

2539
python ni-advanced-async-trigger.py
2640

27-
The output of the webhook should appear in the console output of the Flask application
41+
The output of the webhook should appear in the console output of the FastAPI application.

number-insight/async-callback/app.py

-11
This file was deleted.

number-insight/async-callback/main.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from fastapi import FastAPI, Request
2+
3+
app = FastAPI()
4+
5+
6+
@app.post('/')
7+
async def display_advanced_number_insight_info(request: Request):
8+
data = await request.json()
9+
print(data)
+16-24
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1-
21
import os
32
from os.path import join, dirname
43
from pprint import pprint
54
from dotenv import load_dotenv
6-
import vonage
7-
8-
#Load the environment
9-
envpath = join(dirname(__file__), '../.env')
10-
load_dotenv(envpath)
115

12-
#Init the client
13-
client = vonage.Client(
14-
key=os.getenv('VONAGE_API_KEY'),
15-
secret=os.getenv('VONAGE_API_SECRET')
16-
)
6+
dotenv_path = join(dirname(__file__), '../.env')
7+
load_dotenv(dotenv_path)
178

18-
insight_number = os.getenv('INSIGHT_NUMBER')
9+
VONAGE_API_KEY = os.getenv('VONAGE_API_KEY')
10+
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
11+
INSIGHT_NUMBER = os.getenv('INSIGHT_NUMBER')
1912

20-
#Start the trigger
21-
insight_trigger_json = client.number_insight.get_async_advanced_number_insight(
22-
number=insight_number,
23-
callback=os.getenv('INSIGHT_NUMBER_CALLBACK_WEBHOOK')
13+
from vonage import Auth, Vonage
14+
from vonage_number_insight import (
15+
AdvancedAsyncInsightRequest,
16+
AdvancedAsyncInsightResponse,
2417
)
2518

26-
# You can also pass in JSON
27-
'''insight_trigger_json = client.number_insight.get_async_advanced_number_insight({
28-
"number": insight_number,
29-
"callback": os.getenv('INSIGHT_NUMBER_CALLBACK_WEBHOOK')
30-
})
31-
'''
19+
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))
3220

33-
# Get the response from api - the data will be available on callback webhook
34-
pprint(insight_trigger_json)
21+
insight: AdvancedAsyncInsightResponse = client.number_insight.get_advanced_info_async(
22+
AdvancedAsyncInsightRequest(
23+
number=INSIGHT_NUMBER, callback='https://example.com/insight'
24+
)
25+
)
26+
pprint(insight)

number-insight/ni-advanced.py

+7-4
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
1111
INSIGHT_NUMBER = os.getenv('INSIGHT_NUMBER')
1212

13-
import vonage
13+
from vonage import Auth, Vonage
14+
from vonage_number_insight import AdvancedSyncInsightRequest, AdvancedSyncInsightResponse
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-
insight_json = client.number_insight.get_advanced_number_insight(number=INSIGHT_NUMBER)
18-
pprint(insight_json)
18+
insight: AdvancedSyncInsightResponse = client.number_insight.get_advanced_info_sync(
19+
AdvancedSyncInsightRequest(number=INSIGHT_NUMBER)
20+
)
21+
pprint(insight)

number-insight/ni-basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

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

18-
insight: BasicInsightResponse = client.number_insight.basic_number_insight(
18+
insight: BasicInsightResponse = client.number_insight.get_basic_info(
1919
BasicInsightRequest(number=INSIGHT_NUMBER)
2020
)
2121
pprint(insight)

number-insight/ni-standard.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

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

18-
insight: StandardInsightResponse = client.number_insight.standard_number_insight(
18+
insight: StandardInsightResponse = client.number_insight.get_standard_info(
1919
StandardInsightRequest(number=INSIGHT_NUMBER)
2020
)
2121
pprint(insight)

numbers/buy.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
VONAGE_NUMBER = os.getenv("VONAGE_NUMBER")
1111
COUNTRY_CODE = os.getenv("COUNTRY_CODE")
1212

13-
import vonage
13+
from vonage import Auth, Vonage
14+
from vonage_numbers import NumberParams, NumbersStatus
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-
try:
18-
response = client.numbers.buy_number({"country": COUNTRY_CODE, "msisdn": VONAGE_NUMBER})
19-
print("Number purchased")
20-
except Exception as exc:
21-
print("Error purchasing number", exc)
18+
status: NumbersStatus = client.numbers.buy_number(
19+
params=NumberParams(
20+
country=COUNTRY_CODE,
21+
msisdn=VONAGE_NUMBER,
22+
)
23+
)
24+
25+
print(status.model_dump())

numbers/cancel.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
VONAGE_NUMBER = os.getenv("VONAGE_NUMBER")
1111
COUNTRY_CODE = os.getenv("COUNTRY_CODE")
1212

13-
import vonage
13+
from vonage import Auth, Vonage
14+
from vonage_numbers import NumberParams, NumbersStatus
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-
try:
18-
response = client.numbers.cancel_number({"country": COUNTRY_CODE, "msisdn": VONAGE_NUMBER})
19-
print("Number cancelled")
20-
except Exception as exc:
21-
print("Error cancelling number", exc)
18+
status: NumbersStatus = client.numbers.cancel_number(
19+
NumberParams(country=COUNTRY_CODE, msisdn=VONAGE_NUMBER)
20+
)
21+
22+
print(status.model_dump())

numbers/list.py

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from os.path import join, dirname
3+
from pprint import pprint
34
from dotenv import load_dotenv
45

56
dotenv_path = join(dirname(__file__), "../.env")
@@ -10,17 +11,17 @@
1011
NUMBER_SEARCH_CRITERIA = os.getenv("NUMBER_SEARCH_CRITERIA")
1112
NUMBER_SEARCH_PATTERN = os.getenv("NUMBER_SEARCH_PATTERN")
1213

13-
import vonage
14+
from vonage import Auth, Vonage
15+
from vonage_numbers import ListOwnedNumbersFilter
1416

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

17-
responseData = client.numbers.get_account_numbers(
18-
{"pattern": NUMBER_SEARCH_CRITERIA, "search_pattern": NUMBER_SEARCH_PATTERN}
19+
numbers, count, next = client.numbers.list_owned_numbers(
20+
ListOwnedNumbersFilter(
21+
pattern=NUMBER_SEARCH_CRITERIA, search_pattern=NUMBER_SEARCH_PATTERN
22+
)
1923
)
2024

21-
print(
22-
f'Here are {len(responseData["numbers"])} of the {responseData["count"]} matching numbers in your account:'
23-
)
24-
25-
for number in responseData["numbers"]:
26-
print(f'Tel: {number["msisdn"]} Type: {number["type"]}')
25+
pprint(numbers)
26+
print(count)
27+
print(next)

numbers/search.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
from os.path import join, dirname
3+
from pprint import pprint
34
from dotenv import load_dotenv
45

56
dotenv_path = join(dirname(__file__), "../.env")
@@ -8,28 +9,27 @@
89
VONAGE_API_KEY = os.getenv("VONAGE_API_KEY")
910
VONAGE_API_SECRET = os.getenv("VONAGE_API_SECRET")
1011
COUNTRY_CODE = os.getenv("COUNTRY_CODE")
11-
NUMBER_SEARCH_CRITERIA = os.getenv("NUMBER_SEARCH_CRITERIA")
12-
NUMBER_SEARCH_PATTERN = os.getenv("NUMBER_SEARCH_PATTERN")
1312
VONAGE_NUMBER_TYPE = os.getenv("VONAGE_NUMBER_TYPE")
1413
VONAGE_NUMBER_FEATURES = os.getenv("VONAGE_NUMBER_FEATURES")
1514

16-
import vonage
15+
from vonage import Auth, Vonage
16+
from vonage_numbers import SearchAvailableNumbersFilter
1717

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

20-
responseData = client.numbers.get_available_numbers(
21-
COUNTRY_CODE,
22-
{
23-
"pattern": NUMBER_SEARCH_CRITERIA,
24-
"search_pattern": NUMBER_SEARCH_PATTERN,
25-
"type": VONAGE_NUMBER_TYPE,
26-
"features": VONAGE_NUMBER_FEATURES,
27-
},
20+
numbers, count, next = client.numbers.search_available_numbers(
21+
SearchAvailableNumbersFilter(
22+
country=COUNTRY_CODE,
23+
size=3,
24+
pattern='44701',
25+
search_pattern=1,
26+
type=VONAGE_NUMBER_TYPE,
27+
features=VONAGE_NUMBER_FEATURES,
28+
)
2829
)
30+
pprint(numbers)
31+
print(count)
32+
print(next)
2933

30-
print(
31-
f'Here are {len(responseData["numbers"])} of the {responseData["count"]} matching numbers available for purchase:'
32-
)
33-
34-
for number in responseData["numbers"]:
35-
print(f'Tel: {number["msisdn"]} Cost: {number["cost"]}')
34+
for number in numbers:
35+
print(f'Tel: {number.msisdn} Cost: {number.cost}')

numbers/update.py

+16-18
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,27 @@
99
VONAGE_API_SECRET = os.getenv("VONAGE_API_SECRET")
1010
VONAGE_NUMBER = os.getenv("VONAGE_NUMBER")
1111
COUNTRY_CODE = os.getenv("COUNTRY_CODE")
12-
MESSAGES_APPLICATION_ID = os.getenv("MESSAGES_APPLICATION_ID")
1312
VOICE_CALLBACK_TYPE = os.getenv("VOICE_CALLBACK_TYPE")
1413
VOICE_CALLBACK_VALUE = os.getenv("VOICE_CALLBACK_VALUE")
1514
VOICE_STATUS_URL = os.getenv("VOICE_STATUS_URL")
1615
SMS_CALLBACK_URL = os.getenv("SMS_CALLBACK_URL")
1716

18-
import vonage
17+
from vonage import Auth, Vonage
18+
from vonage_numbers import NumbersStatus, UpdateNumberParams
1919

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

22-
try:
23-
response = client.numbers.update_number(
24-
{
25-
"msisdn": VONAGE_NUMBER,
26-
"country": COUNTRY_CODE,
27-
"messagesCallbackType": "app",
28-
"messagesCallbackValue": MESSAGES_APPLICATION_ID,
29-
"voiceCallbackType": VOICE_CALLBACK_TYPE,
30-
"voiceCallbackValue": VOICE_CALLBACK_VALUE,
31-
"voiceStatusCallback": VOICE_STATUS_URL,
32-
"moHttpUrl": SMS_CALLBACK_URL,
33-
}
22+
status: NumbersStatus = client.numbers.update_number(
23+
UpdateNumberParams(
24+
country=COUNTRY_CODE,
25+
msisdn=VONAGE_NUMBER,
26+
app_id='vonage-application-id',
27+
mo_http_url=SMS_CALLBACK_URL,
28+
mo_smpp_sytem_type='inbound',
29+
voice_callback_type=VOICE_CALLBACK_TYPE,
30+
voice_callback_value=VOICE_CALLBACK_VALUE,
31+
voice_status_callback=VOICE_STATUS_URL,
3432
)
35-
print("Number updated")
36-
except Exception as exc:
37-
print("Error updating number", exc)
33+
)
34+
35+
print(status.model_dump())
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)