Support module execution

This commit is contained in:
Davte 2020-05-03 16:56:59 +02:00
parent 948325cb42
commit bb9e8b5cc8
6 changed files with 144 additions and 48 deletions

View File

@ -11,25 +11,37 @@ Send [`/start`](https://t.me/ciclopibot?start=00help) [@CicloPiBot](https://t.me
* Ask for `/ciclopi` information * Ask for `/ciclopi` information
### "Server" side ### "Server" side
* Clone this repository You may choose between method 1 (`pip`) and method 2 (`git`).
```bash 1. Using `pip`
git clone ssh://git@gogs.davte.it:8445/Davte/ciclopibot.git * Install ciclopibot
# git clone https://gogs.davte.it/Davte/ciclopibot.git ```bash
# git clone git@github.com:Davte/ciclopibot.git pip install ciclopibot
# git clone https://github.com/Davte/ciclopibot.git ```
``` * Run ciclopibot as module
* Run `install.sh`: it will help you perform the following operations. ```bash
* Put a Telegram bot token in gitignored `data/passwords.py` module. python -m ciclopibot -h # Get help
* To get a token, ask [@BotFather](https://t.me/botfather). python -m ciclopibot <your_token_here>
* The bot whose token you use will act as [@CicloPiBot](https://t.me/ciclopibot) as long as you run the script. ```
* Create a python3.5+ virtual environment and install requirements. 1. Using `git`
* Specify `python_virtual_environment` and `python_script` variables in `my_config.sh` * Clone this repository
* Run `run_me.sh` ```bash
```bash git clone ssh://git@gogs.davte.it:8445/Davte/ciclopibot.git
bash run_me.sh; # git clone https://gogs.davte.it/Davte/ciclopibot.git
``` # git clone git@github.com:Davte/ciclopibot.git
* You may edit the file and test your code with your bot. # git clone https://github.com/Davte/ciclopibot.git
* Should you be satisfied of your edits enough, you may fork this repository and open a pull request. ```
* Run `install.sh`: it will help you perform the following operations.
* Put a Telegram bot token in gitignored `data/passwords.py` module.
* To get a token, ask [@BotFather](https://t.me/botfather).
* The bot whose token you use will act as [@CicloPiBot](https://t.me/ciclopibot) as long as you run the script.
* Create a python3.5+ virtual environment and install requirements.
* Specify `python_virtual_environment` and `python_script` variables in `my_config.sh`
* Run `run_me.sh`
```bash
bash run_me.sh;
```
* You may edit the file and test your code with your bot.
* Should you be satisfied of your edits enough, you may fork this repository and open a pull request.
## Credits ## Credits
* [Davte](https://www.davte.it) is the creator and the main author of this repository. * [Davte](https://www.davte.it) is the creator and the main author of this repository.

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 = os.path.dirname( path: str = None,
os.path.abspath( log_file_name: str = None,
__file__ 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(
os.path.abspath(
__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'
try: if errors_file_name is None:
from .data.config import errors_file_name try:
except ImportError: from .data.config import errors_file_name
errors_file_name = 'CicloPi.errors.log' except ImportError:
try: errors_file_name = 'CicloPi.errors.log'
from .data.config import local_host, port if local_host is None:
except ImportError: try:
local_host, port = '127.0.0.1', 3000 from .data.config import local_host
try: except ImportError:
from .data.config import hostname, certificate local_host = 'localhost'
except ImportError: if port is None:
hostname, certificate = '', None try:
from .data.config import port
except ImportError:
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",