Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
refactor(message): using a clearly class to store data instead of a c…
Browse files Browse the repository at this point in the history
…omplex type
  • Loading branch information
NTGNguyen committed Jan 3, 2025
1 parent 0f7a58d commit 1df9158
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
29 changes: 21 additions & 8 deletions src/check_phat_nguoi/notify/message.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from pydantic import BaseModel, Field

from check_phat_nguoi.config import config
from check_phat_nguoi.context import PlateInfoModel, PlatesModel
from check_phat_nguoi.context.plate_context.models.resolution_office import (
Expand All @@ -10,6 +12,13 @@
)


class MessagesModel(BaseModel):
plate: str = Field(description="Biển số")
messages: tuple[str, ...] = Field(
description="List chứa các string chứa các thông tin cụ thể về lỗi vi phạm sau"
)


class Message:
def __init__(self, plates: PlatesModel):
self._plates: PlatesModel = plates
Expand Down Expand Up @@ -58,11 +67,15 @@ def _format_message(
]
)

# FIXME: complex object!!
def format_messages(self) -> dict[str, tuple[str, ...]]:
message_dict: dict[str, tuple[str, ...]] = {}
for plate_info_context in self._plates.plates:
message_dict[plate_info_context.plate] = Message._format_message(
plate_info_context, unpaid_only=config.unpaid_only
)
return message_dict
def format_messages(self) -> tuple[MessagesModel, ...]:
return tuple(
[
MessagesModel(
plate=plate_info_context.plate,
messages=Message._format_message(
plate_info_context, unpaid_only=config.unpaid_only
),
)
for plate_info_context in self._plates.plates
]
)
27 changes: 11 additions & 16 deletions src/check_phat_nguoi/notify/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from check_phat_nguoi.config.dto.notify.telegram import TelegramDTO

from ..constants.notify import SEND_MESSAGE_API_URL_TELEGRAM as API_URL
from .message import MessagesModel
from .noti_engine import NotificationEngine

logger = getLogger(__name__)
Expand All @@ -16,15 +17,15 @@ class Telegram(NotificationEngine):
def __init__(
self,
telegram: TelegramDTO,
message_dict: dict[str, tuple[str, ...]],
messages: tuple[MessagesModel, ...],
):
self._telegram: TelegramDTO = telegram
self._message_dict: dict[str, tuple[str, ...]] = message_dict
self._messages: tuple[MessagesModel, ...] = messages
# FIXME: Heyyyyy refactor timeout hehe
self.timeout = 10
self.session: ClientSession = ClientSession()

async def _send_message(self, message: str) -> None:
async def _send_message(self, message: str, plate: str) -> None:
# FIXME: specify which plate is successfully send, or fail
# FIXME: use fstring instead of string format
url = API_URL.format(bot_token=self._telegram.bot_token)
Expand All @@ -39,31 +40,25 @@ async def _send_message(self, message: str) -> None:
url, json=payload, timeout=ClientTimeout(self.timeout)
) as response:
response.raise_for_status()
logger.info(
"Sending message completed for chat_id:{chat_id} and bot_token:{bot_token}".format(
chat_id=self._telegram.chat_id,
bot_token=self._telegram.bot_token,
)
logger.debug(
f"Sending message completed for chat_id:{self._telegram.chat_id} and bot_token:{self._telegram.bot_token} with plate:{plate}"
)
except asyncio.TimeoutError:
logger.error(
"Time out of {self.timeout} seconds for chat_id:{chat_id} and bot_token:{bot_token}".format(
chat_id=self._telegram.chat_id,
bot_token=self._telegram.bot_token,
)
f"Time out of {self.timeout} seconds for chat_id:{self._telegram.chat_id} and bot_token:{self._telegram.bot_token} with plate{plate}"
)
except ClientConnectionError:
logger.error(
f"Unable to sending message for chat_id:{self._telegram.chat_id} and bot_token:{self._telegram.bot_token}"
f"Unable to sending message for chat_id:{self._telegram.chat_id} and bot_token:{self._telegram.bot_token} with plate{plate}"
)
except Exception as e:
logger.error(e)

async def send_messages(self) -> None:
tasks = (
self._send_message(message)
for message_tuple in self._message_dict.values()
for message in message_tuple
self._send_message(message, messages.plate)
for messages in self._messages
for message in messages.messages
)
await asyncio.gather(*tasks)
await self.session.close()

0 comments on commit 1df9158

Please sign in to comment.