From 75ff0a68ff3837d3ad3681eea8fba49c341639bf Mon Sep 17 00:00:00 2001 From: ennucore Date: Tue, 21 Jun 2022 21:36:44 +0300 Subject: [PATCH] Polling users, sending info --- bot.py | 19 +++++++++++++++++-- main.py | 2 ++ utils.py | 4 +++- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/bot.py b/bot.py index 84b7e47..470627c 100644 --- a/bot.py +++ b/bot.py @@ -53,8 +53,7 @@ class Bot(telebot.TeleBot): 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, keyboard = self.templates.separate_text_and_keyboards(text) - keyboard = utils.create_keyboard(keyboard) if keyboard is not None \ - else self.main_keyboard.get(user.locale, self.main_keyboard.get('ru')) + keyboard = utils.create_keyboard(keyboard) return text, keyboard 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() 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): if isinstance(message, telebot.types.CallbackQuery): message = message.message diff --git a/main.py b/main.py index c2ed24b..f6d3fda 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,6 @@ from bot import Bot from views import views +from threading import Thread import traceback @@ -14,6 +15,7 @@ config = { bot = Bot(config) views(bot) if __name__ == '__main__': + Thread(target=bot.actions_loop).start() while True: try: bot.polling(none_stop=True) diff --git a/utils.py b/utils.py index aa93c29..35bb34a 100644 --- a/utils.py +++ b/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) -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) markup = telebot.types.InlineKeyboardMarkup() if is_inline else telebot.types.ReplyKeyboardMarkup( resize_keyboard=True)