File size: 11,667 Bytes
8478d3e
 
 
 
 
 
 
 
8a5fa76
 
 
 
 
 
 
 
8478d3e
 
 
 
 
 
 
 
8a5fa76
 
 
 
 
8478d3e
8a5fa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8478d3e
 
 
 
 
 
 
 
 
 
8a5fa76
8478d3e
 
 
 
 
 
8a5fa76
 
 
8478d3e
 
 
 
 
 
 
 
 
 
 
 
8a5fa76
8478d3e
 
 
 
 
 
 
8a5fa76
 
8478d3e
8a5fa76
 
 
 
 
 
8478d3e
 
 
 
 
 
8a5fa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8478d3e
 
 
 
 
 
 
 
8a5fa76
 
 
8478d3e
 
 
 
 
8a5fa76
 
8478d3e
8a5fa76
 
 
 
 
 
 
8478d3e
 
 
 
 
8a5fa76
 
 
 
 
8478d3e
 
 
 
 
 
 
 
8a5fa76
8478d3e
8a5fa76
 
 
8478d3e
 
 
 
 
8a5fa76
 
8478d3e
 
8a5fa76
 
 
 
 
 
 
 
 
8478d3e
 
 
8a5fa76
 
 
 
 
8478d3e
8a5fa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8478d3e
8a5fa76
 
 
8478d3e
8a5fa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8478d3e
8a5fa76
 
 
8478d3e
8a5fa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8478d3e
8a5fa76
 
 
 
 
 
 
 
 
 
 
 
 
8478d3e
8a5fa76
8478d3e
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# TWILIO INTEGRATION MODULE FOR JAY'S MOBILE WASH
# Handles Twilio API integration for calls and messages

import os
import json
import logging
import traceback
from datetime import datetime
from pathlib import Path

# Try to import dotenv (it's in our requirements)
try:
    from dotenv import load_dotenv
    load_dotenv()
except ImportError:
    logging.warning("dotenv not available - environment variables must be set directly")

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger('twilio_integration')

# Base paths
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATA_DIR = os.path.join(BASE_DIR, "data")
CONFIG_DIR = os.path.join(BASE_DIR, "config")
LOGS_DIR = os.path.join(BASE_DIR, "logs")

# Create necessary directories
for directory in [DATA_DIR, CONFIG_DIR, LOGS_DIR]:
    try:
        Path(directory).mkdir(parents=True, exist_ok=True)
    except Exception as e:
        logger.error(f"Error creating directory {directory}: {str(e)}")

# Utility function to format phone numbers
def format_phone(number):
    """Format a phone number to E.164 format"""
    # Remove all non-digit characters
    digits = ''.join(filter(str.isdigit, str(number)))
    
    # Handle US numbers
    if len(digits) == 10:
        return f"+1{digits}"
    elif len(digits) == 11 and digits[0] == '1':
        return f"+{digits}"
    else:
        # Just add + if not recognized format
        return f"+{digits}"

# Generate ID utility
def generate_id(prefix='id_'):
    """Generate a unique ID"""
    import random, time
    timestamp = int(time.time() * 1000)
    random_part = random.randint(1000, 9999)
    return f"{prefix}{timestamp}_{random_part}"

# Try importing Twilio
try:
    from twilio.rest import Client
    from twilio.twiml.voice_response import VoiceResponse, Say
    from twilio.twiml.messaging_response import MessagingResponse
    TWILIO_AVAILABLE = True
    logger.info("✅ Twilio library available")
except ImportError:
    TWILIO_AVAILABLE = False
    logger.warning("⚠️ Twilio library not available! SMS/Call functionality will be limited to demo mode.")

# Twilio credentials
TWILIO_ACCOUNT_SID = os.getenv('TWILIO_ACCOUNT_SID')
TWILIO_AUTH_TOKEN = os.getenv('TWILIO_AUTH_TOKEN')
TWILIO_PHONE_NUMBER = os.getenv('TWILIO_PHONE_NUMBER')

# Initialize client variable
client = None

# Check if Twilio is configured
if TWILIO_AVAILABLE and TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN and TWILIO_PHONE_NUMBER:
    TWILIO_CONFIGURED = True
    try:
        client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
        logger.info(f"✅ Connected to Twilio - Phone number: {TWILIO_PHONE_NUMBER}")
    except Exception as e:
        TWILIO_CONFIGURED = False
        logger.error(f"❌ Failed to connect to Twilio: {str(e)}")
else:
    TWILIO_CONFIGURED = False
    if TWILIO_AVAILABLE:
        logger.warning("⚠️ Twilio credentials not set or incomplete. Using demo mode.")

class TwilioManager:
    """Manages Twilio integration for calls and messages"""
    
    def __init__(self):
        """Initialize Twilio manager"""
        self.ready = TWILIO_CONFIGURED
        self.demo_mode = not self.ready  # Default to demo mode if Twilio not configured
        self.client = client
        self.phone_number = TWILIO_PHONE_NUMBER
        
        # Create messages directory
        self.messages_dir = os.path.join(DATA_DIR, "messages")
        Path(self.messages_dir).mkdir(parents=True, exist_ok=True)
        
        logger.info(f"TwilioManager initialized. Demo mode: {self.demo_mode}")
    
    def set_demo_mode(self, value):
        """Set demo mode"""
        self.demo_mode = bool(value)
        logger.info(f"Twilio demo mode set to: {self.demo_mode}")
    
    def set_credentials(self, account_sid, auth_token, phone_number):
        """Set Twilio credentials"""
        global client, TWILIO_CONFIGURED
        
        if not TWILIO_AVAILABLE:
            logger.error("Cannot set credentials: Twilio library not available")
            return False, "Twilio library not available"
        
        try:
            # Save the credentials
            settings_file = os.path.join(CONFIG_DIR, "twilio_settings.json")
            with open(settings_file, 'w') as f:
                json.dump({
                    "account_sid": account_sid,
                    "auth_token": "********",  # Don't save actual token
                    "phone_number": phone_number,
                    "timestamp": datetime.now().isoformat()
                }, f, indent=2)
            
            # Try to create a client with new credentials
            test_client = Client(account_sid, auth_token)
            
            # If successful, update the main client
            self.client = test_client
            self.phone_number = phone_number
            self.ready = True
            TWILIO_CONFIGURED = True
            client = test_client
            
            logger.info(f"Twilio credentials updated successfully")
            return True, "Credentials saved and validated successfully"
        except Exception as e:
            logger.error(f"Failed to set Twilio credentials: {str(e)}")
            return False, f"Failed to set credentials: {str(e)}"
    
    def send_sms(self, to_number, message):
        """Send SMS message"""
        # Format the phone number
        to_number = format_phone(to_number)
        
        if self.demo_mode:
            # In demo mode, just log the message
            logger.info(f"[DEMO] SMS to {to_number}: {message}")
            message_id = generate_id('sms_')
            
            # Save message in history
            self._save_message({
                "id": message_id,
                "to": to_number,
                "from": self.phone_number or "+15551234567",
                "body": message,
                "status": "delivered",
                "direction": "outbound",
                "timestamp": datetime.now().isoformat(),
                "demo": True
            })
            
            return {
                "success": True,
                "id": message_id,
                "status": "delivered (demo)",
                "message": "Message sent in demo mode"
            }
        
        if not self.ready:
            error_msg = "Twilio not configured - cannot send real SMS"
            logger.error(error_msg)
            return {
                "success": False,
                "error": error_msg,
                "message": "Please configure Twilio settings first"
            }
        
        # Send the message using Twilio
        try:
            twilio_message = self.client.messages.create(
                body=message,
                from_=self.phone_number,
                to=to_number
            )
            
            logger.info(f"SMS sent to {to_number}: {twilio_message.sid}")
            
            # Save message in history
            message_data = {
                "id": twilio_message.sid,
                "to": to_number,
                "from": self.phone_number,
                "body": message,
                "status": twilio_message.status,
                "direction": "outbound",
                "timestamp": datetime.now().isoformat(),
                "demo": False
            }
            
            self._save_message(message_data)
            
            return {
                "success": True,
                "id": twilio_message.sid,
                "status": twilio_message.status,
                "message": "Message sent successfully"
            }
        except Exception as e:
            error_msg = f"Failed to send SMS to {to_number}: {str(e)}"
            logger.error(error_msg)
            return {
                "success": False,
                "error": str(e),
                "message": "Failed to send message"
            }
    
    def _save_message(self, message_data):
        """Save message to history file"""
        try:
            # Ensure messages directory exists
            Path(self.messages_dir).mkdir(parents=True, exist_ok=True)
            
            # Save individual message file
            message_id = message_data["id"]
            message_file = os.path.join(self.messages_dir, f"{message_id}.json")
            with open(message_file, 'w') as f:
                json.dump(message_data, f, indent=2)
            
            # Update message list
            self._update_message_list(message_data)
            
            logger.info(f"Saved message to history: {message_id}")
            return True
        except Exception as e:
            logger.error(f"Failed to save message history: {str(e)}")
            return False
    
    def _update_message_list(self, message_data):
        """Update the master message list"""
        message_list_file = os.path.join(DATA_DIR, "message_list.json")
        
        try:
            # Load existing list or create new
            if os.path.exists(message_list_file):
                with open(message_list_file, 'r') as f:
                    message_list = json.load(f)
            else:
                message_list = []
            
            # Add new message to list (limited data)
            message_list.append({
                "id": message_data["id"],
                "to": message_data["to"],
                "from": message_data["from"],
                "timestamp": message_data["timestamp"],
                "direction": message_data.get("direction", "outbound"),
                "preview": message_data["body"][:50] + "..." if len(message_data["body"]) > 50 else message_data["body"]
            })
            
            # Sort by timestamp (newest first)
            message_list.sort(key=lambda x: x.get("timestamp", ""), reverse=True)
            
            # Limit list to most recent 100 messages
            if len(message_list) > 100:
                message_list = message_list[:100]
            
            # Save updated list
            with open(message_list_file, 'w') as f:
                json.dump(message_list, f, indent=2)
            
            return True
        except Exception as e:
            logger.error(f"Failed to update message list: {str(e)}")
            return False
    
    def get_message_history(self, limit=50, offset=0):
        """Get message history"""
        message_list_file = os.path.join(DATA_DIR, "message_list.json")
        
        try:
            if os.path.exists(message_list_file):
                with open(message_list_file, 'r') as f:
                    message_list = json.load(f)
                
                # Slice the list based on offset and limit
                return message_list[offset:offset+limit]
            else:
                return []
        except Exception as e:
            logger.error(f"Failed to get message history: {str(e)}")
            return []
    
    def get_message(self, message_id):
        """Get a specific message by ID"""
        message_file = os.path.join(self.messages_dir, f"{message_id}.json")
        
        try:
            if os.path.exists(message_file):
                with open(message_file, 'r') as f:
                    return json.load(f)
            else:
                return None
        except Exception as e:
            logger.error(f"Failed to get message {message_id}: {str(e)}")
            return None
    
    def is_ready(self):
        """Check if Twilio is configured and ready"""
        return self.ready and not self.demo_mode

# Create the global instance
twilio_manager = TwilioManager()