Create bic_bot and handle telegram roles and privileges
This commit is contained in:
parent
8fa1737faf
commit
bce5c285a3
68
.gitignore
vendored
Normal file
68
.gitignore
vendored
Normal file
@ -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/
|
0
bic_bot/__init__.py
Normal file
0
bic_bot/__init__.py
Normal file
3
bic_bot/__main__.py
Normal file
3
bic_bot/__main__.py
Normal file
@ -0,0 +1,3 @@
|
||||
from . import bot
|
||||
|
||||
bot.run()
|
23
bic_bot/authorization.py
Normal file
23
bic_bot/authorization.py
Normal file
@ -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)
|
95
bic_bot/bot.py
Normal file
95
bic_bot/bot.py
Normal file
@ -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)
|
0
bic_bot/data/__init__.py
Normal file
0
bic_bot/data/__init__.py
Normal file
58
bic_bot/messages.py
Normal file
58
bic_bot/messages.py
Normal file
@ -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': "<b>Choose a language</b>",
|
||||
'it': "<b>Seleziona una lingua</b>"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
supported_languages = {
|
||||
'en': {
|
||||
'flag': '🇬🇧',
|
||||
'name': 'English'
|
||||
},
|
||||
'it': {
|
||||
'flag': '🇮🇹',
|
||||
'name': 'Italiano'
|
||||
}
|
||||
}
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
davtelepot
|
29
run_me.sh
Normal file
29
run_me.sh
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user