Skip to content

team-telnyx/ai / providers/claude/plugins/telnyx-platform/skills/telnyx-twilio-migration

telnyx-twilio-migration

Unverified

Migrate from Twilio to Telnyx. Orchestrates a complete 6-phase migration: discovery, planning, setup, code migration, validation, and cleanup. Covers voice (TwiML to TeXML, Call Control API), messaging, WebRTC, SIP trunking, verify, fax, video, IoT, number lookup, and porting. Includes automated scanners, validation scripts, and integration tests.

FileKindSize
SKILL.mdskill md43.3 KB
references/account-setup-guide.mdmarkdown17.8 KB
references/error-code-mapping.mdmarkdown11.0 KB
references/fax-migration.mdmarkdown14.7 KB
references/iot-migration.mdmarkdown17.9 KB
references/lookup-migration.mdmarkdown14.3 KB
references/messaging-migration.mdmarkdown36.5 KB
references/mobile-sdk-migration.mdmarkdown38.6 KB
references/number-porting.mdmarkdown17.0 KB
references/numbers-migration.mdmarkdown21.1 KB
references/product-mapping.mdmarkdown6.7 KB
references/sdk-api-details/10dlc.mdmarkdown9.8 KB
references/sdk-api-details/ai-assistants.mdmarkdown5.8 KB
references/sdk-api-details/messaging.mdmarkdown16.1 KB
references/sdk-api-details/numbers.mdmarkdown10.1 KB
references/sdk-api-details/voice.mdmarkdown9.6 KB
references/sip-trunking-migration.mdmarkdown18.1 KB
references/texml-verbs.mdmarkdown43.7 KB
references/unsupported-products.mdmarkdown3.5 KB
references/verify-migration.mdmarkdown18.4 KB
references/video-migration.mdmarkdown20.6 KB
references/voice-migration.mdmarkdown37.2 KB
references/webhook-migration.mdmarkdown20.3 KB
references/webrtc-migration.mdmarkdown50.0 KB
scripts/lint-telnyx-correctness.shexecutablescript31.4 KB
scripts/migration-state.shexecutablescript7.3 KB
scripts/post-test-diagnostic.shexecutablescript18.9 KB
scripts/preflight-check.shexecutablescript13.4 KB
scripts/run-discovery.shexecutablescript7.7 KB
scripts/run-validation.shexecutablescript13.1 KB
scripts/scan-twilio-deep.pyexecutablescript64.8 KB
scripts/scan-twilio-usage.shexecutablescript29.1 KB
scripts/test-migration/smoke-test.shexecutablescript10.0 KB
scripts/test-migration/test-fax.shexecutablescript8.7 KB
scripts/test-migration/test-lookup.shexecutablescript5.5 KB
scripts/test-migration/test-messaging.shexecutablescript18.8 KB
scripts/test-migration/test-sip.shexecutablescript29.6 KB
scripts/test-migration/test-verify.shexecutablescript13.6 KB
scripts/test-migration/test-voice.shexecutablescript22.4 KB
scripts/test-migration/test-webhooks-local.pyexecutablescript9.7 KB
scripts/test-migration/test-webrtc.shexecutablescript24.0 KB
scripts/test-migration/webhook-receiver.pyexecutablescript11.8 KB
scripts/validate-migration.shexecutablescript31.1 KB
scripts/validate-texml.shexecutablescript61.4 KB
sdk-reference/curl/10dlc.mdmarkdown25.8 KB
sdk-reference/curl/account-access.mdmarkdown15.6 KB
sdk-reference/curl/account-management.mdmarkdown9.5 KB
sdk-reference/curl/account-notifications.mdmarkdown11.0 KB
sdk-reference/curl/account-reports.mdmarkdown45.5 KB
sdk-reference/curl/account.mdmarkdown8.1 KB
sdk-reference/curl/ai-assistants.mdmarkdown34.3 KB
sdk-reference/curl/ai-inference.mdmarkdown34.5 KB
sdk-reference/curl/fax.mdmarkdown19.8 KB
sdk-reference/curl/iot.mdmarkdown49.4 KB
sdk-reference/curl/messaging-hosted.mdmarkdown23.3 KB
sdk-reference/curl/messaging-profiles.mdmarkdown14.2 KB
sdk-reference/curl/messaging.mdmarkdown29.1 KB
sdk-reference/curl/missions.mdmarkdown23.5 KB
sdk-reference/curl/networking.mdmarkdown39.8 KB
sdk-reference/curl/numbers-compliance.mdmarkdown30.2 KB
sdk-reference/curl/numbers-config.mdmarkdown29.7 KB
sdk-reference/curl/numbers-services.mdmarkdown12.6 KB
sdk-reference/curl/numbers.mdmarkdown24.0 KB
sdk-reference/curl/oauth.mdmarkdown15.6 KB
sdk-reference/curl/porting-in.mdmarkdown35.1 KB
sdk-reference/curl/porting-out.mdmarkdown9.8 KB
sdk-reference/curl/sip-integrations.mdmarkdown27.5 KB
sdk-reference/curl/sip.mdmarkdown47.6 KB
sdk-reference/curl/storage.mdmarkdown4.2 KB
sdk-reference/curl/texml.mdmarkdown31.4 KB
sdk-reference/curl/verify.mdmarkdown13.2 KB
sdk-reference/curl/video.mdmarkdown17.2 KB
sdk-reference/curl/voice-advanced.mdmarkdown32.8 KB
sdk-reference/curl/voice-conferencing.mdmarkdown41.6 KB
sdk-reference/curl/voice-gather.mdmarkdown14.7 KB
sdk-reference/curl/voice-media.mdmarkdown17.8 KB
sdk-reference/curl/voice-streaming.mdmarkdown13.3 KB
sdk-reference/curl/voice.mdmarkdown37.6 KB
sdk-reference/curl/webrtc.mdmarkdown6.5 KB
sdk-reference/go/10dlc.mdmarkdown29.0 KB
sdk-reference/go/account-access.mdmarkdown17.0 KB
sdk-reference/go/account-management.mdmarkdown9.9 KB
sdk-reference/go/account-notifications.mdmarkdown12.4 KB
sdk-reference/go/account-reports.mdmarkdown31.5 KB
sdk-reference/go/account.mdmarkdown9.1 KB
sdk-reference/go/ai-assistants.mdmarkdown37.7 KB
sdk-reference/go/ai-inference.mdmarkdown27.1 KB
sdk-reference/go/fax.mdmarkdown20.2 KB
sdk-reference/go/iot.mdmarkdown51.3 KB
sdk-reference/go/messaging-hosted.mdmarkdown24.5 KB
sdk-reference/go/messaging-profiles.mdmarkdown15.2 KB
sdk-reference/go/messaging.mdmarkdown30.1 KB
sdk-reference/go/missions.mdmarkdown26.9 KB
sdk-reference/go/networking.mdmarkdown42.5 KB
sdk-reference/go/numbers-compliance.mdmarkdown28.2 KB
sdk-reference/go/numbers-config.mdmarkdown29.5 KB
sdk-reference/go/numbers-services.mdmarkdown14.1 KB
sdk-reference/go/numbers.mdmarkdown26.7 KB
sdk-reference/go/oauth.mdmarkdown11.7 KB
sdk-reference/go/porting-in.mdmarkdown41.1 KB
sdk-reference/go/porting-out.mdmarkdown10.9 KB
sdk-reference/go/sip-integrations.mdmarkdown32.3 KB
sdk-reference/go/sip.mdmarkdown49.1 KB
sdk-reference/go/storage.mdmarkdown4.8 KB
sdk-reference/go/texml.mdmarkdown35.7 KB
sdk-reference/go/verify.mdmarkdown14.4 KB
sdk-reference/go/video.mdmarkdown18.6 KB
sdk-reference/go/voice-advanced.mdmarkdown33.2 KB
sdk-reference/go/voice-conferencing.mdmarkdown42.3 KB
sdk-reference/go/voice-gather.mdmarkdown14.5 KB
sdk-reference/go/voice-media.mdmarkdown18.1 KB
sdk-reference/go/voice-streaming.mdmarkdown13.5 KB
sdk-reference/go/voice.mdmarkdown39.2 KB
sdk-reference/go/webrtc.mdmarkdown7.5 KB
sdk-reference/java/10dlc.mdmarkdown31.3 KB
sdk-reference/java/account-access.mdmarkdown18.5 KB
sdk-reference/java/account-management.mdmarkdown11.1 KB
sdk-reference/java/account-notifications.mdmarkdown14.3 KB
sdk-reference/java/account-reports.mdmarkdown36.5 KB
sdk-reference/java/account.mdmarkdown10.3 KB
sdk-reference/java/ai-assistants.mdmarkdown39.2 KB
sdk-reference/java/ai-inference.mdmarkdown30.7 KB
sdk-reference/java/fax.mdmarkdown21.5 KB
sdk-reference/java/iot.mdmarkdown57.5 KB
sdk-reference/java/messaging-hosted.mdmarkdown28.4 KB
sdk-reference/java/messaging-profiles.mdmarkdown15.7 KB
sdk-reference/java/messaging.mdmarkdown32.2 KB
sdk-reference/java/missions.mdmarkdown31.6 KB
sdk-reference/java/networking.mdmarkdown47.1 KB
sdk-reference/java/numbers-compliance.mdmarkdown31.1 KB
sdk-reference/java/numbers-config.mdmarkdown32.2 KB
sdk-reference/java/numbers-services.mdmarkdown15.9 KB
sdk-reference/java/numbers.mdmarkdown28.4 KB
sdk-reference/java/oauth.mdmarkdown13.0 KB
sdk-reference/java/porting-in.mdmarkdown46.2 KB
sdk-reference/java/porting-out.mdmarkdown11.9 KB
sdk-reference/java/sip-integrations.mdmarkdown38.1 KB
sdk-reference/java/sip.mdmarkdown51.9 KB
sdk-reference/java/storage.mdmarkdown5.7 KB
sdk-reference/java/texml.mdmarkdown41.3 KB
sdk-reference/java/verify.mdmarkdown16.0 KB
sdk-reference/java/video.mdmarkdown20.3 KB
sdk-reference/java/voice-advanced.mdmarkdown35.0 KB
sdk-reference/java/voice-conferencing.mdmarkdown46.0 KB
sdk-reference/java/voice-gather.mdmarkdown16.1 KB
sdk-reference/java/voice-media.mdmarkdown19.5 KB
sdk-reference/java/voice-streaming.mdmarkdown14.7 KB
sdk-reference/java/voice.mdmarkdown40.8 KB
sdk-reference/java/webrtc.mdmarkdown8.5 KB
sdk-reference/javascript/10dlc.mdmarkdown28.5 KB
sdk-reference/javascript/account-access.mdmarkdown15.9 KB
sdk-reference/javascript/account-management.mdmarkdown9.6 KB
sdk-reference/javascript/account-notifications.mdmarkdown11.7 KB
sdk-reference/javascript/account-reports.mdmarkdown29.3 KB
sdk-reference/javascript/account.mdmarkdown8.7 KB
sdk-reference/javascript/ai-assistants.mdmarkdown37.5 KB
sdk-reference/javascript/ai-inference.mdmarkdown24.4 KB
sdk-reference/javascript/fax.mdmarkdown19.9 KB
sdk-reference/javascript/iot.mdmarkdown47.4 KB
sdk-reference/javascript/messaging-hosted.mdmarkdown22.4 KB
sdk-reference/javascript/messaging-profiles.mdmarkdown14.8 KB
sdk-reference/javascript/messaging.mdmarkdown29.7 KB
sdk-reference/javascript/missions.mdmarkdown23.8 KB
sdk-reference/javascript/networking.mdmarkdown39.3 KB
sdk-reference/javascript/numbers-compliance.mdmarkdown25.9 KB
sdk-reference/javascript/numbers-config.mdmarkdown28.3 KB
sdk-reference/javascript/numbers-services.mdmarkdown13.0 KB
sdk-reference/javascript/numbers.mdmarkdown26.1 KB
sdk-reference/javascript/oauth.mdmarkdown10.8 KB
sdk-reference/javascript/porting-in.mdmarkdown37.4 KB
sdk-reference/javascript/porting-out.mdmarkdown10.2 KB
sdk-reference/javascript/sip-integrations.mdmarkdown28.5 KB
sdk-reference/javascript/sip.mdmarkdown47.4 KB
sdk-reference/javascript/storage.mdmarkdown4.5 KB
sdk-reference/javascript/texml.mdmarkdown31.9 KB
sdk-reference/javascript/verify.mdmarkdown13.3 KB
sdk-reference/javascript/video.mdmarkdown17.2 KB
sdk-reference/javascript/voice-advanced.mdmarkdown32.6 KB
sdk-reference/javascript/voice-conferencing.mdmarkdown40.4 KB
sdk-reference/javascript/voice-gather.mdmarkdown14.3 KB
sdk-reference/javascript/voice-media.mdmarkdown17.8 KB
sdk-reference/javascript/voice-streaming.mdmarkdown13.4 KB
sdk-reference/javascript/voice.mdmarkdown39.4 KB
sdk-reference/javascript/webrtc.mdmarkdown7.2 KB
sdk-reference/python/10dlc.mdmarkdown28.5 KB
sdk-reference/python/account-access.mdmarkdown15.0 KB
sdk-reference/python/account-management.mdmarkdown9.1 KB
sdk-reference/python/account-notifications.mdmarkdown10.9 KB
sdk-reference/python/account-reports.mdmarkdown28.1 KB
sdk-reference/python/account.mdmarkdown8.1 KB
sdk-reference/python/ai-assistants.mdmarkdown37.3 KB
sdk-reference/python/ai-inference.mdmarkdown23.4 KB
sdk-reference/python/fax.mdmarkdown19.5 KB
sdk-reference/python/iot.mdmarkdown45.3 KB
sdk-reference/python/messaging-hosted.mdmarkdown21.7 KB
sdk-reference/python/messaging-profiles.mdmarkdown14.1 KB
sdk-reference/python/messaging.mdmarkdown29.4 KB
sdk-reference/python/missions.mdmarkdown23.2 KB
sdk-reference/python/networking.mdmarkdown37.1 KB
sdk-reference/python/numbers-compliance.mdmarkdown24.6 KB

scripts/test-migration/test-messaging.sh

View on GitHub
#!/usr/bin/env bash
#
# test-messaging.sh — Send a real test SMS via Telnyx Messaging API
#
# Usage: bash test-messaging.sh --confirm [--dry-run]
#
# Arguments:
#   --confirm    Required to actually send the message
#   --dry-run    Validate setup without sending
#   --help       Show this help and exit
#
# Environment variables (required):
#   TELNYX_API_KEY       Your Telnyx API key
#   TELNYX_TO_NUMBER     Destination number (E.164 format) — agent should ask user
#
# Environment variables (optional — auto-detected/created if not set):
#   TELNYX_FROM_NUMBER            Sender number (auto-detected from account if not set)
#   TELNYX_MESSAGING_PROFILE_ID   Messaging profile ID (auto-detected/created if not set)
#
# Exit codes:
#   0 — Message sent and delivery confirmed
#   1 — Message failed or setup error

set -euo pipefail

# --- Colors (disabled if not a terminal) ---
if [ -t 1 ]; then
  RED='\033[0;31m'
  YELLOW='\033[0;33m'
  GREEN='\033[0;32m'
  BLUE='\033[0;34m'
  BOLD='\033[1m'
  NC='\033[0m'
else
  RED='' YELLOW='' GREEN='' BLUE='' BOLD='' NC=''
fi

# --- Parse arguments ---
CONFIRMED=false
DRY_RUN=false

for arg in "$@"; do
  case "$arg" in
    --confirm) CONFIRMED=true ;;
    --dry-run) DRY_RUN=true ;;
    --help|-h)
      echo "Usage: bash test-messaging.sh --confirm [--dry-run]"
      echo ""
      echo "Sends a real test SMS via Telnyx Messaging API."
      echo ""
      echo "Flags:"
      echo "  --confirm    Required to actually send the message"
      echo "  --dry-run    Validate setup without sending"
      echo ""
      echo "Environment variables:"
      echo "  TELNYX_API_KEY                (required) Your Telnyx API key"
      echo "  TELNYX_TO_NUMBER              (required) Destination phone number, E.164 format"
      echo "  TELNYX_FROM_NUMBER            (optional) Sender number (auto-detected from account)"
      echo "  TELNYX_MESSAGING_PROFILE_ID   (optional) Messaging profile ID (auto-created if needed)"
      exit 0
      ;;
    *)
      echo "Unknown argument: $arg" >&2
      echo "Run with --help for usage." >&2
      exit 2
      ;;
  esac
done

echo -e "${BOLD}Telnyx Messaging Test${NC}"
echo "====================="
echo ""
echo -e "${YELLOW}${BOLD}COST WARNING: This test sends a real SMS (~\$0.004)${NC}"
echo ""

# --- Validate hard prerequisites ---
ERRORS=0

if [ -z "${TELNYX_API_KEY:-}" ]; then
  echo -e "  ${RED}FAIL${NC}  TELNYX_API_KEY is not set"
  ERRORS=$((ERRORS + 1))
else
  echo -e "  ${GREEN}PASS${NC}  TELNYX_API_KEY is set (${TELNYX_API_KEY:0:8}...)"
fi

if [ -z "${TELNYX_TO_NUMBER:-}" ]; then
  echo -e "  ${RED}FAIL${NC}  TELNYX_TO_NUMBER is not set"
  echo -e "         The agent should ask the user for their phone number to receive the test SMS."
  ERRORS=$((ERRORS + 1))
else
  echo -e "  ${GREEN}PASS${NC}  TELNYX_TO_NUMBER: ${TELNYX_TO_NUMBER}"
fi

if ! command -v curl &>/dev/null; then
  echo -e "  ${RED}FAIL${NC}  curl is not installed"
  ERRORS=$((ERRORS + 1))
fi

HAS_JQ=false
if command -v jq &>/dev/null; then
  HAS_JQ=true
  echo -e "  ${GREEN}PASS${NC}  jq is available"
else
  echo -e "  ${YELLOW}WARN${NC}  jq not installed — required for auto-setup. Install with: brew install jq / apt install jq"
  ERRORS=$((ERRORS + 1))
fi

if [ "$ERRORS" -gt 0 ]; then
  echo ""
  echo -e "${RED}${BOLD}Setup validation failed. Fix the errors above.${NC}"
  exit 1
fi

# --- Validate API key ---
echo ""
echo -e "${BOLD}Validating API key...${NC}"

HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \
  -H "Authorization: Bearer ${TELNYX_API_KEY}" \
  "https://api.telnyx.com/v2/balance" 2>/dev/null || echo "000")

if [ "$HTTP_CODE" != "200" ]; then
  echo -e "  ${RED}FAIL${NC}  API key validation failed (HTTP $HTTP_CODE)"
  exit 1
fi
echo -e "  ${GREEN}PASS${NC}  API key is valid"

# --- Auto-detect TELNYX_FROM_NUMBER if not set ---
if [ -z "${TELNYX_FROM_NUMBER:-}" ]; then
  echo ""
  echo -e "${BOLD}Auto-detecting sender number...${NC}"
  # Find a messaging-capable number on the account
  NUMS_RESPONSE=$(curl -s -g \
    -H "Authorization: Bearer ${TELNYX_API_KEY}" \
    "https://api.telnyx.com/v2/phone_numbers?page[size]=50&filter[status]=active" 2>/dev/null || echo "")
  if [ -n "$NUMS_RESPONSE" ]; then
    NUM_TOTAL=$(echo "$NUMS_RESPONSE" | jq -r '.meta.total_results // 0' 2>/dev/null)
    if [ "$NUM_TOTAL" = "0" ] || [ -z "$NUM_TOTAL" ]; then
      echo -e "  ${YELLOW}WARN${NC}  No phone numbers on account"
      echo -e "  ${BLUE}INFO${NC}  Searching for a number to purchase..."
      # Determine country from TELNYX_TO_NUMBER prefix
      TO_COUNTRY="US"
      if [[ "${TELNYX_TO_NUMBER}" == +353* ]]; then TO_COUNTRY="IE"
      elif [[ "${TELNYX_TO_NUMBER}" == +44* ]]; then TO_COUNTRY="GB"
      elif [[ "${TELNYX_TO_NUMBER}" == +1* ]]; then TO_COUNTRY="US"
      elif [[ "${TELNYX_TO_NUMBER}" == +61* ]]; then TO_COUNTRY="AU"
      elif [[ "${TELNYX_TO_NUMBER}" == +49* ]]; then TO_COUNTRY="DE"
      elif [[ "${TELNYX_TO_NUMBER}" == +33* ]]; then TO_COUNTRY="FR"
      fi
      SEARCH_RESPONSE=$(curl -s -g \
        -H "Authorization: Bearer ${TELNYX_API_KEY}" \
        "https://api.telnyx.com/v2/available_phone_numbers?filter[country_code]=${TO_COUNTRY}&filter[features][]=sms&filter[limit]=1" 2>/dev/null || echo "")
      AVAIL_NUMBER=$(echo "$SEARCH_RESPONSE" | jq -r '.data[0].phone_number // empty' 2>/dev/null)
      if [ -n "$AVAIL_NUMBER" ]; then
        AVAIL_COST=$(echo "$SEARCH_RESPONSE" | jq -r '.data[0].cost_information.monthly_cost // "~$1.00"' 2>/dev/null)
        echo -e "  ${YELLOW}${BOLD}PURCHASE REQUIRED${NC}: Need to buy a number for testing"
        echo -e "  Available: ${AVAIL_NUMBER} (${TO_COUNTRY}, monthly cost: ${AVAIL_COST})"
        if [ "$CONFIRMED" = true ]; then
          echo -e "  ${BLUE}INFO${NC}  Purchasing ${AVAIL_NUMBER}..."
          ORDER_RESPONSE=$(curl -s -X POST \
            -H "Authorization: Bearer ${TELNYX_API_KEY}" \
            -H "Content-Type: application/json" \
            -d "{\"phone_numbers\": [{\"phone_number\": \"${AVAIL_NUMBER}\"}]}" \
            "https://api.telnyx.com/v2/number_orders" 2>/dev/null || echo "")
          ORDER_ERROR=$(echo "$ORDER_RESPONSE" | jq -r '.errors[0].detail // empty' 2>/dev/null)
          ORDER_STATUS=$(echo "$ORDER_RESPONSE" | jq -r '.data.status // empty' 2>/dev/null)
          if [ -n "$ORDER_ERROR" ]; then
            echo -e "  ${RED}FAIL${NC}  Could not purchase number: $ORDER_ERROR"
            exit 1
          fi
          echo -e "  ${GREEN}PASS${NC}  Number ordered: ${AVAIL_NUMBER} (status: ${ORDER_STATUS})"
          # Wait for provisioning
          sleep 3
          TELNYX_FROM_NUMBER="$AVAIL_NUMBER"
        else
          echo -e "  Run with --confirm to auto-purchase this number."
          exit 0
        fi
      else
        echo -e "  ${RED}FAIL${NC}  No SMS-capable numbers available in ${TO_COUNTRY}"
        echo -e "         Purchase a number at https://portal.telnyx.com/#/app/numbers/search-numbers"
        exit 1
      fi
    else
      # Pick a sender number — messaging profile assignment is critical
      TO_PREFIX=$(echo "${TELNYX_TO_NUMBER}" | grep -o '^+[0-9]\{1,3\}')

      # 1. Same-country number already on a messaging profile (best)
      TELNYX_FROM_NUMBER=$(echo "$NUMS_RESPONSE" | jq -r --arg pfx "$TO_PREFIX" '
        [.data[] | select(.phone_number | startswith($pfx)) | select(.messaging_profile_id != null and .messaging_profile_id != "")] | .[0].phone_number // empty
      ' 2>/dev/null)
      # 2. Any number already on a messaging profile (number must be on profile to send)
      if [ -z "$TELNYX_FROM_NUMBER" ]; then
        TELNYX_FROM_NUMBER=$(echo "$NUMS_RESPONSE" | jq -r '
          [.data[] | select(.messaging_profile_id != null and .messaging_profile_id != "")] | .[0].phone_number // empty
        ' 2>/dev/null)
      fi
      # 3. Same-country number (will try to assign to profile)
      if [ -z "$TELNYX_FROM_NUMBER" ]; then
        TELNYX_FROM_NUMBER=$(echo "$NUMS_RESPONSE" | jq -r --arg pfx "$TO_PREFIX" '
          [.data[] | select(.phone_number | startswith($pfx))] | .[0].phone_number // empty
        ' 2>/dev/null)
      fi
      # 4. Any active number
      if [ -z "$TELNYX_FROM_NUMBER" ]; then
        TELNYX_FROM_NUMBER=$(echo "$NUMS_RESPONSE" | jq -r '.data[0].phone_number // empty' 2>/dev/null)
      fi
      if [ -n "$TELNYX_FROM_NUMBER" ]; then
        echo -e "  ${GREEN}PASS${NC}  Auto-detected sender: ${TELNYX_FROM_NUMBER}"
      else
        echo -e "  ${RED}FAIL${NC}  Could not determine a sender number from account"
        exit 1
      fi
    fi
  else
    echo -e "  ${RED}FAIL${NC}  Could not fetch phone numbers from API"
    exit 1
  fi
else
  echo -e "  ${GREEN}PASS${NC}  TELNYX_FROM_NUMBER: ${TELNYX_FROM_NUMBER}"
fi

# --- Auto-detect or create messaging profile ---
MESSAGING_PROFILE_ID="${TELNYX_MESSAGING_PROFILE_ID:-}"
if [ -z "$MESSAGING_PROFILE_ID" ]; then
  echo ""
  echo -e "${BOLD}Auto-detecting messaging profile...${NC}"
  PROFILE_RESPONSE=$(curl -s -g \
    -H "Authorization: Bearer ${TELNYX_API_KEY}" \
    "https://api.telnyx.com/v2/messaging_profiles?page[size]=10" 2>/dev/null || echo "")
  PROFILE_COUNT=$(echo "$PROFILE_RESPONSE" | jq -r '.data | length' 2>/dev/null || echo "0")
  if [ "$PROFILE_COUNT" -gt 0 ] 2>/dev/null; then
    MESSAGING_PROFILE_ID=$(echo "$PROFILE_RESPONSE" | jq -r '.data[0].id // empty' 2>/dev/null)
  fi
  if [ -n "$MESSAGING_PROFILE_ID" ]; then
    echo -e "  ${GREEN}PASS${NC}  Using existing messaging profile: ${MESSAGING_PROFILE_ID}"

    # Verify the existing profile's whitelisted_destinations include the destination country (or "*")
    TO_COUNTRY="US"
    if [[ "${TELNYX_TO_NUMBER}" == +353* ]]; then TO_COUNTRY="IE"
    elif [[ "${TELNYX_TO_NUMBER}" == +44* ]]; then TO_COUNTRY="GB"
    elif [[ "${TELNYX_TO_NUMBER}" == +1* ]]; then TO_COUNTRY="US"
    elif [[ "${TELNYX_TO_NUMBER}" == +61* ]]; then TO_COUNTRY="AU"
    elif [[ "${TELNYX_TO_NUMBER}" == +49* ]]; then TO_COUNTRY="DE"
    elif [[ "${TELNYX_TO_NUMBER}" == +33* ]]; then TO_COUNTRY="FR"
    fi

    MP_DETAILS=$(curl -s -g \
      -H "Authorization: Bearer ${TELNYX_API_KEY}" \
      "https://api.telnyx.com/v2/messaging_profiles/${MESSAGING_PROFILE_ID}" 2>/dev/null || echo "")
    MP_WHITELIST_MATCH=$(echo "$MP_DETAILS" | jq -r ".data.whitelisted_destinations // [] | map(select(. == \"${TO_COUNTRY}\" or . == \"*\")) | length" 2>/dev/null)
    if [ "${MP_WHITELIST_MATCH:-0}" = "0" ]; then
      echo -e "  ${BLUE}INFO${NC}  Profile doesn't whitelist ${TO_COUNTRY} — adding it..."
      CURRENT_WL=$(echo "$MP_DETAILS" | jq -r '.data.whitelisted_destinations // []' 2>/dev/null)
      UPDATED_WL=$(echo "$CURRENT_WL" | jq --arg c "$TO_COUNTRY" '. + [$c] | unique' 2>/dev/null)
      MP_UPDATE=$(curl -s -g -X PATCH \
        -H "Authorization: Bearer ${TELNYX_API_KEY}" \
        -H "Content-Type: application/json" \
        -d "{\"whitelisted_destinations\": ${UPDATED_WL}}" \
        "https://api.telnyx.com/v2/messaging_profiles/${MESSAGING_PROFILE_ID}" 2>/dev/null || echo "")
      MP_UPDATE_ERR=$(echo "$MP_UPDATE" | jq -r '.errors[0].detail // empty' 2>/dev/null)
      if [ -n "$MP_UPDATE_ERR" ]; then
        echo -e "  ${YELLOW}WARN${NC}  Could not update whitelist: $MP_UPDATE_ERR"
      else
        echo -e "  ${GREEN}PASS${NC}  Added ${TO_COUNTRY} to messaging profile whitelist"
      fi
    else
      echo -e "  ${GREEN}PASS${NC}  Profile whitelists ${TO_COUNTRY}"
    fi
  else
    # Auto-create a messaging profile
    echo -e "  ${BLUE}INFO${NC}  No messaging profile found — creating one..."
    CREATE_RESPONSE=$(curl -s -g -X POST \
      -H "Authorization: Bearer ${TELNYX_API_KEY}" \
      -H "Content-Type: application/json" \
      -d "{\"name\": \"Migration Test Profile $(date +%s)\", \"whitelisted_destinations\": [\"*\"]}" \
      "https://api.telnyx.com/v2/messaging_profiles" 2>/dev/null || echo "")
    MESSAGING_PROFILE_ID=$(echo "$CREATE_RESPONSE" | jq -r '.data.id // empty' 2>/dev/null)
    CREATE_ERROR=$(echo "$CREATE_RESPONSE" | jq -r '.errors[0].detail // empty' 2>/dev/null)
    if [ -n "$CREATE_ERROR" ]; then
      echo -e "  ${RED}FAIL${NC}  Could not create messaging profile: $CREATE_ERROR"
      exit 1
    fi
    if [ -n "$MESSAGING_PROFILE_ID" ]; then
      echo -e "  ${GREEN}PASS${NC}  Created messaging profile: ${MESSAGING_PROFILE_ID}"
    else
      echo -e "  ${RED}FAIL${NC}  Could not create messaging profile"
      exit 1
    fi
  fi

  # Ensure the from number is assigned to this messaging profile
  echo -e "  ${BLUE}INFO${NC}  Ensuring ${TELNYX_FROM_NUMBER} is assigned to profile..."
  NUM_RESPONSE=$(curl -s -g \
    -H "Authorization: Bearer ${TELNYX_API_KEY}" \
    "https://api.telnyx.com/v2/phone_numbers?filter[phone_number]=${TELNYX_FROM_NUMBER}&page[size]=1" 2>/dev/null || echo "")
  PHONE_ID=$(echo "$NUM_RESPONSE" | jq -r '.data[0].id // empty' 2>/dev/null)
  CURRENT_PROFILE=$(echo "$NUM_RESPONSE" | jq -r '.data[0].messaging_profile_id // empty' 2>/dev/null)
  if [ -n "$PHONE_ID" ]; then
    if [ "$CURRENT_PROFILE" = "$MESSAGING_PROFILE_ID" ]; then
      echo -e "  ${GREEN}PASS${NC}  Number already assigned to this profile"
    else
      ASSIGN_RESPONSE=$(curl -s -g -X PATCH \
        -H "Authorization: Bearer ${TELNYX_API_KEY}" \
        -H "Content-Type: application/json" \
        -d "{\"messaging_profile_id\": \"${MESSAGING_PROFILE_ID}\"}" \
        "https://api.telnyx.com/v2/phone_numbers/${PHONE_ID}/messaging" 2>/dev/null || echo "")
      ASSIGN_ERROR=$(echo "$ASSIGN_RESPONSE" | jq -r '.errors[0].detail // empty' 2>/dev/null)
      if [ -n "$ASSIGN_ERROR" ]; then
        echo -e "  ${YELLOW}WARN${NC}  Could not assign number to profile: $ASSIGN_ERROR"
      else
        echo -e "  ${GREEN}PASS${NC}  Number assigned to messaging profile"
      fi
    fi
  else
    echo -e "  ${YELLOW}WARN${NC}  Could not look up phone number — profile created but number not assigned"
  fi
fi

# --- Dry run exits here ---
if [ "$DRY_RUN" = true ]; then
  echo ""
  echo -e "${BOLD}Dry run complete.${NC} Setup looks good."
  echo "  Would send SMS: ${TELNYX_FROM_NUMBER} -> ${TELNYX_TO_NUMBER}"
  echo "  Messaging profile: ${MESSAGING_PROFILE_ID:-none}"
  echo "  Estimated cost: ~\$0.004"
  echo ""
  echo "Run with --confirm to send the message."
  exit 0
fi

# --- Require --confirm ---
if [ "$CONFIRMED" = false ]; then
  echo ""
  echo -e "${BOLD}What this test will do:${NC}"
  echo "  1. Send an SMS from ${TELNYX_FROM_NUMBER} to ${TELNYX_TO_NUMBER}"
  echo "  2. Poll for delivery status"
  echo "  3. Report message ID, status, and cost"
  echo ""
  echo "  Estimated cost: ~\$0.004"
  echo ""
  echo "Run with --confirm to proceed."
  exit 0
fi

# --- Send the message ---
echo ""
echo -e "${BOLD}Sending SMS...${NC}"
echo "  From: ${TELNYX_FROM_NUMBER}"
echo "  To:   ${TELNYX_TO_NUMBER}"
echo ""

# Build JSON payload
MSG_TEXT="Telnyx migration test $(date -u +%Y-%m-%dT%H:%M:%SZ). If you received this, messaging is working."

if [ -n "$MESSAGING_PROFILE_ID" ]; then
  PAYLOAD="{
    \"from\": \"${TELNYX_FROM_NUMBER}\",
    \"to\": \"${TELNYX_TO_NUMBER}\",
    \"text\": \"${MSG_TEXT}\",
    \"messaging_profile_id\": \"${MESSAGING_PROFILE_ID}\"
  }"
else
  PAYLOAD="{
    \"from\": \"${TELNYX_FROM_NUMBER}\",
    \"to\": \"${TELNYX_TO_NUMBER}\",
    \"text\": \"${MSG_TEXT}\"
  }"
fi

SEND_RESPONSE=$(curl -s -X POST \
  -H "Authorization: Bearer ${TELNYX_API_KEY}" \
  -H "Content-Type: application/json" \
  -d "$PAYLOAD" \
  "https://api.telnyx.com/v2/messages" 2>/dev/null || echo "")

if [ -z "$SEND_RESPONSE" ]; then
  echo -e "  ${RED}FAIL${NC}  No response from API"
  exit 1
fi

# Extract message ID
MESSAGE_ID=""
INITIAL_STATUS=""
if [ "$HAS_JQ" = true ]; then
  MESSAGE_ID=$(echo "$SEND_RESPONSE" | jq -r '.data.id // empty' 2>/dev/null)
  INITIAL_STATUS=$(echo "$SEND_RESPONSE" | jq -r '.data.to[0].status // .data.status // empty' 2>/dev/null)
  API_ERROR=$(echo "$SEND_RESPONSE" | jq -r '.errors[0].detail // empty' 2>/dev/null)

  if [ -n "$API_ERROR" ]; then
    echo -e "  ${RED}FAIL${NC}  API error: $API_ERROR"
    ERROR_CODE=$(echo "$SEND_RESPONSE" | jq -r '.errors[0].code // empty' 2>/dev/null)
    if [ -n "$ERROR_CODE" ]; then
      echo -e "  ${BLUE}INFO${NC}  Error code: $ERROR_CODE"
    fi
    exit 1
  fi
fi

if [ -z "$MESSAGE_ID" ]; then
  echo -e "  ${RED}FAIL${NC}  Could not extract message ID from response"
  if [ "$HAS_JQ" = true ]; then
    echo "  Response: $(echo "$SEND_RESPONSE" | jq -c . 2>/dev/null || echo "$SEND_RESPONSE")"
  fi
  exit 1
fi

echo -e "  ${GREEN}PASS${NC}  Message sent"
echo "  Message ID: ${MESSAGE_ID}"
echo "  Initial Status: ${INITIAL_STATUS:-unknown}"

# --- Poll for delivery status ---
echo ""
echo -e "${BOLD}Polling delivery status...${NC}"

MAX_POLL=30
POLL_INTERVAL=2
POLL_START=$(date +%s)
FINAL_STATUS=""
DELIVERY_CONFIRMED=false

while true; do
  ELAPSED=$(( $(date +%s) - POLL_START ))

  if [ "$ELAPSED" -ge "$MAX_POLL" ]; then
    echo -e "  ${YELLOW}WARN${NC}  Polling timeout after ${MAX_POLL}s"
    break
  fi

  sleep "$POLL_INTERVAL"

  # Check message status via API
  STATUS_RESPONSE=$(curl -s \
    -H "Authorization: Bearer ${TELNYX_API_KEY}" \
    "https://api.telnyx.com/v2/messages/${MESSAGE_ID}" 2>/dev/null || echo "")

  if [ "$HAS_JQ" = true ] && [ -n "$STATUS_RESPONSE" ]; then
    CURRENT_STATUS=$(echo "$STATUS_RESPONSE" | jq -r '.data.to[0].status // .data.status // empty' 2>/dev/null)
    MSG_COST=$(echo "$STATUS_RESPONSE" | jq -r '.data.cost.amount // empty' 2>/dev/null)
    MSG_CURRENCY=$(echo "$STATUS_RESPONSE" | jq -r '.data.cost.currency // empty' 2>/dev/null)

    if [ -n "$CURRENT_STATUS" ] && [ "$CURRENT_STATUS" != "$FINAL_STATUS" ]; then
      echo -e "  ${BLUE}INFO${NC}  Status: ${CURRENT_STATUS} (${ELAPSED}s)"
      FINAL_STATUS="$CURRENT_STATUS"
    fi

    case "$CURRENT_STATUS" in
      delivered)
        DELIVERY_CONFIRMED=true
        break
        ;;
      sent)
        # Keep polling — might still get delivery confirmation
        ;;
      queued|sending)
        # Still in progress
        ;;
      failed|undelivered|sending_failed|delivery_failed)
        echo -e "  ${RED}FAIL${NC}  Message delivery failed: ${CURRENT_STATUS}"
        break
        ;;
    esac
  fi
done

# --- Report ---
echo ""
echo "====================="
echo -e "${BOLD}Results${NC}"
echo "  Message ID:     ${MESSAGE_ID}"
echo "  Final Status:   ${FINAL_STATUS:-unknown}"
if [ -n "${MSG_COST:-}" ] && [ -n "${MSG_CURRENCY:-}" ]; then
  echo "  Cost:           ${MSG_COST} ${MSG_CURRENCY}"
else
  echo "  Cost:           ~\$0.004 (estimated)"
fi

if [ "$DELIVERY_CONFIRMED" = true ]; then
  echo ""
  echo -e "  ${GREEN}${BOLD}PASS${NC}  Message delivered successfully"
  exit 0
elif [ "$FINAL_STATUS" = "sent" ]; then
  echo ""
  echo -e "  ${YELLOW}${BOLD}WARN${NC}  Message sent but delivery not confirmed within polling window"
  echo "  This is normal — delivery receipts can be delayed."
  echo "  Check the Telnyx portal for final status."
  exit 0
else
  echo ""
  echo -e "  ${RED}${BOLD}FAIL${NC}  Message was not delivered"
  echo "  Check that the destination number is valid and can receive SMS."
  exit 1
fi
finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the workStars: 322kUpdated Claude CodeCodexVerifiedInstall
systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixesStars: 322kUpdated Claude CodeCodexVerifiedInstall
test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation codeStars: 322kUpdated Claude CodeCodexVerifiedInstall
skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy.Stars: 198kUpdated Claude CodeCodexVerifiedInstall