-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
169 lines (142 loc) · 6.52 KB
/
Copy pathdatabase.py
File metadata and controls
169 lines (142 loc) · 6.52 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
import sqlite3
from typing import Optional, Dict, Any
from config import settings
class CallDatabase:
def __init__(self, db_path: str = "customer_calls.db"):
self.db_path = db_path
self.init_database()
def init_database(self):
"""Create the database schema and seed sample data."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
# Create customers table
cursor.execute('''
CREATE TABLE IF NOT EXISTS customers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
phone_number TEXT UNIQUE NOT NULL,
name TEXT,
account_id TEXT,
billing_info TEXT,
support_notes TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Create call interactions table
cursor.execute('''
CREATE TABLE IF NOT EXISTS call_interactions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
customer_id INTEGER NOT NULL,
call_type TEXT NOT NULL,
message TEXT,
transcription TEXT,
duration_seconds INTEGER,
agent_name TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (customer_id) REFERENCES customers(id)
)
''')
# Seed sample customer data
sample_customers = [
("15105550101", "Alice Johnson", "ACC001",
"Current plan: Premium, $49.99/month. Last payment: 2026-04-01",
"Prefers email communication. Previously inquired about enterprise features."),
("14155550102", "Bob Smith", "ACC002",
"Current plan: Standard, $19.99/month. Last payment: 2026-04-02",
"New customer. Account created 2026-03-15. First-time user."),
("14155550103", "Carol White", "ACC003",
"Current plan: Basic, $9.99/month. Last payment: 2026-04-03",
"Loyal customer since 2025. Interested in API access."),
(settings.your_phone_number, settings.your_name, "ACC004",
"Current plan: Basic, $9.99/month. Last payment: 2026-04-03",
"Loyal customer since 2025. Interested in API access.")
]
for phone, name, account_id, billing, notes in sample_customers:
cursor.execute('''
INSERT OR IGNORE INTO customers
(phone_number, name, account_id, billing_info, support_notes)
VALUES (?, ?, ?, ?, ?)
''', (phone, name, account_id, billing, notes))
conn.commit()
conn.close()
def get_customer_by_phone(self, phone_number: str) -> Optional[Dict[str, Any]]:
"""Retrieve customer record by phone number."""
conn = sqlite3.connect(self.db_path)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM customers WHERE phone_number = ?
''', (phone_number,))
row = cursor.fetchone()
conn.close()
return dict(row) if row else None
def log_interaction(self, customer_id: int, call_type: str,
message: str = None, transcription: str = None,
agent_name: str = None, duration: int = 0) -> int:
"""Log a call interaction to the database."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
cursor.execute('''
INSERT INTO call_interactions
(customer_id, call_type, message, transcription, agent_name, duration_seconds)
VALUES (?, ?, ?, ?, ?, ?)
''', (customer_id, call_type, message, transcription, agent_name, duration))
conn.commit()
interaction_id = cursor.lastrowid
conn.close()
return interaction_id
def get_customer_history(self, customer_id: int) -> list:
"""Retrieve call history for a customer."""
conn = sqlite3.connect(self.db_path)
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
cursor.execute('''
SELECT * FROM call_interactions
WHERE customer_id = ?
ORDER BY created_at DESC
LIMIT 10
''', (customer_id,))
rows = cursor.fetchall()
conn.close()
return [dict(row) for row in rows]
def insert_customer_and_log_interaction(self, phone_number: str,
call_type: str,
name: str = None,
account_id: str = None,
billing_info: str = None,
support_notes: str = None,
message: str = None,
transcription: str = None,
agent_name: str = None,
duration: int = 0) -> Dict[str, Any]:
"""Insert a new customer and log their initial interaction."""
conn = sqlite3.connect(self.db_path)
cursor = conn.cursor()
try:
# Insert the customer
cursor.execute('''
INSERT INTO customers
(phone_number, name, account_id, billing_info, support_notes)
VALUES (?, ?, ?, ?, ?)
''', (phone_number, name, account_id, billing_info, support_notes))
customer_id = cursor.lastrowid
# Log the interaction
cursor.execute('''
INSERT INTO call_interactions
(customer_id, call_type, message, transcription, agent_name, duration_seconds)
VALUES (?, ?, ?, ?, ?, ?)
''', (customer_id, call_type, message, transcription, agent_name, duration))
interaction_id = cursor.lastrowid
conn.commit()
return {
"customer_id": customer_id,
"interaction_id": interaction_id,
"phone_number": phone_number,
"name": name
}
except sqlite3.IntegrityError as e:
conn.rollback()
raise ValueError(f"Failed to insert customer: {e}")
finally:
conn.close()
# Global database instance
db = CallDatabase()