From fe5840d3fc5ebb9944ef96eb05a00e23f79c0815 Mon Sep 17 00:00:00 2001 From: "Kay Marquardt (Gnadelwartz)" Date: Tue, 23 Apr 2019 11:45:03 +0200 Subject: [PATCH] start evironment config --- bashbot.sh | 8 ++-- doc/1_firstbot.md | 2 +- doc/2_usage.md | 2 +- doc/3_advanced.md | 2 +- doc/4_expert.md | 2 +- doc/5_practice.md | 2 +- doc/6_reference.md | 4 +- doc/7_develop.md | 3 +- doc/8_custom.md | 81 ++++++++++++++++++++++++++++++++++ test/ALL-tests.inc.sh | 2 +- test/ALL-tests.sh | 2 +- test/d-process_message-test.sh | 6 +-- 12 files changed, 99 insertions(+), 17 deletions(-) create mode 100644 doc/8_custom.md diff --git a/bashbot.sh b/bashbot.sh index bed937c..db3f090 100755 --- a/bashbot.sh +++ b/bashbot.sh @@ -10,7 +10,7 @@ # This file is public domain in the USA and all free countries. # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # -#### $$VERSION$$ v0.70-dev2-3-g65cd94a +#### $$VERSION$$ v0.70-dev2-4-g893ee61 # # Exit Codes: # - 0 sucess (hopefully) @@ -609,8 +609,8 @@ process_message() { # main get updates loop, should never terminate start_bot() { local mysleep="100" # ms - local addsleep"50" - local maxsleep="${BASHBOTSLEEP:-5000}" + local addsleep="50" + local maxsleep="${BASHBOT_SLEEP:-5000}" while true; do { UPDATE="$(curl -s "$UPD_URL$OFFSET" | ./${JSONSHFILE})" @@ -671,7 +671,7 @@ if [ "$ME" = "" ]; then fi # use phyton JSON to decode JSON UFT-8, provide bash implementaion as fallback -if [ "${BASHDECODE}" != "yes" ] && which python >/dev/null 2>&1 ; then +if [ "${BASHBOT_DECODE}" != "" ] && which python >/dev/null 2>&1 ; then JsonDecode() { printf '"%s\\n"' "${1//\"/\\\"}" | python -c 'import json, sys; sys.stdout.write(json.load(sys.stdin).encode("utf-8"))' } diff --git a/doc/1_firstbot.md b/doc/1_firstbot.md index eed1c90..7b76bca 100644 --- a/doc/1_firstbot.md +++ b/doc/1_firstbot.md @@ -61,5 +61,5 @@ group. This step is up to you actually. #### [Next Getting started](2_usage.md) -#### $$VERSION$$ v0.70-dev2-0-g4fff4c3 +#### $$VERSION$$ v0.70-dev2-4-g893ee61 diff --git a/doc/2_usage.md b/doc/2_usage.md index bd3b632..6aae752 100644 --- a/doc/2_usage.md +++ b/doc/2_usage.md @@ -159,5 +159,5 @@ send_action "${CHAT[ID]}" "action" #### [Prev Create Bot](1_firstbot.md) #### [Next Advanced Usage](3_advanced.md) -#### $$VERSION$$ v0.70-dev2-0-g4fff4c3 +#### $$VERSION$$ v0.70-dev2-4-g893ee61 diff --git a/doc/3_advanced.md b/doc/3_advanced.md index d4d0118..a7aac14 100644 --- a/doc/3_advanced.md +++ b/doc/3_advanced.md @@ -156,5 +156,5 @@ answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" #### [Prev Advanced Usage](3_advanced.md) #### [Next Expert Use](4_expert.md) -#### $$VERSION$$ v0.70-dev2-0-g4fff4c3 +#### $$VERSION$$ v0.70-dev2-4-g893ee61 diff --git a/doc/4_expert.md b/doc/4_expert.md index 594537f..6a4ef00 100644 --- a/doc/4_expert.md +++ b/doc/4_expert.md @@ -104,5 +104,5 @@ An example crontab is provided in ```examples/bashbot.cron```. #### [Prev Expert Use](4_expert.md) #### [Next Best Practice](5_practice.md) -#### $$VERSION$$ v0.70-dev2-0-g4fff4c3 +#### $$VERSION$$ v0.70-dev2-4-g893ee61 diff --git a/doc/5_practice.md b/doc/5_practice.md index e74fd19..6adcde2 100644 --- a/doc/5_practice.md +++ b/doc/5_practice.md @@ -112,5 +112,5 @@ The second warning is about an unused variable, this is true because in our exam #### [Prev Best Practice](5_practice.md) #### [Next Functions Reference](6_reference.md) -#### $$VERSION$$ v0.70-dev2-0-g4fff4c3 +#### $$VERSION$$ v0.70-dev2-4-g893ee61 diff --git a/doc/6_reference.md b/doc/6_reference.md index 050f00a..4af79c2 100644 --- a/doc/6_reference.md +++ b/doc/6_reference.md @@ -346,7 +346,7 @@ The name of your bot is availible as bash variable "$ME", there is no need to ca Send Input from Telegram to waiting Interactive Chat. #### [Prev Best Practice](5_practice.md) -#### [Next Developer Rules](7_develop.md) +#### [Next Notes for Developers](7_develop.md) -#### $$VERSION$$ v0.70-dev2-0-g4fff4c3 +#### $$VERSION$$ v0.70-dev2-4-g893ee61 diff --git a/doc/7_develop.md b/doc/7_develop.md index e841540..8eafcf2 100644 --- a/doc/7_develop.md +++ b/doc/7_develop.md @@ -69,6 +69,7 @@ fi ``` #### [Prev Function Reference](6_function.md) +#### [Next Bashbot Environment](8_custom.md) -#### $$VERSION$$ v0.70-dev2-0-g4fff4c3 +#### $$VERSION$$ v0.70-dev2-4-g893ee61 diff --git a/doc/8_custom.md b/doc/8_custom.md new file mode 100644 index 0000000..df29da0 --- /dev/null +++ b/doc/8_custom.md @@ -0,0 +1,81 @@ +#### [Home](../README.md) +## Customize bashbots environment +This section describe how you can customize bashbot to your needs by setting environment variables. + + +### Change file locations +In standard setup bashbot is self containing, this means you can place 'telegram-bot-bash' on any location +and run it from there. All files - programm, config, data etc - will reside in 'telegram-bot-bash'. + +If you want other locations for config, data etc, define and export the following environment variables: + +#### BASHBOT_ETC +Location of the config files 'token', 'botadmin', 'botacl' ... +```bash + unset BASHBOT_ETC # keep in telegram-bot-bash (default) + export BASHBOT_ETC "" # keep in telegram-bot-bash + + export BASHBOT_ETC "/etc/bashbot" # unix like config location + + export BASHBOT_ETC "/etc/bashbot/bot1" # multibot configuration bot 1 + export BASHBOT_ETC "/etc/bashbot/bot2" # multibot configuration bot 2 +``` + + e.g. /etc/bashbot + +#### BASHBOT_VAR +Location of runtime data files 'data-bot-bash', 'count', downloaded files ... +```bash + unset BASHBOT_VAR # keep in telegram-bot-bash (default) + export BASHBOT_VAR "" # keep in telegram-bot-bash + + export BASHBOT_VAR "/var/spool/bashbot" # unix like config location + + export BASHBOT_VAR "/var/spool/bashbot/bot1" # multibot configuration bot 1 + export BASHBOT_VAR "/var/spool/bashbot/bot2" # multibot configuration bot 2 +``` + +#### BASHBOT_COMMANDS +Full path to bash script containing your commands, default: './commands.sh' +```bash + unset BASHBOT_COMMANDS # telegram-bot-bash/commands.sh (default) + export BASHBOT_COMMANDS "" # telegram-bot-bash/commands.sh + + export BASHBOT_COMMANDS "/etc/bashbot/commands.sh" # unix like config location + + export BASHBOT_COMMANDS "/etc/bashbot/bot1/commands.sh" # multibot configuration bot 1 + export BASHBOT_COMMANDS "/etc/bashbot/bot2/commands.sh" # multibot configuration bot 2 +``` + +### Change config values + +#### BASHBOT_DECODE +Bashbot offers two variants for decoding JSON UTF format to UTF-8. By default bashbot uses 'json.encode' if python is installed. +If 'BASHBOT_DECODE' is set to any value (not undefined or not empty) the bash only implementation will be used. +```bash + unset BASHBOT_DECODE # autodetect python (default) + export BASHBOT_DECODE "" # autodetect python + + export BASHBOT_DECODE "yes" # force internal + export BASHBOT_DECODE "no" # also force internal! +``` + + +#### BASHBOT_SLEEP +Instead of polling permanently or with a fixed delay, bashbot offers a simple adaptive polling. +If messages are recieved bashbot polls with no dealy. If no messages are availible bashbot add 100ms delay +for every poll until the maximum of BASHBOT_SLEEP ms. +```bash + unset BASHBOT_SLEEP # 5000ms (default) + export BASHBOT_SLEEP "" # 5000ms + + export BASHBOT_SLEEP "1000" # 1s maximum sleep + export BASHBOT_SLEEP "10000" # 10s maximum sleep + export BASHBOT_SLEEP "1" # values < 1000 disables sleep (not recommended) + +``` + +#### [Prev Notes for Developers](7_develop.md) + +#### $$VERSION$$ v0.70-dev2-4-g893ee61 + diff --git a/test/ALL-tests.inc.sh b/test/ALL-tests.inc.sh index 045cda4..cd660e5 100755 --- a/test/ALL-tests.inc.sh +++ b/test/ALL-tests.inc.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.70-dev2-0-g4fff4c3 +#### $$VERSION$$ v0.70-dev2-4-g893ee61 # common variables export TESTME DIRME TESTDIR LOGFILE REFDIR TESTNAME diff --git a/test/ALL-tests.sh b/test/ALL-tests.sh index 86f237b..24e7bc9 100755 --- a/test/ALL-tests.sh +++ b/test/ALL-tests.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash # this has to run once atfer git clone # and every time we create new hooks -#### $$VERSION$$ v0.70-dev2-0-g4fff4c3 +#### $$VERSION$$ v0.70-dev2-4-g893ee61 # magic to ensure that we're always inside the root of our application, # no matter from which directory we'll run script diff --git a/test/d-process_message-test.sh b/test/d-process_message-test.sh index f068e5b..ccc3481 100755 --- a/test/d-process_message-test.sh +++ b/test/d-process_message-test.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -#### $$VERSION$$ v0.70-dev2-0-g4fff4c3 +#### $$VERSION$$ v0.70-dev2-4-g893ee61 # include common functions and definitions # shellcheck source=test/ALL-tests.inc.sh @@ -26,8 +26,8 @@ echo "Check process_message ..." for i in 1 2 do [ "${i}" = "1" ] && ! which python >/dev/null 2>&1 && continue - [ "${i}" = "1" ] && echo " ... JsonDecode Phyton" - [ "${i}" = "2" ] && echo " ... JsonDecode Bash" && export BASHDECODE="yes" + [ "${i}" = "1" ] && echo " ... JsonDecode Phyton" && unset BASHBOT_DECODE + [ "${i}" = "2" ] && echo " ... JsonDecode Bash" && export BASHBOT_DECODE="yes" set -x { process_message "0"; set +x; } >>"${LOGFILE}" 2>&1;