bot / app.py
shashwatIDR's picture
Create app.py
21b6b8b verified
raw
history blame
2.32 kB
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()