Bashbot Version 0.51 shellcheck testet

This commit is contained in:
Kay Marquardt (Gnadelwartz) 2019-04-11 09:59:56 +02:00
parent 035627011a
commit a13ecbcfcf
13 changed files with 52 additions and 26 deletions

View File

@ -73,4 +73,4 @@ No - its not less (in)secure as any other Bot written in any other language. But
If you feel that there's something missing or if you found a bug, feel free to submit a pull request! If you feel that there's something missing or if you found a bug, feel free to submit a pull request!
#### $$VERSION$$ v0.51-0-g4ddd122 #### $$VERSION$$ v0.51-0-g0356270

View File

@ -7,7 +7,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.51-0-g4ddd122 #### $$VERSION$$ v0.51-0-g0356270
SHELL=/bin/sh SHELL=/bin/sh

View File

@ -1,7 +1,7 @@
#!/bin/sh #!/bin/sh
# description: Start or stop telegram-bash-bot # description: Start or stop telegram-bash-bot
# #
#### $$VERSION$$ v0.51-0-g4ddd122 #### $$VERSION$$ v0.51-0-g0356270
# shellcheck disable=SC2009 # shellcheck disable=SC2009
# shellcheck disable=SC2181 # shellcheck disable=SC2181

View File

@ -10,7 +10,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.51-0-g4ddd122 #### $$VERSION$$ v0.51-0-g0356270
# #
# Exit Codes: # Exit Codes:
# - 0 sucess (hopefully) # - 0 sucess (hopefully)

View File

@ -4,7 +4,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
# #
#### $$VERSION$$ v0.51-0-g4ddd122 #### $$VERSION$$ v0.51-0-g0356270
# #
# shellcheck disable=SC2154 # shellcheck disable=SC2154
# shellcheck disable=SC2034 # shellcheck disable=SC2034

View File

@ -70,5 +70,5 @@ git clone --recursive https://github.com/topkecleon/telegram-bot-bash
``` ```
3. Change to directory ```telegram-bot.bash```, run ```./bashbot.sh init``` and follow the instructions. At this stage you are asked for your Bots token given by botfather. 3. Change to directory ```telegram-bot.bash```, run ```./bashbot.sh init``` and follow the instructions. At this stage you are asked for your Bots token given by botfather.
#### $$VERSION$$ v0.51-0-g4ddd122 #### $$VERSION$$ v0.51-0-g0356270

View File

@ -147,6 +147,6 @@ Allowed values: typing for text messages, upload_photo for photos, record_video
send_action "${CHAT[ID]}" "action" send_action "${CHAT[ID]}" "action"
``` ```
#### $$VERSION$$ v0.51-0-g4ddd122 #### $$VERSION$$ v0.51-0-g0356270

View File

@ -97,5 +97,5 @@ To send stickers through an *inline query*:
answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker" answer_inline_query "$iQUERY_ID" "cached_sticker" "identifier for the sticker"
``` ```
#### $$VERSION$$ v0.51-0-g4ddd122 #### $$VERSION$$ v0.51-0-g0356270

View File

@ -102,5 +102,5 @@ An example crontab is provided in ```bashbot.cron```.
- if you run bashbot as an other user or a system service edit ```bashbot.cron``` to fit your needs and replace username```nobody``` with the username you want to run bashbot. copy the modified file to ```/etc/cron.d/bashbot``` - if you run bashbot as an other user or a system service edit ```bashbot.cron``` to fit your needs and replace username```nobody``` with the username you want to run bashbot. copy the modified file to ```/etc/cron.d/bashbot```
#### $$VERSION$$ v0.51-0-g4ddd122 #### $$VERSION$$ v0.51-0-g0356270

View File

@ -25,7 +25,7 @@ Insert your own Bot commands in the ```case ... esac``` block in commands.sh:
after editing commands.sh restart Bot. after editing commands.sh restart Bot.
### Seperate Bot logic from command ### Seperate Bot logic from command
If a Bot command needs more than 2-3 lines of code I recommend to factor it out to bash functions in a seperate file, e.g. If a Bot command needs more than 2-3 lines of code I recommend to factor it out to a bash function in a seperate file, e.g.
```mybotcommands.inc.sh``` and source the file from bashbot.sh. ```mybotcommands.inc.sh``` and source the file from bashbot.sh.
```bash ```bash
source mybotcommands.inc.sh source mybotcommands.inc.sh
@ -44,19 +44,16 @@ If a Bot command needs more than 2-3 lines of code I recommend to factor it out
send_action "${CHAT[ID]}" "typing" send_action "${CHAT[ID]}" "typing"
send_markdown_message "${CHAT[ID]}" "This is bashbot, the Telegram bot written entirely in bash." send_markdown_message "${CHAT[ID]}" "This is bashbot, the Telegram bot written entirely in bash."
;; ;;
[...] #[...]
esac esac
``` ```
Doing it this way keeps command.sh small and clean, while allowing complex tasks to be done in the included function. example ```mybotcommands.inc.sh```: Doing it this way keeps commands.sh small and clean, while allowing complex tasks to be done in the included function. example ```mybotcommands.inc.sh```:
```bash ```bash
#!/bin/bash #!/bin/bash
# #
process_message() { process_message() {
local ARGS="${1#/* }" # remove command /*
local MESSAGE="$1" # store arg local TEXT OUTPUT=""
local ARGS="${MESSAGE#/r* }" # remove command
local TEXT=""
local OUTPUT=""
# process every word in MESSAGE, avoid globbing from MESSAGE # process every word in MESSAGE, avoid globbing from MESSAGE
set -f set -f
@ -65,16 +62,13 @@ process_message() {
set +f set +f
# process links # process links
if [[ "$WORD" == "https://"* ]]; then if [[ "$WORD" == "https://"* ]]; then
# remove utf chars from URL
WORD="$(echo "$WORD" | uni2ascii -q -a F -B)"
REPORT="$(dosomething_with_link "$WORD")" REPORT="$(dosomething_with_link "$WORD")"
# no link, add as text # no link, add as text
else else
# TEXT incl UTF to ascii transformation TEXT="$(echo "${TEXT} $WORD")"
TEXT="$(echo "${TEXT} $WORD"'| iconv -c -f utf-8 -t ascii//TRANSLIT)"
continue continue
fi fi
# compose result components # compose result
OUTPUT="* ${REPORT} ${WORD} ${TEXT}" OUTPUT="* ${REPORT} ${WORD} ${TEXT}"
TEXT="" TEXT=""
done done
@ -87,6 +81,38 @@ process_message() {
``` ```
### Test your Bot with shellcheck ### Test your Bot with shellcheck
Shellcheck is a static linter for shell scripts providing excellent tips and hints for shell coding pittfalls. You can [use it online](https://www.shellcheck.net/) or [install it on your system](https://github.com/koalaman/shellcheck#installing).
#### $$VERSION$$ v0.51-0-g4d5d386 All bashbot scripts are linted by shellcheck.
Shellcheck examples:
```bash
$ shellcheck -x mybotcommands.inc.sh
Line 17:
TEXT="$(echo "${TEXT} $WORD")"
^-- SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'.
```
```bash
$ shellcheck -x notify
OK
$ shellcheck -x question
OK
$ shellcheck -x commands.sh
OK
$ shellcheck -x bashbot.sh
In bashbot.sh line 123:
text="$(echo "$text" | sed 's/ mynewlinestartshere /\r\n/g')" # hack for linebreaks in startproc scripts
^-- SC2001: See if you can use ${variable//search/replace} instead.
In bashbot.sh line 490:
CONTACT[USER_ID]="$(sed -n -e '/\["result",'$PROCESS_NUMBER',"message","contact","user_id"\]/ s/.*\][ \t]"\(.*\)"$/\1/p' <"$TMP")"
^-- SC2034: CONTACT appears unused. Verify it or export it.
```
As you can see there are only two warnings in bashbots scripts. The first is a hint you may use shell substitions instead of sed, but this is only possible for simple cases. The second warning is about an unused variable, this is true because in our examples CONTACT is not used but assigned in case you want to use it :-)
#### $$VERSION$$ v0.51-0-g0356270

2
notify
View File

@ -2,7 +2,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#### $$VERSION$$ v0.51-0-g4ddd122 #### $$VERSION$$ v0.51-0-g0356270
# adjust your language setting here # adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment

View File

@ -3,7 +3,7 @@
# This file is public domain in the USA and all free countries. # This file is public domain in the USA and all free countries.
# Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying) # Elsewhere, consider it to be WTFPLv2. (wtfpl.net/txt/copying)
#### $$VERSION$$ v0.51-0-g4ddd122 #### $$VERSION$$ v0.51-0-g0356270
# adjust your language setting here # adjust your language setting here
# https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment # https://github.com/topkecleon/telegram-bot-bash#setting-up-your-environment

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
# #
#### $$VERSION$$ v0.51-0-g4ddd122 #### $$VERSION$$ v0.51-0-g0356270
# shellcheck disable=SC2016 # shellcheck disable=SC2016
# #
# Easy Versioning in git: # Easy Versioning in git: