-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmessage_logger.py
34 lines (30 loc) · 1.25 KB
/
message_logger.py
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
import sqlite3
"""
Log messages sent through the chatbot and allow for retrievening them later.
This is implemented in SQLListe for testing purposes. Would be better to implement in
a MongoDB or similar document-based database for scalability.
TODO: Create simple analytics for the chatbot, such as the number of messages sent, the most common messages, etc.
"""
class MessageLogger:
def __init__(self, db_path="chatbot.db"):
self.db_path = db_path
self._create_table()
def _create_table(self):
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY,
message TEXT NOT NULL
)
""")
def log_message(self, message):
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute("INSERT INTO messages (message) VALUES (?)", (message,))
conn.commit()
def retrieve_messages(self):
with sqlite3.connect(self.db_path) as conn:
cursor = conn.cursor()
cursor.execute("SELECT id, message FROM messages")
return cursor.fetchall()