AstraOS commited on
Commit
20eb075
·
verified ·
1 Parent(s): 1c4a918

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -27
app.py CHANGED
@@ -2,37 +2,33 @@ 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
  )
 
2
  from fastapi import FastAPI, Request
3
  import uvicorn
4
 
5
+ TOKEN = "6655373829:AAGduLdLyNx7zUtxH73Sp3Z1vHKS35tV9WU" # Only needed for bot processing, not for webhook setup
6
+ WEBHOOK_URL = "https://astraos-testing.hf.space/webhook" # Webhook already set up, we just listen
 
7
 
8
  bot = telebot.TeleBot(TOKEN, parse_mode="HTML")
9
+ app = FastAPI()
10
 
11
+ @app.post("/webhook")
12
+ async def telegram_webhook(request: Request):
13
+ json_data = await request.json()
14
+ update = telebot.types.Update.de_json(json_data)
15
+ bot.process_new_updates([update]) # Process the received Telegram update
16
+ return {"status": "ok"}
17
 
18
+ @app.get("/")
19
+ def home():
20
+ return {"message": "Bot is running via webhook"}
21
 
22
+ @bot.message_handler(commands=["start"])
23
+ def start_command(message):
24
+ bot.reply_to(message, "Hello! I'm running via FastAPI webhook!")
25
+
26
+ if __name__ == "__main__":
27
  bot.run_webhooks(
28
+ listen="0.0.0.0", # Listen on all available network interfaces
29
+ port=7860, # Use the default FastAPI port (adjust as needed)
30
+ url_path="/webhook", # Must match the endpoint we defined
31
+ webhook_url=WEBHOOK_URL, # Already set on Telegram
32
+ max_connections=40, # Default max connections
33
+ drop_pending_updates=True # Optional: drop old updates
 
 
 
 
 
 
 
34
  )