From d667ecc262c8a7117ef7dab4ef7ffe72f3ff01fc Mon Sep 17 00:00:00 2001 From: dcoomber <47242934+dcoomber@users.noreply.github.com> Date: Sun, 27 Sep 2020 20:24:15 +0200 Subject: [PATCH] Merging with topkecleon-develop --- README.html | 79 +++++++++++++++++++++------------------- README.md | 12 ++++-- README.txt | 14 ++++--- bashbot.rc | 2 +- bashbot.sh | 17 ++++++++- commands.sh | 2 +- dev/all-tests.sh | 2 +- dev/git-add.sh | 2 +- dev/hooks/pre-commit.sh | 2 +- dev/hooks/pre-push.sh | 2 +- dev/install-hooks.sh | 2 +- dev/make-distribution.sh | 2 +- dev/make-standalone.sh | 2 +- dev/shellcheck.files | 2 +- dev/version.sh | 2 +- doc/0_install.md | 78 +++++++++++++++++++++++++++++---------- doc/2_usage.md | 2 +- modules/sendMessage.sh | 2 +- mycommands.sh.clean | 2 +- mycommands.sh.dist | 2 +- 20 files changed, 146 insertions(+), 84 deletions(-) diff --git a/README.html b/README.html index e5d8ee0..e3b9070 100644 --- a/README.html +++ b/README.html @@ -93,7 +93,8 @@ Written by Drew (@topkecleon) and Kay M (@gnadelwartz).
Uses JSON.sh and the magic of sed.
Even bashbot is written in bash, it depends on commands typically available in a Unix/Linux Environment. More concrete on the common commands provided by recent versions of coreutils, busybox or toybox, see Developer Notes
-Note for MacOS and BSD Users: As bashbot heavily uses modern bash and (gnu) grep/sed features, bashbot will not run without installing additional software, see Install Bashbot
+Note for MacOS and BSD Users: As bashbot heavily uses modern bash and (gnu) grep/sed features, bashbot will not run without installing additional software, see Install Bashbot
+Note for embedded systems: busybox or toybox ONLY is not sufficient, you need a to install a "real" bash, see also Install Bashbot
Bashbot Documentation and Downloads are available on www.github.com
To install and run bashbot you need access to a linux/unix command line. If you don't know how to get access to a linux/unix command line you should stop reading here :-(
In addition you need a Telegram client and a mobile phone to register an account. If you don't want to register for Telegram you should stop reading here ;-)
After you're registered to Telegram send a message to @botfather, create a new Telegram Bot token and write it down. You need the token to install the bot.
-Now open a terminal and check if bash is installed: which bash && echo "bash installed!"
. If you get an error message bash is not installed.
Now open a terminal and check if bash is installed:
+which bash && echo "bash seems available..."
+
Create a new directory, change to it: mkdir tbb; cd tbb
and download the latest '*.tar.gz' file from https://github.com/topkecleon/telegram-bot-bash/releases. This can be done with the commands:
Extract the '*.tar.gz' file and change to bashbot directory: tar -xzf *.tar.gz; cd telegram-bot-bash
, install bashbot: ./bashbot.sh init
and enter your bot token when asked. All other questions can be answered by hitting the <Return> key.
That's all, now you can start your bot with ./bashbot.sh start
and send him messages:
/start
@@ -196,18 +199,18 @@ It features background tasks and interactive chats, and can serve as an interfac
A powerful tool to improve your scripts is shellcheck
. You can use it online or install shellcheck locally. Shellcheck is used extensively in bashbot development to ensure a high code quality, e.g. it's not allowed to push changes without passing all shellcheck tests. In addition bashbot has a test suite to check if important functionality is working as expected.
Use printf whenever possible
If you're writing a script and it is taking external input (from the user as arguments or file system...), you shouldn't use echo to display it. Use printf whenever possible
- # very simple
- echo "text with variables. PWD=$PWD"
- printf '%s\n' "text with variables. PWD=$PWD"
- -> text with variables. PWD=/home/xxx
-
- # more advanced
- FLOAT="1.2346777892864" INTEGER="12345.123"
- echo "text with variabeles. float=$FLOAT, integer=$INTEGER, PWD=$PWD"
- ->text with variables. float=1.2346777892864, integer=12345.123, PWD=/home/xxx
-
- printf "text with variables. float=%.2f, integer=%d, PWD=%s\n" "" "$INTEGER" "$PWD"
- ->text with variables. float=1.23, integer=12345, PWD=/home/xxx
+ # very simple
+ echo "text with variables. PWD=$PWD"
+ printf '%s\n' "text with variables. PWD=$PWD"
+ -> text with variables. PWD=/home/xxx
+
+ # more advanced
+ FLOAT="1.2346777892864" INTEGER="12345.123"
+ echo "text with variabeles. float=$FLOAT, integer=$INTEGER, PWD=$PWD"
+ ->text with variables. float=1.2346777892864, integer=12345.123, PWD=/home/xxx
+
+ printf "text with variables. float=%.2f, integer=%d, PWD=%s\n" "" "$INTEGER" "$PWD"
+ ->text with variables. float=1.23, integer=12345, PWD=/home/xxx
Do not use #!/usr/bin/env bash
We stay with /bin/bash shebang, because it's more save from security perspective.
Use of a fixed path to the system provided bash makes it harder for attackers or users to place alternative versions of bash and avoids using a possibly broken, mangled or compromised bash executable.
@@ -237,38 +240,38 @@ It features background tasks and interactive chats, and can serve as an interfac
Can I send messages from CLI and scripts?
Of course, you can send messages from CLI and scripts, simply install bashbot as described here, send the message '/start' to set yourself as botadmin and stop the bot with ./bashbot.sh stop
.
Run the following commands in your bash shell or script while you are in the installation directory:
-# prepare bash / script to send commands
-export BASHBOT_HOME="$(pwd)"
-source ./bashbot.sh source
-
-# send me a test message
-send_message "$(getConfigKey "botadmin")" "test"
-
-# send me output of a system command
-send_message "$(getConfigKey "botadmin")" "$(df -h)"
+# prepare bash / script to send commands
+export BASHBOT_HOME="$(pwd)"
+source ./bashbot.sh source
+
+# send me a test message
+send_message "$(getConfigKey "botadmin")" "test"
+
+# send me output of a system command
+send_message "$(getConfigKey "botadmin")" "$(df -h)"
For more information see Expert Use
Blocked by telegram?
This may happen if to many or wrong requests are sent to api.telegram.org, e.g. using a invalid token or not existing API calls. If the block stay for longer time you can ask telegram service to unblock your IP-Adress.
You can check with curl or wget if you are blocked by Telegram:
-curl -m 10 https://api.telegram.org/bot
-#curl: (28) Connection timed out after 10001 milliseconds
-
-wget -t 1 -T 10 https://api.telegram.org/bot
-#Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out.
+curl -m 10 https://api.telegram.org/bot
+#curl: (28) Connection timed out after 10001 milliseconds
+
+wget -t 1 -T 10 https://api.telegram.org/bot
+#Connecting to api.telegram.org (api.telegram.org)|46.38.243.234|:443... failed: Connection timed out.
Since Version 0.96 bashbot offers the option to recover from broken connections (aka blocked). Therefore you can provide a function named bashbotBlockRecover()
in mycommands.sh
. If the function exists it is called every time when a broken connection is detected.
Possible actions are: Check if network is working, change IP-Adress or simply wait some time.
If everything seems OK return 0 for retry or any non 0 value to give up.
-# called when bashbot sedn command failed because we can not connect to telegram
-# return 0 to retry, return non 0 to give up
-bashbotBlockRecover() {
- # place your commands to unblock here, e.g. change IP-Adess or simply wait
- sleep 60 && return 0 # may be temporary
- return 1
- }
-
+# called when bashbot sedn command failed because we can not connect to telegram
+# return 0 to retry, return non 0 to give up
+bashbotBlockRecover() {
+ # place your commands to unblock here, e.g. change IP-Adess or simply wait
+ sleep 60 && return 0 # may be temporary
+ return 1
+ }
+
@Gnadelwartz
That's it!
If you feel that there's something missing or if you found a bug, feel free to submit a pull request!
-$$VERSION$$ v1.0-0-g99217c4
+$$VERSION$$ v1.1-0-gc0eb399