From 5a8ae239ff3adf54b299d4a5970427f0817ef551 Mon Sep 17 00:00:00 2001 From: ennucore Date: Tue, 21 Jun 2022 20:42:45 +0300 Subject: [PATCH] More templates, write usernames, properly edit the keyboard --- bot.py | 2 +- community.py | 4 ++-- templates.py | 3 ++- templates.txt | 24 +++++++++++------------- utils.py | 8 ++++++++ views.py | 5 +++-- 6 files changed, 27 insertions(+), 19 deletions(-) diff --git a/bot.py b/bot.py index e61e358..739cb01 100644 --- a/bot.py +++ b/bot.py @@ -198,7 +198,7 @@ class Bot(telebot.TeleBot): if user is None: continue def_answer = community.default_answers.get(user_id, True) - self.send_template(user_id, 'poll_user', community=community, def_answer=def_answer) + self.send_template(user_id, 'poll_user', community=community, answer=def_answer) community.pool += [user_id] * def_answer community.polled[user_id] = datetime.date.today() self.save_community(community) diff --git a/community.py b/community.py index 831e4e9..a5d1213 100644 --- a/community.py +++ b/community.py @@ -67,7 +67,7 @@ class Community: 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']: + if chat_member_info.is_member or chat_member_info.status in ['creator', 'administrator', 'admin', 'member']: self.members.append(user_id) bot.save_community(self) return True @@ -79,7 +79,7 @@ class Community: if user_id not in self.polled or self.polled[user_id] <= date.today() - timedelta(days=7) ] - def add_answer(self, user_id: int, answer: bool, bot: Bot): + def add_answer(self, user_id: typing.Union[int, bool], answer: bool, bot: Bot): answer = bool(answer) if user_id not in self.users_to_poll(): return diff --git a/templates.py b/templates.py index db0d290..ae962af 100644 --- a/templates.py +++ b/templates.py @@ -2,6 +2,7 @@ import attr import typing import jinja2 import emoji_data_python as edp +from utils import print_user_link """ @@ -37,7 +38,7 @@ class TemplateProvider: def render_template(self, template_name: str, locale: str, **kwargs) -> str: return edp.replace_colons( self.get_template(template_name, locale) - .render(**kwargs, **__builtins__)).strip() + .render(print_user_link=print_user_link, **kwargs, **__builtins__)).strip() def add_template(self, template_string: str): template_name = template_string.split('||<|')[1].split('|>||')[0].strip() diff --git a/templates.txt b/templates.txt index a0e3aa4..bf70b0d 100644 --- a/templates.txt +++ b/templates.txt @@ -19,38 +19,36 @@ Hello, {{ tg_user.first_name }}! ||<| welcome |>|| //| en |// Thank you for adding RandomTea to your community! -Now, the members of your community that wish to participate can press the button below👇. +Press the button below👇 to participate in meetings. >>| Join :-: https://t.me/ranteabot?start=comm-{{ community.chat_id }} |<< //| ru |// -{# todo #} -Спасибо за добавление RandomTea в вашу сообщество! -Теперь пользователи вашего сообщества могут нажать на кнопку ниже👇, чтобы присоединиться к RandomTea. +Спасибо за добавление RandomTea в ваше сообщество! +Нажмите на кнопку ниже👇, чтобы присоединиться к RandomTea. >>| Присоединиться :-: https://t.me/ranteabot?start=comm-{{ community.chat_id }} |<< ||<| community_added |>|| //| en |// -{# todo: name #} -You have joined the community, congrats! +You have joined the community{% if community.name %} {{ community.name }}{% endif %}, congrats! //| ru |// -Вы присоединились к сообществу, хорошего дня! +Вы присоединились к сообществу{% if community.name %} {{ community.name }}{% endif %}, поздравляем! ||<| poll_user |>|| //| en |// Do you want to have any meetings this week{% if community.name %} in the community {{ community.name }}{% endif %}? ->>| {{ "✅" * bool(def_answer) }} Yes :-: meetings_{{ community.chat_id }}_1 |<< ->>| {{ "✅" * (not def_answer) }} No :-: meetings_{{ community.chat_id }}_0 |<< +>>| {{ "✅" * bool(answer) }} Yes :-: meetings_{{ community.chat_id }}_1 |<< +>>| {{ "✅" * (not answer) }} No :-: meetings_{{ community.chat_id }}_0 |<< //| ru |// Хотите ли вы сходить на встречи на этой неделе{% if community.name %} в сообществе {{ community.name }}{% endif %}? ->>| {{ "✅" * bool(def_answer) }} Да :-: meetings_{{ community.chat_id }}_1 |<< ->>| {{ "✅" * (not def_answer) }} Нет :-: meetings_{{ community.chat_id }}_0 |<< +>>| {{ "✅" * bool(answer) }} Да :-: meetings_{{ community.chat_id }}_1 |<< +>>| {{ "✅" * (not answer) }} Нет :-: meetings_{{ community.chat_id }}_0 |<< ||<| meeting_info |>|| //| en |// -You have a meeting this week with {{ person.first_name }} from {{ community.name }}. +You have a meeting this week with {{ print_user_link(person) }} from {{ community.name }}. You should meet in person or online. It is recommended to contact your partner in advance //| ru |// -На этой неделе у Вас встреча с {{ person.first_name }} из {{ community.name }}. +На этой неделе у Вас встреча с {{ print_user_link(person) }} из {{ community.name }}. Вам предлагается встретиться лично за чашкой кофе чая. Рекомендуем написать человеку заранее diff --git a/utils.py b/utils.py index 9a12ef5..aa93c29 100644 --- a/utils.py +++ b/utils.py @@ -2,6 +2,7 @@ import telebot import typing import random import string +from telebot.types import Chat def generate_id() -> str: @@ -40,3 +41,10 @@ def create_keyboard(rows: typing.List[typing.List[str]]): else: markup.row(*(telebot.types.KeyboardButton(text) for text in row)) return markup + + +def print_user_link(person: Chat): + res = f'{person.first_name}' + if person.username: + res += f' (@{person.username})' + return res diff --git a/views.py b/views.py index 899da49..ee3fc48 100644 --- a/views.py +++ b/views.py @@ -57,10 +57,11 @@ def views(bot: Bot): @bot.handle_callback('meetings') def handle_meetings_answer(query, user, args): community_id, answer = args.split('_') + answer = int(answer) community = Community.by_id(int(community_id), bot) community.add_answer(user.user_id, int(answer), bot) - bot.answer_callback_query(query.id, text='👍👍👍') - # todo: edit the keyboard + # edit the keyboard + bot.edit_message_with_template(query, 'poll_user', community=community, answer=answer) @bot.handle_commands(['/send_polls']) def send_polls(msg, _user, _args):