Browse Source

Checking if users have already met, small fixes

master
Lev 3 years ago
parent
commit
7e57b75bf3
  1. 2
      README.md
  2. 9
      bot.py
  3. 10
      community.py

2
README.md

@ -12,6 +12,6 @@ Random meetings for chats, kind of like RandomCoffee.
4. The bot sends the matches and links to their Telegram accounts
TODO:
- Improve scheduling: don't add meetings if the users have already met each other
- Auto-scheduling: run it at every event
- A button to request a meeting
- Add commands like info

9
bot.py

@ -35,9 +35,6 @@ class Bot(telebot.TeleBot):
for template_file in config.get('template_files', []):
self.templates.load_file(template_file)
self.main_keyboard = None
if config.get('main_keyboard') is not None:
kb = config.get('main_keyboard')
self.main_keyboard = {key: utils.create_keyboard(kb[key]) for key in kb.keys()}
def render_template(self, template_name: str, locale: str = 'ru', **kwargs) -> \
typing.Tuple[str, typing.Union[telebot.types.ReplyKeyboardMarkup, telebot.types.InlineKeyboardMarkup]]:
@ -120,7 +117,6 @@ class Bot(telebot.TeleBot):
self.db.users.replace_one({'user_id': user.user_id}, user.dict())
def save_community(self, community: Community):
print(community.dict())
self.db.communities.replace_one({'chat_id': community.chat_id}, community.dict())
def handle_commands(self, cmds: typing.List[str]):
@ -209,6 +205,11 @@ class Bot(telebot.TeleBot):
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)
def community_post_edit_routine(self, community: Community):
community.schedule_meetings()
self.send_meeting_info_in_community(community)
self.save_community(community)
def register_next_step_handler(self, message, callback, *args, **kwargs):
if isinstance(message, telebot.types.CallbackQuery):
message = message.message

10
community.py

@ -89,11 +89,19 @@ class Community:
self.default_answers[user_id] = bool(answer)
bot.save_community(self)
def have_already_met(self, user_id_1: int, user_id_2: int) -> bool:
"""
return True if there is a meeting with these users not earlier than 6 weeks ago
"""
return any(
{l, m} == {user_id_1, user_id_2} and r > date.today() - timedelta(days=42)
for l, m, r in self.scheduled_meetings)
def schedule_meetings(self):
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()
while (user_id_2 := self.pool.pop()) == user_id_1:
while (user_id_2 := self.pool.pop()) == user_id_1 or self.have_already_met(user_id_1, user_id_2):
continue
self.scheduled_meetings.append((user_id_1, user_id_2, today))

Loading…
Cancel
Save