From bce5c285a38435678fade11cea909f4a3f4402ec Mon Sep 17 00:00:00 2001 From: Davte Date: Sat, 19 Jun 2021 16:18:44 +0200 Subject: [PATCH] Create bic_bot and handle telegram roles and privileges --- .gitignore | 68 ++++++++++++++++++++++++++++ README.md | 4 +- bic_bot/__init__.py | 0 bic_bot/__main__.py | 3 ++ bic_bot/authorization.py | 23 ++++++++++ bic_bot/bot.py | 95 ++++++++++++++++++++++++++++++++++++++++ bic_bot/data/__init__.py | 0 bic_bot/messages.py | 58 ++++++++++++++++++++++++ requirements.txt | 1 + run_me.sh | 29 ++++++++++++ 10 files changed, 279 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 bic_bot/__init__.py create mode 100644 bic_bot/__main__.py create mode 100644 bic_bot/authorization.py create mode 100644 bic_bot/bot.py create mode 100644 bic_bot/data/__init__.py create mode 100644 bic_bot/messages.py create mode 100644 requirements.txt create mode 100644 run_me.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..56e25d5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,68 @@ +# local_* files +local_* +my_config.sh + +# Data folder +bic_bot/data/* +!bic_bot/data/__init__.py + +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class +.ipynb_checkpoints/ + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover + +# Translations +*.mo +*.pot + +# Django stuff: +*.log + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ diff --git a/README.md b/README.md index 968a45f..c1baeab 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# bibot +# bic_bot -Breaking Italy bot \ No newline at end of file +Breaking Italy Club bot \ No newline at end of file diff --git a/bic_bot/__init__.py b/bic_bot/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bic_bot/__main__.py b/bic_bot/__main__.py new file mode 100644 index 0000000..0abbe90 --- /dev/null +++ b/bic_bot/__main__.py @@ -0,0 +1,3 @@ +from . import bot + +bot.run() diff --git a/bic_bot/authorization.py b/bic_bot/authorization.py new file mode 100644 index 0000000..861281e --- /dev/null +++ b/bic_bot/authorization.py @@ -0,0 +1,23 @@ +import davtelepot + +from .messages import elevation_messages + + +async def elevate_to_admin(bot: davtelepot.bot.Bot, language: str, user_record: dict): + if len(bot.administrators) < 1: + user_record['privileges'] = 1 + bot.db['users'].upsert( + user_record, + ['id'] + ) + return bot.get_message('elevation', 'granted', language=language) + return bot.get_message('elevation', 'denied', language=language) + + +def init(telegram_bot: davtelepot.bot.Bot): + telegram_bot.messages['elevation'] = elevation_messages + + @telegram_bot.command(command='elevate', + authorization_level='everybody') + async def _elevate_to_admin(bot, language, user_record): + return await elevate_to_admin(bot=bot, language=language, user_record=user_record) \ No newline at end of file diff --git a/bic_bot/bot.py b/bic_bot/bot.py new file mode 100644 index 0000000..b9ec149 --- /dev/null +++ b/bic_bot/bot.py @@ -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) diff --git a/bic_bot/data/__init__.py b/bic_bot/data/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bic_bot/messages.py b/bic_bot/messages.py new file mode 100644 index 0000000..9a708f9 --- /dev/null +++ b/bic_bot/messages.py @@ -0,0 +1,58 @@ +elevation_messages = { + 'denied': { + 'en': "You have no right elevate yourself to Founder! 🚫", + 'it': "Non hai diritto a ottenere i privilegi di Fondatore! 🚫", + }, + 'granted': { + 'en': "You have been elevated to Founder! 👑", + 'it': "Ora sei Fondatore! 👑", + }, +} + +language_messages = { + 'language_command': { + 'name': { + 'en': "/language", + 'it': "/lingua" + }, + 'reply_keyboard_button': { + 'en': "Language 🗣", + 'it': "Lingua 🗣" + }, + 'alias': { + 'en': "Language 🗣", + 'it': "Lingua 🗣" + }, + 'description': { + 'en': "Change language settings", + 'it': "Cambia le impostazioni della lingua" + } + }, + 'language_button': { + 'description': { + 'en': "Change language settings", + 'it': "Cambia le impostazioni della lingua" + }, + 'language_set': { + 'en': "Selected language: English 🇬🇧", + 'it': "Lingua selezionata: Italiano 🇮🇹" + } + }, + 'language_panel': { + 'text': { + 'en': "Choose a language", + 'it': "Seleziona una lingua" + } + } +} + +supported_languages = { + 'en': { + 'flag': '🇬🇧', + 'name': 'English' + }, + 'it': { + 'flag': '🇮🇹', + 'name': 'Italiano' + } +} \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2e56613 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +davtelepot diff --git a/run_me.sh b/run_me.sh new file mode 100644 index 0000000..c3cfcb2 --- /dev/null +++ b/run_me.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# This file must be executable, otherwise the service cannot run it. +# Run `$python_script` while exit code is 65. +# At each iteration, pull news from repository and update dependencies. + +# Get current directory +this_script_directory=$(cd `dirname $0` && pwd); +cd "$this_script_directory"; + +if [ ! -d "$this_script_directory/env" ]; then + python3 -m venv env; + env/bin/pip install -r requirements.txt; +fi + +python_virtual_environment="$this_script_directory/env/bin" +echo "Python script will be run while it exits with value===65."; +i=65; +while [ $i -eq 65 ] +do + echo "Pulling from repository..." + git pull; + echo "Updating dependencies"; + "$python_virtual_environment/pip" install --upgrade --no-cache-dir \ + --no-deps davtelepot; + echo "Running python script"; + "$python_virtual_environment/python" -m bic_bot; + i=$?; +done