Spaces:
Paused
Paused
feat: add startup and shotdowm event on the fastapi
Browse files- app.py +14 -18
- bot_telegram.py +15 -3
app.py
CHANGED
|
@@ -1,29 +1,25 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
import asyncio
|
| 3 |
-
from bot_telegram import TelegramBot, BOT_TOKEN, BASE_URL
|
| 4 |
import logging
|
| 5 |
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
logging.info("Starting bot loop...")
|
| 11 |
-
while True:
|
| 12 |
-
try:
|
| 13 |
-
bot = TelegramBot(
|
| 14 |
-
bot_token=BOT_TOKEN,
|
| 15 |
-
base_url=BASE_URL
|
| 16 |
-
)
|
| 17 |
-
bot.run()
|
| 18 |
-
except Exception as e:
|
| 19 |
-
logging.error(f"Bot error: {e}")
|
| 20 |
-
finally:
|
| 21 |
-
await asyncio.sleep(10)
|
| 22 |
|
| 23 |
@app.on_event("startup")
|
| 24 |
async def startup_event():
|
| 25 |
"""Start the Telegram bot when the FastAPI application starts."""
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
@app.get("/")
|
| 29 |
def greet_json():
|
|
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
|
|
|
| 2 |
import logging
|
| 3 |
|
| 4 |
+
from bot_telegram import init_bot
|
| 5 |
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
bot = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
@app.on_event("startup")
|
| 10 |
async def startup_event():
|
| 11 |
"""Start the Telegram bot when the FastAPI application starts."""
|
| 12 |
+
global bot
|
| 13 |
+
bot = init_bot()
|
| 14 |
+
await bot.run()
|
| 15 |
+
|
| 16 |
+
@app.on_event("shutdown")
|
| 17 |
+
async def shutdown_event():
|
| 18 |
+
"""Stop the Telegram bot when the FastAPI application stops."""
|
| 19 |
+
global bot
|
| 20 |
+
if bot:
|
| 21 |
+
logging.info("Stopping bot...")
|
| 22 |
+
await bot.bot_stop()
|
| 23 |
|
| 24 |
@app.get("/")
|
| 25 |
def greet_json():
|
bot_telegram.py
CHANGED
|
@@ -244,8 +244,20 @@ class TelegramBot:
|
|
| 244 |
self.app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, self.handle_message))
|
| 245 |
self.app.add_handler(MessageHandler(filters.Document.FileExtension("xlsx") | filters.Document.FileExtension("xls"), self.handle_excel))
|
| 246 |
|
| 247 |
-
def run(self):
|
| 248 |
"""Start the bot and listen for messages."""
|
| 249 |
logging.info("Starting Telegram bot...")
|
| 250 |
-
self.app.
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
self.app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, self.handle_message))
|
| 245 |
self.app.add_handler(MessageHandler(filters.Document.FileExtension("xlsx") | filters.Document.FileExtension("xls"), self.handle_excel))
|
| 246 |
|
| 247 |
+
async def run(self):
|
| 248 |
"""Start the bot and listen for messages."""
|
| 249 |
logging.info("Starting Telegram bot...")
|
| 250 |
+
await self.app.initialize()
|
| 251 |
+
await self.app.start()
|
| 252 |
+
await self.app.updater.start_polling()
|
| 253 |
+
|
| 254 |
+
async def bot_stop(self):
|
| 255 |
+
"""Stop the bot."""
|
| 256 |
+
logging.info("Stopping Telegram bot...")
|
| 257 |
+
if self.app.updater:
|
| 258 |
+
await self.app.updater.stop()
|
| 259 |
+
await self.app.stop()
|
| 260 |
+
await self.app.shutdown()
|
| 261 |
+
|
| 262 |
+
def init_bot():
|
| 263 |
+
return TelegramBot(bot_token=BOT_TOKEN,base_url=BASE_URL)
|