CoolFace
Apppublic

hina625/agent-Backend

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes
call_system.py51 linesDownload Raw Back to root
1import os2import argparse3import sys4from dotenv import load_dotenv5from twilio.rest import Client6 7# Load environment configurations (.env file)8load_dotenv()9 10TWILIO_ACCOUNT_SID = os.getenv("TWILIO_ACCOUNT_SID")11TWILIO_AUTH_TOKEN = os.getenv("TWILIO_AUTH_TOKEN")12TWILIO_PHONE_NUMBER = os.getenv("TWILIO_PHONE_NUMBER")13LIVEKIT_SIP_DOMAIN = os.getenv("LIVEKIT_SIP_DOMAIN", "sip.livekit.cloud")14 15def place_call(to_number: str, room_name: str):16    if not TWILIO_ACCOUNT_SID or not TWILIO_AUTH_TOKEN or not TWILIO_PHONE_NUMBER:17        print("ERROR: Missing Twilio configurations in .env file.")18        print("Please make sure TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, and TWILIO_PHONE_NUMBER are configured.")19        sys.exit(1)20 21    print("Connecting to Twilio client...")22    client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)23 24    # Build TwiML instruction to bridge the call to LiveKit SIP25    # We must use the Twilio Phone Number in the SIP URI so LiveKit Trunk can match it correctly.26    sip_uri = f"sip:{TWILIO_PHONE_NUMBER}@{LIVEKIT_SIP_DOMAIN}"27    twiml_instruction = f'<Response><Dial><Sip>{sip_uri}</Sip></Dial></Response>'28 29    print(f"Placing outbound call from {TWILIO_PHONE_NUMBER} to {to_number}...")30    print(f"Bridging call to LiveKit SIP: {sip_uri}")31 32    try:33        call = client.calls.create(34            twiml=twiml_instruction,35            to=to_number,36            from_=TWILIO_PHONE_NUMBER37        )38        print(f"SUCCESS: Call placed successfully! Call SID: {call.sid}")39        print(f"Status: {call.status}")40    except Exception as e:41        print(f"ERROR: Failed to place call: {e}")42        sys.exit(1)43 44if __name__ == "__main__":45    parser = argparse.ArgumentParser(description="Trigger an outbound AI Voice Agent call using Twilio.")46    parser.add_argument("--to", type=str, required=True, help="Destination phone number (E.164 format, e.g. +1234567890)")47    parser.add_argument("--room", type=str, default="outbound-voice-room", help="LiveKit room name to connect the call to")48    49    args = parser.parse_args()50    place_call(args.to, args.room)51