|
@@ -0,0 +1,95 @@
|
|
|
|
+import logging
|
|
|
|
+import os
|
|
|
|
+import sys
|
|
|
|
+
|
|
|
|
+import davtelepot.bot
|
|
|
|
+from davtelepot.messages import (default_unknown_command_message as unknown_command_message,
|
|
|
|
+ default_authorization_denied_message as authorization_denied_message)
|
|
|
|
+
|
|
|
|
+from . import authorization
|
|
|
|
+from .messages import language_messages, supported_languages
|
|
|
|
+
|
|
|
|
+current_path = os.path.dirname(
|
|
|
|
+ os.path.abspath(
|
|
|
|
+ __file__
|
|
|
|
+ )
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def append_to_passwords_file(line_to_append):
|
|
|
|
+ with open(f'{current_path}/data/passwords.py', 'a') as passwords_file:
|
|
|
|
+ passwords_file.write(line_to_append)
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+try:
|
|
|
|
+ from .data.passwords import telegram_token
|
|
|
|
+ if not telegram_token:
|
|
|
|
+ raise ImportError
|
|
|
|
+except ImportError as e:
|
|
|
|
+ try:
|
|
|
|
+ telegram_token = input("Enter telegram bot API token:\n"
|
|
|
|
+ "For more information: https://core.telegram.org/bots/\n\t\t")
|
|
|
|
+ append_to_passwords_file(f'telegram_token = "{telegram_token}"\n')
|
|
|
|
+ except KeyboardInterrupt:
|
|
|
|
+ logging.error("Telegram bot token not provided, aborting...")
|
|
|
|
+ sys.exit(1)
|
|
|
|
+
|
|
|
|
+bic_bot = davtelepot.bot.Bot(token=telegram_token,
|
|
|
|
+ database_url=f'bic_bot/data/bot.db')
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def run():
|
|
|
|
+ try:
|
|
|
|
+ from .data.config import log_file_name
|
|
|
|
+ except ImportError:
|
|
|
|
+ log_file_name = 'bic_bot.log'
|
|
|
|
+ try:
|
|
|
|
+ from .data.config import errors_file_name
|
|
|
|
+ except ImportError:
|
|
|
|
+ errors_file_name = 'bic_bot.errors.log'
|
|
|
|
+
|
|
|
|
+ log_file = f"{current_path}/data/{log_file_name}"
|
|
|
|
+ errors_file = f"{current_path}/data/{errors_file_name}"
|
|
|
|
+
|
|
|
|
+ # Outputs the log in console, log_file and errors_file
|
|
|
|
+ # Log formatter: datetime, module name (filled with spaces up to 15
|
|
|
|
+ # characters), logging level name (filled to 8), message
|
|
|
|
+ # noinspection SpellCheckingInspection
|
|
|
|
+ log_formatter = logging.Formatter(
|
|
|
|
+ "%(asctime)s [%(module)-15s %(levelname)-8s] %(message)s",
|
|
|
|
+ style='%'
|
|
|
|
+ )
|
|
|
|
+ root_logger = logging.getLogger()
|
|
|
|
+ root_logger.setLevel(logging.DEBUG)
|
|
|
|
+
|
|
|
|
+ file_handler = logging.FileHandler(log_file, mode="a", encoding="utf-8")
|
|
|
|
+ file_handler.setFormatter(log_formatter)
|
|
|
|
+ file_handler.setLevel(logging.DEBUG)
|
|
|
|
+ root_logger.addHandler(file_handler)
|
|
|
|
+
|
|
|
|
+ file_handler = logging.FileHandler(errors_file, mode="a", encoding="utf-8")
|
|
|
|
+ file_handler.setFormatter(log_formatter)
|
|
|
|
+ file_handler.setLevel(logging.ERROR)
|
|
|
|
+ root_logger.addHandler(file_handler)
|
|
|
|
+
|
|
|
|
+ console_handler = logging.StreamHandler()
|
|
|
|
+ console_handler.setFormatter(log_formatter)
|
|
|
|
+ console_handler.setLevel(logging.DEBUG)
|
|
|
|
+ root_logger.addHandler(console_handler)
|
|
|
|
+ bic_bot.set_path(current_path)
|
|
|
|
+ bic_bot.set_class_log_file_name(log_file_name)
|
|
|
|
+ bic_bot.set_class_errors_file_name(errors_file_name)
|
|
|
|
+ bic_bot.set_unknown_command_message(
|
|
|
|
+ unknown_command_message
|
|
|
|
+ )
|
|
|
|
+ bic_bot.set_authorization_denied_message(
|
|
|
|
+ authorization_denied_message
|
|
|
|
+ )
|
|
|
|
+ davtelepot.authorization.init(telegram_bot=bic_bot)
|
|
|
|
+ authorization.init(telegram_bot=bic_bot)
|
|
|
|
+ davtelepot.administration_tools.init(telegram_bot=bic_bot)
|
|
|
|
+ davtelepot.languages.init(telegram_bot=bic_bot,
|
|
|
|
+ language_messages=language_messages,
|
|
|
|
+ supported_languages=supported_languages)
|
|
|
|
+ exit_code = bic_bot.run()
|
|
|
|
+ sys.exit(exit_code)
|