|
3 | 3 | from typing import Any, Callable
|
4 | 4 |
|
5 | 5 | import httpx
|
| 6 | +from g4f.Provider import ProviderUtils |
6 | 7 | from loguru import logger
|
7 | 8 | from telegram import Chat as TelegramChat
|
8 | 9 | from telegram import Message as TelegramMessage
|
@@ -36,14 +37,19 @@ def get_telegram_message(update: Update) -> TelegramMessage:
|
36 | 37 | raise ValueError(f"Telegram incoming update does not contain valid message data. Update ID: {update.update_id}")
|
37 | 38 |
|
38 | 39 |
|
39 |
| -def get_prompt_with_replied_message(update: Update, prompt: str) -> str: |
| 40 | +def get_prompt_with_replied_message(update: Update, initial_prompt: str) -> str: |
| 41 | + if not update.message or not update.message.reply_to_message: |
| 42 | + return initial_prompt |
| 43 | + |
40 | 44 | user = get_telegram_user(update)
|
| 45 | + if quoted_user := update.message.reply_to_message.from_user: |
| 46 | + quoted_user_name = quoted_user.name |
| 47 | + else: |
| 48 | + quoted_user_name = "user" |
| 49 | + |
| 50 | + quoted_message = update.message.reply_to_message.caption or update.message.reply_to_message.text |
| 51 | + prompt = f"> {quoted_message}\n>\n> — *{quoted_user_name}*\n\n" f"{user.username}: {initial_prompt}" |
41 | 52 |
|
42 |
| - if update.message and update.message.reply_to_message: |
43 |
| - prompt = (f'<<{update.message.reply_to_message.caption or update.message.reply_to_message.text}.>> ' |
44 |
| - f'{user.username} answered: {prompt}') |
45 |
| - if update.message.reply_to_message.from_user: |
46 |
| - prompt = f' {update.message.reply_to_message.from_user.username} said: ' + prompt |
47 | 53 | return prompt
|
48 | 54 |
|
49 | 55 |
|
@@ -243,9 +249,11 @@ def log_application_settings() -> None:
|
243 | 249 | logger_info = "<red>DISABLED</red>."
|
244 | 250 |
|
245 | 251 | if application_settings.monitoring_url:
|
246 |
| - logger_info = (f"<blue>ACTIVE</blue>." |
247 |
| - f"MONITORING_FREQUENCY_CALL=<blue>{application_settings.monitoring_frequency_call}</blue>." |
248 |
| - f"MONITORING_URL=<blue>{application_settings.monitoring_url}</blue>") |
| 252 | + logger_info = ( |
| 253 | + f"<blue>ACTIVE</blue>." |
| 254 | + f"MONITORING_FREQUENCY_CALL=<blue>{application_settings.monitoring_frequency_call}</blue>." |
| 255 | + f"MONITORING_URL=<blue>{application_settings.monitoring_url}</blue>" |
| 256 | + ) |
249 | 257 |
|
250 | 258 | messages = (
|
251 | 259 | f"Application is initialized using {storage} storage.",
|
@@ -273,14 +281,22 @@ async def run_monitoring(context: ContextTypes.DEFAULT_TYPE) -> None:
|
273 | 281 | if not application_settings.monitoring_url:
|
274 | 282 | return
|
275 | 283 |
|
276 |
| - transport = httpx.AsyncHTTPTransport(retries=application_settings.monitoring_retry_calls, |
277 |
| - proxy=application_settings.monitoring_proxy) |
| 284 | + transport = httpx.AsyncHTTPTransport( |
| 285 | + retries=application_settings.monitoring_retry_calls, proxy=application_settings.monitoring_proxy |
| 286 | + ) |
278 | 287 |
|
279 | 288 | async with httpx.AsyncClient(transport=transport, proxy=application_settings.monitoring_proxy) as client:
|
280 | 289 | try:
|
281 | 290 | result = await client.get(application_settings.monitoring_url)
|
282 | 291 | except Exception as error:
|
283 |
| - logger.error(f'Uptime Checker failed with an Exception: {error}') |
| 292 | + logger.error(f"Uptime Checker failed with an Exception: {error}") |
284 | 293 | return
|
285 | 294 | if result.is_error:
|
286 |
| - logger.error(f'Uptime Checker failed. status_code({result.status_code}) msg: {result.text}') |
| 295 | + logger.error(f"Uptime Checker failed. status_code({result.status_code}) msg: {result.text}") |
| 296 | + |
| 297 | + |
| 298 | +def is_provider_active(model_and_provider_names: tuple[str, str]) -> bool: |
| 299 | + _, provider_name = model_and_provider_names |
| 300 | + if provider := ProviderUtils.convert.get(provider_name): |
| 301 | + return bool(provider.working) |
| 302 | + return False |
0 commit comments