From 3e4a1d25e60be8e44db175ef54f790f8d08b748a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BB=D0=B0=D0=B2=D0=B4=D0=B8=D1=8F=20=D0=94=D1=83?= =?UTF-8?q?=D0=BD=D0=B0=D0=B5=D0=B2=D0=B0?= Date: Fri, 15 Dec 2023 17:08:55 +0700 Subject: [PATCH 1/3] add username in message --- src/bot/services/notify_service.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bot/services/notify_service.py b/src/bot/services/notify_service.py index a26c866..92cf152 100644 --- a/src/bot/services/notify_service.py +++ b/src/bot/services/notify_service.py @@ -74,7 +74,7 @@ async def meeting_notifications(self, plugin: Plugin) -> None: try: plugin.driver.direct_message( receiver_id=user_one.user_id, - message=f"Твои встречи на неделю: {user_two.first_name} {user_two.last_name}", + message=f"Твои встречи на неделю: {user_two.first_name} {user_two.last_name} @{user_two.username}", ) except InvalidOrMissingParameters as error: logger.error(str(error)) From 6bf6d19bb5d1dbf76cb91abf02543dfaee01773c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BB=D0=B0=D0=B2=D0=B4=D0=B8=D1=8F=20=D0=94=D1=83?= =?UTF-8?q?=D0=BD=D0=B0=D0=B5=D0=B2=D0=B0?= Date: Mon, 18 Dec 2023 21:39:48 +0700 Subject: [PATCH 2/3] created class for Message --- src/bot/services/create_message_service.py | 44 ++++++++++ src/bot/services/notify_service.py | 93 +++++++++++----------- src/depends.py | 6 ++ 3 files changed, 98 insertions(+), 45 deletions(-) create mode 100644 src/bot/services/create_message_service.py diff --git a/src/bot/services/create_message_service.py b/src/bot/services/create_message_service.py new file mode 100644 index 0000000..b786c4d --- /dev/null +++ b/src/bot/services/create_message_service.py @@ -0,0 +1,44 @@ +from typing import Any + +from src.bot.schemas import Actions, Attachment, Context, Integration +from src.endpoints import Endpoints + + +class MessageForUsers: + def __init__(self, message_text: str, endpoint_yes: str, endpoint_no: str) -> None: + self.action_yes = Actions( + id="yes", + name="Да", + type="button", + integration=Integration(url=endpoint_yes, context=Context(action="yes")), + ) + self.action_no = Actions( + id="No", + name="Нет", + type="button", + integration=Integration(url=endpoint_no, context=Context(action="no")), + ) + self.message = Attachment(text=message_text, actions=[self.action_yes, self.action_no]) + + def return_message(self) -> dict[str, Any]: + return self.message.model_dump() + + +class FridayMessage(MessageForUsers): + def __init__(self, endpoints: Endpoints): + self._endpoints = endpoints + super().__init__( + message_text="Хочешь ли принять участие в random coffee на следующей неделе?", + endpoint_yes=self._endpoints.add_to_meeting, + endpoint_no=self._endpoints.not_meeting, + ) + + +class WednesdayMessage(MessageForUsers): + def __init__(self, endpoints: Endpoints): + self._endpoints = endpoints + super().__init__( + message_text="Удалось ли вам встретиться?", + endpoint_yes=self._endpoints.match_review_is_complete, + endpoint_no=self._endpoints.match_review_is_not_complete, + ) diff --git a/src/bot/services/notify_service.py b/src/bot/services/notify_service.py index 92cf152..beece64 100644 --- a/src/bot/services/notify_service.py +++ b/src/bot/services/notify_service.py @@ -1,9 +1,8 @@ import structlog -from dependency_injector.wiring import inject from mattermostautodriver.exceptions import InvalidOrMissingParameters from mmpy_bot import Plugin -from src.bot.schemas import Actions, Attachment, Context, Integration +from src.bot.services.create_message_service import MessageForUsers from src.core.db.models import MatchStatusEnum, User from src.core.db.repository.match_review import MatchReviewRepository from src.core.db.repository.user import UserRepository @@ -20,37 +19,41 @@ def __init__( match_repository: UsersMatchRepository, match_review_repository: MatchReviewRepository, endpoints: Endpoints, + direct_friday_message: MessageForUsers, + direct_wednesday_message: MessageForUsers, ) -> None: self._user_repository = user_repository self._match_repository = match_repository self._match_review_repository = match_review_repository self._endpoints = endpoints - - @inject - def direct_friday_message(self) -> Attachment: - action_yes = Actions( - id="yes", - name="Да", - type="button", - integration=Integration(url=self._endpoints.add_to_meeting, context=Context(action="yes")), - ) - - action_no = Actions( - id="No", - name="Нет", - type="button", - integration=Integration(url=self._endpoints.not_meeting, context=Context(action="no")), - ) - - every_friday_message = Attachment( - text="Хочешь ли принять участие в random coffee на следующей неделе?", actions=[action_yes, action_no] - ) - return every_friday_message + self._direct_friday_message = direct_friday_message + self._direct_wednesday_message = direct_wednesday_message + + # @inject + # def direct_friday_message(self) -> Attachment: + # action_yes = Actions( + # id="yes", + # name="Да", + # type="button", + # integration=Integration(url=self._endpoints.add_to_meeting, context=Context(action="yes")), + # ) + # + # action_no = Actions( + # id="No", + # name="Нет", + # type="button", + # integration=Integration(url=self._endpoints.not_meeting, context=Context(action="no")), + # ) + # + # every_friday_message = Attachment( + # text="Хочешь ли принять участие в random coffee на следующей неделе?", actions=[action_yes, action_no] + # ) + # return every_friday_message async def notify_all_users(self, plugin: Plugin, title: str = "Еженедельный опрос") -> None: """Функция отправки еженедельного сообщения (создания поста)""" - friday_attachments = self.direct_friday_message() + friday_attachments = self._direct_friday_message.return_message() users_id = await self._user_repository.get_all_chat_id() if not users_id: logger.error("Пользователи отсутствуют.") @@ -58,7 +61,7 @@ async def notify_all_users(self, plugin: Plugin, title: str = "Еженедел for user_id in users_id: try: plugin.driver.direct_message( - receiver_id=user_id, message=title, props={"attachments": [friday_attachments.model_dump()]} + receiver_id=user_id, message=title, props={"attachments": [friday_attachments]} ) except InvalidOrMissingParameters: logger.error(f"Пользователя с таким user_id {user_id} нет в matter_most") @@ -83,37 +86,37 @@ async def set_match_review_answer(self, user_id: str, answer: str) -> None: match = await self._match_repository.get_by_user_id(user_id) await self._match_review_repository.set_match_review_answer(match, user_id, answer) - @inject - def direct_wednesday_message(self) -> Attachment: - action_yes = Actions( - id="yes", - name="Да", - type="button", - integration=Integration(url=self._endpoints.match_review_is_complete, context=Context(action="yes")), - ) - - action_no = Actions( - id="No", - name="Нет", - type="button", - integration=Integration(url=self._endpoints.match_review_is_not_complete, context=Context(action="no")), - ) - - every_wednesday_message = Attachment(text="Удалось ли вам встретиться?", actions=[action_yes, action_no]) - return every_wednesday_message + # @inject + # def direct_wednesday_message(self) -> Attachment: + # action_yes = Actions( + # id="yes", + # name="Да", + # type="button", + # integration=Integration(url=self._endpoints.match_review_is_complete, context=Context(action="yes")), + # ) + # + # action_no = Actions( + # id="No", + # name="Нет", + # type="button", + # integration=Integration(url=self._endpoints.match_review_is_not_complete, context=Context(action="no")), + # ) + # + # every_wednesday_message = Attachment(text="Удалось ли вам встретиться?", actions=[action_yes, action_no]) + # return every_wednesday_message async def match_review_notifications( self, plugin: Plugin, title: str = "Опрос по результатам встречи", ) -> None: - attachments = self.direct_wednesday_message() + attachments = self._direct_wednesday_message.return_message() for match in await self._match_repository.get_by_status(status=MatchStatusEnum.ONGOING): pair: list[User] = [match.object_user_one, match.object_user_two] for user_one, user_two in zip(pair, pair[::-1]): try: plugin.driver.direct_message( - receiver_id=user_one.user_id, message=title, props={"attachments": [attachments.model_dump()]} + receiver_id=user_one.user_id, message=title, props={"attachments": [attachments]} ) except InvalidOrMissingParameters as error: logger.error(str(error)) diff --git a/src/depends.py b/src/depends.py index acb09d8..bccbf18 100644 --- a/src/depends.py +++ b/src/depends.py @@ -3,6 +3,7 @@ from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine from src.bot.services.admin import AdminService +from src.bot.services.create_message_service import FridayMessage, WednesdayMessage from src.bot.services.matching import MatchingService from src.bot.services.notify_service import NotifyService from src.bot.services.registration import RegistrationService @@ -34,12 +35,17 @@ class Container(containers.DeclarativeContainer): matching_service = providers.Factory( MatchingService, user_repository=user_repository, match_repository=match_repository ) + direct_friday_message = providers.Factory(FridayMessage, endpoints=endpoints) + direct_wednesday_message = providers.Factory(WednesdayMessage, endpoints=endpoints) week_routine_service = providers.Factory( NotifyService, user_repository=user_repository, match_repository=match_repository, match_review_repository=match_review_repository, endpoints=endpoints, + direct_friday_message=direct_friday_message, + direct_wednesday_message=direct_wednesday_message, ) + # Scheduler scheduler: AsyncIOScheduler = providers.Singleton(AsyncIOScheduler) From a781f496a721fa268626065688eb62d9250b668e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=BB=D0=B0=D0=B2=D0=B4=D0=B8=D1=8F=20=D0=94=D1=83?= =?UTF-8?q?=D0=BD=D0=B0=D0=B5=D0=B2=D0=B0?= Date: Mon, 25 Dec 2023 13:48:24 +0700 Subject: [PATCH 3/3] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20depends=20=D1=84=D0=BE=D1=80=D0=BC=D0=B8=D1=80=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D1=81=D0=BE=D0=BE=D0=B1=D1=89?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B9.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/bot/services/create_message_service.py | 21 ------------ src/bot/services/notify_service.py | 40 ---------------------- src/depends.py | 22 ++++++++---- 3 files changed, 16 insertions(+), 67 deletions(-) diff --git a/src/bot/services/create_message_service.py b/src/bot/services/create_message_service.py index b786c4d..6b06c9f 100644 --- a/src/bot/services/create_message_service.py +++ b/src/bot/services/create_message_service.py @@ -1,7 +1,6 @@ from typing import Any from src.bot.schemas import Actions, Attachment, Context, Integration -from src.endpoints import Endpoints class MessageForUsers: @@ -22,23 +21,3 @@ def __init__(self, message_text: str, endpoint_yes: str, endpoint_no: str) -> No def return_message(self) -> dict[str, Any]: return self.message.model_dump() - - -class FridayMessage(MessageForUsers): - def __init__(self, endpoints: Endpoints): - self._endpoints = endpoints - super().__init__( - message_text="Хочешь ли принять участие в random coffee на следующей неделе?", - endpoint_yes=self._endpoints.add_to_meeting, - endpoint_no=self._endpoints.not_meeting, - ) - - -class WednesdayMessage(MessageForUsers): - def __init__(self, endpoints: Endpoints): - self._endpoints = endpoints - super().__init__( - message_text="Удалось ли вам встретиться?", - endpoint_yes=self._endpoints.match_review_is_complete, - endpoint_no=self._endpoints.match_review_is_not_complete, - ) diff --git a/src/bot/services/notify_service.py b/src/bot/services/notify_service.py index beece64..81b89b2 100644 --- a/src/bot/services/notify_service.py +++ b/src/bot/services/notify_service.py @@ -29,27 +29,6 @@ def __init__( self._direct_friday_message = direct_friday_message self._direct_wednesday_message = direct_wednesday_message - # @inject - # def direct_friday_message(self) -> Attachment: - # action_yes = Actions( - # id="yes", - # name="Да", - # type="button", - # integration=Integration(url=self._endpoints.add_to_meeting, context=Context(action="yes")), - # ) - # - # action_no = Actions( - # id="No", - # name="Нет", - # type="button", - # integration=Integration(url=self._endpoints.not_meeting, context=Context(action="no")), - # ) - # - # every_friday_message = Attachment( - # text="Хочешь ли принять участие в random coffee на следующей неделе?", actions=[action_yes, action_no] - # ) - # return every_friday_message - async def notify_all_users(self, plugin: Plugin, title: str = "Еженедельный опрос") -> None: """Функция отправки еженедельного сообщения (создания поста)""" @@ -86,25 +65,6 @@ async def set_match_review_answer(self, user_id: str, answer: str) -> None: match = await self._match_repository.get_by_user_id(user_id) await self._match_review_repository.set_match_review_answer(match, user_id, answer) - # @inject - # def direct_wednesday_message(self) -> Attachment: - # action_yes = Actions( - # id="yes", - # name="Да", - # type="button", - # integration=Integration(url=self._endpoints.match_review_is_complete, context=Context(action="yes")), - # ) - # - # action_no = Actions( - # id="No", - # name="Нет", - # type="button", - # integration=Integration(url=self._endpoints.match_review_is_not_complete, context=Context(action="no")), - # ) - # - # every_wednesday_message = Attachment(text="Удалось ли вам встретиться?", actions=[action_yes, action_no]) - # return every_wednesday_message - async def match_review_notifications( self, plugin: Plugin, diff --git a/src/depends.py b/src/depends.py index bccbf18..8762727 100644 --- a/src/depends.py +++ b/src/depends.py @@ -3,7 +3,7 @@ from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine from src.bot.services.admin import AdminService -from src.bot.services.create_message_service import FridayMessage, WednesdayMessage +from src.bot.services.create_message_service import MessageForUsers from src.bot.services.matching import MatchingService from src.bot.services.notify_service import NotifyService from src.bot.services.registration import RegistrationService @@ -27,6 +27,19 @@ class Container(containers.DeclarativeContainer): user_repository = providers.Factory(UserRepository, sessionmaker=sessionmaker) match_repository = providers.Factory(UsersMatchRepository, sessionmaker=sessionmaker) match_review_repository = providers.Factory(MatchReviewRepository, sessionmaker=sessionmaker) + # Messages + ask_for_random_coffee_message = providers.Factory( + MessageForUsers, + message_text="Хочешь ли принять участие в random coffee на следующей неделе?", + endpoint_yes=endpoints.provided.add_to_meeting, + endpoint_no=endpoints.provided.not_meeting, + ) + ask_how_meeting_go_message = providers.Factory( + MessageForUsers, + message_text="Удалось ли вам встретиться?", + endpoint_yes=endpoints.provided.match_review_is_complete, + endpoint_no=endpoints.provided.match_review_is_not_complete, + ) # Services admin_service = providers.Factory( AdminService, admin_repository=admin_repository, admin_username=settings.provided.ADMIN_USERNAME @@ -35,17 +48,14 @@ class Container(containers.DeclarativeContainer): matching_service = providers.Factory( MatchingService, user_repository=user_repository, match_repository=match_repository ) - direct_friday_message = providers.Factory(FridayMessage, endpoints=endpoints) - direct_wednesday_message = providers.Factory(WednesdayMessage, endpoints=endpoints) week_routine_service = providers.Factory( NotifyService, user_repository=user_repository, match_repository=match_repository, match_review_repository=match_review_repository, endpoints=endpoints, - direct_friday_message=direct_friday_message, - direct_wednesday_message=direct_wednesday_message, + direct_friday_message=ask_for_random_coffee_message, + direct_wednesday_message=ask_how_meeting_go_message, ) - # Scheduler scheduler: AsyncIOScheduler = providers.Singleton(AsyncIOScheduler)