← back to Butlr

scripts/dial-sequence.sh

40 lines

#!/usr/bin/env bash
# Dial a list of (name, phone, address) tuples one at a time.
# Waits for each call to terminate before starting the next.
# Usage: dial-sequence.sh <list.tsv>
#   list.tsv format: name<TAB>phone<TAB>address  (one per line, # for comments)
set -e
TOKEN=${BUTLR_OWNER_TOKEN:-$(grep '^BUTLR_OWNER_TOKEN=' /root/public-projects/butlr/.env | cut -d= -f2-)}
GOAL=${DIAL_GOAL:-'Confirm hours today and whether weekend hours differ. Apologize sincerely that this is an AI calling on Steve behalf. Keep under 60 seconds.'}
ENDPOINT=${BUTLR_URL:-https://butlr.agentabrams.com}/api/quick-dial
LIST="${1:?usage: dial-sequence.sh <list.tsv>}"
TERMINAL_RE='^(done|completed|failed|cancelled|canceled|error|no_answer|busy|voicemail)$'

while IFS=$'\t' read -r name phone addr; do
  [[ -z "$name" || "$name" == \#* ]] && continue
  printf '\n→ DIALING %s (%s) ...\n' "$name" "$phone"
  resp=$(curl -sS -m 20 -X POST "$ENDPOINT" \
    -H "X-Butlr-Owner-Token: $TOKEN" \
    --data-urlencode "business_name=$name — $addr" \
    --data-urlencode "business_phone=$phone" \
    --data-urlencode "goal=$GOAL" \
    --data-urlencode "category=other")
  id=$(echo "$resp" | /usr/bin/python3 -c 'import sys,json; print(json.load(sys.stdin).get("id",""))' 2>/dev/null || echo '')
  if [[ -z "$id" ]]; then
    printf '  ✗ dial failed: %s\n' "$resp"
    continue
  fi
  printf '  id=%s — polling until terminal...\n' "$id"
  while true; do
    st=$(curl -sS -m 8 "$ENDPOINT"."/../calls/$id/status" -H "X-Butlr-Owner-Token: $TOKEN" 2>/dev/null \
      | /usr/bin/python3 -c 'import sys,json
try: print(json.load(sys.stdin).get("status","?"))
except: print("ERR")' 2>/dev/null || echo 'ERR')
    if [[ "$st" =~ $TERMINAL_RE ]]; then
      printf '  ✓ %s (%s) → %s\n' "$name" "$id" "$st"
      break
    fi
    sleep 15
  done
done < "$LIST"