Browse Source

Polling users, sending info

master
Lev 2 years ago
parent
commit
75ff0a68ff
  1. 19
      bot.py
  2. 2
      main.py
  3. 4
      utils.py

19
bot.py

@ -53,8 +53,7 @@ class Bot(telebot.TeleBot):
typing.Tuple[str, typing.Union[telebot.types.ReplyKeyboardMarkup, telebot.types.InlineKeyboardMarkup]]: typing.Tuple[str, typing.Union[telebot.types.ReplyKeyboardMarkup, telebot.types.InlineKeyboardMarkup]]:
text: str = self.templates.render_template(template_name, user.locale, user=user, **kwargs) text: str = self.templates.render_template(template_name, user.locale, user=user, **kwargs)
text, keyboard = self.templates.separate_text_and_keyboards(text) text, keyboard = self.templates.separate_text_and_keyboards(text)
keyboard = utils.create_keyboard(keyboard) if keyboard is not None \ keyboard = utils.create_keyboard(keyboard)
else self.main_keyboard.get(user.locale, self.main_keyboard.get('ru'))
return text, keyboard return text, keyboard
def edit_message_with_template(self, query: telebot.types.CallbackQuery, template: str, **kwargs): def edit_message_with_template(self, query: telebot.types.CallbackQuery, template: str, **kwargs):
@ -224,6 +223,22 @@ class Bot(telebot.TeleBot):
community.task_last_timestamps['scheduling'] = datetime.date.today() community.task_last_timestamps['scheduling'] = datetime.date.today()
self.save_community(community) self.save_community(community)
def actions_loop(self):
while True:
try:
for community in self.iter_communities():
try:
self.run_necessary_actions_for_community(community)
except KeyboardInterrupt as e:
raise e
except Exception:
print(f'An exception occurred: {traceback.format_exc()}')
except KeyboardInterrupt as e:
raise e
except Exception:
print(f'An exception occurred while iterating through communities: {traceback.format_exc()}')
time.sleep(60)
def register_next_step_handler(self, message, callback, *args, **kwargs): def register_next_step_handler(self, message, callback, *args, **kwargs):
if isinstance(message, telebot.types.CallbackQuery): if isinstance(message, telebot.types.CallbackQuery):
message = message.message message = message.message

2
main.py

@ -1,5 +1,6 @@
from bot import Bot from bot import Bot
from views import views from views import views
from threading import Thread
import traceback import traceback
@ -14,6 +15,7 @@ config = {
bot = Bot(config) bot = Bot(config)
views(bot) views(bot)
if __name__ == '__main__': if __name__ == '__main__':
Thread(target=bot.actions_loop).start()
while True: while True:
try: try:
bot.polling(none_stop=True) bot.polling(none_stop=True)

4
utils.py

@ -28,7 +28,9 @@ def create_inline_button(text: str, callback_data: str) -> telebot.types.InlineK
return telebot.types.InlineKeyboardButton(text, callback_data=callback_data) return telebot.types.InlineKeyboardButton(text, callback_data=callback_data)
def create_keyboard(rows: typing.List[typing.List[str]]): def create_keyboard(rows: typing.Optional[typing.List[typing.List[str]]]):
if rows is None:
return None
is_inline = not isinstance(rows[0][0], str) is_inline = not isinstance(rows[0][0], str)
markup = telebot.types.InlineKeyboardMarkup() if is_inline else telebot.types.ReplyKeyboardMarkup( markup = telebot.types.InlineKeyboardMarkup() if is_inline else telebot.types.ReplyKeyboardMarkup(
resize_keyboard=True) resize_keyboard=True)

Loading…
Cancel
Save