Rename app.py to _newer_app.py
Browse files- app.py → _newer_app.py +35 -4
app.py → _newer_app.py
RENAMED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import logging
|
| 2 |
import os
|
| 3 |
from fastapi import FastAPI, Request
|
|
|
|
| 4 |
from telegram import Update
|
| 5 |
from telegram.ext import Application, CommandHandler, MessageHandler, filters, CallbackContext
|
| 6 |
import asyncio
|
|
@@ -96,9 +97,11 @@ telegram_app.add_handler(CommandHandler("start", start))
|
|
| 96 |
telegram_app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
| 97 |
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
| 102 |
try:
|
| 103 |
async with httpx.AsyncClient() as client:
|
| 104 |
response = await client.get("https://api.telegram.org")
|
|
@@ -106,11 +109,39 @@ async def test_network():
|
|
| 106 |
except Exception as e:
|
| 107 |
print(f"Network test failed: {e}")
|
| 108 |
|
| 109 |
-
|
| 110 |
await init_telegram()
|
| 111 |
await telegram_app.start()
|
| 112 |
print("Telegram bot initialized successfully!")
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
|
|
|
|
| 1 |
import logging
|
| 2 |
import os
|
| 3 |
from fastapi import FastAPI, Request
|
| 4 |
+
from contextlib import asynccontextmanager
|
| 5 |
from telegram import Update
|
| 6 |
from telegram.ext import Application, CommandHandler, MessageHandler, filters, CallbackContext
|
| 7 |
import asyncio
|
|
|
|
| 97 |
telegram_app.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
| 98 |
|
| 99 |
|
| 100 |
+
@asynccontextmanager
|
| 101 |
+
async def lifespan(app: FastAPI):
|
| 102 |
+
print("Starting application...")
|
| 103 |
+
|
| 104 |
+
# Check internet connection
|
| 105 |
try:
|
| 106 |
async with httpx.AsyncClient() as client:
|
| 107 |
response = await client.get("https://api.telegram.org")
|
|
|
|
| 109 |
except Exception as e:
|
| 110 |
print(f"Network test failed: {e}")
|
| 111 |
|
| 112 |
+
# Initialize Telegram
|
| 113 |
await init_telegram()
|
| 114 |
await telegram_app.start()
|
| 115 |
print("Telegram bot initialized successfully!")
|
| 116 |
|
| 117 |
+
yield # Wait until app closes
|
| 118 |
+
|
| 119 |
+
print("Shutting down application...")
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
app = FastAPI(lifespan=lifespan)
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
@app.get("/")
|
| 126 |
+
async def root():
|
| 127 |
+
return {"message": "Hello, FastAPI with Telegram Bot!"}
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
# Initialize Telegram Bot properly on startup
|
| 131 |
+
# @app.on_event("startup")
|
| 132 |
+
# async def test_network():
|
| 133 |
+
# try:
|
| 134 |
+
# async with httpx.AsyncClient() as client:
|
| 135 |
+
# response = await client.get("https://api.telegram.org")
|
| 136 |
+
# print(f"Network test successful! Status Code: {response.status_code}")
|
| 137 |
+
# except Exception as e:
|
| 138 |
+
# print(f"Network test failed: {e}")
|
| 139 |
+
|
| 140 |
+
# async def startup_event():
|
| 141 |
+
# await init_telegram()
|
| 142 |
+
# await telegram_app.start()
|
| 143 |
+
# print("Telegram bot initialized successfully!")
|
| 144 |
+
|
| 145 |
|
| 146 |
|
| 147 |
|