Support module execution

(cherry picked from commit bb9e8b5cc89abd470f3e19b538e7498779ff08b4)
This commit is contained in:
Davte 2020-05-03 16:56:59 +02:00
parent 9d2f9b06dc
commit 80adecfd64
6 changed files with 144 additions and 48 deletions

View File

@ -11,6 +11,18 @@ Send [`/start`](https://t.me/ciclopibot?start=00help) [@CicloPiBot](https://t.me
* Ask for `/ciclopi` information * Ask for `/ciclopi` information
### "Server" side ### "Server" side
You may choose between method 1 (`pip`) and method 2 (`git`).
1. Using `pip`
* Install ciclopibot
```bash
pip install ciclopibot
```
* Run ciclopibot as module
```bash
python -m ciclopibot -h # Get help
python -m ciclopibot <your_token_here>
```
1. Using `git`
* Clone this repository * Clone this repository
```bash ```bash
git clone ssh://git@gogs.davte.it:8445/Davte/ciclopibot.git git clone ssh://git@gogs.davte.it:8445/Davte/ciclopibot.git

View File

@ -3,7 +3,7 @@
__author__ = "Davide Testa" __author__ = "Davide Testa"
__email__ = "davide@davte.it" __email__ = "davide@davte.it"
__license__ = "GNU General Public License v3.0" __license__ = "GNU General Public License v3.0"
__version__ = "1.1.17" __version__ = "1.2.0"
__maintainer__ = "Davide Testa" __maintainer__ = "Davide Testa"
__contact__ = "t.me/davte" __contact__ = "t.me/davte"

53
ciclopibot/__main__.py Normal file
View File

@ -0,0 +1,53 @@
"""Run a local copy of CicloPiBot."""
# Standard library modules
import argparse
# Project modules
from . import bot
def main():
# Parse command-line arguments
cli_parser = argparse.ArgumentParser(description=__doc__,
allow_abbrev=False)
cli_parser.add_argument('--bot_token', '--token', '--t', type=str,
default=None,
required=False,
help='telegram bot token')
cli_parser.add_argument('--path', type=str,
default=None,
required=False,
help='path where data should be stored')
cli_parser.add_argument('--log_file_name', '--log', type=str,
default=None,
required=False,
help='file to store full log')
cli_parser.add_argument('--errors_file_name', '--err', type=str,
default=None,
required=False,
help='file to store error log')
cli_parser.add_argument('--local_host', '--host', type=str,
default=None,
required=False,
help='local host address for linked web-app')
cli_parser.add_argument('--port', type=int,
default=None,
required=False,
help='local host port for linked web-app')
cli_parser.add_argument('--hostname', type=str,
default=None,
required=False,
help='host name for webhooks')
cli_parser.add_argument('--certificate', type=str,
default=None,
required=False,
help='certificate for webhooks')
cli_arguments = vars(cli_parser.parse_args())
bot.main(
**cli_arguments
)
if __name__ == '__main__':
main()

View File

@ -10,34 +10,64 @@ import davtelepot
# Project modules # Project modules
from . import ciclopi, messages from . import ciclopi, messages
from .data.passwords import bot_token
from .messages import ( from .messages import (
default_help_messages, language_messages, supported_languages default_help_messages, language_messages, supported_languages
) )
def main(): def main(bot_token: str = None,
path: str = None,
log_file_name: str = None,
errors_file_name: str = None,
local_host: str = None,
port: int = None,
hostname: str = None,
certificate: str = None):
if bot_token is None:
try:
from .data.passwords import bot_token
except ImportError:
logging.error(
"Missing bot token. Create a bot with t.me/BotFather and "
"provide its token here to run a local copy of CicloPiBot."
)
return
if path is None:
path = os.path.dirname( path = os.path.dirname(
os.path.abspath( os.path.abspath(
__file__ __file__
) )
) )
if log_file_name is None:
try: try:
from .data.config import log_file_name from .data.config import log_file_name
except ImportError: except ImportError:
log_file_name = 'CicloPi.info.log' log_file_name = 'CicloPi.info.log'
if errors_file_name is None:
try: try:
from .data.config import errors_file_name from .data.config import errors_file_name
except ImportError: except ImportError:
errors_file_name = 'CicloPi.errors.log' errors_file_name = 'CicloPi.errors.log'
if local_host is None:
try: try:
from .data.config import local_host, port from .data.config import local_host
except ImportError: except ImportError:
local_host, port = '127.0.0.1', 3000 local_host = 'localhost'
if port is None:
try: try:
from .data.config import hostname, certificate from .data.config import port
except ImportError: except ImportError:
hostname, certificate = '', None port = 3000
if hostname is None:
try:
from .data.config import hostname
except ImportError:
hostname = ''
if certificate is None:
try:
from .data.config import certificate
except ImportError:
certificate = None
log_file = f"{path}/data/{log_file_name}" log_file = f"{path}/data/{log_file_name}"
errors_file = f"{path}/data/{errors_file_name}" errors_file = f"{path}/data/{errors_file_name}"

View File

@ -1,15 +1,15 @@
#!/bin/bash #!/bin/bash
# Get current directory # Get current directory
this_script_directory=$(cd `dirname $0` && pwd); this_script_directory=$(cd "$(dirname "$0")" && pwd);
cd "$this_script_directory"; cd "$this_script_directory" || exit 1;
configuration_file="$this_script_directory/my_config.sh"; configuration_file="$this_script_directory/my_config.sh";
passwords_file="$this_script_directory/ciclopibot/data/passwords.py"; passwords_file="$this_script_directory/ciclopibot/data/passwords.py";
# Create intermediate path for passwords_file, if it does not exist # Create intermediate path for passwords_file, if it does not exist
mkdir -p "$this_script_directory/ciclopibot/data/"; mkdir -r "$this_script_directory/ciclopibot/data/";
read -p "Enter a name for your virtual environment read -r "Enter a name for your virtual environment
" venv_name; " venv_name;
python3 -m venv "$venv_name"; python3 -m venv "$venv_name";
"$venv_name"/bin/pip install -r "$this_script_directory/requirements.txt"; "$venv_name"/bin/pip install -r "$this_script_directory/requirements.txt";
@ -18,8 +18,8 @@ python3 -m venv "$venv_name";
# Other systems may require a different path. # Other systems may require a different path.
echo "python_virtual_environment=\"$(pwd)/$venv_name/bin\";" >> "$configuration_file"; echo "python_virtual_environment=\"$(pwd)/$venv_name/bin\";" >> "$configuration_file";
echo "python_script=\"$this_script_directory/ciclopibot/bot.py\";" >> "$configuration_file"; echo "python_script=\"$this_script_directory/ciclopibot/bot.py\";" >> "$configuration_file";
read -p "Enter a valid Telegram bot token " bot_token; read -r "Enter a valid Telegram bot token " bot_token;
echo "bot_token = \"$bot_token\"" >> $passwords_file; echo "bot_token = \"$bot_token\"" >> "$passwords_file";
# Run bot # Run bot
bash run_me.sh; bash run_me.sh;

View File

@ -57,6 +57,7 @@ setuptools.setup(
install_requires=[ install_requires=[
'davtelepot', 'davtelepot',
], ],
python_requires='>=3.5',
classifiers=[ classifiers=[
"Development Status :: 5 - Production/Stable", "Development Status :: 5 - Production/Stable",
"Environment :: Console", "Environment :: Console",