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: if user is None:
continue continue
def_answer = community.default_answers.get(user_id, True) 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.pool += [user_id] * def_answer
community.polled[user_id] = datetime.date.today() community.polled[user_id] = datetime.date.today()
self.save_community(community) self.save_community(community)

4
community.py

@ -67,7 +67,7 @@ class Community:
def add_member(self, user_id: int, bot: Bot) -> bool: def add_member(self, user_id: int, bot: Bot) -> bool:
chat_member_info: ChatMember = bot.get_chat_member(self.chat_id, user_id) 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) self.members.append(user_id)
bot.save_community(self) bot.save_community(self)
return True 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) 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) answer = bool(answer)
if user_id not in self.users_to_poll(): if user_id not in self.users_to_poll():
return return

3
templates.py

@ -2,6 +2,7 @@ import attr
import typing import typing
import jinja2 import jinja2
import emoji_data_python as edp 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: def render_template(self, template_name: str, locale: str, **kwargs) -> str:
return edp.replace_colons( return edp.replace_colons(
self.get_template(template_name, locale) 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): def add_template(self, template_string: str):
template_name = template_string.split('||<|')[1].split('|>||')[0].strip() template_name = template_string.split('||<|')[1].split('|>||')[0].strip()

24
templates.txt

@ -19,38 +19,36 @@ Hello, <b>{{ tg_user.first_name }}</b>!
||<| welcome |>|| ||<| welcome |>||
//| en |// //| en |//
Thank you for adding RandomTea to your community! 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 }} |<< >>| Join :-: https://t.me/ranteabot?start=comm-{{ community.chat_id }} |<<
//| ru |// //| ru |//
{# todo #} Спасибо за добавление RandomTea в ваше сообщество!
Спасибо за добавление RandomTea в вашу сообщество! Нажмите на кнопку ниже👇, чтобы присоединиться к RandomTea.
Теперь пользователи вашего сообщества могут нажать на кнопку ниже👇, чтобы присоединиться к RandomTea.
>>| Присоединиться :-: https://t.me/ranteabot?start=comm-{{ community.chat_id }} |<< >>| Присоединиться :-: https://t.me/ranteabot?start=comm-{{ community.chat_id }} |<<
||<| community_added |>|| ||<| community_added |>||
//| en |// //| en |//
{# todo: name #} You have joined the community{% if community.name %} <b>{{ community.name }}</b>{% endif %}, congrats!
You have joined the community, congrats!
//| ru |// //| ru |//
Вы присоединились к сообществу, хорошего дня! Вы присоединились к сообществу{% if community.name %} <b>{{ community.name }}</b>{% endif %}, поздравляем!
||<| poll_user |>|| ||<| poll_user |>||
//| en |// //| en |//
Do you want to have any meetings this week{% if community.name %} in the community <b>{{ community.name }}</b>{% endif %}? 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 |<< >>| {{ "✅" * bool(answer) }} Yes :-: meetings_{{ community.chat_id }}_1 |<<
>>| {{ "✅" * (not def_answer) }} No :-: meetings_{{ community.chat_id }}_0 |<< >>| {{ "✅" * (not answer) }} No :-: meetings_{{ community.chat_id }}_0 |<<
//| ru |// //| ru |//
Хотите ли вы сходить на встречи на этой неделе{% if community.name %} в сообществе <b>{{ community.name }}</b>{% endif %}? Хотите ли вы сходить на встречи на этой неделе{% if community.name %} в сообществе <b>{{ community.name }}</b>{% endif %}?
>>| {{ "✅" * bool(def_answer) }} Да :-: meetings_{{ community.chat_id }}_1 |<< >>| {{ "✅" * bool(answer) }} Да :-: meetings_{{ community.chat_id }}_1 |<<
>>| {{ "✅" * (not def_answer) }} Нет :-: meetings_{{ community.chat_id }}_0 |<< >>| {{ "✅" * (not answer) }} Нет :-: meetings_{{ community.chat_id }}_0 |<<
||<| meeting_info |>|| ||<| meeting_info |>||
//| en |// //| 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 You should meet in person or online. It is recommended to contact your partner in advance
//| ru |// //| 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> чая. Рекомендуем написать человеку заранее Вам предлагается встретиться лично за чашкой <s>кофе</s> чая. Рекомендуем написать человеку заранее

8
utils.py

@ -2,6 +2,7 @@ import telebot
import typing import typing
import random import random
import string import string
from telebot.types import Chat
def generate_id() -> str: def generate_id() -> str:
@ -40,3 +41,10 @@ def create_keyboard(rows: typing.List[typing.List[str]]):
else: else:
markup.row(*(telebot.types.KeyboardButton(text) for text in row)) markup.row(*(telebot.types.KeyboardButton(text) for text in row))
return markup 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') @bot.handle_callback('meetings')
def handle_meetings_answer(query, user, args): def handle_meetings_answer(query, user, args):
community_id, answer = args.split('_') community_id, answer = args.split('_')
answer = int(answer)
community = Community.by_id(int(community_id), bot) community = Community.by_id(int(community_id), bot)
community.add_answer(user.user_id, int(answer), bot) community.add_answer(user.user_id, int(answer), bot)
bot.answer_callback_query(query.id, text='👍👍👍') # edit the keyboard
# todo: edit the keyboard bot.edit_message_with_template(query, 'poll_user', community=community, answer=answer)
@bot.handle_commands(['/send_polls']) @bot.handle_commands(['/send_polls'])
def send_polls(msg, _user, _args): def send_polls(msg, _user, _args):

Loading…
Cancel
Save