Beispiel #1
0
def rent_number(
    sms_callback_url: str, client: nexmo.Client, country_code: str = "US"
) -> dict:
    """Rents a number for the given country.

    NOTE: This immediately charges us for the number (for at least a month).
    """
    numbers = client.get_available_numbers(
        country_code, {"features": "SMS,VOICE", "type": "mobile-lvn"}
    )

    error = RuntimeError("No numbers available.")

    for number in numbers["numbers"]:
        try:
            client.buy_number(
                {"country": number["country"], "msisdn": number["msisdn"]}
            )

            setup_number(
                number=number["msisdn"],
                country=number["country"],
                sms_callback_url=sms_callback_url,
                client=client,
            )

            # Normalize the number.
            number["msisdn"] = normalize_number(number["msisdn"])

            return number

        except nexmo.Error as error:
            continue

    raise error
Beispiel #2
0
def rent_number(sms_callback_url: str,
                client: nexmo.Client,
                country_code: str = "US") -> dict:
    """Rents a number for the given country.

    NOTE: This immediately charges us for the number (for at least a month).
    """

    # Try to get SMS and VOICE numbers first.
    numbers = client.get_available_numbers(country_code, {
        "features": "SMS,VOICE",
        "type": "mobile-lvn"
    })

    # If that fails, get a VOICE-only number.
    if not numbers.get("numbers", []):
        numbers = client.get_available_numbers(country_code, {
            "features": "VOICE",
            "type": "mobile-lvn"
        })

    error = RuntimeError("No numbers available.")

    for number in numbers.get("numbers", []):
        try:
            client.buy_number({
                "country": number["country"],
                "msisdn": number["msisdn"]
            })

            setup_number(
                number=number["msisdn"],
                country=number["country"],
                sms_callback_url=sms_callback_url,
                client=client,
            )

            # normalize the number. Nexmo sends it back in E164 format *without* the leading +
            number["msisdn"] = normalize_e164_number(number["msisdn"])

            return number

        except nexmo.Error as nexmo_error:
            error = nexmo_error
            continue

    raise error