AstraOS commited on
Commit
13f6c83
·
verified ·
1 Parent(s): 267d6f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -30
app.py CHANGED
@@ -1,39 +1,49 @@
1
- import telebot
2
- from fastapi import FastAPI, Request
3
- import uvicorn
 
 
 
4
 
5
- TOKEN = "6655373829:AAGduLdLyNx7zUtxH73Sp3Z1vHKS35tV9WU"
6
- WEBHOOK_URL = "https://astraos-testing.hf.space/webhook" # Replace with your actual webhook URL
7
 
8
- bot = telebot.TeleBot(TOKEN, parse_mode="HTML")
 
 
9
 
10
- app = FastAPI()
 
 
 
11
 
12
- @app.post("/webhook")
13
- async def webhook(request: Request):
14
- json_data = await request.json()
15
- update = telebot.types.Update.de_json(json_data)
16
- bot.process_new_updates([update])
17
- return {"status": "ok"}
18
 
19
- @app.get("/")
20
- def home():
21
- return {"message": "Bot is running"}
 
22
 
23
- @app.on_event("startup")
24
- async def on_startup():
25
- bot.remove_webhook()
26
- bot.set_webhook(WEBHOOK_URL)
27
- print(f"Webhook set to {WEBHOOK_URL}")
28
 
29
- @app.on_event("shutdown")
30
- async def on_shutdown():
31
- bot.remove_webhook()
32
- print("Webhook removed")
33
 
34
- @bot.message_handler(commands=["start"])
35
- def start_command(message):
36
- bot.reply_to(message, "Hello! I am a FastAPI Telegram bot!")
37
 
38
- # if __name__ == "__main__":
39
- # uvicorn.run(app, host="0.0.0.0", port=7860)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from telegram import Update
4
+ from telegram.ext import Application, CommandHandler, ContextTypes
5
+ import logging
6
+ import threading
7
 
 
 
8
 
9
+ # Enable logging
10
+ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
11
+ logger = logging.getLogger(__name__)
12
 
13
+ # Load bot token from environment variable
14
+ BOT_TOKEN = "6655373829:AAGduLdLyNx7zUtxH73Sp3Z1vHKS35tV9WU"
15
+ if not BOT_TOKEN:
16
+ raise ValueError("Bot token is not set in environment variables")
17
 
18
+ # Create the bot application
19
+ application = Application.builder().token(BOT_TOKEN).build()
 
 
 
 
20
 
21
+ # Define the /start command handler
22
+ async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
23
+ """Sends a welcome message when the user starts the bot."""
24
+ await update.message.reply_text("Hello! This bot is running on Hugging Face Spaces 🚀")
25
 
26
+ # Add command handlers
27
+ application.add_handler(CommandHandler("start", start))
 
 
 
28
 
29
+ # Function to run the polling
30
+ def start_polling():
31
+ application.run_polling()
 
32
 
 
 
 
33
 
34
+
35
+
36
+
37
+ # Streamlit UI
38
+ st.title("Telegram Bot on Streamlit")
39
+ st.write("This bot is running using Streamlit and Python-Telegram-Bot.")
40
+
41
+ # Add a text area to show messages (could be used for bot responses)
42
+ message_area = st.empty()
43
+
44
+ # Start a new thread to run polling
45
+ thread = threading.Thread(target=start_polling)
46
+ thread.start()
47
+
48
+ # Display information
49
+ st.write("Bot is actively running and polling Telegram.")