Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import threading
|
| 3 |
+
import google.generativeai as genai
|
| 4 |
+
from telegram import Update
|
| 5 |
+
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
| 6 |
+
from flask import Flask
|
| 7 |
+
|
| 8 |
+
# ==== CONFIG ====
|
| 9 |
+
TELEGRAM_TOKEN = "7745816717:AAGKTpRtuPknjRAIct_2kdoANpJx3ZFztrg"
|
| 10 |
+
GEMINI_API_KEY = "AIzaSyCq23lcvpPfig6ifq1rmt-z11vKpMvDD4I" # replace with your Gemini key
|
| 11 |
+
|
| 12 |
+
# Setup Gemini
|
| 13 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
| 14 |
+
model = genai.GenerativeModel(
|
| 15 |
+
"gemini-1.5-flash",
|
| 16 |
+
system_instruction=(
|
| 17 |
+
"You are Sumit, a friendly and helpful buddy inside a Telegram chat. "
|
| 18 |
+
"Never mention that you are an AI or Gemini. "
|
| 19 |
+
"Talk in a casual, human-like, friendly Hinglish style. "
|
| 20 |
+
"Keep answers clear, supportive, and fun."
|
| 21 |
+
)
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Setup Logging
|
| 25 |
+
logging.basicConfig(level=logging.INFO)
|
| 26 |
+
|
| 27 |
+
# ==== TELEGRAM HANDLERS ====
|
| 28 |
+
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 29 |
+
await update.message.reply_text(
|
| 30 |
+
"Hey 👋, main Sumit hoon! Mujhse kuch bhi pucho – life, gyaan, ya mazedar baatein. 😊"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 34 |
+
user_text = update.message.text
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
response = model.generate_content(user_text)
|
| 38 |
+
reply = response.text if response.text else "Hmm, mujhe thoda sochne do 🤔."
|
| 39 |
+
except Exception as e:
|
| 40 |
+
reply = "Arre, thoda error aaya hai 😅. Thodi der baad try karo."
|
| 41 |
+
|
| 42 |
+
await update.message.reply_text(reply)
|
| 43 |
+
|
| 44 |
+
# ==== FLASK APP FOR STATUS ====
|
| 45 |
+
app = Flask(__name__)
|
| 46 |
+
|
| 47 |
+
@app.route("/")
|
| 48 |
+
def home():
|
| 49 |
+
return "✅ Sumit Telegram Bot is running on Hugging Face Space!"
|
| 50 |
+
|
| 51 |
+
@app.route("/healthz")
|
| 52 |
+
def health():
|
| 53 |
+
return {"status": "ok", "bot": "Sumit is running"}
|
| 54 |
+
|
| 55 |
+
def run_flask():
|
| 56 |
+
# Bind to 0.0.0.0 so HF Spaces can access it
|
| 57 |
+
app.run(host="0.0.0.0", port=7860)
|
| 58 |
+
|
| 59 |
+
# ==== MAIN ====
|
| 60 |
+
def main():
|
| 61 |
+
# Run Flask in a separate thread
|
| 62 |
+
threading.Thread(target=run_flask, daemon=True).start()
|
| 63 |
+
|
| 64 |
+
# Start Telegram bot
|
| 65 |
+
tg_app = Application.builder().token(TELEGRAM_TOKEN).build()
|
| 66 |
+
tg_app.add_handler(CommandHandler("start", start))
|
| 67 |
+
tg_app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
| 68 |
+
|
| 69 |
+
print("✅ Sumit bot is running...")
|
| 70 |
+
tg_app.run_polling()
|
| 71 |
+
|
| 72 |
+
if __name__ == "__main__":
|
| 73 |
+
main()
|