Last active
February 16, 2020 20:30
-
-
Save davidkovsky/6936af845a6c42f095f1c5d84079851e to your computer and use it in GitHub Desktop.
Script from ClueCon Weekly Telnyx API presentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# NOTES: | |
# This is a simple sample script to search for, purchase, and configure a number via the Telnyx API. | |
# This was used on ClueCon Weekly as a very simple demo of how you can interact with the Telnyx API. | |
# Please scroll all the way to the bottom to see the list of method calls and, if desired, uncomment the | |
# method calls that will result in an actual number purchase. | |
# Requires ruby >= 2.3!!! | |
require 'uri' | |
require 'net/http' | |
require 'openssl' | |
require 'json' | |
# Fill in the email associated with your Telnyx account: | |
API_USER = "" | |
# Fill in your API Token from portal.telnyx.com/#/app/api-tokens: | |
API_TOKEN = "" | |
############################# | |
# Number Search: | |
# Searches for a number with area code of 301 | |
############################# | |
# curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" --header "x-api-user: email" --header "x-api-token: token" -d "{ | |
# \"search_type\": 1, | |
# \"search_descriptor\": { | |
# \"npa\": \"301\" | |
# }, | |
# \"with_result\": true | |
# }" "https://api.telnyx.com/origination/number_searches" | |
def search(area_code) | |
url = URI("https://api.telnyx.com/origination/number_searches") | |
http = Net::HTTP.new(url.host, url.port) | |
http.use_ssl = true | |
request = Net::HTTP::Post.new(url) | |
request["accept"] = 'application/json' | |
request["x-api-user"] = API_USER | |
request["x-api-token"] = API_TOKEN | |
request["content-type"] = 'application/json' | |
request.body = <<~EOS | |
{ | |
"search_type": 1, | |
"limit": 3, | |
"search_descriptor": | |
{ | |
"npa": "#{area_code}" | |
}, | |
"with_result": true | |
} | |
EOS | |
response = http.request(request) | |
puts response.code | |
puts response.read_body | |
number = JSON.parse(response.read_body)["result"].first["number"] | |
puts "first number from search results: #{number}" | |
return number | |
end | |
############################# | |
# Number Order: | |
# NOTE: THIS WILL ACTUALLY PURCHASE THE NUMBER FOUND ABOVE AND ATTACH IT TO YOUR ACCOUNT!! | |
############################# | |
# curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" --header "x-api-user: email" --header "x-api-token: API-token" -d "{ | |
# \"requested_numbers\": [ | |
# \"3036475090\" | |
# ] | |
# }" "https://api.telnyx.com/origination/number_orders" | |
def purchase(number) | |
puts "******************** Number Order:" | |
url = URI("https://api.telnyx.com/origination/number_orders") | |
http = Net::HTTP.new(url.host, url.port) | |
http.use_ssl = true | |
request = Net::HTTP::Post.new(url) | |
request["accept"] = 'application/json' | |
request["x-api-user"] = API_USER | |
request["x-api-token"] = API_TOKEN | |
request["content-type"] = 'application/json' | |
request.body = <<~EOS | |
{ | |
"requested_numbers": [ | |
"#{number}" | |
] | |
} | |
EOS | |
response = http.request(request) | |
puts "purchase response:" | |
puts response.code | |
puts response.read_body | |
return response.code == "200" | |
end | |
############################# | |
# Number Status Confirmation: | |
############################# | |
# curl -X GET --header "Accept: application/json" --header "x-api-user: email" --header "x-api-token: API-token" "https://api.telnyx.com/origination/numbers/3035555555" | |
def confirm_active(number) | |
puts "******************** Number Status Confirmation:" | |
url = URI("https://api.telnyx.com/origination/numbers/#{number}") | |
http = Net::HTTP.new(url.host, url.port) | |
http.use_ssl = true | |
request = Net::HTTP::Get.new(url) | |
request["accept"] = 'application/json' | |
request["x-api-user"] = API_USER | |
request["x-api-token"] = API_TOKEN | |
request["content-type"] = 'application/json' | |
response = http.request(request) | |
puts "GET purchased number response:" | |
puts response.code | |
puts response.read_body | |
# Purchase Pending = 1, Purchase Failed = 2, Port Pending = 3, Active = 4, Deleted = 5, Port Failed = 6 | |
pending = 1 | |
success = 4 | |
status = JSON.parse(response.read_body)["status"] | |
while status == pending do | |
puts "current number status is: #{status}. Trying again in one second." | |
sleep 1 | |
response = http.request(request) | |
puts "GET purchased number response:" | |
puts response.code | |
puts response.read_body | |
status = JSON.parse(response.read_body)["status"] | |
end | |
if status == success | |
puts "Success!" | |
return true | |
end | |
puts "There was a problem: status is #{status}" | |
return false | |
end | |
# ############################# | |
# Get Connection ID: | |
# ############################# | |
# | |
# curl -X GET --header "Accept: application/json" --header "x-api-user: email" --header "x-api-token: API-Token" "https://api.telnyx.com/security/connections" | |
# | |
# puts "******************** Get Connection ID:" | |
def get_connection_id(connection_name) | |
url = URI("https://api.telnyx.com/security/connections") | |
http = Net::HTTP.new(url.host, url.port) | |
http.use_ssl = true | |
request = Net::HTTP::Get.new(url) | |
request["accept"] = 'application/json' | |
request["x-api-user"] = API_USER | |
request["x-api-token"] = API_TOKEN | |
request["content-type"] = 'application/json' | |
response = http.request(request) | |
puts "GET connections response:" | |
puts response.code | |
puts response.read_body | |
connections = JSON.parse(response.read_body) | |
connection = connections.select{ |conn| conn["connection_name"] == connection_name }.first | |
connection_id = connection["id"] | |
puts "connection_id is: #{connection_id}" | |
if response.code == "200" | |
return connection_id | |
end | |
puts "Failed to find a connection on your Telnyx account named #{connection_name}." | |
return false | |
end | |
############################# | |
# Configure Number: | |
############################# | |
# curl -X PUT --header "Content-Type: application/json" --header "Accept: application/json" --header "x-api-user: email" --header "x-api-token: API-token" -d "{ | |
# \"connection_id\": \"connection_id\" | |
# }" "https://api.telnyx.com/origination/numbers/5555555555" | |
def attach_number_to_connection(number, connection_name) | |
connection_id = get_connection_id(connection_name) | |
puts "******************** Attach Connection to Number:" | |
url = URI("https://api.telnyx.com/origination/numbers/#{number}") | |
http = Net::HTTP.new(url.host, url.port) | |
http.use_ssl = true | |
request = Net::HTTP::Put.new(url) | |
request["accept"] = 'application/json' | |
request["x-api-user"] = API_USER | |
request["x-api-token"] = API_TOKEN | |
request["content-type"] = 'application/json' | |
request.body = <<~EOS | |
{ | |
"connection_id": #{connection_id} | |
} | |
EOS | |
response = http.request(request) | |
puts "PUT numbers response:" | |
puts response.code | |
puts response.read_body | |
end | |
# Fill in as desired: | |
area_code = "303" | |
connection_name = "name-of-your-connection" | |
# And now we can search for a number in the area code specified above: | |
number = search(area_code) | |
# NOTE: Uncomment the lines below ONLY if you want to purchase a number on your Telnyx account: | |
# attach_number_to_connection(number, connection_name) if purchase(number) && confirm_active(number) | |
# puts "And now we should be ready to receive a call on #{number.slice(0, 3)}-#{number.slice(3, 3)}-#{number.slice(6, 4)}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment