İlk sürüm: Bot kodları Gitea'ya hazır

This commit is contained in:
2026-02-14 22:01:07 +03:00
commit 0d63f153c4
7 changed files with 208 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
import sqlite3
class DatabaseManager:
def __init__(self,db_name="bot_hafiza.db"):
self.db_name=db_name
self.tabloyu_hazirla()
def baglan(self):
return sqlite3.connect(self.db_name)
def tabloyu_hazirla(self):
sql_komutu = """
CREATE TABLE IF NOT EXISTS hafiza (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
role TEXT,
content TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
"""
with self.baglan() as conn:
cursor=conn.cursor()
cursor.execute(sql_komutu)
conn.commit()
def mesaj_kaydet(self, user_id, role, content):
"""Sohbetin her adımını veritabanına bir satır olarak ekler."""
sql = "INSERT INTO hafiza (user_id, role, content) VALUES (?, ?, ?)"
with self.baglan() as conn:
cursor = conn.cursor()
cursor.execute(sql, (user_id, role, content))
conn.commit()
def gecmisi_getir(self, user_id, limit=10 ):
"""Gemini'ye hatırlatmak için son 10 mesajı veritabanından çeker."""
sql = "SELECT role, content FROM hafiza WHERE user_id = ? ORDER BY id DESC LIMIT ?"
with self.baglan() as conn:
cursor = conn.cursor()
cursor.execute(sql, (user_id, limit))
sonuclar = cursor.fetchall()
return sonuclar[::-1]