-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwilio_messaging.py
More file actions
111 lines (89 loc) · 3.94 KB
/
twilio_messaging.py
File metadata and controls
111 lines (89 loc) · 3.94 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
import os
import json
from datetime import datetime
from pathlib import Path
from twilio.rest import Client
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def send_deployment_sms(phone_number: str, project_dir: str, deployment_url: str = None):
"""Send SMS notification with deployment URL or project summary"""
# Get Twilio credentials from environment
account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
from_phone = os.environ.get("TWILIO_PHONE_NUMBER")
if not all([account_sid, auth_token, from_phone]):
print("Error: Missing Twilio credentials in environment variables")
return False
client = Client(account_sid, auth_token)
try:
# Build message based on available information
if deployment_url:
message_text = f"🎉 Your website is ready!\n\n"
message_text += f"View it here: {deployment_url}\n\n"
message_text += f"Built with Voice Vibe Coding"
else:
# Try to read project summary from artifacts
summary_file = Path(project_dir) / "artifacts" / "summary.json"
if summary_file.exists():
with open(summary_file, "r") as f:
summary = json.load(f)
message_text = f"✅ Voice Vibe Coding Update\n\n"
message_text += f"Project: {summary.get('projectName', 'Your website')}\n"
message_text += f"Status: {summary.get('status', 'Processing complete')}\n\n"
message_text += f"Check your email for details."
else:
message_text = f"✅ Voice Vibe Coding: Your project is being processed.\n\n"
message_text += f"Project ID: {Path(project_dir).name}\n"
message_text += f"We'll notify you when it's ready!"
# Send SMS
message = client.messages.create(
body=message_text,
from_=from_phone,
to=phone_number
)
print(f"SMS sent successfully. SID: {message.sid}")
return True
except Exception as e:
print(f"Error sending SMS: {e}")
return False
def send_error_notification(phone_number: str, error_message: str):
"""Send SMS notification about an error"""
# Get Twilio credentials from environment
account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
from_phone = os.environ.get("TWILIO_PHONE_NUMBER")
if not all([account_sid, auth_token, from_phone]):
print("Error: Missing Twilio credentials in environment variables")
return False
client = Client(account_sid, auth_token)
try:
message_text = f"⚠️ Voice Vibe Coding Update\n\n"
message_text += f"We encountered an issue processing your request.\n\n"
message_text += f"Error: {error_message[:100]}...\n\n"
message_text += f"Please try again or contact support."
message = client.messages.create(
body=message_text,
from_=from_phone,
to=phone_number
)
print(f"Error notification sent. SID: {message.sid}")
return True
except Exception as e:
print(f"Error sending error notification: {e}")
return False
if __name__ == "__main__":
# Test the SMS functionality
import sys
if len(sys.argv) < 2:
print("Usage: python twilio_messaging.py <phone_number> [deployment_url]")
sys.exit(1)
test_phone = sys.argv[1]
test_url = sys.argv[2] if len(sys.argv) > 2 else None
# Create a test project directory
test_project = Path("projects/test/20250101_120000")
result = send_deployment_sms(test_phone, str(test_project), test_url)
if result:
print("Test SMS sent successfully!")
else:
print("Failed to send test SMS")