Repository navigation
Expand file tree
/
Copy pathtest_outbound_call.py
More file actions
120 lines (91 loc) · 3.47 KB
/
Copy pathtest_outbound_call.py
File metadata and controls
120 lines (91 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
"""
Script to test outbound calling functionality.
This dispatches the agent to place an outbound call.
"""
import asyncio
import os
import sys
import json
import random
# from dotenv import load_dotenv
from livekit import api
from livekit.protocol.sip import CreateSIPParticipantRequest, SIPParticipantInfo
# Load environment variables
# load_dotenv(".env.local")
async def make_outbound_call(phone_number: str, sip_trunk_id: str = None):
"""
Dispatch the agent to make an outbound call.
Args:
phone_number: The phone number to call (e.g., "+15105550123")
sip_trunk_id: The SIP trunk ID to use (optional, can be set in metadata)
"""
# Get LiveKit credentials from environment
livekit_url = "https://test-v0gstj3o.livekit.cloud"
livekit_api_key = "APIsxDLvM5EywoZ"
livekit_api_secret = "Ekr4w73mdf1eWNjxjAVzwwfMX6jyt1nxyVucrkclUpQB"
# Create LiveKit API client
lkapi = api.LiveKitAPI(
url=livekit_url,
api_key=livekit_api_key,
api_secret=livekit_api_secret
)
# Generate a unique room name
room_name = f"outbound-{''.join(str(random.randint(0, 9)) for _ in range(10))}"
# Prepare metadata with phone number and SIP trunk ID
metadata = {
"phone_number": phone_number
}
if sip_trunk_id:
metadata["sip_trunk_id"] = sip_trunk_id
try:
print(f"📞 Dispatching agent to call {phone_number}...")
print(f" Room: {room_name}")
# Create dispatch to trigger the agent
# response = await lkapi.agent_dispatch.create_dispatch(
# api.CreateAgentDispatchRequest(
# agent_name="my-telephony-agent",
# room=room_name,
# metadata=json.dumps(metadata)
# ))
#sends agent to room, but does not place the call
response = await lkapi.agent_dispatch.create_dispatch(
api.CreateAgentDispatchRequest(
agent_name="my-telephony-agent",
room=room_name,
))
#places the call
request = CreateSIPParticipantRequest(
sip_trunk_id = sip_trunk_id,
sip_call_to = phone_number,
room_name = room_name,
participant_identity = "sip-test",
participant_name = "Test Caller",
krisp_enabled = True,
wait_until_answered = True
)
try:
participant = await lkapi.sip.create_sip_participant(request)
print(f"Successfully created {participant}")
except Exception as e:
print(f"Error creating SIP participant: {e}")
finally:
await lkapi.aclose()
print(f"✅ Agent dispatched successfully!")
print(f" Room: {room_name}")
print(f" Dispatch ID: {response.dispatch_id if hasattr(response, 'dispatch_id') else 'N/A'}")
print(f"\n The agent is now placing the call to {phone_number}")
print(f" Monitor your agent logs to see the call progress")
except Exception as e:
print(f"❌ Failed to dispatch agent: {e}")
sys.exit(1)
async def main():
"""Main entry point for the script."""
print("🚀 LiveKit Outbound Call Test")
print("="*50)
phone_number = "+13179854301"
sip_trunk_id = "ST_XAbYyQgcKzMm"
# Make the call
await make_outbound_call(phone_number, sip_trunk_id)
if __name__ == "__main__":
asyncio.run(main())