Spaces:
Sleeping

Maya-Bot-AI / app.py
abhi8989's picture
Update app.py
98e8a6f verified
Raw History Blame Contribute Delete
1.75 kB
import os
import asyncio
from threading import Thread
from flask import Flask
from telegram import Update
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes
from groq import Groq
# 1. Flask App for Hugging Face Health Check
webapp = Flask(__name__)
@webapp.route('/')
def health():
return "Maya is alive and running!"
def run_web():
# Hugging Face uses port 7860 by default
webapp.run(host="0.0.0.0", port=7860)
# 2. Get API Keys from Secrets
TELEGRAM_TOKEN = os.getenv("TELEGRAM_TOKEN")
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
client = Groq(api_key=GROQ_API_KEY)
# 3. Bot Functions
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Hi! I am Maya. How can I help you today?")
async def chat(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_message = update.message.text
try:
response = client.chat.completions.create(
messages=[{"role": "user", "content": user_message}],
model="llama-3.3-70b-versatile"
)
bot_reply = response.choices[0].message.content
await update.message.reply_text(bot_reply)
except Exception as e:
await update.message.reply_text("Sorry, I'm having a bit of a headache. Try again later!")
# 4. Main Entry Point
if __name__ == '__main__':
# Background thread mein web server start karein
Thread(target=run_web).start()
# Telegram Bot start karein
app = ApplicationBuilder().token(TELEGRAM_TOKEN).build()
app.add_handler(CommandHandler('start', start))
app.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), chat))
print("Maya is starting...")
app.run_polling()