Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/bot/services/create_message_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import Any

from src.bot.schemas import Actions, Attachment, Context, Integration


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()
57 changes: 10 additions & 47 deletions src/bot/services/notify_service.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,45 +19,28 @@ 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

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("Пользователи отсутствуют.")
return None
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")
Expand All @@ -74,7 +56,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))
Expand All @@ -83,37 +65,18 @@ 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,
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))
16 changes: 16 additions & 0 deletions src/depends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 MessageForUsers
from src.bot.services.matching import MatchingService
from src.bot.services.notify_service import NotifyService
from src.bot.services.registration import RegistrationService
Expand All @@ -26,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
Expand All @@ -40,6 +54,8 @@ class Container(containers.DeclarativeContainer):
match_repository=match_repository,
match_review_repository=match_review_repository,
endpoints=endpoints,
direct_friday_message=ask_for_random_coffee_message,
direct_wednesday_message=ask_how_meeting_go_message,
)
# Scheduler
scheduler: AsyncIOScheduler = providers.Singleton(AsyncIOScheduler)