Files
eamco_voipms/app/main.py
2025-09-23 22:40:16 -04:00

126 lines
4.9 KiB
Python

import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
from settings_dev import settings
import requests
from fastapi import FastAPI, HTTPException, status
from .voipms_client import update_did_routing, get_forwardings
from .database import Session
from .models import Call
app = FastAPI(
title="EAMCO VoIP.ms Controller",
description="An API to manage routing for a VoIP.ms DID.",
version="1.0.0",
)
@app.get("/test/forwardings")
def test_get_forwardings():
try:
result = get_forwardings(phone_number=settings.target_cellphone_1)
return {
"message": f"Forwarding entry for {settings.target_cellphone_1}",
"voipms_response": result
}
except HTTPException as e:
raise e
@app.get("/test/did")
def test_did_info():
params = {
"api_username": settings.voipms_api_username,
"api_password": settings.voipms_api_password,
"method": "getDIDsInfo", # Correct: plural "DIDs"
"did": settings.target_did, # Filters to one DID
}
try:
response = requests.get(settings.voipms_api_url, params=params)
print(f"Test Request URL: {response.request.url}")
print(f"Test Response: {response.json()}")
return response.json()
except requests.exceptions.RequestException as e:
return {"error": f"Failed to connect to VoIP.ms API: {str(e)}", "params": params}
except Exception as e:
return {"error": f"Unexpected error: {str(e)}", "params": params}
@app.get("/", tags=["General"])
def read_root():
"""A simple root endpoint to confirm the API is running."""
return {"message": f"Welcome to the VoIP.ms API for DID: {settings.target_did}"}
@app.post("/route/sip", status_code=status.HTTP_200_OK, tags=["DID Routing"])
def route_to_sip_account():
"""
Routes the target DID to the pre-configured SIP account.
"""
try:
# Use sub-account ID from TARGET_SIP_ACCOUNT
sip_account_id = settings.target_sip_account.split('@')[0] # Extract '407323_auburnoil'
routing_string = f"account:{sip_account_id}" # e.g., 'account:407323_auburnoil'
result = update_did_routing(did=settings.target_did, routing=routing_string)
target_phone = sip_account_id
db = Session()
db.add(Call(current_phone=target_phone))
db.commit()
db.close()
return {
"message": f"Successfully routed DID {settings.target_did} to SIP account {settings.target_sip_account}",
"voipms_response": result
}
except HTTPException as e:
raise e
@app.post("/route/cellphone1", status_code=status.HTTP_200_OK, tags=["DID Routing"])
def route_to_cellphone_1():
"""
Routes the target DID to the pre-configured Cellphone #1.
"""
try:
# Get the forwarding entry for the phone number
forwarding = get_forwardings(phone_number=settings.target_cellphone_1)
forwarding_id = forwarding.get("forwarding") # e.g., '998692'
if not forwarding_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"No forwarding ID found for phone number {settings.target_cellphone_1}"
)
routing_string = f"fwd:{forwarding_id}" # e.g., 'fwd:998692'
result = update_did_routing(did=settings.target_did, routing=routing_string)
target_phone = settings.target_cellphone_1
db = Session()
db.add(Call(current_phone=target_phone))
db.commit()
db.close()
return {
"message": f"Successfully routed DID {settings.target_did} to Cellphone #1 ({settings.target_cellphone_1})",
"voipms_response": result
}
except HTTPException as e:
raise e
@app.post("/route/cellphone2", status_code=status.HTTP_200_OK, tags=["DID Routing"])
def route_to_cellphone_2():
"""
Routes the target DID to the pre-configured Cellphone #2.
"""
try:
# Get the forwarding entry for the phone number
forwarding = get_forwardings(phone_number=settings.target_cellphone_2)
forwarding_id = forwarding.get("forwarding") # e.g., ID for 9143306100
if not forwarding_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"No forwarding ID found for phone number {settings.target_cellphone_2}"
)
routing_string = f"fwd:{forwarding_id}" # e.g., 'fwd:<ID>'
result = update_did_routing(did=settings.target_did, routing=routing_string)
target_phone = settings.target_cellphone_2
db = Session()
db.add(Call(current_phone=target_phone))
db.commit()
db.close()
return {
"message": f"Successfully routed DID {settings.target_did} to Cellphone #2 ({settings.target_cellphone_2})",
"voipms_response": result
}
except HTTPException as e:
raise e