File size: 1,560 Bytes
7ad85cf a6e3f28 20eb075 7ad85cf |
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 |
import os
import streamlit as st
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes
import logging
import asyncio
import threading
app = FastAPI()
# Enable logging
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
# Load bot token
BOT_TOKEN = "6655373829:AAGduLdLyNx7zUtxH73Sp3Z1vHKS35tV9WU"
if not BOT_TOKEN:
raise ValueError("Bot token is not set in environment variables")
# Create the bot application
application = Application.builder().token(BOT_TOKEN).build()
# Define the /start command handler
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
await update.message.reply_text("Hello! This bot is running on Hugging Face Spaces π")
# Add command handlers
application.add_handler(CommandHandler("start", start))
# Function to run polling in a thread-safe way
def run_polling():
loop = asyncio.new_event_loop() # Create a new event loop
asyncio.set_event_loop(loop) # Set it for the current thread
loop.run_until_complete(application.run_polling(close_loop=True)) # Run polling in the loop
# Streamlit UI
st.title("Telegram Bot on Streamlit")
st.write("This bot is running using Streamlit and Python-Telegram-Bot.")
# Button to start the bot
if st.button("Start Bot"):
thread = threading.Thread(target=run_polling, daemon=True) # Run polling in a background thread
thread.start()
st.write("Bot started successfully and is now polling Telegram!") |