Spaces:
Running
Running
File size: 2,318 Bytes
c3ea42c 4584969 c3ea42c 4584969 c3ea42c 4584969 c3ea42c 4584969 c3ea42c 4584969 c3ea42c 4584969 c3ea42c 4584969 c3ea42c 4584969 c3ea42c |
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import logging
import threading
import google.generativeai as genai
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
from flask import Flask
# ==== CONFIG ====
TELEGRAM_TOKEN = "7745816717:AAGKTpRtuPknjRAIct_2kdoANpJx3ZFztrg"
GEMINI_API_KEY = "AIzaSyCq23lcvpPfig6ifq1rmt-z11vKpMvDD4I" # replace with your Gemini key
# Setup Gemini
genai.configure(api_key=GEMINI_API_KEY)
model = genai.GenerativeModel(
"gemini-1.5-flash",
system_instruction=(
"You are Sumit, a friendly and helpful buddy inside a Telegram chat. "
"Never mention that you are an AI or Gemini. "
"Talk in a casual, human-like, friendly Hinglish style. "
"Keep answers clear, supportive, and fun."
)
)
# Setup Logging
logging.basicConfig(level=logging.INFO)
# ==== TELEGRAM HANDLERS ====
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text(
"Hey π, main Sumit hoon! Mujhse kuch bhi pucho β life, gyaan, ya mazedar baatein. π"
)
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_text = update.message.text
try:
response = model.generate_content(user_text)
reply = response.text if response.text else "Hmm, mujhe thoda sochne do π€."
except Exception as e:
reply = "Arre, thoda error aaya hai π
. Thodi der baad try karo."
await update.message.reply_text(reply)
# ==== FLASK APP FOR STATUS ====
app = Flask(__name__)
@app.route("/")
def home():
return "β
Sumit Telegram Bot is running on Hugging Face Space!"
@app.route("/healthz")
def health():
return {"status": "ok", "bot": "Sumit is running"}
def run_flask():
# Bind to 0.0.0.0 so HF Spaces can access it
app.run(host="0.0.0.0", port=7860)
# ==== MAIN ====
def main():
# Run Flask in a separate thread
threading.Thread(target=run_flask, daemon=True).start()
# Start Telegram bot
tg_app = Application.builder().token(TELEGRAM_TOKEN).build()
tg_app.add_handler(CommandHandler("start", start))
tg_app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
print("β
Sumit bot is running...")
tg_app.run_polling()
if __name__ == "__main__":
main()
|