-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_both_calls.py
More file actions
258 lines (211 loc) Β· 9.89 KB
/
test_both_calls.py
File metadata and controls
258 lines (211 loc) Β· 9.89 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python3
"""
Test both warning and resolution calls
"""
import asyncio
import os
import time
import httpx
from datetime import datetime
from typing import Dict, Any
class AURAVoiceService:
def __init__(self):
self.api_key = os.getenv("VAPI_API_KEY")
self.phone_number_id = os.getenv("VAPI_PHONE_NUMBER_ID")
if not self.api_key or not self.phone_number_id:
print("β VAPI credentials not configured!")
exit(1)
else:
print("β
VAPI configured - making REAL calls")
async def send_warning_call(self, phone_number: str, message: str) -> Dict[str, Any]:
"""Send the warning call to homeowner"""
# Ensure phone number is in E.164 format
if not phone_number.startswith('+'):
if phone_number.startswith('1') and len(phone_number) == 11:
phone_number = '+' + phone_number
elif len(phone_number) == 10:
phone_number = '+1' + phone_number
# Create assistant context for the warning call
assistant_context = f"""
You are AURA, an AI smart home management system. You are calling a homeowner about a potential weather event.
URGENT ALERT: {message}
INSTRUCTIONS:
1. Clearly communicate the urgent alert message above
2. Ask if they want you to prepare their home for the event
3. If they say "yes" or agree, respond: "Great. Executing resilience now. We'll give you a ring when we've made our plan."
4. If they say "no" or decline, respond: "Understood. We'll continue monitoring the situation and will contact you if conditions change."
5. Keep the conversation brief and professional
6. Always end with a clear next step
Remember: You are a helpful AI assistant managing their smart home. Be reassuring but urgent about the situation.
"""
async with httpx.AsyncClient() as client:
try:
call_payload = {
"phoneNumberId": self.phone_number_id,
"customer": {"number": phone_number},
"assistant": {
"firstMessage": f"This is AURA. {message}",
"model": {
"provider": "xai",
"model": "grok-3",
"temperature": 0.1,
"messages": [
{"role": "system", "content": assistant_context},
{
"role": "user",
"content": message,
},
],
},
"voice": {"provider": "11labs", "voiceId": "burt"},
},
}
print(f"π Making WARNING call to {phone_number}")
response = await client.post(
"https://api.vapi.ai/call",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
},
json=call_payload,
)
if response.status_code == 201:
call_data = response.json()
return {
"success": True,
"call_id": call_data.get("id"),
"message": "Warning call initiated successfully"
}
else:
return {
"success": False,
"error": f"API error: {response.status_code} - {response.text}"
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
async def send_resolution_call(self, phone_number: str, profit: float = 4.15) -> Dict[str, Any]:
"""Send the resolution call with final report"""
# Ensure phone number is in E.164 format
if not phone_number.startswith('+'):
if phone_number.startswith('1') and len(phone_number) == 11:
phone_number = '+' + phone_number
elif len(phone_number) == 10:
phone_number = '+1' + phone_number
# Create the resolution message
resolution_message = f"This is AURA with a final report. The home is now secure and operating on battery power. The energy sale was successful, generating a profit of ${profit:.2f}. The situation is managed."
# Create assistant context for the resolution call
assistant_context = f"""
You are AURA, an AI smart home management system. You are calling a homeowner with a final report after successfully managing a weather event.
FINAL REPORT: {resolution_message}
INSTRUCTIONS:
1. Clearly communicate the final report message above
2. Provide a brief summary of what was accomplished:
- Home is now secure and operating on battery power
- Energy sale was successful
- Profit generated: ${profit:.2f}
- Situation is fully managed
3. Ask if they have any questions about the actions taken
4. Keep the conversation brief and professional
5. End with reassurance that their home is protected
Remember: You are providing a positive update about successful home protection. Be confident and reassuring.
"""
async with httpx.AsyncClient() as client:
try:
call_payload = {
"phoneNumberId": self.phone_number_id,
"customer": {"number": phone_number},
"assistant": {
"firstMessage": resolution_message,
"model": {
"provider": "xai",
"model": "grok-3",
"temperature": 0.1,
"messages": [
{"role": "system", "content": assistant_context},
{
"role": "user",
"content": resolution_message,
},
],
},
"voice": {"provider": "11labs", "voiceId": "burt"},
},
}
print(f"π Making RESOLUTION call to {phone_number}")
response = await client.post(
"https://api.vapi.ai/call",
headers={
"Authorization": f"Bearer {self.api_key}",
"Content-Type": "application/json",
},
json=call_payload,
)
if response.status_code == 201:
call_data = response.json()
return {
"success": True,
"call_id": call_data.get("id"),
"message": "Resolution call initiated successfully"
}
else:
return {
"success": False,
"error": f"API error: {response.status_code} - {response.text}"
}
except Exception as e:
return {
"success": False,
"error": str(e)
}
async def test_both_calls():
"""Test both warning and resolution calls"""
print("π Testing Both Warning and Resolution Calls")
print("=" * 60)
# Initialize voice service
voice_service = AURAVoiceService()
# Test number
phone_number = "+19259892492"
# Step 1: Send warning call
print("\nπ¨ Step 1: Sending WARNING call")
print("-" * 40)
warning_message = "Our analyst agents have detected a 92% probability of a grid-straining heatwave event at 4 pm today. Would you like us to prepare your home?"
warning_result = await voice_service.send_warning_call(phone_number, warning_message)
print(f"Warning Call Result:")
print(f"Success: {warning_result.get('success', False)}")
print(f"Call ID: {warning_result.get('call_id', 'N/A')}")
print(f"Message: {warning_result.get('message', 'N/A')}")
if warning_result.get('error'):
print(f"Error: {warning_result.get('error')}")
# Wait for warning call to be answered (30 seconds)
print(f"\nβ³ Waiting 30 seconds for warning call to be answered...")
await asyncio.sleep(30)
# Step 2: Simulate home actions (battery charging, AC cooling, etc.)
print(f"\nπ Step 2: Simulating home actions")
print("-" * 40)
print(" - Charging battery to 100%")
print(" - Pre-cooling home to 68Β°F")
print(" - Preparing for energy sale")
print(" - Actions completed successfully")
# Wait a bit more before sending resolution call
print(f"\nβ³ Waiting 10 seconds before sending resolution call...")
await asyncio.sleep(10)
# Step 3: Send resolution call
print(f"\nβ
Step 3: Sending RESOLUTION call")
print("-" * 40)
resolution_result = await voice_service.send_resolution_call(phone_number, profit=4.15)
print(f"Resolution Call Result:")
print(f"Success: {resolution_result.get('success', False)}")
print(f"Call ID: {resolution_result.get('call_id', 'N/A')}")
print(f"Message: {resolution_result.get('message', 'N/A')}")
if resolution_result.get('error'):
print(f"Error: {resolution_result.get('error')}")
# Summary
print(f"\nπ Summary:")
print(f"Warning call: {'β
Success' if warning_result.get('success') else 'β Failed'}")
print(f"Resolution call: {'β
Success' if resolution_result.get('success') else 'β Failed'}")
print(f"\nβ
Both calls test completed!")
if __name__ == "__main__":
asyncio.run(test_both_calls())