Skip to content

Commit 1782871

Browse files
authored
Merge pull request #63 from superdiana/master
Adding Code Snippets
2 parents f19c993 + f51e837 commit 1782871

File tree

7 files changed

+186
-0
lines changed

7 files changed

+186
-0
lines changed

number-insight/ni-advanced-async-callback.py

Whitespace-only changes.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import nexmo, os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
from pprint import pprint
5+
6+
#Load the environment
7+
envpath = join(dirname(__file__),'./.env')
8+
load_dotenv(envpath)
9+
10+
#Init the client
11+
client = nexmo.Client(
12+
key = os.getenv('NEXMO_API_KEY'),
13+
secret = os.getenv('NEXMO_API_SECRET')
14+
)
15+
16+
insight_number = input("Enter the number: ")
17+
18+
#Start the trigger
19+
insight_trigger_json = client.get_advanced_number_insight(
20+
number=insight_number,
21+
callback=os.getenv('INSIGHT_NUMBER_CALLBACK_WEBHOOK')
22+
)
23+
24+
#If you are in love with the json format you can use the variant below
25+
'''insight_trigger_json = client.get_advanced_number_insight({
26+
"number": insight_number,
27+
"callback": os.getenv('INSIGHT_NUMBER_CALLBACK_WEBHOOK')
28+
})
29+
'''
30+
31+
#Get the response from api - the data will be available on callback webhook
32+
pprint(insight_trigger_json)

secrets/create-secret.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import nexmo, os, getpass
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
5+
#Load the environment
6+
envpath = join(dirname(__file__),'./.env')
7+
load_dotenv(envpath)
8+
9+
#Init the client
10+
client = nexmo.Client(
11+
key = os.getenv('NEXMO_API_KEY'),
12+
secret = os.getenv('NEXMO_API_SECRET')
13+
)
14+
15+
#Get data from user from keyword
16+
api_key = input("Enter your api_key: ")
17+
#Use getpass instead of input to mask secret
18+
new_api_secret = getpass.getpass("Enter secret: ")
19+
20+
#Create the secret
21+
try:
22+
response = client.create_secret(api_key, new_api_secret)
23+
if "id" in response:
24+
print("Secret was created\nId: {}\nCreated at: {}\nLinks: {}".format(response["id"], response["created_at"], response["_links"]["self"]["href"]))
25+
except:
26+
print(
27+
"Error: Secret does not meet complexity requirements. Please check the link below for more details:\n",
28+
"https://developer.nexmo.com/api-errors/account/secret-management#validation"
29+
)

secrets/revoke-secret.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import nexmo, os
2+
from os.path import join, dirname
3+
from dotenv import load_dotenv
4+
5+
#Load the environment
6+
envpath = join(dirname(__file__),'./.env')
7+
load_dotenv(envpath)
8+
9+
#Init the client
10+
client = nexmo.Client(
11+
key = os.getenv('NEXMO_API_KEY'),
12+
secret = os.getenv('NEXMO_API_SECRET')
13+
)
14+
15+
try:
16+
#Get the data from standard input
17+
api_key = input("Enter the api key: ")
18+
secret_id = input("Enter the secret id you want to delete: ")
19+
client.delete_secret(api_key, secret_id)
20+
print("Secret removed")
21+
except:
22+
print("Error when try to removing secret")

sms/send-signed-sms.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#Import dependencies
2+
import nexmo, os
3+
from os.path import join, dirname
4+
from dotenv import load_dotenv
5+
6+
#Load the environment
7+
envpath = join(dirname(__file__),'./.env')
8+
load_dotenv(envpath)
9+
10+
#Init the client
11+
client = nexmo.Client(
12+
key = os.getenv('NEXMO_API_KEY'),
13+
signature_secret = os.getenv('NEXMO_SIGNATURE_SECRET'),
14+
signature_method = 'md5'
15+
)
16+
17+
#Define variables - replace FROM_NUMBER and TO_NUMBER with actual numbers
18+
from_number = os.getenv('FROM_NUMBER')
19+
to_number = os.getenv('TO_NUMBER')
20+
text = 'A text message sent using the Nexmo SMS API'
21+
22+
#Sending the sms
23+
response = client.send_message({
24+
"from": from_number,
25+
"to": to_number,
26+
"text": text
27+
})
28+
29+
if response["messages"][0]["status"] == "0":
30+
print("Message sent successfully.")
31+
else:
32+
print(f"Message failed with error: {response['messages'][0]['error-text']}")

verify/verify-signed-sms.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import nexmo
2+
from flask import Flask, request
3+
4+
app = Flask(__name__)
5+
6+
@app.route('/webhooks/inbound', methods=['GET','POST'])
7+
def inbound():
8+
9+
#Get the params
10+
if request.is_json:
11+
params = request.get_json()
12+
else:
13+
params = request.args or request.form
14+
15+
if "sig" in params:
16+
#Init the client, just when needed
17+
client = nexmo.Client(
18+
key = os.getenv('NEXMO_API_KEY'),
19+
secret = os.getenv('NEXMO_API_SECRET'),
20+
signature_secret = os.getenv('NEXMO_SIGNATURE_SECRET'),
21+
signature_method = 'md5'
22+
)
23+
#Check signature from params
24+
if client.check_signature(params):
25+
print("Valid signature")
26+
else:
27+
print("Invalid signature")
28+
else:
29+
print("Signature not detected in params, Nothing to compare")
30+
31+
return "All OK.", 200

voice/transfer-call-inline-ncco.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Import dependencies
2+
import nexmo
3+
import os
4+
from os.path import join, dirname
5+
from dotenv import load_dotenv
6+
7+
# Load the environment
8+
envpath = join(dirname(__file__), './.env')
9+
load_dotenv(envpath)
10+
11+
# Init the client
12+
print(os.getenv("NEXMO_PRIVATE_KEY"))
13+
client = nexmo.Client(
14+
application_id=os.getenv('NEXMO_APPLICATION_ID'),
15+
private_key=os.getenv("NEXMO_PRIVATE_KEY")
16+
)
17+
18+
response = client.create_call({
19+
"to": [{"type": "phone", "number": os.getenv('TO_NUMBER')}],
20+
"from": {"type": "phone", "number": os.getenv('FROM_NUMBER')},
21+
"ncco": [
22+
{
23+
"action": "talk",
24+
"text": "This is just a text whilst you tranfer to another NCCO"
25+
}
26+
]
27+
})
28+
29+
response = client.update_call(
30+
response["uuid"], {
31+
"action": "transfer",
32+
"destination": {
33+
"type": "ncco",
34+
"ncco": [{"action": "talk", "text": "hello world"}]
35+
}
36+
}
37+
)
38+
39+
40+
print(response)

0 commit comments

Comments
 (0)