Update app.py
Browse files
app.py
CHANGED
@@ -20,7 +20,7 @@ TELEGRAM_BOT_TOKEN = os.environ.get("TELEGRAM_BOT_TOKEN")
|
|
20 |
AI_API_ENDPOINT = os.environ.get("AI_API_ENDPOINT", "https://new.yangtb2024.me/v1/chat/completions")
|
21 |
AI_API_KEY = os.environ.get("AI_API_KEY")
|
22 |
AI_MODEL = os.environ.get("AI_MODEL", "gemini-2.0-flash-exp")
|
23 |
-
|
24 |
|
25 |
AI_API_HEADERS = {
|
26 |
"Content-Type": "application/json",
|
@@ -120,30 +120,28 @@ class EventEmitter:
|
|
120 |
{"document": [content], "metadata": [{"name": title, "source": url, "html": False}]},
|
121 |
)
|
122 |
|
123 |
-
|
|
|
124 |
try:
|
125 |
-
url = f"{
|
126 |
-
logging.info(f"Checking Cloudflare Worker health at: {url}")
|
127 |
async with aiohttp.ClientSession() as session:
|
128 |
async with session.get(url) as response:
|
129 |
-
logging.info(f"Cloudflare Worker health check response status: {response.status}")
|
130 |
if response.ok:
|
131 |
-
logging.info("
|
132 |
return True
|
133 |
else:
|
134 |
error_text = await response.text()
|
135 |
-
logging.error(f"
|
136 |
return False
|
137 |
except Exception as e:
|
138 |
-
logging.error(f"
|
139 |
return False
|
140 |
|
141 |
-
|
142 |
async def set_bot_commands(bot: Bot):
|
143 |
max_retries = 3
|
144 |
for attempt in range(max_retries):
|
145 |
try:
|
146 |
-
url = f"{
|
147 |
async with aiohttp.ClientSession() as session:
|
148 |
async with session.post(url) as response:
|
149 |
if not response.ok:
|
@@ -155,7 +153,7 @@ async def set_bot_commands(bot: Bot):
|
|
155 |
(command[0], command[1])
|
156 |
for command in BOT_COMMANDS
|
157 |
]
|
158 |
-
url = f"{
|
159 |
async with aiohttp.ClientSession() as session:
|
160 |
async with session.post(url, json={"commands": commands}) as response:
|
161 |
if not response.ok:
|
@@ -512,7 +510,7 @@ async def send_event_message(chat_id, event, context):
|
|
512 |
|
513 |
|
514 |
async def send_telegram_message(chat_id, text, context, options=None):
|
515 |
-
url = f"{
|
516 |
json_data = {"chat_id": chat_id, "text": text}
|
517 |
if options:
|
518 |
json_data.update(options)
|
@@ -558,9 +556,9 @@ def get_help_message():
|
|
558 |
|
559 |
async def main():
|
560 |
logging.basicConfig(level=logging.INFO)
|
561 |
-
|
562 |
-
if not await
|
563 |
-
logging.error("
|
564 |
return
|
565 |
|
566 |
application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|
|
|
20 |
AI_API_ENDPOINT = os.environ.get("AI_API_ENDPOINT", "https://new.yangtb2024.me/v1/chat/completions")
|
21 |
AI_API_KEY = os.environ.get("AI_API_KEY")
|
22 |
AI_MODEL = os.environ.get("AI_MODEL", "gemini-2.0-flash-exp")
|
23 |
+
PHP_PROXY_URL = os.environ.get("PHP_PROXY_URL")
|
24 |
|
25 |
AI_API_HEADERS = {
|
26 |
"Content-Type": "application/json",
|
|
|
120 |
{"document": [content], "metadata": [{"name": title, "source": url, "html": False}]},
|
121 |
)
|
122 |
|
123 |
+
|
124 |
+
async def check_php_proxy_health():
|
125 |
try:
|
126 |
+
url = f"{PHP_PROXY_URL}/health"
|
|
|
127 |
async with aiohttp.ClientSession() as session:
|
128 |
async with session.get(url) as response:
|
|
|
129 |
if response.ok:
|
130 |
+
logging.info("PHP proxy health check passed.")
|
131 |
return True
|
132 |
else:
|
133 |
error_text = await response.text()
|
134 |
+
logging.error(f"PHP proxy health check failed: {response.status}, {error_text}")
|
135 |
return False
|
136 |
except Exception as e:
|
137 |
+
logging.error(f"PHP proxy health check error: {e}")
|
138 |
return False
|
139 |
|
|
|
140 |
async def set_bot_commands(bot: Bot):
|
141 |
max_retries = 3
|
142 |
for attempt in range(max_retries):
|
143 |
try:
|
144 |
+
url = f"{PHP_PROXY_URL}/bot{TELEGRAM_BOT_TOKEN}/deleteMyCommands"
|
145 |
async with aiohttp.ClientSession() as session:
|
146 |
async with session.post(url) as response:
|
147 |
if not response.ok:
|
|
|
153 |
(command[0], command[1])
|
154 |
for command in BOT_COMMANDS
|
155 |
]
|
156 |
+
url = f"{PHP_PROXY_URL}/bot{TELEGRAM_BOT_TOKEN}/setMyCommands"
|
157 |
async with aiohttp.ClientSession() as session:
|
158 |
async with session.post(url, json={"commands": commands}) as response:
|
159 |
if not response.ok:
|
|
|
510 |
|
511 |
|
512 |
async def send_telegram_message(chat_id, text, context, options=None):
|
513 |
+
url = f"{PHP_PROXY_URL}/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
|
514 |
json_data = {"chat_id": chat_id, "text": text}
|
515 |
if options:
|
516 |
json_data.update(options)
|
|
|
556 |
|
557 |
async def main():
|
558 |
logging.basicConfig(level=logging.INFO)
|
559 |
+
|
560 |
+
if not await check_php_proxy_health():
|
561 |
+
logging.error("PHP proxy health check failed. Exiting.")
|
562 |
return
|
563 |
|
564 |
application = Application.builder().token(TELEGRAM_BOT_TOKEN).build()
|