diff --git a/main.py b/main.py index eb3625c..4a73d8a 100644 --- a/main.py +++ b/main.py @@ -379,6 +379,30 @@ class LabCleaningBot: print(emoji, "direct", sourceNumber, isRemove) + def create_or_update_task_group(self, task: Task) -> Result[str, str]: + chatgroup = task.chatgroup + + # Check if the group still exists + if chatgroup is not None: + info = self.api.get_group(chatgroup) + + if is_err(info): + # group does not exist (or so...) + print(info.unwrap_err()) + chatgroup = None + + # Create a group for the task if it does not exist + if chatgroup is None: + create_result = self.api.create_group(CreateGroupRequest(name=task.name, members=[self.api.number])) + + if is_err(create_result): + return Err(create_result.unwrap_err()) + else: + chatgroup = create_result.unwrap().id + + task.chatgroup = chatgroup + self.api.update_group_members(chatgroup, [r.user.name for r in task.accepted_requests()]) + async def sync_members_and_tasks(self, session: Session): # Async routine that syncs active members using sync_members_as_active_users(), # sends out new requests for tasks to active users and handles timeouts for the @@ -435,6 +459,7 @@ You have time to answer for {format_seconds(task.timeout)}.""" recipients=[request.user.name]) res = self.api.send_message(message) + self.create_or_update_task_group(task) session.commit() diff --git a/models.py b/models.py index 59ad7fc..54477ad 100644 --- a/models.py +++ b/models.py @@ -84,6 +84,7 @@ class Task(SQLModel, table=True): required_number_of_participants: int due: datetime.datetime timeout: int # in seconds + chatgroup: str | None = None # None = to be created participation_requests: list["ParticipationRequest"] = Relationship(back_populates="task")