From 0d63f153c4e20716c44cc6193e0df98d5c4c71a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mehmet=20k=C4=B1y=C4=B1k?= Date: Sat, 14 Feb 2026 22:01:07 +0300 Subject: [PATCH] =?UTF-8?q?=C4=B0lk=20s=C3=BCr=C3=BCm:=20Bot=20kodlar?= =?UTF-8?q?=C4=B1=20Gitea'ya=20haz=C4=B1r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 ++++ Dockerfile | 11 +++++++ ai_beyin.py | 75 +++++++++++++++++++++++++++++++++++++++++++++ bot.py | 56 +++++++++++++++++++++++++++++++++ database_manager.py | 48 +++++++++++++++++++++++++++++ docker-compose.yml | 9 ++++++ requirements.txt | 3 ++ 7 files changed, 208 insertions(+) create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 ai_beyin.py create mode 100644 bot.py create mode 100644 database_manager.py create mode 100644 docker-compose.yml create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb4e973 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.env + +bot_hafiza.db + +__pycache__/ +*.pyc \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..79c378c --- /dev/null +++ b/Dockerfile @@ -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"] \ No newline at end of file diff --git a/ai_beyin.py b/ai_beyin.py new file mode 100644 index 0000000..9514057 --- /dev/null +++ b/ai_beyin.py @@ -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) diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..7328bf9 --- /dev/null +++ b/bot.py @@ -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() \ No newline at end of file diff --git a/database_manager.py b/database_manager.py new file mode 100644 index 0000000..cfcf8c8 --- /dev/null +++ b/database_manager.py @@ -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] diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..622d0e6 --- /dev/null +++ b/docker-compose.yml @@ -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 \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..10baf3a --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +python-telegram-bot +google-genai +python-dotenv \ No newline at end of file