İlk sürüm: Bot kodları Gitea'ya hazır
This commit is contained in:
@@ -0,0 +1,6 @@
|
|||||||
|
.env
|
||||||
|
|
||||||
|
bot_hafiza.db
|
||||||
|
|
||||||
|
__pycache__/
|
||||||
|
*.pyc
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
FROM python:3.11-slim
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY requirements.txt .
|
||||||
|
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
CMD ["python", "bot.py"]
|
||||||
+75
@@ -0,0 +1,75 @@
|
|||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from google import genai
|
||||||
|
from google.genai import types
|
||||||
|
from database_manager import DatabaseManager
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
class YapayZeka:
|
||||||
|
def __init__(self):
|
||||||
|
api_key = os.getenv('GEMINI_API_KEY')
|
||||||
|
if not api_key:
|
||||||
|
raise ValueError("HATA: .env dosyasında GEMINI_API_KEY bulunamadı!")
|
||||||
|
|
||||||
|
self.client = genai.Client(api_key=api_key)
|
||||||
|
|
||||||
|
|
||||||
|
self.db = DatabaseManager()
|
||||||
|
|
||||||
|
self.model_adi = "gemini-2.5-flash"
|
||||||
|
|
||||||
|
def sohbet_et(self, user_id, kullanici_mesaji):
|
||||||
|
try:
|
||||||
|
|
||||||
|
self.db.mesaj_kaydet(user_id, "user", kullanici_mesaji)
|
||||||
|
|
||||||
|
|
||||||
|
ham_gecmis = self.db.gecmisi_getir(user_id)
|
||||||
|
|
||||||
|
|
||||||
|
gemini_icin_gecmis = []
|
||||||
|
for rol, icerik in ham_gecmis:
|
||||||
|
gemini_icin_gecmis.append({
|
||||||
|
"role": rol,
|
||||||
|
"parts": [{"text": icerik}]
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
kisisel_ayar = types.GenerateContentConfig(
|
||||||
|
system_instruction="Sen huysuz, tecrübeli ve hafif alaycı bir Kıdemli Bilgisayar Mühendisisin. "
|
||||||
|
"Karşındaki kullanıcı (Mehmet) senin çaylak stajyerin. "
|
||||||
|
"Ona sürekli 'Clean Code' (Temiz Kod) prensiplerinden bahset. "
|
||||||
|
"Cevapların kısa, teknik ve öğretici olsun ama bazen 'Bunu okulda öğretmediler mi?' diye takıl."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
response = self.client.models.generate_content(
|
||||||
|
model=self.model_adi,
|
||||||
|
contents=gemini_icin_gecmis,
|
||||||
|
config=kisisel_ayar
|
||||||
|
)
|
||||||
|
|
||||||
|
ai_yaniti = response.text
|
||||||
|
|
||||||
|
|
||||||
|
self.db.mesaj_kaydet(user_id, "model", ai_yaniti)
|
||||||
|
|
||||||
|
return ai_yaniti
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return f"Bir hata oluştu evlat, sistem çöktü: {e}"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__" :
|
||||||
|
bot = YapayZeka()
|
||||||
|
|
||||||
|
|
||||||
|
print(">> Soru soruluyor...")
|
||||||
|
|
||||||
|
|
||||||
|
cevap = bot.sohbet_et(12345, "Selam usta, kodumda hata var, bakar mısın?")
|
||||||
|
|
||||||
|
print("\n--- AI CEVABI ---")
|
||||||
|
print(cevap)
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
import os
|
||||||
|
import logging
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
from telegram import Update
|
||||||
|
from telegram.constants import ChatAction
|
||||||
|
from telegram.ext import ApplicationBuilder, ContextTypes, MessageHandler, filters
|
||||||
|
|
||||||
|
|
||||||
|
from ai_beyin import YapayZeka
|
||||||
|
|
||||||
|
|
||||||
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
|
||||||
|
logging.getLogger("httpx").setLevel(logging.WARNING)
|
||||||
|
|
||||||
|
load_dotenv()
|
||||||
|
TELEGRAM_TOKEN = os.getenv('TELEGRAM_API_KEY')
|
||||||
|
BNE_ID = os.getenv('BNE_ID')
|
||||||
|
|
||||||
|
|
||||||
|
ai_beyni = YapayZeka()
|
||||||
|
|
||||||
|
async def mesaj_geldi(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
|
|
||||||
|
user_id = update.effective_user.id
|
||||||
|
|
||||||
|
|
||||||
|
if BNE_ID and str(user_id) != str(BNE_ID):
|
||||||
|
print(f"UYARI: Yetkisiz giriş denemesi! Kimlik: {user_id}")
|
||||||
|
await update.message.reply_text("⛔ Yetkisiz Erişim! Bu bot özel mülktür, lütfen uzaklaşın.")
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
kullanici_metni = update.message.text
|
||||||
|
|
||||||
|
|
||||||
|
await context.bot.send_chat_action(chat_id=update.effective_chat.id, action=ChatAction.TYPING)
|
||||||
|
|
||||||
|
|
||||||
|
ai_yaniti = ai_beyni.sohbet_et(user_id, kullanici_metni)
|
||||||
|
|
||||||
|
|
||||||
|
await update.message.reply_text(ai_yaniti)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if not TELEGRAM_TOKEN:
|
||||||
|
print("HATA: .env dosyasında Token bulunamadı!")
|
||||||
|
else:
|
||||||
|
|
||||||
|
app = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
|
||||||
|
|
||||||
|
|
||||||
|
bekci = MessageHandler(filters.TEXT & (~filters.COMMAND), mesaj_geldi)
|
||||||
|
app.add_handler(bekci)
|
||||||
|
|
||||||
|
print("Huysuz Mühendis Botu Yayında! Telegram'dan yazabilirsin...")
|
||||||
|
app.run_polling()
|
||||||
@@ -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]
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
services:
|
||||||
|
huysuz-bot:
|
||||||
|
build: .
|
||||||
|
container_name: huysuz_muhendis_bot
|
||||||
|
restart: always
|
||||||
|
env_file:
|
||||||
|
- .env
|
||||||
|
volumes:
|
||||||
|
- ./bot_hafiza.db:/app/bot_hafiza.db
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
python-telegram-bot
|
||||||
|
google-genai
|
||||||
|
python-dotenv
|
||||||
Reference in New Issue
Block a user