AstraOS commited on
Commit
a6e3f28
·
verified ·
1 Parent(s): 1db903a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -40
app.py CHANGED
@@ -1,40 +1,38 @@
1
- import os
2
- from telegram import Update
3
- from telegram.ext import Application, CommandHandler, ContextTypes
4
- import logging
5
- import threading
6
- import asyncio
7
-
8
- # Enable logging
9
- logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
10
- logger = logging.getLogger(__name__)
11
-
12
- # Load bot token from environment variable
13
- BOT_TOKEN = "6655373829:AAGduLdLyNx7zUtxH73Sp3Z1vHKS35tV9WU"
14
- if not BOT_TOKEN:
15
- raise ValueError("Bot token is not set in environment variables")
16
-
17
- # Create the bot application
18
- application = Application.builder().token(BOT_TOKEN).build()
19
-
20
- # Define the /start command handler
21
- async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
22
- """Sends a welcome message when the user starts the bot."""
23
- await update.message.reply_text("Hello! This bot is running on Hugging Face Spaces 🚀")
24
-
25
- # Add command handlers
26
- application.add_handler(CommandHandler("start", start))
27
-
28
- # Function to run the polling
29
- def start_polling():
30
- # Create a new event loop for this thread
31
- loop = asyncio.new_event_loop()
32
- asyncio.set_event_loop(loop)
33
- loop.run_until_complete(application.run_polling())
34
-
35
- # Start a new thread to run polling
36
- thread = threading.Thread(target=start_polling)
37
- thread.start()
38
-
39
- # Display information in terminal/log
40
- print("Bot is actively running and polling Telegram.")
 
1
+ import telebot
2
+ from fastapi import FastAPI, Request
3
+ import uvicorn
4
+
5
+ # Replace with your bot token and externally set webhook URL
6
+ TOKEN = "6655373829:AAGduLdLyNx7zUtxH73Sp3Z1vHKS35tV9WU"
7
+ WEBHOOK_URL = "https://astraos-testing.hf.space/webhook"
8
+
9
+ bot = telebot.TeleBot(TOKEN, parse_mode="HTML")
10
+
11
+ # Define your bot handlers as usual:
12
+ @bot.message_handler(commands=['start'])
13
+ def handle_start(message):
14
+ bot.reply_to(message, "Hello! This is a webhook-only Telebot running with FastAPI.")
15
+
16
+ @bot.message_handler(func=lambda message: True)
17
+ def echo_all(message):
18
+ bot.reply_to(message, f"You said: {message.text}")
19
+
20
+ if __name__ == '__main__':
21
+ # Run the webhook listener.
22
+ # Since you already set up the webhook externally, this call only
23
+ # listens for incoming updates at the given URL path.
24
+ bot.run_webhooks(
25
+ listen='127.0.0.1', # IP address to listen on
26
+ port=443, # Port to listen on (HTTPS)
27
+ url_path='webhook', # The URL path part (i.e. /webhook)
28
+ webhook_url=WEBHOOK_URL, # Your externally set webhook URL
29
+ certificate=None, # (Optional) Path to SSL certificate file
30
+ certificate_key=None, # (Optional) Path to the certificate key file
31
+ max_connections=40, # (Optional) Maximum simultaneous connections
32
+ allowed_updates=None, # (Optional) List of update types you want your bot to receive
33
+ ip_address=None, # (Optional) Fixed IP address to use for webhook requests
34
+ drop_pending_updates=False, # (Optional) Drop any pending updates on startup
35
+ timeout=20, # (Optional) Request connection timeout in seconds
36
+ secret_token=None, # (Optional) Secret token for verifying webhook requests
37
+ secret_token_length=20 # (Optional) Length of the secret token (default: 20)
38
+ )