Add BASHBOT_HOME, commandline examples

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-05-23 12:26:53 +02:00
parent 51aa2ed4b3
commit 80a47780ce
5 changed files with 136 additions and 23 deletions

View File

@ -76,7 +76,11 @@
<li>Prepare a new version</li>
<li>Bashbot testsuite</li>
</ul></li>
<li><a href="doc/8_custom.md">Customize bashbot environment</a></li>
<li><a href="doc/8_custom.md">Expert Use</a>
<ul>
<li>Use from CLI and Scripts</li>
<li>Customize Bashbot Environment</li>
</ul></li>
<li><a href="examples/README.md">Examples</a></li>
</ul>
<h2 id="security-considerations">Security Considerations</h2>
@ -108,6 +112,6 @@
<p><span class="citation">@Gnadelwartz</span></p>
<h2 id="thats-it">Thats it!</h2>
<p>If you feel that theres something missing or if you found a bug, feel free to submit a pull request!</p>
<h4 id="version-v0.90-dev-0-g75691dc"><br /><span class="math display"><em>V</em><em>E</em><em>R</em><em>S</em><em>I</em><em>O</em><em>N</em></span><br /> v0.90-dev-0-g75691dc</h4>
<h4 id="version-v0.90-dev-2-g51aa2ed"><br /><span class="math display"><em>V</em><em>E</em><em>R</em><em>S</em><em>I</em><em>O</em><em>N</em></span><br /> v0.90-dev-2-g51aa2ed</h4>
</body>
</html>

View File

@ -55,7 +55,9 @@ Bashbot [Documentation](https://github.com/topkecleon/telegram-bot-bash) and [Do
* Test, Add, Push changes
* Prepare a new version
* Bashbot testsuite
* [Customize bashbot environment](doc/8_custom.md)
* [Expert Use](doc/8_custom.md)
* Use from CLI and Scripts
* Customize Bashbot Environment
* [Examples](examples/README.md)
@ -109,4 +111,4 @@ Hey no Problem, if you are finished with your cool bot run ```dev/make-standalon
If you feel that there's something missing or if you found a bug, feel free to submit a pull request!
#### $$VERSION$$ v0.90-dev-0-g75691dc
#### $$VERSION$$ v0.90-dev-2-g51aa2ed

View File

@ -67,7 +67,9 @@ all](https://core.telegram.org/bots#3-how-do-i-create-a-bot)
* Test, Add, Push changes
* Prepare a new version
* Bashbot testsuite
* [Customize bashbot environment](doc/8_custom.md)
* [Expert Use](doc/8_custom.md)
* Use from CLI and Scripts
* Customize Bashbot Environment
* [Examples](examples/README.md)
@ -158,4 +160,4 @@ down Version of your Bot](doc/7_develop.md)
If you feel that there's something missing or if you found a bug, feel free to
submit a pull request!
#### $$VERSION$$ v0.90-dev-0-g75691dc
#### $$VERSION$$ v0.90-dev-2-g51aa2ed

View File

@ -11,7 +11,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.90-dev-0-g75691dc
#### $$VERSION$$ v0.90-dev-2-g51aa2ed
#
# Exit Codes:
# - 0 sucess (hopefully)
@ -35,6 +35,13 @@ fi
export SCRIPT SCRIPTDIR MODULEDIR RUNDIR RUNUSER
SCRIPT="$0"
SCRIPTDIR="$(dirname "$0")"
if [ "$BASHBOT_HOME" != "" ]; then
SCRIPTDIR="$BASHBOT_HOME"
[ "${BASHBOT_ETC}" = "" ] && BASHBOT_ETC="$BASHBOT_HOME"
[ "${BASHBOT_VAR}" = "" ] && BASHBOT_VAR="$BASHBOT_HOME"
fi
MODULEDIR="${SCRIPTDIR}/modules"
RUNDIR="${SCRIPTDIR}"
@ -42,7 +49,7 @@ RUNDIR="${SCRIPTDIR}"
RUNUSER="${USER}" # USER is overwritten by bashbot array
if [ "$1" != "source" ] && ! cd "${RUNDIR}" ; then
if [ "$1" != "source" ] && [ "$BASHBOT_HOME" != "" ] && ! cd "${RUNDIR}" ; then
echo -e "${RED}ERROR: Can't change to ${RUNDIR} ...${NC}"
exit 1
fi
@ -52,6 +59,7 @@ if [ ! -w "." ]; then
ls -ld .
fi
TOKENFILE="${BASHBOT_ETC:-.}/token"
if [ ! -f "${TOKENFILE}" ]; then
if [ "${CLEAR}" = "" ] && [ "$1" != "init" ]; then

View File

@ -1,16 +1,97 @@
#### [Home](../README.md)
## Customize bashbots environment
## Expert use
### Use bashbot from CLI and scripts
You can use bashbot to send messages, locations, venues, pictures etc from command line and scripts.
For interactive use and script use should set and export BASHBOT_HOME to bashbots installation dir,
e.g. '/usr/local/telegram-bot-bash'. see [Bashbot environemnt](#Bashbot-environment)
**Note:** If you don't set BASHBOT_HOME bashbot is expecting you are in the installation directory
of bashbot and will use relative pathnames to access ressources.
#### Environment variable exported from bashbot
If you have sourced 'bashbot.sh' you have the following bashot internal variables availible to
locate varoius ressources:
```bash
COMMANDS # default ./commands.sh"
MODULEDIR # default ./modules"
TOKENFILE # default ./token"
BOTADMIN # default ./botadmin"
BOTACL # default ./botacl"
TMPDIR # default ./data-bot-bash"
COUNTFILE # default ./count"
BOTTOKEN # default content of ${TOKENFILE}
URL # telegram api URL -default https://api.telegram.org/bot${BOTTOKEN}"
```
#### Interacctive use
For Test purposes you can use bashbot functions from bash command line after setting BASHBOT_HOME
and sourcing 'bashbot.sh'.
```bash
# are we running bash?
echo $SHELL
/bin/bash
# source bashbot.sh WITHOUT BASHBOT_HOME set
./bashbot.sh source
# output bashbot internal variables
echo $COMMANDS $MODULEDIR $TOKENFILE $BOTADMIN $BOTACL $TMPDIR $COUNTFILE
./commands.sh ./modules ./token ./botadmin ./botacl ./data-bot-bash ./count
# source bashbot.sh WITH BASHBOT_HOME set
export BASHBOT_HOME=/usr/local/telegram-bot-bash
source ./bashbot.sh source
# output bashbot internal variables
echo $COMMANDS $MODULEDIR $TOKENFILE $BOTADMIN $BOTACL $TMPDIR $COUNTFILE
/usr/local/telegram-bot-bash/commands.sh /usr/local/telegram-bot-bash/modules /usr/local/telegram-bot-bash/token
/usr/local/telegram-bot-bash/botadmin /usr/local/telegram-bot-bash/botacl /usr/local/telegram-bot-bash/data-bot-bash
/usr/local/telegram-bot-bash/count
```
Now you can use the bashot 'send_xxx' functions to send Message, Locations, Pictures etc. to any Telegram
User or Chat you are in. See [Send Messages](2_usage.md#sending-messages).
*Examples:* You can test this by sending messages to yourself:
```bash
# fist Hello World
send_normal_message "$(< $BOTADMIN)" "Hello World! This is my first message"
# now with some markdown and HTML
send_markdown_message "$(< $BOTADMIN)" '*Hello World!* _This is my first markdown message_'
send_html_message "$(< $BOTADMIN)" '<b>Hello World!</b> <em>This is my first HTML message</em>'
send_keyboard "$(< $BOTADMIN)" 'Do you like it?' '[ "Yep" , "No" ]'
```
Nnow something more useful ...
```bash
# sending output from system commands:
send_normal_message "$(< $BOTADMIN)" "$(date)"
send_normal_message "$(< $BOTADMIN)" "$(uptime)"
send_normal_message "$(< $BOTADMIN)" '`'$(free)'`'
# same but sent as monospaced text
send_markdown_message "$(< $BOTADMIN)" "\`$(free)\`"
```
### Bashbot environment
This section describe how you can customize bashbot to your needs by setting environment variables.
### Change file locations
#### Change file locations
In standard setup bashbot is self containing, this means you can place 'telegram-bot-bash' any location
and run it from there. All files - programm, config, data etc - will reside in 'telegram-bot-bash'.
If you want to have other locations for config, data etc, define and export the following environment variables.
**Note: all specified directories and files must exist or running 'bashbot.sh' will fail.**
#### BASHBOT_ETC
##### BASHBOT_ETC
Location of the files ```commands.sh```, ```mycommands.sh```, ```token```, ```botadmin```, ```botacl``` ...
```bash
unset BASHBOT_ETC # keep in telegram-bot-bash (default)
@ -24,7 +105,7 @@ Location of the files ```commands.sh```, ```mycommands.sh```, ```token```, ```bo
e.g. /etc/bashbot
#### BASHBOT_VAR
##### BASHBOT_VAR
Location of runtime data ```data-bot-bash```, ```count```
```bash
unset BASHBOT_VAR # keep in telegram-bot-bash (default)
@ -36,7 +117,7 @@ Location of runtime data ```data-bot-bash```, ```count```
export BASHBOT_VAR "/var/spool/bashbot/bot2" # multibot configuration bot 2
```
#### BASHBOT_JSONSH
##### BASHBOT_JSONSH
Full path to JSON.sh script, default: './JSON.sh/JSON.sh', must end with '/JSON.sh'.
```bash
unset BASHBOT_JSONSH # telegram-bot-bash/JSON.sh/JSON.sh (default)
@ -46,9 +127,25 @@ Full path to JSON.sh script, default: './JSON.sh/JSON.sh', must end with '/JSON.
```
### Change config values
##### BASHBOT_HOME
Bashbot Home directory (where bashbot is installed). If set bashbot autodetection of installation dir is disabled
and value of BASHBOT_HOME is used.
#### BASHBOT_URL
This is usefull if you want to force bashbot to always use full pathnames instead of relative ones or if you
want to tell your own scripts where to look for bashbot.sh.
```bash
unset BASHBOT_HOME # autodetection (default)
export BASHBOT_HOME "" # autodetection
export BASHBOT_HOME "/usr/local/telegram-bot-bash" # unix like location
export BASHBOT_HOME "/usr/local/bin" # Note: you MUST set ETC, VAR and JSONSH correct to make this work!
```
----
#### Change config values
##### BASHBOT_URL
Uses given URL instead of offical telegram API URL, useful if you have your own telegram server or for testing.
```bash
@ -60,9 +157,9 @@ Uses given URL instead of offical telegram API URL, useful if you have your own
```
#### BASHBOT_TOKEN
##### BASHBOT_TOKEN
#### BASHBOT_WGET
##### BASHBOT_WGET
Bashbot uses ```curl``` to communicate with telegram server. if ```curl``` is not availible ```wget``` is used.
If 'BASHBOT_WGET' is set to any value (not undefined or not empty) wget is used even is curl is availible.
```bash
@ -74,7 +171,7 @@ If 'BASHBOT_WGET' is set to any value (not undefined or not empty) wget is used
```
#### BASHBOT_SLEEP
##### 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.
@ -88,10 +185,10 @@ for every poll until the maximum of BASHBOT_SLEEP ms.
```
### Testet configs as of v.07 release
#### Testet configs as of v.07 release
**Note: Environment variables are not stored, you must setup them before every call to bashbot.sh, e.g. from a script.**
#### simple Unix like config, for one bot. bashbot is installed in '/usr/local/telegram-bot-bash'
##### simple Unix like config, for one bot. bashbot is installed in '/usr/local/telegram-bot-bash'
```bash
# Note: all dirs and files must exist!
export BASHBOT_ETC "/etc/bashbot"
@ -100,7 +197,7 @@ for every poll until the maximum of BASHBOT_SLEEP ms.
/usr/local/telegram-bot-bash/bashbot.sh start
```
#### Unix like config for one bot. bashbot.sh is installed in '/usr/bin'
##### Unix like config for one bot. bashbot.sh is installed in '/usr/bin'
```bash
# Note: all dirs and files must exist!
export BASHBOT_ETC "/etc/bashbot"
@ -110,7 +207,7 @@ for every poll until the maximum of BASHBOT_SLEEP ms.
/usr/local/bin/bashbot.sh start
```
#### simple multibot config, everything is keept inside 'telegram-bot-bash' dir
##### simple multibot config, everything is keept inside 'telegram-bot-bash' dir
```bash
# config for running Bot 1
# Note: all dirs and files must exist!
@ -131,5 +228,5 @@ for every poll until the maximum of BASHBOT_SLEEP ms.
#### [Prev Notes for Developers](7_develop.md)
#### $$VERSION$$ v0.90-dev-0-g75691dc
#### $$VERSION$$ v0.90-dev-2-g51aa2ed