Skip to content

Adding Code Snippets #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 3, 2020
Empty file.
32 changes: 32 additions & 0 deletions number-insight/ni-advanced-async-trigger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import nexmo, os
from os.path import join, dirname
from dotenv import load_dotenv
from pprint import pprint

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

#Init the client
client = nexmo.Client(
key = os.getenv('NEXMO_API_KEY'),
secret = os.getenv('NEXMO_API_SECRET')
)

insight_number = input("Enter the number: ")

#Start the trigger
insight_trigger_json = client.get_advanced_number_insight(
number=insight_number,
callback=os.getenv('INSIGHT_NUMBER_CALLBACK_WEBHOOK')
)

#If you are in love with the json format you can use the variant below
'''insight_trigger_json = client.get_advanced_number_insight({
"number": insight_number,
"callback": os.getenv('INSIGHT_NUMBER_CALLBACK_WEBHOOK')
})
'''

#Get the response from api - the data will be available on callback webhook
pprint(insight_trigger_json)
29 changes: 29 additions & 0 deletions secrets/create-secret.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import nexmo, os, getpass
from os.path import join, dirname
from dotenv import load_dotenv

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

#Init the client
client = nexmo.Client(
key = os.getenv('NEXMO_API_KEY'),
secret = os.getenv('NEXMO_API_SECRET')
)

#Get data from user from keyword
api_key = input("Enter your api_key: ")
#Use getpass instead of input to mask secret
new_api_secret = getpass.getpass("Enter secret: ")

#Create the secret
try:
response = client.create_secret(api_key, new_api_secret)
if "id" in response:
print("Secret was created\nId: {}\nCreated at: {}\nLinks: {}".format(response["id"], response["created_at"], response["_links"]["self"]["href"]))
except:
print(
"Error: Secret does not meet complexity requirements. Please check the link below for more details:\n",
"https://developer.nexmo.com/api-errors/account/secret-management#validation"
)
22 changes: 22 additions & 0 deletions secrets/revoke-secret.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import nexmo, os
from os.path import join, dirname
from dotenv import load_dotenv

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

#Init the client
client = nexmo.Client(
key = os.getenv('NEXMO_API_KEY'),
secret = os.getenv('NEXMO_API_SECRET')
)

try:
#Get the data from standard input
api_key = input("Enter the api key: ")
secret_id = input("Enter the secret id you want to delete: ")
client.delete_secret(api_key, secret_id)
print("Secret removed")
except:
print("Error when try to removing secret")
32 changes: 32 additions & 0 deletions sms/send-signed-sms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#Import dependencies
import nexmo, os
from os.path import join, dirname
from dotenv import load_dotenv

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

#Init the client
client = nexmo.Client(
key = os.getenv('NEXMO_API_KEY'),
signature_secret = os.getenv('NEXMO_SIGNATURE_SECRET'),
signature_method = 'md5'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a difference between md5 hash and HMAC?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, HMAC needs a cryptographic function like md5, sha1 to make the calculations... In other words HMAC depends on a cryptographic functions and other variables like a secret. Is there a specific requirement for either? i just went with md5 as the standard.

)

#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 Nexmo SMS API'

#Sending the sms
response = client.send_message({
"from": from_number,
"to": to_number,
"text": text
})

if response["messages"][0]["status"] == "0":
print("Message sent successfully.")
else:
print(f"Message failed with error: {response['messages'][0]['error-text']}")
31 changes: 31 additions & 0 deletions verify/verify-signed-sms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import nexmo
from flask import Flask, request

app = Flask(__name__)

@app.route('/webhooks/inbound', methods=['GET','POST'])
def inbound():

#Get the params
if request.is_json:
params = request.get_json()
else:
params = request.args or request.form

if "sig" in params:
#Init the client, just when needed
client = nexmo.Client(
key = os.getenv('NEXMO_API_KEY'),
secret = os.getenv('NEXMO_API_SECRET'),
signature_secret = os.getenv('NEXMO_SIGNATURE_SECRET'),
signature_method = 'md5'
)
#Check signature from params
if client.check_signature(params):
print("Valid signature")
else:
print("Invalid signature")
else:
print("Signature not detected in params, Nothing to compare")

return "All OK.", 200
40 changes: 40 additions & 0 deletions voice/transfer-call-inline-ncco.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Import dependencies
import nexmo
import os
from os.path import join, dirname
from dotenv import load_dotenv

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

# Init the client
print(os.getenv("NEXMO_PRIVATE_KEY"))
client = nexmo.Client(
application_id=os.getenv('NEXMO_APPLICATION_ID'),
private_key=os.getenv("NEXMO_PRIVATE_KEY")
)

response = client.create_call({
"to": [{"type": "phone", "number": os.getenv('TO_NUMBER')}],
"from": {"type": "phone", "number": os.getenv('FROM_NUMBER')},
"ncco": [
{
"action": "talk",
"text": "This is just a text whilst you tranfer to another NCCO"
}
]
})

response = client.update_call(
response["uuid"], {
"action": "transfer",
"destination": {
"type": "ncco",
"ncco": [{"action": "talk", "text": "hello world"}]
}
}
)


print(response)