#!/usr/bin/env bash
# Is this number on the do-not-call list? Exits 0 either way and prints the answer. Needs: curl, jq.
set -euo pipefail
BASE="${RELATIONSAI_API_BASE:-https://voice-api.relationsai.com/v1}"
: "${PHONE:?Set PHONE to the number in E.164 format}"

entries=$(curl -sS --fail-with-body -G "$BASE/dnc-entries" \
  --data-urlencode "phone_number=$PHONE" \
  -H "Authorization: Bearer $RELATIONSAI_API_KEY") || { echo "$entries" >&2; exit 1; }

if [ "$(jq '.data | length' <<<"$entries")" -gt 0 ]; then
  jq -r '.data[] | "\(.phone_number) is on the do-not-call list (\(.id))"' <<<"$entries"
else
  echo "$PHONE is not on the do-not-call list"
fi
