feat(config): add proxy support and API timeout
Build and Push (dev) / build (push) Successful in 3m31s
Build and Push (dev) / build (push) Successful in 3m31s
This commit is contained in:
+60
-11
@@ -1,10 +1,18 @@
|
||||
import logging
|
||||
|
||||
from aiogram import Bot
|
||||
|
||||
from aiohttp import BasicAuth
|
||||
from aiogram.client.session.aiohttp import AiohttpSession
|
||||
|
||||
from aiogram.enums import ParseMode
|
||||
from aiogram.client.default import DefaultBotProperties
|
||||
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
|
||||
from aiogram.exceptions import TelegramAPIError, TelegramBadRequest, TelegramNetworkError
|
||||
from aiogram.exceptions import (
|
||||
TelegramAPIError,
|
||||
TelegramBadRequest,
|
||||
TelegramNetworkError,
|
||||
)
|
||||
|
||||
from config import Config
|
||||
from typing import Dict, Any, List, Optional
|
||||
@@ -17,14 +25,51 @@ class NotificationBot:
|
||||
self.config = config
|
||||
|
||||
try:
|
||||
self.bot: Bot = Bot(
|
||||
token=config.bot_token,
|
||||
default=DefaultBotProperties(
|
||||
parse_mode=ParseMode.HTML,
|
||||
disable_notification=True,
|
||||
link_preview_is_disabled=True,
|
||||
),
|
||||
)
|
||||
if config.proxy_telegram:
|
||||
proxy = (
|
||||
f"{config.proxy_protocol}://{config.proxy_host}:{config.proxy_port}"
|
||||
)
|
||||
|
||||
if config.proxy_username and config.proxy_password:
|
||||
logger.info(
|
||||
"Инициализация Telegram бота с прокси: %s://%s:%s (с авторизацией)",
|
||||
config.proxy_protocol,
|
||||
config.proxy_host,
|
||||
config.proxy_port
|
||||
)
|
||||
auth = BasicAuth(
|
||||
login=config.proxy_username, password=config.proxy_password
|
||||
)
|
||||
self.session = AiohttpSession(proxy=(proxy, auth))
|
||||
else:
|
||||
logger.info(
|
||||
"Инициализация Telegram бота с прокси: %s://%s:%s (без авторизации)",
|
||||
config.proxy_protocol,
|
||||
config.proxy_host,
|
||||
config.proxy_port
|
||||
)
|
||||
self.session = AiohttpSession(proxy=proxy)
|
||||
|
||||
self.bot: Bot = Bot(
|
||||
token=config.bot_token,
|
||||
default=DefaultBotProperties(
|
||||
parse_mode=ParseMode.HTML,
|
||||
disable_notification=True,
|
||||
link_preview_is_disabled=True,
|
||||
),
|
||||
session=self.session,
|
||||
)
|
||||
else:
|
||||
logger.info("Инициализация Telegram бота без прокси")
|
||||
self.bot: Bot = Bot(
|
||||
token=config.bot_token,
|
||||
default=DefaultBotProperties(
|
||||
parse_mode=ParseMode.HTML,
|
||||
disable_notification=True,
|
||||
link_preview_is_disabled=True,
|
||||
),
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"Бот для уведомлений инициализирован (admin_id=%s)", config.admin_id
|
||||
)
|
||||
@@ -33,7 +78,9 @@ class NotificationBot:
|
||||
logger.error(f"Не удалось инициализировать Бот для уведомлений: {e}")
|
||||
raise
|
||||
|
||||
async def _notify(self, message: str, reply_to: int = None, link: str = None) -> Optional[int]:
|
||||
async def _notify(
|
||||
self, message: str, reply_to: int = None, link: str = None
|
||||
) -> Optional[int]:
|
||||
try:
|
||||
logger.debug("Отправка уведомления (reply_to=%s, link=%s)", reply_to, link)
|
||||
keyboard = None
|
||||
@@ -142,7 +189,9 @@ class NotificationBot:
|
||||
logger.error(f"Неожиданная ошибка в success: {e}")
|
||||
return None
|
||||
|
||||
async def failure(self, post: Dict[str, Any], reason: str, post_message_id: int) -> Optional[int]:
|
||||
async def failure(
|
||||
self, post: Dict[str, Any], reason: str, post_message_id: int
|
||||
) -> Optional[int]:
|
||||
try:
|
||||
permalink = post.get("links", {}).get("permalink")
|
||||
logger.warning(
|
||||
|
||||
Reference in New Issue
Block a user