Browse Source

Getting communities for a user, started writing meeting requests

master
Lev 3 years ago
parent
commit
71f80b2618
  1. 2
      README.md
  2. 9
      community.py
  3. 15
      templates.txt
  4. 13
      user.py
  5. 15
      views.py

2
README.md

@ -12,8 +12,6 @@ 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 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"

9
community.py

@ -70,6 +70,12 @@ class Community:
Thread(target=upd_data).start()
return data
def can_user_request_a_meeting(self, user_id: int) -> bool:
# Check if there isn't a meeting in archived_meetings with its scheduling date today or yesterday
return not any(
r in [date.today(), date.today() - timedelta(days=1)] and user_id in [l, m]
for l, m, r in self.scheduled_meetings + self.archived_meetings)
def add_member(self, user_id: int, bot: Bot) -> bool:
chat_member_info: ChatMember = bot.get_chat_member(self.chat_id, user_id)
if chat_member_info.is_member or chat_member_info.status in ['creator', 'administrator', 'admin', 'member']:
@ -81,7 +87,8 @@ class Community:
def users_to_poll(self) -> typing.List[int]:
return [
user_id for user_id in self.members
if user_id not in self.polled or self.polled[user_id] <= date.today() - timedelta(days=7)
if (user_id not in self.polled or self.polled[user_id] <= date.today() - timedelta(days=7))
and self.can_user_request_a_meeting(user_id)
]
def add_answer(self, user_id: typing.Union[int, bool], answer: bool, bot: Bot):

15
templates.txt

@ -45,12 +45,23 @@ Do you want to have any meetings this week{% if community.name %} in the communi
||<| meeting_info |>||
//| en |//
You have a meeting this week with {{ print_user_link(person) }} from <i>{{ community.name }}</i>.
You have a meeting with {{ print_user_link(person) }} from <i>{{ community.name }}</i>.
You should meet in person or online. It is recommended to contact your partner in advance
//| ru |//
На этой неделе у Вас встреча с {{ print_user_link(person) }} из <i>{{ community.name }}</i>.
У Вас встреча с {{ print_user_link(person) }} из <i>{{ community.name }}</i>.
Вам предлагается встретиться лично за чашкой <s>кофе</s> чая. Рекомендуем написать человеку заранее
||<| request_meeting |>||
//| en |//
What community do you want to have a meeting in?
{% for id, name in comm_list %}
>>| {{ name }} :-: request_meeting_{{ id }} |<<
{% endfor %}
//| ru |//
В каком сообществе вы хотите встретиться?
{% for id, name in comm_list %}
>>| {{ name }} :-: request_meeting_{{ id }} |<<
{% endfor %}
||<| canceled |>||
//| en |//

13
user.py

@ -3,6 +3,10 @@ from dataclasses import dataclass, field, asdict
import typing
import time
if typing.TYPE_CHECKING:
from bot import Bot
from community import Community
@dataclass
class User:
@ -38,3 +42,12 @@ class User:
def is_admin(self) -> bool:
return self.user_id in [218952152]
def get_communities(self, bot: Bot) -> typing.Iterable[Community]:
"""
Returns a list of community ids for which the user is a member
Perform a MongoDB query to get the list of communities for which the user is a member
"""
for community in bot.db.communities.find({'members': self.user_id}):
yield Community.from_dict(community)

15
views.py

@ -23,6 +23,21 @@ def views(bot: Bot):
def handle_help(msg, _user, _args):
bot.reply_with_template(msg, 'help')
@bot.handle_commands(['/request', '📝 Request a meeting', '📝 Запросить встречу'])
def request_meeting(msg, user, _args):
comm_ids_and_names = [(community.chat_id, community.name) for community in user.communities]
bot.reply_with_template(msg, 'request_meeting', comm_list=comm_ids_and_names)
@bot.handle_callback('request_meeting')
def request_meeting_callback(msg, user, args):
community = Community.by_id(int(args), bot)
if community.can_user_request_a_meeting(user.user_id):
# todo: request the meeting and write the template
bot.reply_with_template(msg, 'request_meeting_success', community=community)
else:
bot.reply_with_template(msg, 'request_meeting_failure', community=community)
bot.answer_callback_query(msg.id)
@bot.handle_commands(['/send_all'])
def send_spam(msg, user, _args):
if not user.is_admin():

Loading…
Cancel
Save