From 56998f905aaff67b59ff8a189dcf2df4bc2ef9a6 Mon Sep 17 00:00:00 2001 From: ennucore Date: Tue, 21 Jun 2022 21:23:53 +0300 Subject: [PATCH] Running necessary actions for community --- README.md | 4 +++- bot.py | 20 +++++++++++++++++--- community.py | 11 ++++++++--- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 111a592..07a91b4 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ Random meetings for chats, kind of like RandomCoffee. 4. The bot sends the matches and links to their Telegram accounts TODO: -- Auto-scheduling: run it at every event +- Auto-scheduling: run it once a day and send meeting info - A button to request a meeting +- Add limitations to the number of meetings - Add commands like info +- In templates replace "this week" with "soon" diff --git a/bot.py b/bot.py index b28b2d6..84b7e47 100644 --- a/bot.py +++ b/bot.py @@ -15,6 +15,10 @@ import traceback import time +POLL_HOUR = 10 +SCHEDULING_HOUR = 20 + + def locale_from_ietf(_ietf_locale: str) -> str: # if not ietf_locale: # return 'en' @@ -204,10 +208,20 @@ class Bot(telebot.TeleBot): person_1, person_2 = self.get_chat(meeting[0]), self.get_chat(meeting[1]) self.send_template(meeting[0], 'meeting_info', community=community, meeting=meeting, person=person_2) self.send_template(meeting[1], 'meeting_info', community=community, meeting=meeting, person=person_1) + community.archived_meetings += community.scheduled_meetings + community.scheduled_meetings = [] + self.save_community(community) - def community_post_edit_routine(self, community: Community): - community.schedule_meetings() - self.send_meeting_info_in_community(community) + def run_necessary_actions_for_community(self, community: Community): + if community.task_last_timestamps.get('poll', datetime.date.today() - datetime.timedelta(days=1)) \ + < datetime.date.today() and datetime.datetime.now().hour >= POLL_HOUR: + self.poll_users_in_community(community) + community.task_last_timestamps['poll'] = datetime.date.today() + if community.task_last_timestamps.get('scheduling', datetime.date.today() - datetime.timedelta(days=1)) \ + < datetime.date.today() and datetime.datetime.now().hour >= SCHEDULING_HOUR: + community.schedule_meetings() + self.send_meeting_info_in_community(community) + community.task_last_timestamps['scheduling'] = datetime.date.today() self.save_community(community) def register_next_step_handler(self, message, callback, *args, **kwargs): diff --git a/community.py b/community.py index 1965fb0..959baf7 100644 --- a/community.py +++ b/community.py @@ -23,11 +23,12 @@ class Community: name: str = '' members: typing.List[int] = field(default_factory=list) pool: typing.List[int] = field(default_factory=list) # [user_id] - polled: typing.Dict[int, date] = field(default_factory=dict) # Days since the user was asked + polled: typing.Dict[int, date] = field(default_factory=dict) # Days since the user was asked scheduled_meetings: typing.List[typing.Tuple[int, int, date]] = field( default_factory=list) # (user_id_1, user_id_2, meeting date) archived_meetings: typing.List[typing.Tuple[int, int, date]] = field(default_factory=list) - default_answers: typing.Dict[int, bool] = field(default_factory=dict) # Last answers + default_answers: typing.Dict[int, bool] = field(default_factory=dict) # Last answers + task_last_timestamps: typing.Dict[str, date] = field(default_factory=dict) # {'poll': ..., 'scheduling': ...} start_timestamp: int = field(default_factory=lambda: int(time.time())) def dict(self) -> dict: @@ -36,6 +37,8 @@ class Community: data['default_answers'] = {str(k): self.default_answers[k] for k in self.default_answers} data['scheduled_meetings'] = [(l, m, r.toordinal()) for l, m, r in self.scheduled_meetings] data['archived_meetings'] = [(l, m, r.toordinal()) for l, m, r in self.archived_meetings] + data['task_last_timestamps'] = {str(k): self.task_last_timestamps[k].toordinal() for k in + self.task_last_timestamps} return data @classmethod @@ -47,6 +50,8 @@ class Community: self = cls(**data_) self.polled = {int(k): convert_to_date(self.polled[k]) for k in self.polled} self.default_answers = {int(k): self.default_answers[k] for k in self.default_answers} + self.task_last_timestamps = {k: convert_to_date(self.task_last_timestamps[k]) for k in + self.task_last_timestamps} self.scheduled_meetings = [(int(l), int(m), convert_to_date(r)) for l, m, r in self.scheduled_meetings] self.archived_meetings = [(int(l), int(m), convert_to_date(r)) for l, m, r in self.archived_meetings] return self @@ -98,7 +103,7 @@ class Community: for l, m, r in self.scheduled_meetings) def schedule_meetings(self): - random.shuffle(self.pool) # It's called RandomTea for a reason + random.shuffle(self.pool) # It's called RandomTea for a reason today = date.today() while len(self.pool) > 1: user_id_1 = self.pool.pop()