Created
November 1, 2020 18:14
-
-
Save paulc/d9e64661809dd2ec549f5fc8e08cc0e2 to your computer and use it in GitHub Desktop.
Nexmo CLI
This file contains hidden or 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
#!/usr/bin/env python3 | |
import os | |
import click | |
import nexmo | |
from tabulate import tabulate | |
client = nexmo.Client(os.environ['NEXMO_KEY'], os.environ['NEXMO_SECRET']) | |
@click.group() | |
def cli(): | |
pass | |
@cli.command() | |
@click.option("--from","_from",required=True) | |
@click.option("--to",required=True) | |
@click.option("--text") | |
@click.option("--url") | |
def send_message(_from,to,text,url): | |
opts = {"from":_from,"to":to,"text":text} | |
if url: | |
opts["callback"] = url | |
opts["status-report-req"] = True | |
response = client.send_message(opts) | |
click.echo(response) | |
@cli.command() | |
@click.option("--pattern") | |
@click.option("--search",type=click.Choice(["start","any","end"])) | |
@click.option("--size",type=int) | |
@click.option("--page",type=int) | |
def list_numbers(pattern,search,size,page): | |
opts = {} | |
if pattern: | |
opts["pattern"] = pattern | |
if search: | |
opts["search_pattern"] = ["start","any","end"].index(search) | |
if size: | |
opts["size"] = size | |
if page: | |
opts["index"] = page | |
nums = client.get_account_numbers(**opts) | |
if nums: | |
click.secho("Count: {}".format(nums["count"]),fg="red") | |
click.echo(tabulate(nums["numbers"],headers="keys")) | |
else: | |
click.secho("No numbers available",fg="red") | |
@cli.command() | |
@click.option("--country",default="GB") | |
@click.option("--msisdn",required=True) | |
@click.option("--url",required=True) | |
def update_number(country,msisdn,url): | |
response = client.update_number(country=country,msisdn=msisdn,moHttpUrl=url) | |
if response["error-code"] == "200": | |
click.secho("Success: {}".format(response["error-code-label"]),fg="green") | |
else: | |
click.secho("Error: {}".format(response["error-code-label"]),fg="red",err=True) | |
@cli.command() | |
@click.option("--country",default="GB") | |
@click.option("--type",default="mobile-lvn",type=click.Choice(["landline","landline-toll-free","mobile-lvn"])) | |
@click.option("--features",default="SMS",type=click.Choice(["SMS","VOICE","SMS,VOICE"])) | |
@click.option("--pattern") | |
@click.option("--search",type=click.Choice(["start","any","end"])) | |
@click.option("--size",type=int) | |
@click.option("--page",type=int) | |
def search_numbers(country,type,features,pattern,search,size,page): | |
opts = { "type":type, "features":features } | |
if pattern: | |
opts["pattern"] = pattern | |
if search: | |
opts["search_pattern"] = ["start","any","end"].index(search) | |
if size: | |
opts["size"] = size | |
if page: | |
opts["index"] = page | |
nums = client.get_available_numbers(country,opts) | |
if nums: | |
click.secho("Count: {}".format(nums["count"]),fg="red") | |
click.echo(tabulate(nums["numbers"],headers="keys")) | |
else: | |
click.secho("No numbers available",fg="red") | |
@cli.command() | |
@click.option("--country",default="GB") | |
@click.option("--msisdn",required=True) | |
def buy_number(country,msisdn): | |
nums = client.get_available_numbers(country,dict(pattern=msisdn)) | |
if not nums: | |
click.secho("MSISDN: {} not found".format(msisdn),fg="red",err=True) | |
return | |
if nums["count"] != 1: | |
click.secho("MSISDN: {} found multiple matches".format(msisdn),fg="red",err=True) | |
return | |
click.echo(tabulate(nums["numbers"],headers="keys")) | |
if click.confirm("Buy number?"): | |
response = client.buy_number(dict(country=country,msisdn=msisdn)) | |
if response["error-code"] == "200": | |
click.secho("Success: {}".format(response["error-code-label"]),fg="green") | |
else: | |
click.secho("Error: {}".format(response["error-code-label"]),fg="red",err=True) | |
if __name__ == "__main__": | |
cli() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment