well-svc-alert/well-voicettscall.py
2025-06-16 10:50:51 -07:00

75 lines
2.6 KiB
Python

#!/usr/bin/env python3
import os
import requests
import json
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# --- Configuration ---
TELNYX_API_KEY = os.environ.get("TELNYX_API_KEY") # Make sure this matches your KEY019...
TELNYX_FROM_NUMBER = os.environ.get("TELNYX_FROM_NUMBER", "+16505820706")
# This Connection ID MUST be associated with a Call Control Application
# configured to send webhooks to your webhook server/path.
TELNYX_CONNECTION_ID = os.environ.get("TELNYX_CONNECTION_ID", "2671409623596009055")
TELNYX_API_URL = "https://api.telnyx.com/v2/calls"
TELNYX_WEBHOOK_URL = "http://eluxnetworks.net:1998/telnyx-webhook" # Define your webhook URL
if not TELNYX_API_KEY:
print("Error: TELNYX_API_KEY not found in environment variables or .env file.")
exit(1)
if not TELNYX_CONNECTION_ID:
print("Error: TELNYX_CONNECTION_ID not found in environment variables or .env file.")
exit(1)
# --- Get Call Details ---
to_number = input("Enter the phone number to call (e.g., +14082397258): ")
tts_message = input("Enter the message to speak after answer: ")
# --- Prepare API Request ---
headers = {
"Authorization": f"Bearer {TELNYX_API_KEY}",
"Content-Type": "application/json",
"Accept": "application/json"
}
payload = {
"to": to_number,
"from": TELNYX_FROM_NUMBER,
"connection_id": TELNYX_CONNECTION_ID,
"custom_headers": [
{
"name": "X-TTS-Payload", # Custom header to pass the message
"value": tts_message
}
# Add other custom headers if needed
],
"webhook_url": TELNYX_WEBHOOK_URL,
"webhook_url_method": "POST"
# You might need to specify the webhook URL here if the connection_id doesn't have one pre-configured
# "webhook_url": "http://eluxnetworks.net:1998/", # Example - USE HTTPS!
}
print("\nInitiating call...")
print(f"Payload: {json.dumps(payload, indent=2)}")
# --- Make API Call ---
try:
response = requests.post(TELNYX_API_URL, headers=headers, json=payload)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
print("\nCall initiated successfully!")
print("Response:")
print(json.dumps(response.json(), indent=2))
except requests.exceptions.RequestException as e:
print(f"\nError initiating call: {e}")
if e.response is not None:
print(f"Status Code: {e.response.status_code}")
try:
print(f"Response Body: {json.dumps(e.response.json(), indent=2)}")
except json.JSONDecodeError:
print(f"Response Body: {e.response.text}")
except Exception as e:
print(f"\nAn unexpected error occurred: {e}")