Browse Source

More templates, write usernames, properly edit the keyboard

master
Lev 2 years ago
parent
commit
5a8ae239ff
  1. 2
      bot.py
  2. 4
      community.py
  3. 3
      templates.py
  4. 24
      templates.txt
  5. 8
      utils.py
  6. 5
      views.py

2
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)

4
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

3
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()

24
templates.txt

@ -19,38 +19,36 @@ Hello, <b>{{ tg_user.first_name }}</b>!
||<| 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 %} <b>{{ community.name }}</b>{% endif %}, congrats!
//| ru |//
Вы присоединились к сообществу, хорошего дня!
Вы присоединились к сообществу{% if community.name %} <b>{{ community.name }}</b>{% endif %}, поздравляем!
||<| poll_user |>||
//| en |//
Do you want to have any meetings this week{% if community.name %} in the community <b>{{ community.name }}</b>{% 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 %} в сообществе <b>{{ community.name }}</b>{% 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 <a href="tg://user?id={{ person.id }}">{{ person.first_name }}</a> from <i>{{ community.name }}</i>.
You have a meeting this week 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 |//
На этой неделе у Вас встреча с <a href="tg://user?id={{ person.id }}">{{ person.first_name }}</a> из <i>{{ community.name }}</i>.
На этой неделе у Вас встреча с {{ print_user_link(person) }} из <i>{{ community.name }}</i>.
Вам предлагается встретиться лично за чашкой <s>кофе</s> чая. Рекомендуем написать человеку заранее

8
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'<a href="tg://user?id={person.id}">{person.first_name}</a>'
if person.username:
res += f' (@{person.username})'
return res

5
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):

Loading…
Cancel
Save