# Add a number to the do-not-call list. Safe to repeat. Python 3.9+ with requests.
import os
import sys

import requests

base = os.environ.get("RELATIONSAI_API_BASE", "https://voice-api.relationsai.com/v1")
phone = os.environ.get("PHONE") or sys.exit("Set PHONE to the number in E.164 format, for example +12025550107")

r = requests.post(
    f"{base}/dnc-entries",
    headers={
        "Authorization": f"Bearer {os.environ['RELATIONSAI_API_KEY']}",
    },
    json={"phone_number": phone},
    timeout=30,
)
entry = r.json()
if not r.ok:
    err = entry["error"]
    sys.exit(f"{err['code']}: {err['message']} (request {err['request_id']})")
print(f"Added {entry['phone_number']} as {entry['id']}" if r.status_code == 201 else f"Already listed as {entry['id']}")
