Skip to content

Commit

Permalink
renamed verify and verifyv2, add numbers and number insight snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
maxkahan committed Nov 14, 2024
1 parent 14ff0e6 commit b99c5de
Show file tree
Hide file tree
Showing 32 changed files with 128 additions and 134 deletions.
4 changes: 0 additions & 4 deletions number-insight/async-callback/.env.dist

This file was deleted.

13 changes: 0 additions & 13 deletions number-insight/async-callback/Pipfile

This file was deleted.

46 changes: 30 additions & 16 deletions number-insight/async-callback/README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
This quick demo shows how to accept a Number Insight Async Webhook
# Verifying Signed Webhooks Demo

This quick demo shows how to recieve an incoming Number Insight webhook.

## Usage

### Setup
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
```

### Start Your Localhost Tunnel

Start ngrok with `ngrok http 8000`. ngrok will give you a forwarding address you can now use to recieve event webhooks.

### Start the FastAPI Server

Install using either `pipenv` or `virtualenv`, and then set up the webhooks.
Run the FastAPI server with

#### `pipenv`
1. Run `pipenv install`
1. Copy `.env.dist` to `.env` and fill in your credentials
1. Run `pipenv run flask`
```bash
fastapi dev number-insight/async-callback/main.py
```

#### `virtualenv`
1. Run `virtualenv env`
1. Run `source env/bin/activate`
1. Run `pip install -r requirements.txt`
1. Copy `.env.dist` to `.env` and fill in your credentials
1. Run `flask run`
### Trigger the Lookup

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

python ni-advanced-async-trigger.py

The output of the webhook should appear in the console output of the Flask application
The output of the webhook should appear in the console output of the FastAPI application.
11 changes: 0 additions & 11 deletions number-insight/async-callback/app.py

This file was deleted.

9 changes: 9 additions & 0 deletions number-insight/async-callback/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from fastapi import FastAPI, Request

app = FastAPI()


@app.post('/')
async def display_advanced_number_insight_info(request: Request):
data = await request.json()
print(data)
40 changes: 16 additions & 24 deletions number-insight/ni-advanced-async-trigger.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@

import os
from os.path import join, dirname
from pprint import pprint
from dotenv import load_dotenv
import vonage

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

#Init the client
client = vonage.Client(
key=os.getenv('VONAGE_API_KEY'),
secret=os.getenv('VONAGE_API_SECRET')
)
dotenv_path = join(dirname(__file__), '../.env')
load_dotenv(dotenv_path)

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

#Start the trigger
insight_trigger_json = client.number_insight.get_async_advanced_number_insight(
number=insight_number,
callback=os.getenv('INSIGHT_NUMBER_CALLBACK_WEBHOOK')
from vonage import Auth, Vonage
from vonage_number_insight import (
AdvancedAsyncInsightRequest,
AdvancedAsyncInsightResponse,
)

# You can also pass in JSON
'''insight_trigger_json = client.number_insight.get_async_advanced_number_insight({
"number": insight_number,
"callback": os.getenv('INSIGHT_NUMBER_CALLBACK_WEBHOOK')
})
'''
client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))

# Get the response from api - the data will be available on callback webhook
pprint(insight_trigger_json)
insight: AdvancedAsyncInsightResponse = client.number_insight.get_advanced_info_async(
AdvancedAsyncInsightRequest(
number=INSIGHT_NUMBER, callback='https://example.com/insight'
)
)
pprint(insight)
11 changes: 7 additions & 4 deletions number-insight/ni-advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
VONAGE_API_SECRET = os.getenv('VONAGE_API_SECRET')
INSIGHT_NUMBER = os.getenv('INSIGHT_NUMBER')

import vonage
from vonage import Auth, Vonage
from vonage_number_insight import AdvancedSyncInsightRequest, AdvancedSyncInsightResponse

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

insight_json = client.number_insight.get_advanced_number_insight(number=INSIGHT_NUMBER)
pprint(insight_json)
insight: AdvancedSyncInsightResponse = client.number_insight.get_advanced_info_sync(
AdvancedSyncInsightRequest(number=INSIGHT_NUMBER)
)
pprint(insight)
2 changes: 1 addition & 1 deletion number-insight/ni-basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

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

insight: BasicInsightResponse = client.number_insight.basic_number_insight(
insight: BasicInsightResponse = client.number_insight.get_basic_info(
BasicInsightRequest(number=INSIGHT_NUMBER)
)
pprint(insight)
2 changes: 1 addition & 1 deletion number-insight/ni-standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

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

insight: StandardInsightResponse = client.number_insight.standard_number_insight(
insight: StandardInsightResponse = client.number_insight.get_standard_info(
StandardInsightRequest(number=INSIGHT_NUMBER)
)
pprint(insight)
18 changes: 11 additions & 7 deletions numbers/buy.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
VONAGE_NUMBER = os.getenv("VONAGE_NUMBER")
COUNTRY_CODE = os.getenv("COUNTRY_CODE")

import vonage
from vonage import Auth, Vonage
from vonage_numbers import NumberParams, NumbersStatus

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

try:
response = client.numbers.buy_number({"country": COUNTRY_CODE, "msisdn": VONAGE_NUMBER})
print("Number purchased")
except Exception as exc:
print("Error purchasing number", exc)
status: NumbersStatus = client.numbers.buy_number(
params=NumberParams(
country=COUNTRY_CODE,
msisdn=VONAGE_NUMBER,
)
)

print(status.model_dump())
15 changes: 8 additions & 7 deletions numbers/cancel.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
VONAGE_NUMBER = os.getenv("VONAGE_NUMBER")
COUNTRY_CODE = os.getenv("COUNTRY_CODE")

import vonage
from vonage import Auth, Vonage
from vonage_numbers import NumberParams, NumbersStatus

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

try:
response = client.numbers.cancel_number({"country": COUNTRY_CODE, "msisdn": VONAGE_NUMBER})
print("Number cancelled")
except Exception as exc:
print("Error cancelling number", exc)
status: NumbersStatus = client.numbers.cancel_number(
NumberParams(country=COUNTRY_CODE, msisdn=VONAGE_NUMBER)
)

print(status.model_dump())
21 changes: 11 additions & 10 deletions numbers/list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from os.path import join, dirname
from pprint import pprint
from dotenv import load_dotenv

dotenv_path = join(dirname(__file__), "../.env")
Expand All @@ -10,17 +11,17 @@
NUMBER_SEARCH_CRITERIA = os.getenv("NUMBER_SEARCH_CRITERIA")
NUMBER_SEARCH_PATTERN = os.getenv("NUMBER_SEARCH_PATTERN")

import vonage
from vonage import Auth, Vonage
from vonage_numbers import ListOwnedNumbersFilter

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.numbers.get_account_numbers(
{"pattern": NUMBER_SEARCH_CRITERIA, "search_pattern": NUMBER_SEARCH_PATTERN}
numbers, count, next = client.numbers.list_owned_numbers(
ListOwnedNumbersFilter(
pattern=NUMBER_SEARCH_CRITERIA, search_pattern=NUMBER_SEARCH_PATTERN
)
)

print(
f'Here are {len(responseData["numbers"])} of the {responseData["count"]} matching numbers in your account:'
)

for number in responseData["numbers"]:
print(f'Tel: {number["msisdn"]} Type: {number["type"]}')
pprint(numbers)
print(count)
print(next)
36 changes: 18 additions & 18 deletions numbers/search.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
from os.path import join, dirname
from pprint import pprint
from dotenv import load_dotenv

dotenv_path = join(dirname(__file__), "../.env")
Expand All @@ -8,28 +9,27 @@
VONAGE_API_KEY = os.getenv("VONAGE_API_KEY")
VONAGE_API_SECRET = os.getenv("VONAGE_API_SECRET")
COUNTRY_CODE = os.getenv("COUNTRY_CODE")
NUMBER_SEARCH_CRITERIA = os.getenv("NUMBER_SEARCH_CRITERIA")
NUMBER_SEARCH_PATTERN = os.getenv("NUMBER_SEARCH_PATTERN")
VONAGE_NUMBER_TYPE = os.getenv("VONAGE_NUMBER_TYPE")
VONAGE_NUMBER_FEATURES = os.getenv("VONAGE_NUMBER_FEATURES")

import vonage
from vonage import Auth, Vonage
from vonage_numbers import SearchAvailableNumbersFilter

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.numbers.get_available_numbers(
COUNTRY_CODE,
{
"pattern": NUMBER_SEARCH_CRITERIA,
"search_pattern": NUMBER_SEARCH_PATTERN,
"type": VONAGE_NUMBER_TYPE,
"features": VONAGE_NUMBER_FEATURES,
},
numbers, count, next = client.numbers.search_available_numbers(
SearchAvailableNumbersFilter(
country=COUNTRY_CODE,
size=3,
pattern='44701',
search_pattern=1,
type=VONAGE_NUMBER_TYPE,
features=VONAGE_NUMBER_FEATURES,
)
)
pprint(numbers)
print(count)
print(next)

print(
f'Here are {len(responseData["numbers"])} of the {responseData["count"]} matching numbers available for purchase:'
)

for number in responseData["numbers"]:
print(f'Tel: {number["msisdn"]} Cost: {number["cost"]}')
for number in numbers:
print(f'Tel: {number.msisdn} Cost: {number.cost}')
34 changes: 16 additions & 18 deletions numbers/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,27 @@
VONAGE_API_SECRET = os.getenv("VONAGE_API_SECRET")
VONAGE_NUMBER = os.getenv("VONAGE_NUMBER")
COUNTRY_CODE = os.getenv("COUNTRY_CODE")
MESSAGES_APPLICATION_ID = os.getenv("MESSAGES_APPLICATION_ID")
VOICE_CALLBACK_TYPE = os.getenv("VOICE_CALLBACK_TYPE")
VOICE_CALLBACK_VALUE = os.getenv("VOICE_CALLBACK_VALUE")
VOICE_STATUS_URL = os.getenv("VOICE_STATUS_URL")
SMS_CALLBACK_URL = os.getenv("SMS_CALLBACK_URL")

import vonage
from vonage import Auth, Vonage
from vonage_numbers import NumbersStatus, UpdateNumberParams

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

try:
response = client.numbers.update_number(
{
"msisdn": VONAGE_NUMBER,
"country": COUNTRY_CODE,
"messagesCallbackType": "app",
"messagesCallbackValue": MESSAGES_APPLICATION_ID,
"voiceCallbackType": VOICE_CALLBACK_TYPE,
"voiceCallbackValue": VOICE_CALLBACK_VALUE,
"voiceStatusCallback": VOICE_STATUS_URL,
"moHttpUrl": SMS_CALLBACK_URL,
}
status: NumbersStatus = client.numbers.update_number(
UpdateNumberParams(
country=COUNTRY_CODE,
msisdn=VONAGE_NUMBER,
app_id='vonage-application-id',
mo_http_url=SMS_CALLBACK_URL,
mo_smpp_sytem_type='inbound',
voice_callback_type=VOICE_CALLBACK_TYPE,
voice_callback_value=VOICE_CALLBACK_VALUE,
voice_status_callback=VOICE_STATUS_URL,
)
print("Number updated")
except Exception as exc:
print("Error updating number", exc)
)

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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit b99c5de

Please sign in to comment.