mirror of
https://github.com/acgnhiki/blrec.git
synced 2024-12-28 09:30:13 +08:00
chore: clean up code
This commit is contained in:
parent
e44614fe4a
commit
cc44f20c2f
@ -201,6 +201,7 @@ class PushplusNotifier(MessageNotifier):
|
||||
super()._do_disable()
|
||||
logger.debug('Disabled Pushplus notifier')
|
||||
|
||||
|
||||
class TelegramNotifier(MessageNotifier):
|
||||
provider = Telegram.get_instance()
|
||||
|
||||
@ -210,4 +211,4 @@ class TelegramNotifier(MessageNotifier):
|
||||
|
||||
def _do_disable(self) -> None:
|
||||
super()._do_disable()
|
||||
logger.debug('Disabled Telegram notifier')
|
||||
logger.debug('Disabled Telegram notifier')
|
||||
|
@ -12,7 +12,9 @@ import aiohttp
|
||||
from ..utils.patterns import Singleton
|
||||
|
||||
|
||||
__all__ = 'MessagingProvider', 'EmailService', 'Serverchan', 'Pushplus', 'Telegram'
|
||||
__all__ = (
|
||||
'MessagingProvider', 'EmailService', 'Serverchan', 'Pushplus', 'Telegram'
|
||||
)
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@ -144,10 +146,12 @@ class Pushplus(MessagingProvider):
|
||||
if response['code'] != 200:
|
||||
raise HTTPException(response['code'], response['msg'])
|
||||
|
||||
|
||||
class TelegramResponse(TypedDict):
|
||||
ok: bool
|
||||
result: dict
|
||||
|
||||
|
||||
class Telegram(MessagingProvider):
|
||||
def __init__(self, token: str = '', chatid: str = '') -> None:
|
||||
super().__init__()
|
||||
@ -163,7 +167,7 @@ class Telegram(MessagingProvider):
|
||||
raise ValueError('No token supplied')
|
||||
if not self.chatid:
|
||||
raise ValueError('No chatid supplied')
|
||||
|
||||
|
||||
async def _post_message(self, title: str, content: str) -> None:
|
||||
url = f'https://api.telegram.org/bot{self.token}/sendMessage'
|
||||
payload = {
|
||||
@ -175,6 +179,8 @@ class Telegram(MessagingProvider):
|
||||
async with aiohttp.ClientSession(raise_for_status=True) as session:
|
||||
async with session.post(url, json=payload) as res:
|
||||
response = cast(TelegramResponse, await res.json())
|
||||
print(response)
|
||||
if not response['ok']:
|
||||
raise HTTPException(response['result']['error_code'], response['result']['description'])
|
||||
raise HTTPException(
|
||||
response['result']['error_code'],
|
||||
response['result']['description'],
|
||||
)
|
||||
|
@ -14,7 +14,6 @@ from typing import (
|
||||
|
||||
|
||||
import toml
|
||||
from blrec.notification.providers import Telegram
|
||||
from pydantic import BaseModel as PydanticBaseModel
|
||||
from pydantic import Field, BaseSettings, validator, PrivateAttr
|
||||
from pydantic.networks import HttpUrl, EmailStr
|
||||
@ -356,21 +355,26 @@ class PushplusSettings(BaseModel):
|
||||
raise ValueError('token is invalid')
|
||||
return value
|
||||
|
||||
|
||||
class TelegramSettings(BaseModel):
|
||||
token: str = ''
|
||||
chatid: str = ''
|
||||
|
||||
@validator('token')
|
||||
def _validate_token(cls, value: str) -> str:
|
||||
if value != '' and not re.fullmatch(r'[0-9]{8,10}:[a-zA-Z0-9_-]{35}', value):
|
||||
if value != '' and not re.fullmatch(
|
||||
r'[0-9]{8,10}:[a-zA-Z0-9_-]{35}', value
|
||||
):
|
||||
raise ValueError('token is invalid')
|
||||
return value
|
||||
|
||||
@validator('chatid')
|
||||
def _validate_chatid(cls, value: str) -> str:
|
||||
if value != '' and not re.fullmatch(r'(-|[0-9]){0,}', value):
|
||||
raise ValueError('chatid is invalid')
|
||||
return value
|
||||
|
||||
|
||||
class NotifierSettings(BaseModel):
|
||||
enabled: bool = False
|
||||
|
||||
@ -399,11 +403,13 @@ class PushplusNotificationSettings(
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class TelegramNotificationSettings(
|
||||
TelegramSettings,NotifierSettings, NotificationSettings
|
||||
TelegramSettings, NotifierSettings, NotificationSettings
|
||||
):
|
||||
pass
|
||||
|
||||
|
||||
class WebHookEventSettings(BaseModel):
|
||||
live_began: bool = True
|
||||
live_ended: bool = True
|
||||
|
@ -21,7 +21,9 @@ from .models import (
|
||||
)
|
||||
from .typing import KeySetOfSettings
|
||||
from ..webhook import WebHook
|
||||
from ..notification import Notifier, EmailService, Serverchan, Pushplus, Telegram
|
||||
from ..notification import (
|
||||
Notifier, EmailService, Serverchan, Pushplus, Telegram
|
||||
)
|
||||
from ..logging import configure_logger
|
||||
from ..exception import NotFoundError
|
||||
if TYPE_CHECKING:
|
||||
|
Loading…
Reference in New Issue
Block a user